database

package
v0.5.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 19, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package database provides cross-database query building utilities

Package database provides performance tracking for database operations

Index

Constants

View Source
const (
	PostgreSQL = "postgresql"
	Oracle     = "oracle"
)

Database type constants

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

func ValidateDatabaseType(dbType string) error

ValidateDatabaseType returns nil if dbType is one of the supported database types. If dbType is not supported, it returns an error describing the invalid value and listing the supported types.

Types

type BasicStatement added in v0.2.0

type BasicStatement struct {
	*sql.Stmt
}

BasicStatement wraps sql.Stmt to implement database.Statement

func (*BasicStatement) Close added in v0.2.0

func (s *BasicStatement) Close() error

func (*BasicStatement) Exec added in v0.2.0

func (s *BasicStatement) Exec(ctx context.Context, args ...any) (sql.Result, error)

func (*BasicStatement) Query added in v0.2.0

func (s *BasicStatement) Query(ctx context.Context, args ...any) (*sql.Rows, error)

func (*BasicStatement) QueryRow added in v0.2.0

func (s *BasicStatement) QueryRow(ctx context.Context, args ...any) *sql.Row

type Interface

type Interface = database.Interface

Interface defines the common database operations supported by the framework

func NewConnection

func NewConnection(cfg *config.DatabaseConfig, log logger.Logger) (Interface, error)

NewConnection creates a new database connection according to cfg and returns it wrapped with performance tracking. The concrete driver is selected by cfg.Type (supported: "postgresql", "oracle"). If cfg.Type is unsupported an error is returned; if the chosen driver fails to initialize, that underlying error is returned.

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 TrackedConnection added in v0.2.0

type TrackedConnection struct {
	// contains filtered or unexported fields
}

TrackedConnection wraps database.Interface to provide performance tracking

func (*TrackedConnection) Begin added in v0.2.0

func (tc *TrackedConnection) Begin(ctx context.Context) (database.Tx, error)

Begin starts a transaction with tracking wrapper

func (*TrackedConnection) BeginTx added in v0.2.0

func (tc *TrackedConnection) BeginTx(ctx context.Context, opts *sql.TxOptions) (database.Tx, error)

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

func (tc *TrackedConnection) Prepare(ctx context.Context, query string) (database.Statement, error)

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

func (*TrackedConnection) QueryRow added in v0.2.0

func (tc *TrackedConnection) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

QueryRow executes a query that returns at most one row with tracking

func (*TrackedConnection) Stats added in v0.2.0

func (tc *TrackedConnection) Stats() (map[string]any, error)

Stats returns database connection statistics (no tracking needed)

type TrackedDB

type TrackedDB struct {
	*sql.DB
	// contains filtered or unexported fields
}

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

func (db *TrackedDB) PrepareContext(ctx context.Context, query string) (database.Statement, error)

PrepareContext prepares a statement with context and tracks performance

func (*TrackedDB) QueryContext

func (db *TrackedDB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query with context and tracks performance

func (*TrackedDB) QueryRowContext

func (db *TrackedDB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a single row query 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)

func (*TrackedStatement) Exec added in v0.2.0

func (ts *TrackedStatement) Exec(ctx context.Context, args ...any) (sql.Result, error)

Exec executes a prepared statement with tracking

func (*TrackedStatement) Query added in v0.2.0

func (ts *TrackedStatement) Query(ctx context.Context, args ...any) (*sql.Rows, error)

Query executes a prepared query with tracking

func (*TrackedStatement) QueryRow added in v0.2.0

func (ts *TrackedStatement) QueryRow(ctx context.Context, args ...any) *sql.Row

QueryRow executes a prepared query that returns a single row with tracking

type TrackedStmt

type TrackedStmt struct {
	database.Statement
	// contains filtered or unexported fields
}

TrackedStmt wraps database.Statement to provide performance tracking for prepared statements

func (*TrackedStmt) Exec added in v0.2.0

func (s *TrackedStmt) Exec(ctx context.Context, args ...any) (sql.Result, error)

Exec executes a prepared statement with context and tracks performance

func (*TrackedStmt) Query added in v0.2.0

func (s *TrackedStmt) Query(ctx context.Context, args ...any) (*sql.Rows, error)

Query executes a prepared query with context and tracks performance

func (*TrackedStmt) QueryRow added in v0.2.0

func (s *TrackedStmt) QueryRow(ctx context.Context, args ...any) *sql.Row

QueryRow executes a prepared single row query 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

func (tt *TrackedTransaction) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

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

type TrackedTx struct {
	*sql.Tx
	// contains filtered or unexported fields
}

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

func (*TrackedTx) QueryContext

func (tx *TrackedTx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query within a transaction with context and tracks performance

func (*TrackedTx) QueryRowContext

func (tx *TrackedTx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a single row query within a transaction and tracks performance

type TrackingContext added in v0.5.0

type TrackingContext struct {
	Logger   logger.Logger
	Vendor   string
	Settings trackingSettings
}

TrackingContext groups tracking-related parameters to reduce function parameter count

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL