Documentation ¶
Overview ¶
Package dbkit provides helpers for working with different SQL databases (MySQL, PostgreSQL, SQLite, and MSSQL).
Index ¶
- Constants
- Variables
- func DoInTx(ctx context.Context, dbConn *sql.DB, fn func(tx *sql.Tx) error) (err error)
- func DoInTxWithOpts(ctx context.Context, dbConn *sql.DB, txOpts *sql.TxOptions, ...) (err error)
- func GetIsRetryable(d driver.Driver) retry.IsRetryable
- func InitOpenedDB(db *sql.DB, cfg *Config, ping bool) error
- func MakeMSSQLDSN(cfg *MSSQLConfig) string
- func MakeMySQLDSN(cfg *MySQLConfig) string
- func MakePostgresDSN(cfg *PostgresConfig) string
- func MakeSQLiteDSN(cfg *SQLiteConfig) string
- func RegisterIsRetryableFunc(d driver.Driver, retryable retry.IsRetryable)
- type Config
- func (c *Config) DriverNameAndDSN() (driverName, dsn string)
- func (c *Config) KeyPrefix() string
- func (c *Config) Set(dp config.DataProvider) error
- func (c *Config) SetProviderDefaults(dp config.DataProvider)
- func (c *Config) SupportedDialects() []Dialect
- func (c *Config) TxIsolationLevel() sql.IsolationLevel
- type Dialect
- type MSSQLConfig
- type MetricsCollector
- type MetricsCollectorOpts
- type MySQLConfig
- type Parameter
- type PostgresConfig
- type PostgresErrCode
- type PostgresSSLMode
- type SQLiteConfig
Constants ¶
const ( DefaultMaxIdleConns = 2 DefaultMaxOpenConns = 10 DefaultConnMaxLifetime = 10 * time.Minute // Official recommendation from the DBA team )
Default values of connection parameters
const MSSQLDefaultTxLevel = sql.LevelReadCommitted
MSSQLDefaultTxLevel contains transaction isolation level which will be used by default for MSSQL.
const (
MetricsLabelQuery = "query"
)
Prometheus labels.
const MySQLDefaultTxLevel = sql.LevelReadCommitted
MySQLDefaultTxLevel contains transaction isolation level which will be used by default for MySQL.
const PgReadWriteParam = "read-write"
PgReadWriteParam read-write session attribute value name
const PgTargetSessionAttrs = "target_session_attrs"
PgTargetSessionAttrs session attrs parameter name
const PostgresDefaultSSLMode = PostgresSSLModeVerifyCA
PostgresDefaultSSLMode contains Postgres SSL mode which will be used by default.
const PostgresDefaultTxLevel = sql.LevelReadCommitted
PostgresDefaultTxLevel contains transaction isolation level which will be used by default for Postgres.
Variables ¶
var DefaultQueryDurationBuckets = []float64{0.001, 0.01, 0.1, 0.25, 0.5, 1, 2.5, 5, 10}
DefaultQueryDurationBuckets is default buckets into which observations of executing SQL queries are counted.
Functions ¶
func DoInTx ¶
DoInTx begins a new transaction, calls passed function and do commit or rollback depending on whether the function returns an error or not.
func DoInTxWithOpts ¶
func DoInTxWithOpts(ctx context.Context, dbConn *sql.DB, txOpts *sql.TxOptions, fn func(tx *sql.Tx) error) (err error)
DoInTxWithOpts is a bit more configurable version of DoInTx that allows passing tx options.
func GetIsRetryable ¶
func GetIsRetryable(d driver.Driver) retry.IsRetryable
GetIsRetryable returns a function that can tell for given driver if error is retryable.
func InitOpenedDB ¶
InitOpenedDB initializes early opened *sql.DB instance.
func MakeMSSQLDSN ¶
func MakeMSSQLDSN(cfg *MSSQLConfig) string
MakeMSSQLDSN makes DSN for opening MSSQL database.
func MakeMySQLDSN ¶
func MakeMySQLDSN(cfg *MySQLConfig) string
MakeMySQLDSN makes DSN for opening MySQL database.
func MakePostgresDSN ¶
func MakePostgresDSN(cfg *PostgresConfig) string
MakePostgresDSN makes DSN for opening Postgres database.
func MakeSQLiteDSN ¶
func MakeSQLiteDSN(cfg *SQLiteConfig) string
MakeSQLiteDSN makes DSN for opening SQLite database.
func RegisterIsRetryableFunc ¶
func RegisterIsRetryableFunc(d driver.Driver, retryable retry.IsRetryable)
RegisterIsRetryableFunc registers callback to determinate specific DB error is retryable or not. Several registered functions will be called one after another in FIFO order before some function returns true. Note: this function is not concurrent-safe. Typical scenario: register all custom IsRetryable in module init()
Types ¶
type Config ¶
type Config struct { Dialect Dialect MaxOpenConns int MaxIdleConns int ConnMaxLifetime time.Duration MySQL MySQLConfig MSSQL MSSQLConfig SQLite SQLiteConfig Postgres PostgresConfig // contains filtered or unexported fields }
Config represents a set of configuration parameters working with SQL databases.
func NewConfigWithKeyPrefix ¶
NewConfigWithKeyPrefix creates a new instance of the Config. Allows to specify key prefix which will be used for parsing configuration parameters.
func (*Config) DriverNameAndDSN ¶
DriverNameAndDSN returns driver name and DSN for connecting.
func (*Config) KeyPrefix ¶
KeyPrefix returns a key prefix with which all configuration parameters should be presented.
func (*Config) Set ¶
func (c *Config) Set(dp config.DataProvider) error
Set sets configuration values from config.DataProvider.
func (*Config) SetProviderDefaults ¶
func (c *Config) SetProviderDefaults(dp config.DataProvider)
SetProviderDefaults sets default configuration values in config.DataProvider.
func (*Config) SupportedDialects ¶
SupportedDialects returns list of supported dialects.
func (*Config) TxIsolationLevel ¶
func (c *Config) TxIsolationLevel() sql.IsolationLevel
TxIsolationLevel returns transaction isolation level from parsed config for specified dialect.
type Dialect ¶
type Dialect string
Dialect defines possible values for planned supported SQL dialects.
type MSSQLConfig ¶
type MSSQLConfig struct { Host string Port int User string Password string Database string TxIsolationLevel sql.IsolationLevel }
MSSQLConfig represents a set of configuration parameters for working with MSSQL.
type MetricsCollector ¶
type MetricsCollector struct {
QueryDurations *prometheus.HistogramVec
}
MetricsCollector represents collector of metrics.
func NewMetricsCollector ¶
func NewMetricsCollector() *MetricsCollector
NewMetricsCollector creates a new metrics collector.
func NewMetricsCollectorWithOpts ¶
func NewMetricsCollectorWithOpts(opts MetricsCollectorOpts) *MetricsCollector
NewMetricsCollectorWithOpts is a more configurable version of creating MetricsCollector.
func (*MetricsCollector) AllMetrics ¶
func (c *MetricsCollector) AllMetrics() []prometheus.Collector
AllMetrics returns a list of metrics of this collector. This can be used to register these metrics in push gateway.
func (*MetricsCollector) MustCurryWith ¶
func (c *MetricsCollector) MustCurryWith(labels prometheus.Labels) *MetricsCollector
MustCurryWith curries the metrics collector with the provided labels.
func (*MetricsCollector) MustRegister ¶
func (c *MetricsCollector) MustRegister()
MustRegister does registration of metrics collector in Prometheus and panics if any error occurs.
func (*MetricsCollector) Unregister ¶
func (c *MetricsCollector) Unregister()
Unregister cancels registration of metrics collector in Prometheus.
type MetricsCollectorOpts ¶
type MetricsCollectorOpts struct { // Namespace is a namespace for metrics. It will be prepended to all metric names. Namespace string // QueryDurationBuckets is a list of buckets into which observations of executing SQL queries are counted. QueryDurationBuckets []float64 // ConstLabels is a set of labels that will be applied to all metrics. ConstLabels prometheus.Labels // CurryingLabelNames is a list of label names that will be curried with the provided labels. // See MetricsCollector.MustCurryWith method for more details. // Keep in mind that if this list is not empty, // MetricsCollector.MustCurryWith method must be called further with the same labels. // Otherwise, the collector will panic. CurriedLabelNames []string }
MetricsCollectorOpts represents an options for MetricsCollector.
type MySQLConfig ¶
type MySQLConfig struct { Host string Port int User string Password string Database string TxIsolationLevel sql.IsolationLevel }
MySQLConfig represents a set of configuration parameters for working with MySQL.
type Parameter ¶
Parameter represent DB connection parameter. Value will be url-encoded before adding into the connection string.
type PostgresConfig ¶
type PostgresConfig struct { Host string Port int User string Password string Database string TxIsolationLevel sql.IsolationLevel SSLMode PostgresSSLMode SearchPath string AdditionalParameters []Parameter }
PostgresConfig represents a set of configuration parameters for working with Postgres.
type PostgresErrCode ¶
type PostgresErrCode string
PostgresErrCode defines the type for Postgres error codes.
const ( PgxErrCodeUniqueViolation PostgresErrCode = "23505" PgxErrCodeDeadlockDetected PostgresErrCode = "40P01" PgxErrCodeSerializationFailure PostgresErrCode = "40001" PgxErrFeatureNotSupported PostgresErrCode = "0A000" // nolint: staticcheck // lib/pq using is deprecated. Use pgx Postgres driver. PostgresErrCodeUniqueViolation PostgresErrCode = "unique_violation" // nolint: staticcheck // lib/pq using is deprecated. Use pgx Postgres driver. PostgresErrCodeDeadlockDetected PostgresErrCode = "deadlock_detected" PostgresErrCodeSerializationFailure PostgresErrCode = "serialization_failure" )
Postgres error codes (will be filled gradually).
type PostgresSSLMode ¶
type PostgresSSLMode string
PostgresSSLMode defines possible values for Postgres sslmode connection parameter.
const ( PostgresSSLModeDisable PostgresSSLMode = "disable" PostgresSSLModeRequire PostgresSSLMode = "require" PostgresSSLModeVerifyCA PostgresSSLMode = "verify-ca" PostgresSSLModeVerifyFull PostgresSSLMode = "verify-full" )
Postgres SSL modes.
type SQLiteConfig ¶
type SQLiteConfig struct {
Path string
}
SQLiteConfig represents a set of configuration parameters for working with SQLite.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package dbrutil provides utilities and helpers for dbr query builder.
|
Package dbrutil provides utilities and helpers for dbr query builder. |
dbrtest
Package dbrtest provides objects and helpers for writings tests for code that uses dbr and dbrutils packages.
|
Package dbrtest provides objects and helpers for writings tests for code that uses dbr and dbrutils packages. |
Package distrlock contains DML (distributed lock manager) implementation (now DMLs based on MySQL and PostgreSQL are supported).
|
Package distrlock contains DML (distributed lock manager) implementation (now DMLs based on MySQL and PostgreSQL are supported). |
Package goquutil provides auxiliary routines for working with goqu query builder.
|
Package goquutil provides auxiliary routines for working with goqu query builder. |
internal
|
|
testing
Package testing contains internal testing utilities we apply in go-dbkit.
|
Package testing contains internal testing utilities we apply in go-dbkit. |
Package migrate provides functionality for applying database migrations.
|
Package migrate provides functionality for applying database migrations. |
Package mssql provides helpers for working MSSQL database.
|
Package mssql provides helpers for working MSSQL database. |
Package mysql provides helpers for working MySQL database.
|
Package mysql provides helpers for working MySQL database. |
Package pgx provides helpers for working Postgres database via jackc/pgx driver.
|
Package pgx provides helpers for working Postgres database via jackc/pgx driver. |
Package postgres provides helpers for working Postgres database.
|
Package postgres provides helpers for working Postgres database. |
Package sqlite provides helpers for working SQLite database.
|
Package sqlite provides helpers for working SQLite database. |