Documentation
¶
Index ¶
- func ApplyBindings(args ...any) (err error)
- func Nullify[T comparable](value T) any
- func RegisterVirtualFunc(name string, handler func(driverName string, args string) (string, error))
- type Binder
- type DB
- func (db *DB) Begin() (*Tx, error)
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)
- func (db *DB) Close() (err error)
- func (db *DB) ConformArgPlaceholders(stmt string) stringdeprecated
- func (db *DB) DriverName() string
- func (db *DB) Exec(query string, args ...any) (sql.Result, error)
- func (db *DB) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (db *DB) InsertReturnID(ctx context.Context, idColumn string, stmt string, args ...any) (int64, error)
- func (db *DB) Migrate(sequenceName string, fileSys fs.FS) (err error)
- func (db *DB) NowUTC() stringdeprecated
- func (db *DB) Prepare(query string) (*sql.Stmt, error)
- func (db *DB) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (db *DB) Query(query string, args ...any) (*sql.Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (db *DB) QueryRow(query string, args ...any) *sql.Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (db *DB) RegexpTextSearch(searchableColumns ...string) stringdeprecated
- func (db *DB) UnpackQuery(query string) (string, error)
- type Executor
- type Null
- type Tx
- func (tx *Tx) DriverName() string
- func (tx *Tx) Exec(query string, args ...any) (sql.Result, error)
- func (tx *Tx) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (tx *Tx) InsertReturnID(ctx context.Context, idColumn string, stmt string, args ...any) (int64, error)
- func (tx *Tx) Prepare(query string) (*sql.Stmt, error)
- func (tx *Tx) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
- func (tx *Tx) Query(query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) QueryRow(query string, args ...any) *sql.Row
- func (tx *Tx) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
- func (tx *Tx) UnpackQuery(query string) (string, error)
- type UnsafeSQL
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ApplyBindings ¶
ApplyBindings should be called after scanning values from the result set to perform all late binding.
func Nullify ¶
func Nullify[T comparable](value T) any
Nullify returns nil if the value equals to the zero value of its Go data type, else it returns the value. Use this construct to convert zero values to nil when writing to a nullable database column.
Example:
db.Exec( "INSERT INTO my_table (id, desc, modified_time) VALUES (?,?,?)", obj.ID, sequel.Nullify(obj.Description), sequel.Nullify(obj.ModifiedTime), )
func RegisterVirtualFunc ¶ added in v1.2.0
RegisterVirtualFunc registers a virtual SQL function that will be replaced in queries before execution. The name is matched case-insensitively, e.g. registering "NOW_UTC" matches NOW_UTC(), now_utc(), Now_Utc(), etc. The handler receives the driver name and the string found between the parentheses, and returns the replacement SQL expression, or an error.
Types ¶
type Binder ¶
Binder is a thin wrapper over sql.Null that allows for late-binding of its value.
func Bind ¶
Bind applies a binding function to the scanned value.
Example:
var obj Object
args := []any{
&obj.ID,
sequel.Bind(func(tags string) {
return json.Unmarshal([]byte(tags), &obj.Tags)
}),
sequel.Bind(func(modifiedTime time.Time) {
obj.Year, obj.Month, obj.Day = modifiedTime.Date()
return nil
}),
}
db.QueryRow("SELECT id, tags, modified_time FROM my_table WHERE id=?", id).Scan(args...)
sequel.ApplyBindings(args...)
type DB ¶
DB is an enhanced database connection that
- Limits the size of the connection pool to each server to approx the sqrt of the number of clients
- Performs schema migration
- Automatically creates and connects to a localhost database while testing
func Open ¶
Open returns a database connection to the named data source.
If a driver name is not provided, it is inferred from the data source name on a best-effort basis. Drivers currently supported: "mysql" (MySQL), "pgx" (Postgres), "mssql" (SQL Server) or "sqlite" (SQLite).
Example data source name for each of the supported drivers:
- mysql: username:password@tcp(hostname:3306)/
- pgx: postgres://username:password@hostname:5432/
- mssql: sqlserver://username:password@hostname:1433
- sqlite: file:path/to/database.sqlite
func OpenTesting ¶
OpenTesting opens a connection to a uniquely named database for testing purposes. A database is created for each unique test at the database instance pointed to by the input DSN.
If a driver name is not provided, it is inferred from the data source name on a best-effort basis. Drivers currently supported: "mysql" (MySQL), "pgx" (Postgres), "mssql" (SQL Server) or "sqlite" (SQLite).
If a data source name is not provided, the following defaults are used based on the driver name:
- (empty): SQLite in-memory database
- sqlite: SQLite in-memory database
- mysql: root:root@tcp(127.0.0.1:3306)/
- pgx: postgres://postgres:postgres@127.0.0.1:5432/
- mssql: sqlserver://sa:Password123@127.0.0.1:1433
func (*DB) Begin ¶ added in v1.4.0
Begin starts a transaction and returns a sequel.Tx that applies virtual function expansion and placeholder conforming.
func (*DB) BeginTx ¶ added in v1.4.0
BeginTx starts a transaction with the given options and returns a sequel.Tx that applies virtual function expansion and placeholder conforming.
func (*DB) ConformArgPlaceholders
deprecated
func (*DB) DriverName ¶
DriverName is the name of the driver: "mysql", "pgx", "mssql" or "sqlite".
func (*DB) Exec ¶ added in v1.2.0
Exec shadows sql.DB.Exec and conforms arg placeholders for the driver.
func (*DB) ExecContext ¶ added in v1.2.0
ExecContext shadows sql.DB.ExecContext and conforms arg placeholders for the driver.
func (*DB) InsertReturnID ¶ added in v1.3.0
func (db *DB) InsertReturnID(ctx context.Context, idColumn string, stmt string, args ...any) (int64, error)
InsertReturnID executes an INSERT statement and returns the auto-generated ID for the named ID column.
func (*DB) Migrate ¶
Migrate reads all #.sql files from the FS, and executes any new migrations in order of their file name. The order of execution is guaranteed only within the context of a sequence name.
func (*DB) Prepare ¶ added in v1.2.0
Prepare shadows sql.DB.Prepare and conforms arg placeholders for the driver.
func (*DB) PrepareContext ¶ added in v1.2.0
PrepareContext shadows sql.DB.PrepareContext and conforms arg placeholders for the driver.
func (*DB) Query ¶ added in v1.2.0
Query shadows sql.DB.Query and conforms arg placeholders for the driver.
func (*DB) QueryContext ¶ added in v1.2.0
QueryContext shadows sql.DB.QueryContext and conforms arg placeholders for the driver.
func (*DB) QueryRow ¶ added in v1.2.0
QueryRow shadows sql.DB.QueryRow and conforms arg placeholders for the driver.
func (*DB) QueryRowContext ¶ added in v1.2.0
QueryRowContext shadows sql.DB.QueryRowContext and conforms arg placeholders for the driver.
func (*DB) RegexpTextSearch
deprecated
func (*DB) UnpackQuery ¶ added in v1.2.0
UnpackQuery expands virtual functions (e.g. NOW_UTC(), REGEXP_TEXT_SEARCH()) into driver-specific SQL expressions, and conforms arg placeholders to the syntax expected by the driver (e.g. ? to $1, $2 for PostgreSQL).
type Executor ¶ added in v1.4.1
type Executor interface {
Exec(query string, args ...any) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Query(query string, args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
QueryRow(query string, args ...any) *sql.Row
QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
Prepare(query string) (*sql.Stmt, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
InsertReturnID(ctx context.Context, idColumn string, stmt string, args ...any) (int64, error)
DriverName() string
UnpackQuery(query string) (string, error)
}
Executor is the interface satisfied by both DB and Tx.
type Null ¶
Null is a thin wrapper over sql.Null that allows for reading NULL values.
func Nullable ¶
Nullable is a simple binder that interprets NULL values to be the zero value of their Go data type.
Example:
var obj Object
args := []any{
&obj.ID,
sequel.Nullable(&obj.Description),
sequel.Nullable(&obj.ModifiedTime),
}
db.QueryRow("SELECT id, desc, modified_time FROM my_table WHERE id=?", id).Scan(args...)
sequel.ApplyBindings(args...)
type Tx ¶ added in v1.4.0
Tx is an in-progress database transaction that shadows sql.Tx methods to apply virtual function expansion and placeholder conforming.
func (*Tx) DriverName ¶ added in v1.4.0
DriverName is the name of the driver: "mysql", "pgx", "mssql" or "sqlite".
func (*Tx) Exec ¶ added in v1.4.0
Exec shadows sql.Tx.Exec and conforms arg placeholders for the driver.
func (*Tx) ExecContext ¶ added in v1.4.0
ExecContext shadows sql.Tx.ExecContext and conforms arg placeholders for the driver.
func (*Tx) InsertReturnID ¶ added in v1.4.0
func (tx *Tx) InsertReturnID(ctx context.Context, idColumn string, stmt string, args ...any) (int64, error)
InsertReturnID executes an INSERT statement and returns the auto-generated ID for the named ID column.
func (*Tx) Prepare ¶ added in v1.4.0
Prepare shadows sql.Tx.Prepare and conforms arg placeholders for the driver.
func (*Tx) PrepareContext ¶ added in v1.4.0
PrepareContext shadows sql.Tx.PrepareContext and conforms arg placeholders for the driver.
func (*Tx) Query ¶ added in v1.4.0
Query shadows sql.Tx.Query and conforms arg placeholders for the driver.
func (*Tx) QueryContext ¶ added in v1.4.0
QueryContext shadows sql.Tx.QueryContext and conforms arg placeholders for the driver.
func (*Tx) QueryRow ¶ added in v1.4.0
QueryRow shadows sql.Tx.QueryRow and conforms arg placeholders for the driver.
func (*Tx) QueryRowContext ¶ added in v1.4.0
QueryRowContext shadows sql.Tx.QueryRowContext and conforms arg placeholders for the driver.
func (*Tx) UnpackQuery ¶ added in v1.4.0
UnpackQuery expands virtual functions (e.g. NOW_UTC(), REGEXP_TEXT_SEARCH()) into driver-specific SQL expressions, and conforms arg placeholders to the syntax expected by the driver (e.g. ? to $1, $2 for PostgreSQL).