Versions in this module Expand all Collapse all v1 v1.0.0 Nov 20, 2026 Changes in this version + const ChDriver + const DefaultMigrationsDirectory + const DefaultSQLiteBusyTimeoutMs + const DefaultSQLiteJournalMode + const DefaultSQLiteSynchronous + const DefaultSQLiteTxLock + const PgxDriver + const PqDriver + const ReasonCheckFailed + const ReasonLagExceeded + const ReasonNoReplica + const ReasonNotChecked + const ReasonOK + const ReasonReplayUnknown + const SQLiteDriver + var ErrCheckViolation = errors.New("sqlkit.check_violation") + var ErrClusterNoPrimary = errors.New("sqlkit.cluster.no_primary") + var ErrClusterOption = errors.New("sqlkit.cluster.option") + var ErrConfigNoDSN = errors.New("sqlkit.config.no_dsn") + var ErrConfigNoDatabase = errors.New("sqlkit.config.no_database") + var ErrConfigNoHost = errors.New("sqlkit.config.no_host") + var ErrConfigNoPassword = errors.New("sqlkit.config.no_password") + var ErrConfigNoPort = errors.New("sqlkit.config.no_port") + var ErrConfigNoUsername = errors.New("sqlkit.config.no_username") + var ErrConnNotSelected = errors.New("sqlkit.connection.not_selected") + var ErrConnectionIsNotShard = errors.New("sqlkit.connection_is_not_shard") + var ErrConnectionKeyEmpty = errors.New("sqlkit.connection_key_empty") + var ErrConnectionStringDuplicate = errors.New("sqlkit.connection_string_duplicate") + var ErrConnectionStringEmpty = errors.New("sqlkit.connection_string_empty") + var ErrDuplicate = errors.New("sqlkit.duplicate") + var ErrExecContext = errors.New("sqlkit.ExecContext") + var ErrForeignKeyViolation = errors.New("sqlkit.foreign_key_violation") + var ErrGetContext = errors.New("sqlkit.GetContext") + var ErrInvalidCursor = errors.New("sqlkit.pager.invalid_cursor") + var ErrInvalidPager = errors.New("sqlkit.pager.invalid") + var ErrMethodNotSupportedInSingle = errors.New("sqlkit.method_not_supported_in_single") + var ErrMigrateGetDriver = errors.New("sqlkit.migrate.get_driver") + var ErrMigrateLock = errors.New("sqlkit.migrate.lock") + var ErrMigrateOpenConn = errors.New("sqlkit.migrate.open_conn") + var ErrMigrateReadMigrationsDir = errors.New("sqlkit.migrate.read_migrations_dir") + var ErrMigrateUnsupportedDriver = errors.New("sqlkit.migrate.unsupported_driver") + var ErrMigrateUp = errors.New("sqlkit.migrate.up") + var ErrNamedExecContext = errors.New("sqlkit.NamedExecContext") + var ErrNamedQueryRowxContext = errors.New("sqlkit.NamedQueryRowxContext") + var ErrNotNull = errors.New("sqlkit.not_null") + var ErrOpenConnect = errors.New("sqlkit.open_connect") + var ErrPing = errors.New("sqlkit.ping") + var ErrPrepareContext = errors.New("sqlkit.PrepareContext") + var ErrPrepareNamedContext = errors.New("sqlkit.PrepareNamedContext") + var ErrQueryContext = errors.New("sqlkit.QueryContext") + var ErrQueryRowxContext = errors.New("sqlkit.QueryRowxContext") + var ErrQueryxContext = errors.New("sqlkit.QueryxContext") + var ErrReplicaUnavailable = errors.New("sqlkit.cluster.replica_unavailable") + var ErrSelectContext = errors.New("sqlkit.SelectContext") + var ErrStatementExecuteQuery = errors.New("sqlkit.statement.execute_query") + var ErrStatementPrepare = errors.New("sqlkit.statement.prepare") + var ErrTransactorBegin = errors.New("sqlkit.transactor.begin") + var ErrTransactorCommit = errors.New("sqlkit.transactor.commit") + var ErrTransactorRollback = errors.New("sqlkit.transactor.rollback") + var ErrTxOptionNotSupported = errors.New("sqlkit.transactor.option_not_supported") + func Atomic(ctx context.Context, conn *sqlx.DB, fn func(ctx context.Context) error) error + func BeginTx(ctx context.Context, tr Transactor, opts ...TxOption) (context.Context, error) + func Classify(err error) error + func Connect(driverName, connectionString string, timeout time.Duration, ...) (*sqlx.DB, error) + func ConnectFromConfig(cfg Config, opts ...func(connection *sqlx.DB)) (*sqlx.DB, error) + func ConnectionString(host string, port int, username, password string, database string, ...) string + func Dollar(start int) func(v any) string + func DriverName(db DB) string + func Duplicate(err error) (bool, string) + func DuplicateFields(err error) (fields []string, ok bool) + func EachShard(conn DB, fn func(conn DB) error) (err error) + func EachShardAsync(conn DB, fn func(conn DB) error, limit ...int) (err error) + func ForcePrimary(ctx context.Context) context.Context + func GetTx(ctx context.Context) (*sqlx.Tx, bool) + func HandleError(err error) error + func IsCheckViolation(err error) bool + func IsDuplicate(err error) bool + func IsForeignKey(err error) bool + func IsNoRows(err error) bool + func IsNotNull(err error) bool + func MaxConnectionsOption(open, idle int) func(conn *sqlx.DB) + func MaxTimeOption(lifetime, idle time.Duration) func(conn *sqlx.DB) + func Page(pageSize int64, page int) (offset int, limit int) + func Placeholder(driverName string, start int) func(v any) string + func PreferReplica(ctx context.Context) context.Context + func Question() func(v any) string + func Rebind(db DB, query string) string + func RegisterDriver(driverName string, driver driver.Driver) + func ReplicaPreferred(ctx context.Context) bool + func ReplicaRequired(ctx context.Context) bool + func RequireReplica(ctx context.Context) context.Context + func RewriteTx(key string, original context.Context, toCopy context.Context) context.Context + func RunTransaction(conn *sqlx.DB, transactionActions func(tx *sqlx.Tx) error) error + func RunTransactionContext(ctx context.Context, conn *sqlx.DB, transactionActions func(tx *sqlx.Tx) error) (err error) + func SQLiteDSN(path string, opts SQLiteOptions) string + func SetTx(ctx context.Context, tx *sqlx.Tx) context.Context + func WithConnMaxIdleTime(d time.Duration) func(*sqlx.DB) + func WithConnMaxLifetime(d time.Duration) func(*sqlx.DB) + func WithMaxIdle(n int) func(*sqlx.DB) + func WithMaxOpen(n int) func(*sqlx.DB) + func WithTx(ctx context.Context, tr Transactor, fn func(ctx context.Context) error, ...) (err error) + type Arguments struct + func NewArguments(args ...any) *Arguments + func (a *Arguments) Add(arg any) *Arguments + func (a *Arguments) AddMany(args ...any) string + func (a *Arguments) Args() []any + func (a *Arguments) Number() string + type Cluster struct + func ConnectCluster(cfg ClusterConfig, opts ...ClusterOption) (*Cluster, error) + func NewCluster(primary *sqlx.DB, opts ...ClusterOption) (*Cluster, error) + func (c *Cluster) Close() error + func (c *Cluster) Connection() *sqlx.DB + func (c *Cluster) DriverName() string + func (c *Cluster) EachShard(_ func(conn DB) error) error + func (c *Cluster) EachShardAsync(_ func(conn DB) error, _ ...int) error + func (c *Cluster) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) + func (c *Cluster) GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error + func (c *Cluster) NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error) + func (c *Cluster) NamedQueryRowxContext(ctx context.Context, query string, arg any) (*sqlx.Row, error) + func (c *Cluster) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) + func (c *Cluster) PrepareNamedContext(ctx context.Context, query string) (*sqlx.NamedStmt, error) + func (c *Cluster) Primary() DB + func (c *Cluster) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error) + func (c *Cluster) QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row + func (c *Cluster) QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error) + func (c *Cluster) Replica(ctx context.Context) DB + func (c *Cluster) SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error + func (c *Cluster) State() ReplicaState + func (c *Cluster) States() []ReplicaState + type ClusterConfig struct + LagCheckIntervalSeconds int + MaxLagSeconds int + Primary Config + Replicas []ReplicaConfig + ReplicasEnabled bool + type ClusterOption func(*clusterConfig) error + func WithLagCheckInterval(d time.Duration) ClusterOption + func WithMaxLag(d time.Duration) ClusterOption + func WithNamedReplica(name string, db *sqlx.DB) ClusterOption + func WithRecoveryConflictRetry(on bool) ClusterOption + func WithReplica(replica *sqlx.DB) ClusterOption + func WithReplicaBalancer(b ReplicaBalancer) ClusterOption + func WithStateHook(fn func(ReplicaState)) ClusterOption + type Config struct + BinaryParameters bool + ClickhouseScheme string + DSN string + Database string + Driver string + Host string + MaxIdleConnections int + MaxIdleTime int + MaxLifetime int + MaxOpenConnections int + MigrationDatabase string + MigrationHost string + MigrationPassword string + MigrationPort int + MigrationUsername string + Password string + Port int + ReadTimeout int + SQLite SQLiteOptions + SSLMode string + TimeoutMs int + Username string + WriteTimeout int + func (c *Config) ConnectionString() string + func (c *Config) MigrationConnectionString() string + func (c *Config) Validate() error + type ConnectionSelector func(ctx context.Context, connections []ShardConnect) ShardConnect + type Connections struct + func ConnectShards(driverName string, connectionStrings []ShardConnectString, ...) (*Connections, error) + func (c *Connections) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error) + func (c *Connections) Close() error + func (c *Connections) Connections() []ShardConnect + func (c *Connections) Get(ctx context.Context) (ShardConnect, error) + func (c *Connections) RawConnections() []*sqlx.DB + type Connector struct + func NewConnector() *Connector + func (connector *Connector) BinaryParameters(binaryParameters bool) *Connector + func (connector *Connector) Build() string + func (connector *Connector) BuildClickhouse() string + func (connector *Connector) BuildMigration() string + func (connector *Connector) ClickhouseScheme(scheme string) *Connector + func (connector *Connector) Connect(driverName string, options ...func(connection *sqlx.DB)) (*sqlx.DB, error) + func (connector *Connector) ConnectMigration(driverName string, options ...func(connection *sqlx.DB)) (*sqlx.DB, error) + func (connector *Connector) ConnectionMaxLifetime(connectionMaxLifetime time.Duration) *Connector + func (connector *Connector) Database(database string) *Connector + func (connector *Connector) Host(host string) *Connector + func (connector *Connector) MaxIdleConnections(maxIdleConnections int) *Connector + func (connector *Connector) MaxIdleTime(maxIdleTime time.Duration) *Connector + func (connector *Connector) MaxOpenConnections(maxOpenConnections int) *Connector + func (connector *Connector) MigrationDatabase(database string) *Connector + func (connector *Connector) MigrationHost(host string) *Connector + func (connector *Connector) MigrationPassword(password string) *Connector + func (connector *Connector) MigrationPort(port int) *Connector + func (connector *Connector) MigrationUsername(username string) *Connector + func (connector *Connector) Password(password string) *Connector + func (connector *Connector) Port(port int) *Connector + func (connector *Connector) ReadTimeout(readTimeout int) *Connector + func (connector *Connector) SSLMode(mode string) *Connector + func (connector *Connector) Schema(schema string) *Connector + func (connector *Connector) String() string + func (connector *Connector) Timeout(timeout time.Duration) *Connector + func (connector *Connector) Username(username string) *Connector + func (connector *Connector) WriteTimeout(writeTimeout int) *Connector + type DB interface + Connection func() *sqlx.DB + EachShard func(fn func(conn DB) error) error + EachShardAsync func(fn func(conn DB) error, limit ...int) error + func NewDB(conn *sqlx.DB) DB + func NewDBWithOptions(conn *sqlx.DB, opts ...DBOption) DB + func NewShardDB(connections *Connections) DB + type DBOption func(*clientSingle) + func WithPaging(mode PagingMode) DBOption + type GetContext interface + GetContext func(ctx context.Context, dest interface{}, query string, args ...interface{}) error + type Migrator struct + func NewMigrator(ctx context.Context, conn *sqlx.DB, database string) *Migrator + func (m *Migrator) Migrate() error + func (m *Migrator) MigrateContext(ctx context.Context) error + func (m *Migrator) WithFS(fsys fs.FS, dir string) *Migrator + func (m *Migrator) WithMigrationsDirectory(directory string) *Migrator + func (m *Migrator) WithMigrationsTable(table string) *Migrator + type NamedExecContext interface + NamedExecContext func(ctx context.Context, query string, arg interface{}) (sql.Result, error) + type NamedQueryRowxContext interface + NamedQueryRowxContext func(ctx context.Context, query string, arg any) (*sqlx.Row, error) + type PageRequest struct + Cursor string + Page int + Size int + type PageResult struct + HasMore bool + Items []T + NextCursor *string + func PageOf[T any](rows []T, p *Pager, key func(T) []any) PageResult[T] + type Pager struct + func AllPager(opts ...PagerOption) *Pager + func CursorPager(req PageRequest, opts ...PagerOption) *Pager + func NewPager(db DB, req PageRequest, opts ...PagerOption) *Pager + func OffsetPager(req PageRequest, opts ...PagerOption) *Pager + func (p *Pager) Err() error + func (p *Pager) Limit() int + func (p *Pager) LimitOffset() string + func (p *Pager) Offset() int + func (p *Pager) OrderBy() string + func (p *Pager) OrderByColumns() []string + func (p *Pager) Page() int + func (p *Pager) Size() int + func (p *Pager) Where(ph func(v any) string) (string, []any) + type PagerOption func(*pagerConfig) + func Asc(column string) PagerOption + func Desc(column string) PagerOption + func MaxSize(n int) PagerOption + type PagingMode int + const PagingCursor + const PagingOffset + type PrepareContext interface + PrepareNamedContext func(ctx context.Context, query string) (*sqlx.NamedStmt, error) + type ReplicaBalancer func(usable []ReplicaState) int + func LeastLag() ReplicaBalancer + func RoundRobin() ReplicaBalancer + type ReplicaConfig struct + Name string + type ReplicaState struct + Lag time.Duration + Name string + Reason string + Usable bool + func (s ReplicaState) String() string + type SQLiteOptions struct + BusyTimeoutMs int + DisableForeignKeys bool + JournalMode string + Pragmas string + Synchronous string + TxLock string + type SelectContext interface + SelectContext func(ctx context.Context, dest interface{}, query string, args ...interface{}) error + type ShardConnect interface + Close func() error + Conn func() *sqlx.DB + Key func() string + type ShardConnectString struct + ConnectionString string + Key string + type Transaction interface + Commit func(ctx context.Context) error + Context func() context.Context + Rollback func(ctx context.Context) error + type Transactor interface + Begin func(ctx context.Context) (context.Context, error) + Commit func(ctx context.Context) error + IsTx func(ctx context.Context) bool + Key func() string + Rewrite func(original, toCopy context.Context) context.Context + Rollback func(ctx context.Context) error + func NewTransactor(provider TransactorConnectionProvider) Transactor + type TransactorConnectionProvider interface + BeginTxx func(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error) + type TxBeginner interface + BeginTx func(ctx context.Context, opts ...TxOption) (context.Context, error) + type TxOption func(*txConfig) + func Isolation(level sql.IsolationLevel) TxOption + func Join() TxOption + func ReadOnly() TxOption