database

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2025 License: MIT Imports: 14 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 (
	DefaultSlowQueryThreshold = tracking.DefaultSlowQueryThreshold
	DefaultMaxQueryLength     = tracking.DefaultMaxQueryLength
)

Re-export internal constants

View Source
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

View Source
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

func ValidateDatabaseType(dbType string) error

ValidateDatabaseType reports an error when dbType is not among the supported database types.

Types

type Connector added in v0.9.0

type Connector func(*config.DatabaseConfig, logger.Logger) (Interface, error)

Connector creates database connections from configuration

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) Close added in v0.9.0

func (m *DbManager) Close() error

Close closes all database connections and stops cleanup

func (*DbManager) Get added in v0.9.0

func (m *DbManager) Get(ctx context.Context, key string) (Interface, error)

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) Size added in v0.9.0

func (m *DbManager) Size() int

Size returns the number of active connections

func (*DbManager) StartCleanup added in v0.9.0

func (m *DbManager) StartCleanup(interval time.Duration)

StartCleanup starts the background cleanup routine for idle connections

func (*DbManager) Stats added in v0.9.0

func (m *DbManager) Stats() map[string]any

Stats returns statistics about the connection pool

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

type Interface = types.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

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

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

type Statement = types.Statement

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 TrackedDB

type TrackedDB = tracking.DB

Re-export the internal tracking implementation as the public API

type TrackedStatement added in v0.2.0

type TrackedStatement = tracking.Statement

Re-export the internal tracking implementation as the public API

type TrackedStmt

type TrackedStmt = tracking.Statement

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

type TrackingContext = tracking.Context

Re-export the internal tracking implementation as the public API

type Tx added in v0.8.0

type Tx = types.Tx

Tx defines the interface for database transactions. This type alias maintains backward compatibility.

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.

Jump to

Keyboard shortcuts

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