Documentation
¶
Overview ¶
Package database provides cross-database query building utilities
Package database provides performance tracking for database operations
Index ¶
- Constants
- Variables
- func GetSupportedDatabaseTypes() []string
- func ValidateDatabaseType(dbType string) error
- type Connector
- type DbManager
- type DbManagerOptions
- type Interface
- type QueryBuilder
- type QueryBuilderInterface
- type Statement
- type TenantStore
- type TrackedConnection
- type TrackedDB
- type TrackedStatement
- type TrackedStmt
- type TrackedTransaction
- type TrackedTx
- type TrackingContext
- type Tx
Constants ¶
const ( DefaultSlowQueryThreshold = tracking.DefaultSlowQueryThreshold DefaultMaxQueryLength = tracking.DefaultMaxQueryLength )
Re-export internal constants
const ( PostgreSQL = types.PostgreSQL Oracle = types.Oracle MongoDB = types.MongoDB )
Re-export database vendor identifiers so existing callers using the database package continue to compile while the single source of truth lives in types.
Variables ¶
var ( NewTrackedDB = tracking.NewDB NewTrackedConnection = tracking.NewConnection TrackDBOperation = tracking.TrackDBOperation NewTrackingSettings = tracking.NewSettings RegisterConnectionPoolMetrics = tracking.RegisterConnectionPoolMetrics )
Re-export internal functions as public API
Functions ¶
func GetSupportedDatabaseTypes ¶
func GetSupportedDatabaseTypes() []string
GetSupportedDatabaseTypes returns a list of supported database types
func ValidateDatabaseType ¶
ValidateDatabaseType reports an error when dbType is not among the supported database types.
Types ¶
type DbManager ¶ added in v0.9.0
type DbManager struct {
// contains filtered or unexported fields
}
DbManager manages database connections by string keys. It provides lazy initialization, LRU eviction, and cleanup for database connections. The manager is key-agnostic - it doesn't know about tenants, just manages named connections.
func NewDbManager ¶ added in v0.9.0
func NewDbManager(resourceSource TenantStore, log logger.Logger, opts DbManagerOptions, connector Connector) *DbManager
NewDbManager creates a new database manager
func (*DbManager) Get ¶ added in v0.9.0
Get returns a database connection for the given key. For single-tenant, use key "". For multi-tenant, use the tenant ID. Connections are created lazily and cached with LRU eviction.
func (*DbManager) StartCleanup ¶ added in v0.9.0
StartCleanup starts the background cleanup routine for idle connections
func (*DbManager) StopCleanup ¶ added in v0.9.0
func (m *DbManager) StopCleanup()
StopCleanup stops the background cleanup routine
type DbManagerOptions ¶ added in v0.9.0
type DbManagerOptions struct {
MaxSize int // Maximum number of connections to keep (0 = no limit)
IdleTTL time.Duration // Time after which idle connections are cleaned up (0 = no cleanup)
}
DbManagerOptions configures the DbManager
type Interface ¶
Interface defines the common database operations supported by the framework. This type alias maintains backward compatibility while the actual interfaces are now defined in the database/types package to avoid import cycles.
func NewConnection ¶
NewConnection creates a tracked database connection for the provided configuration. NewConnection creates a tracked database connection based on the provided configuration.
It initializes a concrete driver connection for the configured database type, wraps it with performance/tracing tracking, and attaches server metadata (host, port and an OTel namespace) to the tracking wrapper when available.
Errors are returned if cfg is nil, cfg.Type is not supported (supported: "postgresql", "oracle"), or if the underlying driver initialization fails.
type QueryBuilder ¶
type QueryBuilder struct {
*builder.QueryBuilder
}
QueryBuilder provides vendor-specific SQL query building. This is a compatibility wrapper around the internal implementation.
func NewQueryBuilder ¶
func NewQueryBuilder(vendor string) *QueryBuilder
NewQueryBuilder creates a new query builder for the specified database vendor. This function maintains backward compatibility while using the improved internal implementation.
func (*QueryBuilder) Delete ¶
func (qb *QueryBuilder) Delete(table string) types.DeleteQueryBuilder
Delete creates a DELETE query builder that returns the interface type. This method overrides the embedded builder to provide the correct interface.
func (*QueryBuilder) Filter ¶ added in v0.13.0
func (qb *QueryBuilder) Filter() types.FilterFactory
Filter returns a FilterFactory for creating composable WHERE clause filters. This method overrides the embedded builder to provide the correct interface.
func (*QueryBuilder) Select ¶
func (qb *QueryBuilder) Select(columns ...any) types.SelectQueryBuilder
Select creates a SELECT query builder that returns the interface type. This method overrides the embedded builder to provide the correct interface.
func (*QueryBuilder) Update ¶
func (qb *QueryBuilder) Update(table string) types.UpdateQueryBuilder
Update creates an UPDATE query builder that returns the interface type. This method overrides the embedded builder to provide the correct interface.
type QueryBuilderInterface ¶ added in v0.8.1
type QueryBuilderInterface = types.QueryBuilderInterface
QueryBuilderInterface defines the interface for vendor-specific SQL query building. This type alias maintains backward compatibility while enabling dependency injection.
type Statement ¶ added in v0.8.0
Statement defines the interface for prepared statements. This type alias maintains backward compatibility.
type TenantStore ¶ added in v0.9.0
type TenantStore interface {
// DBConfig returns the database configuration for the given key.
// For single-tenant apps, key will be "". For multi-tenant, key will be the tenant ID.
DBConfig(ctx context.Context, key string) (*config.DatabaseConfig, error)
}
TenantStore provides per-key database configurations. This interface abstracts where tenant-specific database configs come from.
type TrackedConnection ¶ added in v0.2.0
type TrackedConnection = tracking.Connection
Re-export the internal tracking implementation as the public API
type TrackedStatement ¶ added in v0.2.0
Re-export the internal tracking implementation as the public API
type TrackedStmt ¶
Re-export the internal tracking implementation as the public API
type TrackedTransaction ¶ added in v0.2.0
type TrackedTransaction = tracking.Transaction
Re-export the internal tracking implementation as the public API
type TrackedTx ¶
type TrackedTx = tracking.Transaction
Re-export the internal tracking implementation as the public API
type TrackingContext ¶ added in v0.5.0
Re-export the internal tracking implementation as the public API
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
builder
Package builder provides cross-database query building utilities.
|
Package builder provides cross-database query building utilities. |
|
mocks
Package mocks provides shared mock implementations for testing database components.
|
Package mocks provides shared mock implementations for testing database components. |
|
tracking
Package tracking provides performance tracking for database operations.
|
Package tracking provides performance tracking for database operations. |
|
Package testing provides utilities for testing database logic in go-bricks applications.
|
Package testing provides utilities for testing database logic in go-bricks applications. |
|
Package types contains the core database interface definitions for go-bricks.
|
Package types contains the core database interface definitions for go-bricks. |