Documentation
¶
Overview ¶
Package database provides a simple layer on top of database/sql to ease common database tasks like transaction handling, prepared statement caching, schema updates and the like.
Index ¶
- Constants
- func DB2Time(msec int64) time.Time
- func NewID() string
- func NoRows(err error) bool
- func Now() int64
- func Time2DB(t time.Time) int64
- type Config
- type Driver
- type Tx
- func (tx *Tx) CommitTx(ctx context.Context) error
- func (tx *Tx) ExecTx(ctx context.Context, query string, args ...any) error
- func (tx *Tx) Now() time.Time
- func (tx *Tx) QueryRowTx(ctx context.Context, query string, args ...any) (*sql.Row, error)
- func (tx *Tx) QueryTx(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (tx *Tx) RollbackUncommitedTx(ctx context.Context) error
- type Type
Constants ¶
const ( // SchemaNone indicates no schema has been setup so far. SchemaNone int = -1 )
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config interface {
// Name gets the name of the database represented by this configuration.
Name() string
// Type gets the database type represented by this configuration.
Type() Type
// DriverName gets the name of the sql driver providing access to the database
// represented by this configuration.
DriverName() string
// DSN get the Data Source Name to be used for accessing the database.
DSN() string
// DSN get the Data Source Name with any sensitive data redacted.
RedactedDSN() string
// SchemaScripts gets the schema updated scripts to be applied to the database
// during schema initialization or a schema update.
SchemaScripts() [][]byte
}
Config interface provides a generic way to different database drivers.
type Driver ¶
type Driver struct {
// contains filtered or unexported fields
}
Driver represents an open database connection ready to execute SQL statements.
func (*Driver) BeginTx ¶
BeginTx ensures a database transaction is in place.
Make sure to always close the returned Tx instance by invoking[Tx.RollbackUncommitedTx].
Invoke Tx.CommitTx to commit all database updates performed within the BeginTx block. If Tx.CommitTx is not called before the call to Tx.RollbackUncommitedTx, it will be implicitly rolled back.
In case of an outer to BeginTx call for the given context, the already opened transaction is re-used. Closing the transaction behaves in the same manner. Only after the last BeginTx block is closed by invoking Tx.RollbackUncommitedTx and only if all BeginTx blocks have been commited by invoking Tx.CommitTx the actual database transaction is committed.
func (*Driver) Ping ¶
Ping pings the database (see sql.DB.PingContext).
func (*Driver) UpdateSchema ¶
UpdateSchema is used to update the database schema to the lates schema version.
The current schema version is determined by querying the database. Any necessary schema update is performed by getting the update scripts via [Config.SchemaScripts] and executing them as needed.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx represents a database transation (see [BeginTx]).
func (*Tx) CommitTx ¶
Commit commits all changes since the corresponding Driver.BeginTx call.
See Driver.BeginTx for details of the database transaction lifecycle.
func (*Tx) ExecTx ¶
ExecTx executes a SQL statement (see sql.DB.ExecContext).
func (*Tx) Now ¶
Now returns the time the transaction was started, hence returning a consistent time value across the whole transaction lifecycle.
func (*Tx) QueryRowTx ¶
QueryRowTx queries a single row (see sql.DB.QueryRowContext).
func (*Tx) QueryTx ¶
QueryTx executes a database query (see sql.DB.QueryContext).
func (*Tx) RollbackUncommitedTx ¶ added in v0.0.4
RollbackUncommitedTx performs a rollback in case the transaction has not been commited.
See Driver.BeginTx for details of the database transaction lifecycle.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package memory provides a memory based database configuration (by actually providing a SQLite3 database with memory backend).
|
Package memory provides a memory based database configuration (by actually providing a SQLite3 database with memory backend). |
|
Package sqlite provides a PostgreSQL based database configuration.
|
Package sqlite provides a PostgreSQL based database configuration. |
|
Package sqlite provides a SQLite3 based database configuration.
|
Package sqlite provides a SQLite3 based database configuration. |