Documentation
¶
Overview ¶
Package dbkit provides helpers for working with different SQL databases (MySQL, PostgreSQL, SQLite, and MSSQL).
Example ¶
// Configure the database using the dbkit.Config struct. // In this example, we're using MySQL. Adjust Dialect and config fields for your target DB. cfg := &dbkit.Config{ Dialect: dbkit.DialectMySQL, MySQL: dbkit.MySQLConfig{ Host: os.Getenv("MYSQL_HOST"), Port: 3306, User: os.Getenv("MYSQL_USER"), Password: os.Getenv("MYSQL_PASSWORD"), Database: os.Getenv("MYSQL_DATABASE"), }, MaxOpenConns: 16, MaxIdleConns: 8, } // Open the database connection. // The 2nd parameter is a boolean that indicates whether to ping the database. db, err := dbkit.Open(cfg, true) if err != nil { log.Fatalf("failed to open database: %v", err) } defer db.Close() // Execute a transaction with a custom retry policy (exponential backoff with 3 retries, starting from 10ms). retryPolicy := retry.NewConstantBackoffPolicy(10*time.Millisecond, 3) if err = dbkit.DoInTx(context.Background(), db, func(tx *sql.Tx) error { // Execute your transactional operations here. // Example: _, err := tx.Exec("UPDATE users SET last_login = ? WHERE id = ?", time.Now(), 1) return nil }, dbkit.WithRetryPolicy(retryPolicy)); err != nil { log.Fatal(err) }
Index ¶
- Constants
- Variables
- func DoInTx(ctx context.Context, dbConn *sql.DB, fn func(tx *sql.Tx) error, ...) (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 Open(cfg *Config, ping bool) (*sql.DB, error)
- func RegisterIsRetryableFunc(d driver.Driver, retryable retry.IsRetryable)
- func UnregisterAllIsRetryableFuncs(d driver.Driver)
- 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 ConfigOption
- type Dialect
- type DoInTxOption
- type IsolationLevel
- func (il IsolationLevel) MarshalJSON() ([]byte, error)
- func (il *IsolationLevel) MarshalText() ([]byte, error)
- func (il IsolationLevel) MarshalYAML() (interface{}, error)
- func (il IsolationLevel) String() string
- func (il *IsolationLevel) UnmarshalJSON(data []byte) error
- func (il *IsolationLevel) UnmarshalText(text []byte) error
- func (il *IsolationLevel) UnmarshalYAML(value *yaml.Node) error
- type MSSQLConfig
- type MySQLConfig
- type PostgresConfig
- type PostgresSSLMode
- type PrometheusMetrics
- func (pm *PrometheusMetrics) AllMetrics() []prometheus.Collector
- func (pm *PrometheusMetrics) MustCurryWith(labels prometheus.Labels) *PrometheusMetrics
- func (pm *PrometheusMetrics) MustRegister()
- func (pm *PrometheusMetrics) ObserveQueryDuration(query string, duration time.Duration)
- func (pm *PrometheusMetrics) Unregister()
- type PrometheusMetricsOpts
- type SQLiteConfig
Examples ¶
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 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.
const PrometheusMetricsLabelQuery = "query"
PrometheusMetricsLabelQuery is a label name for SQL query in Prometheus metrics.
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 ¶
func DoInTx(ctx context.Context, dbConn *sql.DB, fn func(tx *sql.Tx) error, options ...DoInTxOption) (err error)
DoInTx begins a new transaction, calls passed function and do commit or rollback depending on whether the function returns an error or not.
func GetIsRetryable ¶
func GetIsRetryable(d driver.Driver) retry.IsRetryable
GetIsRetryable returns a function that can tell for a 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 Open ¶ added in v0.4.0
Open opens a new database connection using the provided configuration. If ping is true, it will check the connection by sending a ping to the 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()
func UnregisterAllIsRetryableFuncs ¶ added in v0.4.0
UnregisterAllIsRetryableFuncs removes previously registered IsRetryable function for the given driver.
Types ¶
type Config ¶
type Config struct { Dialect Dialect `mapstructure:"dialect" yaml:"dialect" json:"dialect"` MaxOpenConns int `mapstructure:"maxOpenConns" yaml:"maxOpenConns" json:"maxOpenConns"` MaxIdleConns int `mapstructure:"maxIdleConns" yaml:"maxIdleConns" json:"maxIdleConns"` ConnMaxLifetime config.TimeDuration `mapstructure:"connMaxLifeTime" yaml:"connMaxLifeTime" json:"connMaxLifeTime"` MySQL MySQLConfig `mapstructure:"mysql" yaml:"mysql" json:"mysql"` MSSQL MSSQLConfig `mapstructure:"mssql" yaml:"mssql" json:"mssql"` SQLite SQLiteConfig `mapstructure:"sqlite3" yaml:"sqlite3" json:"sqlite3"` Postgres PostgresConfig `mapstructure:"postgres" yaml:"postgres" json:"postgres"` // contains filtered or unexported fields }
Config represents a set of configuration parameters working with SQL databases.
func NewConfig ¶
func NewConfig(supportedDialects []Dialect, options ...ConfigOption) *Config
NewConfig creates a new instance of the Config.
func NewConfigWithKeyPrefix ¶
NewConfigWithKeyPrefix creates a new instance of the Config with a key prefix. This prefix will be used by config.Loader. Deprecated: use NewConfig with WithKeyPrefix instead.
func NewDefaultConfig ¶ added in v0.4.0
func NewDefaultConfig(supportedDialects []Dialect, options ...ConfigOption) *Config
NewDefaultConfig creates a new instance of the Config with default values.
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. Implements config.KeyPrefixProvider interface.
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 the 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 ConfigOption ¶ added in v0.4.0
type ConfigOption func(*configOptions)
ConfigOption is a type for functional options for the Config.
func WithKeyPrefix ¶ added in v0.4.0
func WithKeyPrefix(keyPrefix string) ConfigOption
WithKeyPrefix returns a ConfigOption that sets a key prefix for parsing configuration parameters. This prefix will be used by config.Loader.
type Dialect ¶
type Dialect string
Dialect defines possible values for planned supported SQL dialects.
type DoInTxOption ¶ added in v0.4.0
type DoInTxOption func(*doInTxOptions)
DoInTxOption is a functional option for DoInTx.
func WithRetryPolicy ¶ added in v0.4.0
func WithRetryPolicy(policy retry.Policy) DoInTxOption
WithRetryPolicy sets retry policy for DoInTx.
func WithTxOptions ¶ added in v0.4.0
func WithTxOptions(txOpts *sql.TxOptions) DoInTxOption
WithTxOptions sets transaction options for DoInTx.
type IsolationLevel ¶ added in v0.4.0
type IsolationLevel sql.IsolationLevel
func (IsolationLevel) MarshalJSON ¶ added in v0.4.0
func (il IsolationLevel) MarshalJSON() ([]byte, error)
MarshalJSON encodes as a human-readable string in JSON. Implements json.Marshaler interface.
func (*IsolationLevel) MarshalText ¶ added in v0.4.0
func (il *IsolationLevel) MarshalText() ([]byte, error)
MarshalText encodes as a human-readable string in text. Implements encoding.TextMarshaler interface.
func (IsolationLevel) MarshalYAML ¶ added in v0.4.0
func (il IsolationLevel) MarshalYAML() (interface{}, error)
MarshalYAML encodes as a human-readable string in YAML. Implements yaml.Marshaler interface.
func (IsolationLevel) String ¶ added in v0.4.0
func (il IsolationLevel) String() string
String returns the human-readable string representation. Implements fmt.Stringer interface.
func (*IsolationLevel) UnmarshalJSON ¶ added in v0.4.0
func (il *IsolationLevel) UnmarshalJSON(data []byte) error
UnmarshalJSON allows decoding string representation of isolation level from JSON. Implements json.Unmarshaler interface.
func (*IsolationLevel) UnmarshalText ¶ added in v0.4.0
func (il *IsolationLevel) UnmarshalText(text []byte) error
UnmarshalText allows decoding from text. Implements encoding.TextUnmarshaler interface, which is used by mapstructure.TextUnmarshallerHookFunc.
func (*IsolationLevel) UnmarshalYAML ¶ added in v0.4.0
func (il *IsolationLevel) UnmarshalYAML(value *yaml.Node) error
UnmarshalYAML allows decoding from YAML. Implements yaml.Unmarshaler interface.
type MSSQLConfig ¶
type MSSQLConfig struct { Host string `mapstructure:"host" yaml:"host" json:"host"` Port int `mapstructure:"port" yaml:"port" json:"port"` User string `mapstructure:"user" yaml:"user" json:"user"` Password string `mapstructure:"password" yaml:"password" json:"password"` Database string `mapstructure:"database" yaml:"database" json:"database"` TxIsolationLevel IsolationLevel `mapstructure:"txLevel" yaml:"txLevel" json:"txLevel"` AdditionalParameters map[string]string `mapstructure:"additionalParameters" yaml:"additionalParameters" json:"additionalParameters"` }
MSSQLConfig represents a set of configuration parameters for working with MSSQL.
type MySQLConfig ¶
type MySQLConfig struct { Host string `mapstructure:"host" yaml:"host" json:"host"` Port int `mapstructure:"port" yaml:"port" json:"port"` User string `mapstructure:"user" yaml:"user" json:"user"` Password string `mapstructure:"password" yaml:"password" json:"password"` Database string `mapstructure:"database" yaml:"database" json:"database"` TxIsolationLevel IsolationLevel `mapstructure:"txLevel" yaml:"txLevel" json:"txLevel"` }
MySQLConfig represents a set of configuration parameters for working with MySQL.
type PostgresConfig ¶
type PostgresConfig struct { Host string `mapstructure:"host" yaml:"host" json:"host"` Port int `mapstructure:"port" yaml:"port" json:"port"` User string `mapstructure:"user" yaml:"user" json:"user"` Password string `mapstructure:"password" yaml:"password" json:"password"` Database string `mapstructure:"database" yaml:"database" json:"database"` TxIsolationLevel IsolationLevel `mapstructure:"txLevel" yaml:"txLevel" json:"txLevel"` SSLMode PostgresSSLMode `mapstructure:"sslMode" yaml:"sslMode" json:"sslMode"` SearchPath string `mapstructure:"searchPath" yaml:"searchPath" json:"searchPath"` AdditionalParameters map[string]string `mapstructure:"additionalParameters" yaml:"additionalParameters" json:"additionalParameters"` }
PostgresConfig represents a set of configuration parameters for working with Postgres.
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 PrometheusMetrics ¶ added in v0.4.0
type PrometheusMetrics struct {
QueryDurations *prometheus.HistogramVec
}
PrometheusMetrics represents collector of metrics.
func NewPrometheusMetrics ¶ added in v0.4.0
func NewPrometheusMetrics() *PrometheusMetrics
NewPrometheusMetrics creates a new metrics collector.
func NewPrometheusMetricsWithOpts ¶ added in v0.4.0
func NewPrometheusMetricsWithOpts(opts PrometheusMetricsOpts) *PrometheusMetrics
NewPrometheusMetricsWithOpts is a more configurable version of creating PrometheusMetrics.
func (*PrometheusMetrics) AllMetrics ¶ added in v0.4.0
func (pm *PrometheusMetrics) AllMetrics() []prometheus.Collector
AllMetrics returns a list of metrics of this collector. This can be used to register these metrics in push gateway.
func (*PrometheusMetrics) MustCurryWith ¶ added in v0.4.0
func (pm *PrometheusMetrics) MustCurryWith(labels prometheus.Labels) *PrometheusMetrics
MustCurryWith curries the metrics collector with the provided labels.
func (*PrometheusMetrics) MustRegister ¶ added in v0.4.0
func (pm *PrometheusMetrics) MustRegister()
MustRegister does registration of metrics collector in Prometheus and panics if any error occurs.
func (*PrometheusMetrics) ObserveQueryDuration ¶ added in v0.4.0
func (pm *PrometheusMetrics) ObserveQueryDuration(query string, duration time.Duration)
ObserveQueryDuration observes the duration of executing SQL query.
func (*PrometheusMetrics) Unregister ¶ added in v0.4.0
func (pm *PrometheusMetrics) Unregister()
Unregister cancels registration of metrics collector in Prometheus.
type PrometheusMetricsOpts ¶ added in v0.5.0
type PrometheusMetricsOpts 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 PrometheusMetrics.MustCurryWith method for more details. // Keep in mind that if this list is not empty, // PrometheusMetrics.MustCurryWith method must be called further with the same labels. // Otherwise, the collector will panic. CurriedLabelNames []string }
PrometheusMetricsOpts represents an options for PrometheusMetrics.
type SQLiteConfig ¶
type SQLiteConfig struct {
Path string `mapstructure:"path" yaml:"path" json:"path"`
}
SQLiteConfig represents a set of configuration parameters for working with SQLite.
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 with the MySQL database using the github.com/go-sql-driver/mysql driver.
|
Package mysql provides helpers for working with the MySQL database using the github.com/go-sql-driver/mysql driver. |
Package pgx provides helpers for working with the Postgres database using the github.com/jackc/pgx driver.
|
Package pgx provides helpers for working with the Postgres database using the github.com/jackc/pgx driver. |
Package postgres provides helpers for working with the Postgres database using the github.com/lib/pq driver.
|
Package postgres provides helpers for working with the Postgres database using the github.com/lib/pq driver. |
Package sqlite provides helpers for working SQLite database.
|
Package sqlite provides helpers for working SQLite database. |