Documentation ¶
Index ¶
- Constants
- func CloseConnectionPoolIfNeeded(ctx context.Context, dbConnectionPool DBConnectionPool) error
- func CloseRows(ctx context.Context, rows *sqlx.Rows)
- func DBTxRollback(ctx context.Context, dbTx DBTransaction, err error, logMessage string)
- func Migrate(dbURL string, dir migrate.MigrationDirection, count int, ...) (int, error)
- func RunInTransaction(ctx context.Context, dbConnectionPool DBConnectionPool, opts *sql.TxOptions, ...) error
- func RunInTransactionWithPostCommit(ctx context.Context, opts *TransactionOptions) error
- func RunInTransactionWithResult[T any](ctx context.Context, dbConnectionPool DBConnectionPool, opts *sql.TxOptions, ...) (result T, err error)
- type AtomicFunctionWithPostCommit
- type ConnectionPoolWithRouter
- func (m ConnectionPoolWithRouter) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
- func (m ConnectionPoolWithRouter) Close() error
- func (m ConnectionPoolWithRouter) DSN(ctx context.Context) (string, error)
- func (m ConnectionPoolWithRouter) Ping(ctx context.Context) error
- func (m ConnectionPoolWithRouter) SqlDB(ctx context.Context) (*sql.DB, error)
- func (m ConnectionPoolWithRouter) SqlxDB(ctx context.Context) (*sqlx.DB, error)
- type DBConnectionPool
- type DBConnectionPoolImplementation
- func (db *DBConnectionPoolImplementation) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
- func (db *DBConnectionPoolImplementation) DSN(ctx context.Context) (string, error)
- func (db *DBConnectionPoolImplementation) Ping(ctx context.Context) error
- func (db *DBConnectionPoolImplementation) SqlDB(ctx context.Context) (*sql.DB, error)
- func (db *DBConnectionPoolImplementation) SqlxDB(ctx context.Context) (*sqlx.DB, error)
- type DBConnectionPoolWithMetrics
- func (dbc *DBConnectionPoolWithMetrics) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
- func (dbc *DBConnectionPoolWithMetrics) Close() error
- func (dbc *DBConnectionPoolWithMetrics) DSN(ctx context.Context) (string, error)
- func (dbc *DBConnectionPoolWithMetrics) Ping(ctx context.Context) error
- func (dbc *DBConnectionPoolWithMetrics) SqlDB(ctx context.Context) (*sql.DB, error)
- func (dbc *DBConnectionPoolWithMetrics) SqlxDB(ctx context.Context) (*sqlx.DB, error)
- type DBTransaction
- type DBTransactionWithMetrics
- type DataSourceRouter
- type MigrationTableName
- type PostCommitFunction
- type QueryType
- type SQLExecuter
- type SQLExecuterWithMetrics
- func (sqlExec *SQLExecuterWithMetrics) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (sqlExec *SQLExecuterWithMetrics) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (sqlExec *SQLExecuterWithMetrics) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (sqlExec *SQLExecuterWithMetrics) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
- func (sqlExec *SQLExecuterWithMetrics) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
- func (sqlExec *SQLExecuterWithMetrics) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- type SQLExecutorWithRouter
- func (m SQLExecutorWithRouter) DriverName() string
- func (s SQLExecutorWithRouter) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s SQLExecutorWithRouter) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- func (s SQLExecutorWithRouter) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (s SQLExecutorWithRouter) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (s SQLExecutorWithRouter) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
- func (s SQLExecutorWithRouter) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
- func (s SQLExecutorWithRouter) Rebind(query string) string
- func (s SQLExecutorWithRouter) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
- type TransactionOptions
Constants ¶
const ( MaxDBConnIdleTime = 10 * time.Second MaxOpenDBConns = 30 )
Variables ¶
This section is empty.
Functions ¶
func CloseConnectionPoolIfNeeded ¶
func CloseConnectionPoolIfNeeded(ctx context.Context, dbConnectionPool DBConnectionPool) error
CloseConnectionPoolIfNeeded closes the given DB connection pool if it's open and not nil.
func DBTxRollback ¶
func DBTxRollback(ctx context.Context, dbTx DBTransaction, err error, logMessage string)
DBTxRollback rolls back the transaction if there is an error.
func Migrate ¶
func Migrate(dbURL string, dir migrate.MigrationDirection, count int, migrationRouter migrations.MigrationRouter) (int, error)
func RunInTransaction ¶
func RunInTransaction(ctx context.Context, dbConnectionPool DBConnectionPool, opts *sql.TxOptions, atomicFunction func(dbTx DBTransaction) error) error
RunInTransaction runs the given atomic function in an atomic database transaction and returns an error. Boilerplate code for database transactions.
func RunInTransactionWithPostCommit ¶
func RunInTransactionWithPostCommit(ctx context.Context, opts *TransactionOptions) error
RunInTransactionWithPostCommit runs the given atomic function in an atomic database transaction. If the atomic function succeeds, it returns a postCommit function to be executed after the transaction is committed.
func RunInTransactionWithResult ¶
func RunInTransactionWithResult[T any](ctx context.Context, dbConnectionPool DBConnectionPool, opts *sql.TxOptions, atomicFunction func(dbTx DBTransaction) (T, error)) (result T, err error)
RunInTransactionWithResult runs the given atomic function in an atomic database transaction and returns a result and an error. Boilerplate code for database transactions.
Types ¶
type AtomicFunctionWithPostCommit ¶
type AtomicFunctionWithPostCommit func(dbTx DBTransaction) (PostCommitFunction, error)
type ConnectionPoolWithRouter ¶
type ConnectionPoolWithRouter struct {
SQLExecutorWithRouter
}
ConnectionPoolWithRouter implements the DBConnectionPool interface
func NewConnectionPoolWithRouter ¶
func NewConnectionPoolWithRouter(dataSourceRouter DataSourceRouter) (*ConnectionPoolWithRouter, error)
NewConnectionPoolWithRouter creates a new ConnectionPoolWithRouter
func (ConnectionPoolWithRouter) BeginTxx ¶
func (m ConnectionPoolWithRouter) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
func (ConnectionPoolWithRouter) Close ¶
func (m ConnectionPoolWithRouter) Close() error
func (ConnectionPoolWithRouter) DSN ¶
func (m ConnectionPoolWithRouter) DSN(ctx context.Context) (string, error)
func (ConnectionPoolWithRouter) Ping ¶
func (m ConnectionPoolWithRouter) Ping(ctx context.Context) error
type DBConnectionPool ¶
type DBConnectionPool interface { SQLExecuter BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error) Close() error Ping(ctx context.Context) error SqlDB(ctx context.Context) (*sql.DB, error) SqlxDB(ctx context.Context) (*sqlx.DB, error) DSN(ctx context.Context) (string, error) }
DBConnectionPool is an interface that wraps the sqlx.DB structs methods and includes the RunInTransaction helper.
func OpenDBConnectionPool ¶
func OpenDBConnectionPool(dataSourceName string) (DBConnectionPool, error)
OpenDBConnectionPool opens a new database connection pool. It returns an error if it can't connect to the database.
func OpenDBConnectionPoolWithMetrics ¶
func OpenDBConnectionPoolWithMetrics(dataSourceName string, monitorService monitor.MonitorServiceInterface) (DBConnectionPool, error)
OpenDBConnectionPoolWithMetrics opens a new database connection pool with the monitor service. It returns an error if it can't connect to the database.
type DBConnectionPoolImplementation ¶
DBConnectionPoolImplementation is a wrapper around sqlx.DB that implements DBConnectionPool.
func (*DBConnectionPoolImplementation) BeginTxx ¶
func (db *DBConnectionPoolImplementation) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
func (*DBConnectionPoolImplementation) DSN ¶
func (db *DBConnectionPoolImplementation) DSN(ctx context.Context) (string, error)
func (*DBConnectionPoolImplementation) Ping ¶
func (db *DBConnectionPoolImplementation) Ping(ctx context.Context) error
type DBConnectionPoolWithMetrics ¶
type DBConnectionPoolWithMetrics struct { SQLExecuterWithMetrics // contains filtered or unexported fields }
DBConnectionPoolWithMetrics is a wrapper around sqlx.DB that implements DBConnectionPool with the monitoring service.
func NewDBConnectionPoolWithMetrics ¶
func NewDBConnectionPoolWithMetrics(dbConnectionPool DBConnectionPool, monitorServiceInterface monitor.MonitorServiceInterface) (*DBConnectionPoolWithMetrics, error)
func (*DBConnectionPoolWithMetrics) BeginTxx ¶
func (dbc *DBConnectionPoolWithMetrics) BeginTxx(ctx context.Context, opts *sql.TxOptions) (DBTransaction, error)
func (*DBConnectionPoolWithMetrics) Close ¶
func (dbc *DBConnectionPoolWithMetrics) Close() error
func (*DBConnectionPoolWithMetrics) DSN ¶
func (dbc *DBConnectionPoolWithMetrics) DSN(ctx context.Context) (string, error)
func (*DBConnectionPoolWithMetrics) Ping ¶
func (dbc *DBConnectionPoolWithMetrics) Ping(ctx context.Context) error
type DBTransaction ¶
type DBTransaction interface { SQLExecuter Rollback() error Commit() error }
DBTransaction is an interface that wraps the sqlx.Tx structs methods.
type DBTransactionWithMetrics ¶
type DBTransactionWithMetrics struct { SQLExecuterWithMetrics // contains filtered or unexported fields }
DBTransactionWithMetrics is an interface that implements DBTransaction with the monitoring service.
func NewDBTransactionWithMetrics ¶
func NewDBTransactionWithMetrics(dbTransaction DBTransaction, monitorServiceInterface monitor.MonitorServiceInterface) (*DBTransactionWithMetrics, error)
func (*DBTransactionWithMetrics) Commit ¶
func (dbTx *DBTransactionWithMetrics) Commit() error
func (*DBTransactionWithMetrics) Rollback ¶
func (dbTx *DBTransactionWithMetrics) Rollback() error
type DataSourceRouter ¶
type DataSourceRouter interface { GetDataSource(ctx context.Context) (DBConnectionPool, error) GetAllDataSources() ([]DBConnectionPool, error) AnyDataSource() (DBConnectionPool, error) }
type MigrationTableName ¶
type MigrationTableName string
type PostCommitFunction ¶
type PostCommitFunction func() error
type SQLExecuter ¶
type SQLExecuter interface { DriverName() string ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error sqlx.PreparerContext sqlx.QueryerContext Rebind(query string) string SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error }
SQLExecuter is an interface that wraps the *sqlx.DB and *sqlx.Tx structs methods.
type SQLExecuterWithMetrics ¶
type SQLExecuterWithMetrics struct { SQLExecuter // contains filtered or unexported fields }
SQLExecuterWithMetrics is a wrapper around SQLExecuter that implements the monitoring service.
func NewSQLExecuterWithMetrics ¶
func NewSQLExecuterWithMetrics(sqlExec SQLExecuter, monitorServiceInterface monitor.MonitorServiceInterface) (*SQLExecuterWithMetrics, error)
func (*SQLExecuterWithMetrics) ExecContext ¶
func (sqlExec *SQLExecuterWithMetrics) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContext is a wrapper around DBConnetionPool ExecContext that includes monitoring the db query.
func (*SQLExecuterWithMetrics) GetContext ¶
func (sqlExec *SQLExecuterWithMetrics) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
QueryContext is a wrapper around QueryerContext interface QueryContext that includes monitoring the db query.
func (*SQLExecuterWithMetrics) QueryContext ¶
func (sqlExec *SQLExecuterWithMetrics) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryContext is a wrapper around QueryerContext interface QueryContext that includes monitoring the db query.
func (*SQLExecuterWithMetrics) QueryRowxContext ¶
func (sqlExec *SQLExecuterWithMetrics) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row
QueryRowxContext is a wrapper around QueryerContext interface QueryRowxContext that includes monitoring the db query.
func (*SQLExecuterWithMetrics) QueryxContext ¶
func (sqlExec *SQLExecuterWithMetrics) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)
QueryxContext is a wrapper around QueryerContext interface QueryxContext that includes monitoring the db query.
func (*SQLExecuterWithMetrics) SelectContext ¶
func (sqlExec *SQLExecuterWithMetrics) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
SelectContext is a wrapper around DBConnetionPool SelectContext that includes monitoring the db query.
type SQLExecutorWithRouter ¶
type SQLExecutorWithRouter struct {
// contains filtered or unexported fields
}
func NewSQLExecutorWithRouter ¶
func NewSQLExecutorWithRouter(router DataSourceRouter) (*SQLExecutorWithRouter, error)
func (SQLExecutorWithRouter) DriverName ¶
func (m SQLExecutorWithRouter) DriverName() string
func (SQLExecutorWithRouter) ExecContext ¶
func (SQLExecutorWithRouter) GetContext ¶
func (s SQLExecutorWithRouter) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
func (SQLExecutorWithRouter) PrepareContext ¶
func (SQLExecutorWithRouter) QueryContext ¶
func (SQLExecutorWithRouter) QueryRowxContext ¶
func (SQLExecutorWithRouter) QueryxContext ¶
func (SQLExecutorWithRouter) Rebind ¶
func (s SQLExecutorWithRouter) Rebind(query string) string
func (SQLExecutorWithRouter) SelectContext ¶
func (s SQLExecutorWithRouter) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error
type TransactionOptions ¶
type TransactionOptions struct { DBConnectionPool DBConnectionPool AtomicFunctionWithPostCommit AtomicFunctionWithPostCommit TxOptions *sql.TxOptions }