Documentation
¶
Overview ¶
Package sqlkit is a type-safe SQL control layer for Go 1.25+.
sqlkit provides generic Table[T], an expr-builder for WHERE/SET, explicit Join/On with FK auto-join, tag-based composite indexes, safe-by-default migration engine, and support for SELECT DISTINCT, REPLACE INTO, UNION, INTERSECT/EXCEPT, ForUpdate, and RETURNING across mysql, sqlite, and postgres.
Index ¶
- Constants
- Variables
- func GetTable[T any](db *Database) (*table.Table[T], error)
- func NewCreate[T any](opts ...table.Option) (*query.Create[T], error)
- func NewDelete[T any](opts ...table.Option) (*query.Delete[T], error)
- func NewInsert[T any](opts ...table.Option) (*query.Insert[T], error)
- func NewReplace[T any](opts ...table.Option) (*query.Replace[T], error)
- func NewTable[T any](opts ...table.Option) (*table.Table[T], error)
- func NewTransport(db *sql.DB, d dialect.Dialect) query.Transport
- func NewUpdate[T any](opts ...table.Option) (*query.Update[T], error)
- func Query[T any](q query.Queryable) *query.QueryBuilder[T]
- func Register[T any](db *Database, opts ...table.Option) (*table.Table[T], error)
- type Builder
- type ConflictSet
- type Create
- type DBTransport
- type Database
- func (db *Database) Begin(ctx context.Context) (*Transaction, error)
- func (db *Database) Close() error
- func (db *Database) DB() *sql.DB
- func (db *Database) Dialect() dialect.Dialect
- func (db *Database) Exec(ctx context.Context, b query.Builder) (sql.Result, error)
- func (db *Database) Migrate(ctx context.Context, opts ...migrate.Option) (*migrate.Result, error)
- func (db *Database) MigrateFiles(ctx context.Context, target int) (*migrate.Result, error)
- func (db *Database) Register(tbl any) error
- func (db *Database) RegisterMigration(f migrate.File)
- func (db *Database) TableByName(name string) (*table.TableInfo, error)
- func (db *Database) TableFor(rt reflect.Type) (*table.TableInfo, bool)
- func (db *Database) Tables() []*table.TableInfo
- func (db *Database) Transport() query.Transport
- type DatabaseOption
- type Delete
- type ErrMigrationRequiresFile
- type ErrTableInvalidError
- type Field
- type Index
- type Insert
- type JoinKind
- type LockMode
- type LockOption
- type Option
- type QueryBuilder
- type Relation
- type Replace
- type Select
- type Table
- type TableInfo
- type Transaction
- func (t *Transaction) Begin(ctx context.Context) (*Transaction, error)
- func (t *Transaction) Commit(ctx context.Context) error
- func (t *Transaction) Dialect() dialect.Dialect
- func (t *Transaction) Exec(ctx context.Context, b query.Builder) (sql.Result, error)
- func (t *Transaction) ExecContext(ctx context.Context, queryStr string, args ...any) (sql.Result, error)
- func (t *Transaction) QueryContext(ctx context.Context, queryStr string, args ...any) (*sql.Rows, error)
- func (t *Transaction) QueryRowContext(ctx context.Context, queryStr string, args ...any) *sql.Row
- func (t *Transaction) Rollback(ctx context.Context) error
- func (t *Transaction) TableFor(rt reflect.Type) (*table.TableInfo, bool)
- func (t *Transaction) Transport() query.Transport
- type Transport
- type Unique
- type Update
Constants ¶
const ( JoinInner = query.JoinInner JoinLeft = query.JoinLeft JoinRight = query.JoinRight JoinFull = query.JoinFull LockNone = query.LockNone LockUpdate = query.LockUpdate LockNowait = query.LockNowait LockSkipLocked = query.LockSkipLocked )
Re-exported constants.
Variables ¶
var ( ErrTableInvalid = table.ErrTableInvalid ErrBuilderInvalid = query.ErrBuilderInvalid ErrNoRows = query.ErrNoRows ErrMoreThanRequested = query.ErrMoreThanRequested ErrTableNotFound = query.ErrTableNotFound )
Re-exported sentinel errors.
var ( WithTableName = table.WithTableName WithIndexPrefix = table.WithIndexPrefix WithUniquePrefix = table.WithUniquePrefix WithPrimaryKey = table.WithPrimaryKey WithIndex = table.WithIndex WithIndexMethod = table.WithIndexMethod WithUnique = table.WithUnique )
Re-exported table option constructors.
var ( // ErrTransportNotSupported is returned when an operation requires a DBTransport-compatible transport but the current transport does not implement DBTransport. ErrTransportNotSupported = errors.New("sqlkit: transport not supported") // ErrConflictingOption is returned when conflicting migration options are supplied (e.g., WithRegistry shadows the database-level Registry). ErrConflictingOption = errors.New("sqlkit: conflicting option") )
var (
NewSelect = query.NewSelect
)
Re-exported query/builder functions.
Functions ¶
func GetTable ¶
GetTable returns the *table.Table[T] previously registered for type T, or an error wrapping ErrTableNotFound.
func NewTable ¶
Generic constructor wrappers (Go does not allow function variables with type parameters).
func NewTransport ¶
NewTransport wraps a *sql.DB as a query.Transport and DBTransport for use with NewDatabaseWithTransport.
Types ¶
type Builder ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type ConflictSet ¶
type ConflictSet = query.ConflictSet
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Create ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type DBTransport ¶
type DBTransport interface {
query.Transport
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
Close() error
Driver() driver.Driver
}
DBTransport is the lifecycle surface of a connection-backed transport.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database binds a Transport to a dialect and a registry of tables.
func NewDatabase ¶
NewDatabase creates a Database from an existing *sql.DB and a Dialect. Defaults: indexPrefix = "idx_", uniquePrefix = "uq_", scanCacheSize = 256.
func NewDatabaseWithTransport ¶
NewDatabaseWithTransport creates a Database from a custom Transport implementation. Defaults: indexPrefix = "idx_", uniquePrefix = "uq_", scanCacheSize = 256.
func Open ¶
Open opens a database connection via database/sql for the given driver, DSN, and dialect name, and returns a Database configured with sensible defaults.
func (*Database) Begin ¶
func (db *Database) Begin(ctx context.Context) (*Transaction, error)
Begin starts a top-level database transaction. The transport must implement DBTransport.
func (*Database) Close ¶
Close closes the underlying database connection. The transport must implement DBTransport.
func (*Database) DB ¶
DB returns the underlying *sql.DB, or nil when the Database is backed by a custom Transport that does not expose a *sql.DB.
func (*Database) Exec ¶
Exec builds and executes the SQL produced by a query.Builder, returning the sql.Result.
func (*Database) Migrate ¶
Migrate diffs the registered schema against the live database and applies safe changes automatically, then runs any pending file migrations up to the target version.
func (*Database) MigrateFiles ¶
MigrateFiles runs file migrations up to the target version and then applies the auto-diff of registered tables against the live schema, all in one transaction. It is a shorthand for Migrate(ctx, migrate.WithTargetVersion(target)).
func (*Database) Register ¶
Register stores a table descriptor so it can be looked up by reflect.Type later. Typical usage is through the generic Register[T] helper.
func (*Database) RegisterMigration ¶
RegisterMigration registers a programmatic migration file in the database-level Registry.
func (*Database) TableByName ¶
TableByName returns the TableInfo registered under the given table name, or an error wrapping ErrTableNotFound.
func (*Database) TableFor ¶
TableFor returns the TableInfo registered for the given reflect.Type, or false.
type DatabaseOption ¶
type DatabaseOption func(*Database)
DatabaseOption configures a Database during construction.
func WithDatabaseIndexPrefix ¶
func WithDatabaseIndexPrefix(prefix string) DatabaseOption
WithDatabaseIndexPrefix sets the global index name prefix for all tables registered on this Database. Defaults to "idx_".
func WithDatabaseScanCacheSize ¶
func WithDatabaseScanCacheSize(n int) DatabaseOption
WithDatabaseScanCacheSize sets the size of the per-Database scan-plan LRU cache. Set to 0 to disable caching. Defaults to 256.
func WithDatabaseUniquePrefix ¶
func WithDatabaseUniquePrefix(prefix string) DatabaseOption
WithDatabaseUniquePrefix sets the global unique constraint name prefix for all tables registered on this Database. Defaults to "uq_".
type Delete ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type ErrMigrationRequiresFile ¶
type ErrMigrationRequiresFile = migrate.ErrMigrationRequiresFile
ErrMigrationRequiresFile wraps migrate.ErrMigrationRequiresFile.
type ErrTableInvalidError ¶
type ErrTableInvalidError = table.ErrTableInvalidError
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Field ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Index ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Insert ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type JoinKind ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type LockMode ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type LockOption ¶
type LockOption = query.LockOption
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Option ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type QueryBuilder ¶
type QueryBuilder[T any] = query.QueryBuilder[T]
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Relation ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Replace ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Select ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Table ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type TableInfo ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
Transaction wraps a *sql.Tx with nested savepoint support.
func (*Transaction) Begin ¶
func (t *Transaction) Begin(ctx context.Context) (*Transaction, error)
Begin starts a nested transaction backed by a savepoint. Each nested level must be committed or rolled back before the parent.
func (*Transaction) Commit ¶
func (t *Transaction) Commit(ctx context.Context) error
Commit releases the current savepoint. Nested commits do not persist until the top-level transaction commits.
func (*Transaction) Dialect ¶
func (t *Transaction) Dialect() dialect.Dialect
func (*Transaction) Exec ¶
Exec builds and executes the SQL produced by a query.Builder within the transaction.
func (*Transaction) ExecContext ¶
func (*Transaction) QueryContext ¶
func (*Transaction) QueryRowContext ¶
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback(ctx context.Context) error
Rollback rolls back to the most recent savepoint. Nested rollbacks undo only the work since that savepoint.
func (*Transaction) Transport ¶
func (t *Transaction) Transport() query.Transport
type Transport ¶
Re-exported type aliases: the root sqlkit package mirrors the primary types from the table/query subpackages so callers can import a single package.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dialect adapts SQL rendering — identifiers, placeholders, type mapping, autoincrement syntax, LIMIT/OFFSET — to one database engine.
|
Package dialect adapts SQL rendering — identifiers, placeholders, type mapping, autoincrement syntax, LIMIT/OFFSET — to one database engine. |
|
examples
|
|
|
demo
command
Demonstration of ALL go-sqlkit features.
|
Demonstration of ALL go-sqlkit features. |
|
Package expr provides a composable WHERE/SET expression tree and struct-match helpers that turn non-zero struct fields into AND-equalities.
|
Package expr provides a composable WHERE/SET expression tree and struct-match helpers that turn non-zero struct fields into AND-equalities. |
|
Package migrate diffs registered table metadata against the live database schema, classifies each change as safe or risky, auto-applies the safe ones, and routes the risky ones to file migrations.
|
Package migrate diffs registered table metadata against the live database schema, classifies each change as safe or risky, auto-applies the safe ones, and routes the risky ones to file migrations. |
|
Package query provides type-safe SQL query builders, JOINs, statement builders (Create, Insert, Update, Delete, Select, Replace), and the Transport execution interface.
|
Package query provides type-safe SQL query builders, JOINs, statement builders (Create, Insert, Update, Delete, Select, Replace), and the Transport execution interface. |
|
Package table provides reflection-driven table metadata for Go struct types.
|
Package table provides reflection-driven table metadata for Go struct types. |