Documentation
¶
Index ¶
- Constants
- Variables
- func AdoptTX(ctx context.Context, id string) (ctx2 context.Context, release func(), err error)
- func CtxAdopted(ctx context.Context) bool
- func CtxWithTX(ctx context.Context, tx TX) context.Context
- func IlikeSql(driver dbDriver, match string) string
- func Scan(target interface{}, rows *sql.Rows) error
- type DB
- type DebugLevel
- type Exec
- type NullJson
- type NullRawMessage
- type NullTime
- type PlaceholderMode
- type Query
- type TX
Constants ¶
const POSTGRES = "postgres"
The driver strings must match the driver from the stdlib
const SQLITE3 = "sqlite3"
Variables ¶
var ErrMismatchedRowsAffected error = errors.New("Mismatched rows affected")
var ErrQueryReturnedZeroRows error = errors.New("Query returned 0 rows")
Functions ¶
func AdoptTX ¶
AdoptTX resolves a leased transaction and returns a ctx carrying it, marked so that ExecTX joins it directly instead of refusing to nest. It serializes adopters: a second AdoptTX for the same lease blocks until the first calls release. release is idempotent and must be called when the adopter is done. An unknown, stopped, ended, or failed lease is an error.
func CtxAdopted ¶
CtxAdopted reports whether ctx carries a transaction joined via AdoptTX. Callers can use this to refuse work that cannot complete inside a leased TX — e.g. waiting on side effects only visible after the owner commits.
func IlikeSql ¶
IlikeSql returns driver compatible ILIKE where clause snippet. match is escaped using %...%. This panics for unknown driver. E.g. "schule" and driver "postgres" this will return "ILIKE 'schule'"
func Scan ¶
Scan reads data from the given rows into the target.
*int64, *string, etc: First column of first row *struct: First row []int64, []*int64, []string, []*string: First column, all rows []struct, []*struct: All columns, all rows
The mapping into structs is done by analyzing the struct's tag names and using the given "db" key for the mapping. The mapping works on exported fields only. Use "-" as mapping name to ignore the field.
Types ¶
type DB ¶
type DB interface {
Query
Exec
// Begin starts a read-write transaction.
Begin() (TX, error)
// BeginRead starts a read-only transaction.
BeginRead() (TX, error)
// BeginContext starts a transaction with the given options.
BeginContext(context.Context, *sql.TxOptions) (TX, error)
// Close closes the database connection.
Close() error
// IsClosed reports whether Close was called.
IsClosed() bool
// Name returns the name of the connected database.
Name() (string, error)
// DB returns the wrapped stdlib sql.DB handle.
DB() *sql.DB
// Log returns a debug handle which logs every statement; queries are
// run an extra time to print their result table. Development only.
Log() DB
// Version returns the database server version.
Version() (string, error)
// ExecTX runs f inside a transaction: commit if f returns nil,
// rollback otherwise.
ExecTX(context.Context, func(context.Context) error, *sql.TxOptions) error
}
type DebugLevel ¶
type DebugLevel int
const ( PANIC DebugLevel = 1 ERROR DebugLevel = 2 UPDATE DebugLevel = 4 INSERT DebugLevel = 8 EXEC DebugLevel = 16 QUERY DebugLevel = 32 QUERY_DUMP DebugLevel = 64 )
type Exec ¶
type Exec interface {
Query
// ExecContext executes the statement.
ExecContext(context.Context, string, ...any) error
// Exec works like ExecContext with context.Background().
Exec(string, ...any) error
// ExecContextRowsAffected executes the statement and returns the number
// of affected rows and the last insert id (if the driver supports it).
ExecContextRowsAffected(context.Context, string, ...any) (int64, int64, error)
// Insert inserts one struct (or each row of a slice, one statement per
// row) into the table and writes the generated primary key back into
// the row (in slice mode only into an int64 key).
Insert(string, any) error
// InsertBulk inserts a slice of structs with one multi-row INSERT
// (COPY FROM on postgres inside ExecTX). Generated primary keys are NOT
// read back; use InsertBulkReadbackIdsContext when they are needed.
InsertBulk(string, any) error
// InsertBulkContext works like InsertBulk with a context.
InsertBulkContext(context.Context, string, any) error
// InsertBulkOnConflictDoNothingContext works like InsertBulkContext but
// adds ON CONFLICT (cols...) DO NOTHING to the statement.
InsertBulkOnConflictDoNothingContext(context.Context, string, any, ...string) error
// InsertBulkReadbackIdsContext works like InsertBulkContext but writes the
// generated primary keys back into the rows with one INSERT ... RETURNING
// (so no COPY). The batch needs a single settable auto-assigned integer
// primary key ("pk,omitempty") and no pre-set key.
InsertBulkReadbackIdsContext(context.Context, string, any) error
// InsertContext works like Insert with a context.
InsertContext(context.Context, string, any) error
// Save inserts the struct if its primary key is zero, otherwise it
// updates the row.
Save(string, any) error
// SaveContext works like Save with a context.
SaveContext(context.Context, string, any) error
// Update updates the table row matching the struct's primary key,
// setting all non-readonly columns (omitempty columns are skipped
// when their value is zero).
Update(string, any) error
// UpdateContext works like Update with a context.
UpdateContext(context.Context, string, any) error
// UpdateBulkContext updates each row of a slice, one statement per row.
UpdateBulkContext(context.Context, string, any) error
}
type NullRawMessage ¶
type NullRawMessage struct {
Data json.RawMessage
Valid bool
}
func (*NullRawMessage) Scan ¶
func (nj *NullRawMessage) Scan(value any) error
type PlaceholderMode ¶
type PlaceholderMode int
const ( DOLLAR PlaceholderMode = 1 QUESTION PlaceholderMode = 2 )
type Query ¶
type Query interface {
// QueryContext runs the query and scans the result into target: a
// pointer to a struct, a slice of structs / struct pointers, a scalar
// or a slice of scalars.
QueryContext(context.Context, any, string, ...any) error
// Query works like QueryContext with context.Background().
Query(any, string, ...any) error
// Driver returns the database driver the connection runs on.
Driver() dbDriver
// EscValue returns s as an escaped SQL string literal (single quoted).
EscValue(string) string
}
type TX ¶
type TX interface {
Query
Exec
// BeforeCommit registers f to run before the commit; an error from f
// aborts the commit.
BeforeCommit(func() error)
// AfterCommit registers f to run after a successful commit.
AfterCommit(func())
// AfterRollback registers f to run after a rollback.
AfterRollback(func())
// AfterTransaction registers f to run after the transaction ended,
// after a successful commit or rollback (not when the underlying
// commit/rollback itself fails).
AfterTransaction(func())
// ActiveTX reports whether the transaction is still open.
ActiveTX() bool
// IsWriteMode reports whether the transaction is read-write.
IsWriteMode() bool
// Lease registers the open write TX under a crypto-random id so another
// goroutine can join it via AdoptTX; stop invalidates the id (Commit and
// Rollback invalidate all leases of the TX as well).
Lease() (id string, stop func())
// Commit commits the transaction.
Commit() error
// Rollback rolls the transaction back.
Rollback() error
// EscValue returns s as an escaped SQL string literal (single quoted).
EscValue(string) string
}