Documentation
¶
Overview ¶
Package database provides cross-database query building utilities
Package database provides performance tracking for database operations
Index ¶
- Constants
- func GetSupportedDatabaseTypes() []string
- func NewTrackedConnection(conn database.Interface, log logger.Logger, cfg *config.DatabaseConfig) database.Interface
- func ValidateDatabaseType(dbType string) error
- type BasicStatement
- type Connector
- type DbManager
- type DbManagerOptions
- type Interface
- type QueryBuilder
- func (qb *QueryBuilder) BuildBooleanValue(value bool) any
- func (qb *QueryBuilder) BuildCaseInsensitiveLike(column, value string) squirrel.Sqlizer
- func (qb *QueryBuilder) BuildCurrentTimestamp() string
- func (qb *QueryBuilder) BuildLimitOffset(query squirrel.SelectBuilder, limit, offset int) squirrel.SelectBuilder
- func (qb *QueryBuilder) BuildUUIDGeneration() string
- func (qb *QueryBuilder) BuildUpsert(table string, conflictColumns []string, ...) (query string, args []any, err error)
- func (qb *QueryBuilder) Delete(table string) squirrel.DeleteBuilder
- func (qb *QueryBuilder) EscapeIdentifier(identifier string) string
- func (qb *QueryBuilder) Insert(table string) squirrel.InsertBuilder
- func (qb *QueryBuilder) InsertWithColumns(table string, columns ...string) squirrel.InsertBuilder
- func (qb *QueryBuilder) Select(columns ...string) squirrel.SelectBuilder
- func (qb *QueryBuilder) Update(table string) squirrel.UpdateBuilder
- func (qb *QueryBuilder) Vendor() string
- type QueryBuilderInterface
- type Statement
- type TenantStore
- type TrackedConnection
- func (tc *TrackedConnection) Begin(ctx context.Context) (database.Tx, error)
- func (tc *TrackedConnection) BeginTx(ctx context.Context, opts *sql.TxOptions) (database.Tx, error)
- func (tc *TrackedConnection) Close() error
- func (tc *TrackedConnection) CreateMigrationTable(ctx context.Context) error
- func (tc *TrackedConnection) DatabaseType() string
- func (tc *TrackedConnection) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (tc *TrackedConnection) GetMigrationTable() string
- func (tc *TrackedConnection) Health(ctx context.Context) error
- func (tc *TrackedConnection) Prepare(ctx context.Context, query string) (database.Statement, error)
- func (tc *TrackedConnection) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tc *TrackedConnection) QueryRow(ctx context.Context, query string, args ...any) types.Row
- func (tc *TrackedConnection) Stats() (map[string]any, error)
- type TrackedDB
- func (db *TrackedDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *TrackedDB) PrepareContext(ctx context.Context, query string) (database.Statement, error)
- func (db *TrackedDB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *TrackedDB) QueryRowContext(ctx context.Context, query string, args ...any) types.Row
- type TrackedStatement
- type TrackedStmt
- type TrackedTransaction
- func (tt *TrackedTransaction) Commit() error
- func (tt *TrackedTransaction) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (tt *TrackedTransaction) Prepare(ctx context.Context, query string) (database.Statement, error)
- func (tt *TrackedTransaction) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tt *TrackedTransaction) QueryRow(ctx context.Context, query string, args ...any) types.Row
- func (tt *TrackedTransaction) Rollback() error
- type TrackedTx
- type TrackingContext
- type Tx
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 ¶
This section is empty.
Functions ¶
func GetSupportedDatabaseTypes ¶
func GetSupportedDatabaseTypes() []string
GetSupportedDatabaseTypes returns a list of supported database types
func NewTrackedConnection ¶ added in v0.2.0
func NewTrackedConnection(conn database.Interface, log logger.Logger, cfg *config.DatabaseConfig) database.Interface
NewTrackedConnection returns a database.Interface that wraps conn and records query/operation metrics and logs. The wrapper delegates all calls to the provided conn, uses conn.DatabaseType() as the vendor identifier, and derives per-connection tracking settings from cfg via newTrackingSettings.
func ValidateDatabaseType ¶
ValidateDatabaseType reports an error when dbType is not among the supported database types.
Types ¶
type BasicStatement ¶ added in v0.2.0
BasicStatement wraps sql.Stmt to implement database.Statement
func (*BasicStatement) Close ¶ added in v0.2.0
func (s *BasicStatement) Close() error
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. It returns an error when cfg is nil, cfg.Type is unsupported (supported: "postgresql", "oracle"), or driver initialization fails.
type QueryBuilder ¶
type QueryBuilder struct {
// contains filtered or unexported fields
}
QueryBuilder provides vendor-specific SQL query building
func NewQueryBuilder ¶
func NewQueryBuilder(vendor string) *QueryBuilder
NewQueryBuilder creates a new query builder for the specified database vendor
func (*QueryBuilder) BuildBooleanValue ¶
func (qb *QueryBuilder) BuildBooleanValue(value bool) any
BuildBooleanValue converts Go boolean to vendor-specific boolean representation
func (*QueryBuilder) BuildCaseInsensitiveLike ¶
func (qb *QueryBuilder) BuildCaseInsensitiveLike(column, value string) squirrel.Sqlizer
BuildCaseInsensitiveLike creates a case-insensitive LIKE condition based on vendor
func (*QueryBuilder) BuildCurrentTimestamp ¶
func (qb *QueryBuilder) BuildCurrentTimestamp() string
BuildCurrentTimestamp returns the current timestamp function for the vendor
func (*QueryBuilder) BuildLimitOffset ¶
func (qb *QueryBuilder) BuildLimitOffset(query squirrel.SelectBuilder, limit, offset int) squirrel.SelectBuilder
BuildLimitOffset creates LIMIT/OFFSET clause based on vendor
func (*QueryBuilder) BuildUUIDGeneration ¶
func (qb *QueryBuilder) BuildUUIDGeneration() string
BuildUUIDGeneration returns the UUID generation function for the vendor
func (*QueryBuilder) BuildUpsert ¶
func (qb *QueryBuilder) BuildUpsert(table string, conflictColumns []string, insertColumns, updateColumns map[string]any) (query string, args []any, err error)
BuildUpsert creates an UPSERT/MERGE query based on vendor
func (*QueryBuilder) Delete ¶
func (qb *QueryBuilder) Delete(table string) squirrel.DeleteBuilder
Delete creates a new DELETE query builder
func (*QueryBuilder) EscapeIdentifier ¶
func (qb *QueryBuilder) EscapeIdentifier(identifier string) string
EscapeIdentifier escapes database identifiers (table names, column names) for the vendor
func (*QueryBuilder) Insert ¶
func (qb *QueryBuilder) Insert(table string) squirrel.InsertBuilder
Insert creates a new INSERT query builder
func (*QueryBuilder) InsertWithColumns ¶ added in v0.3.0
func (qb *QueryBuilder) InsertWithColumns(table string, columns ...string) squirrel.InsertBuilder
InsertWithColumns creates an INSERT builder and applies vendor-specific quoting to the provided column list (e.g., quotes reserved words on Oracle).
func (*QueryBuilder) Select ¶
func (qb *QueryBuilder) Select(columns ...string) squirrel.SelectBuilder
Select creates a new SELECT query builder
func (*QueryBuilder) Update ¶
func (qb *QueryBuilder) Update(table string) squirrel.UpdateBuilder
Update creates a new UPDATE query builder
func (*QueryBuilder) Vendor ¶
func (qb *QueryBuilder) Vendor() string
Vendor returns the database vendor
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 struct {
// contains filtered or unexported fields
}
TrackedConnection wraps database.Interface to provide performance tracking
func (*TrackedConnection) BeginTx ¶ added in v0.2.0
BeginTx starts a transaction with options and tracking wrapper
func (*TrackedConnection) Close ¶ added in v0.2.0
func (tc *TrackedConnection) Close() error
Close closes the database connection (no tracking needed)
func (*TrackedConnection) CreateMigrationTable ¶ added in v0.2.0
func (tc *TrackedConnection) CreateMigrationTable(ctx context.Context) error
CreateMigrationTable creates the migration table if it doesn't exist with tracking
func (*TrackedConnection) DatabaseType ¶ added in v0.2.0
func (tc *TrackedConnection) DatabaseType() string
DatabaseType returns the database type (no tracking needed)
func (*TrackedConnection) Exec ¶ added in v0.2.0
func (tc *TrackedConnection) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
Exec executes a query without returning any rows with tracking
func (*TrackedConnection) GetMigrationTable ¶ added in v0.2.0
func (tc *TrackedConnection) GetMigrationTable() string
GetMigrationTable returns the migration table name (no tracking needed)
func (*TrackedConnection) Health ¶ added in v0.2.0
func (tc *TrackedConnection) Health(ctx context.Context) error
Health checks database connectivity (no tracking needed)
func (*TrackedConnection) Prepare ¶ added in v0.2.0
Prepare creates a prepared statement with tracking
func (*TrackedConnection) Query ¶ added in v0.2.0
func (tc *TrackedConnection) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
Query executes a query that returns rows with tracking
type TrackedDB ¶
TrackedDB wraps sql.DB to provide request-scoped performance tracking
func NewTrackedDB ¶
func NewTrackedDB(db *sql.DB, log logger.Logger, vendor string, cfg *config.DatabaseConfig) *TrackedDB
NewTrackedDB creates a TrackedDB that wraps the provided *sql.DB to record query and execution metrics (durations, truncated queries, optional parameter logging). It uses log for emitting structured logs, vendor to identify the database type, and derives per-connection tracking settings from cfg (when non-nil) to override defaults.
func (*TrackedDB) ExecContext ¶
func (db *TrackedDB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
ExecContext executes a query without returning rows and tracks performance
func (*TrackedDB) PrepareContext ¶
PrepareContext prepares a statement with context and tracks performance
type TrackedStatement ¶ added in v0.2.0
type TrackedStatement struct {
// contains filtered or unexported fields
}
TrackedStatement wraps database.Statement to provide performance tracking
func (*TrackedStatement) Close ¶ added in v0.2.0
func (ts *TrackedStatement) Close() error
Close closes the prepared statement (no tracking needed)
type TrackedStmt ¶
TrackedStmt wraps database.Statement to provide performance tracking for prepared statements
func (*TrackedStmt) Exec ¶ added in v0.2.0
Exec executes a prepared statement with context and tracks performance
type TrackedTransaction ¶ added in v0.2.0
type TrackedTransaction struct {
// contains filtered or unexported fields
}
TrackedTransaction wraps database.Tx to provide performance tracking
func (*TrackedTransaction) Commit ¶ added in v0.2.0
func (tt *TrackedTransaction) Commit() error
Commit commits the transaction (no tracking needed)
func (*TrackedTransaction) Exec ¶ added in v0.2.0
func (tt *TrackedTransaction) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
Exec executes a query without returning rows within the transaction with tracking
func (*TrackedTransaction) Prepare ¶ added in v0.2.0
func (tt *TrackedTransaction) Prepare(ctx context.Context, query string) (database.Statement, error)
Prepare creates a prepared statement within the transaction with tracking
func (*TrackedTransaction) Query ¶ added in v0.2.0
func (tt *TrackedTransaction) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
Query executes a query within the transaction with tracking
func (*TrackedTransaction) QueryRow ¶ added in v0.2.0
QueryRow executes a query that returns a single row within the transaction with tracking
func (*TrackedTransaction) Rollback ¶ added in v0.2.0
func (tt *TrackedTransaction) Rollback() error
Rollback rolls back the transaction (no tracking needed)
type TrackedTx ¶
TrackedTx wraps sql.Tx to provide performance tracking for transactions
func (*TrackedTx) ExecContext ¶
func (tx *TrackedTx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
ExecContext executes a query within a transaction and tracks performance
type TrackingContext ¶ added in v0.5.0
TrackingContext groups tracking-related parameters to reduce function parameter count
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 types contains the core database interface definitions for go-bricks.
|
Package types contains the core database interface definitions for go-bricks. |