database

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 20 Imported by: 0

README

Common Go database code shared between my projects

GoDoc Build Coverage Go Report Card

License

This project is subject to the the MIT License. See LICENSE information for details.

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

This section is empty.

Variables

This section is empty.

Functions

func DB2Time

func DB2Time(msec int64) time.Time

DB2Time converts the given database time value to the corresponding time.Time value.

func NewID

func NewID() string

NewID generates a id suitable for use as a primary key.

func NoRows

func NoRows(err error) bool

NoRows checks whether the given error is sql.ErrNoRows.

func Now

func Now() int64

Now gets the current time as database compatible int64 type and based on UTC timezone.

func Time2DB

func Time2DB(t time.Time) int64

Time2DB converts the given time.Time value to the corresponding database time value.

Types

type Config

type Config interface {
	// 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 Open

func Open(config Config) (*Driver, error)

Open opens the database represented by the given Config instance.

func (*Driver) BeginTx

func (d *Driver) BeginTx(ctx context.Context) (context.Context, *Tx, error)

BeginTx ensures a database transaction is in place.

Make sure to always close the returned Tx instance by invoking[Tx.CloseTx].

Invoke Tx.CommitTx to commit all database updates performed within the BeginTx block. If Tx.CommitTx is not called before the call to Tx.CloseTx, it will be implicitly rolled back.

In case of an outer to BeginTx call for the identical 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.CloseTx and only if all BeginTx blocks have been commited by invoking Tx.CommitTx the actual database transaction is committed.

func (*Driver) Close

func (d *Driver) Close() error

Close closes the database connection and all related resources.

func (*Driver) Ping

func (d *Driver) Ping(ctx context.Context) error

Ping pings the database (see sql.DB.PingContext).

func (*Driver) UpdateSchema

func (d *Driver) UpdateSchema(ctx context.Context, target Schema) (Schema, Schema, error)

UpdateSchema is used to update the database schema to the given target 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 Schema

type Schema int

Schema version type.

const (
	// SchemaNone indicates no schema has been setup so far.
	SchemaNone Schema = -1
	// Schema0 indicates the basic schema is in place.
	Schema0 Schema = 0
)

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx represents a database transation (see [BeginTx]).

func (*Tx) CloseTx

func (tx *Tx) CloseTx(ctx context.Context) error

Close closes the transaction returned by Driver.BeginTx.

If Tx.CommitTx has not been invoked for the transaction, the transaction is implicitly rolled back.

func (*Tx) CommitTx

func (tx *Tx) CommitTx(ctx context.Context) error

Commit commits all changes since the corresponding Driver.BeginTx call. See Driver.BeginTx and Tx.CloseTx for details of the database transaction lifecycle.

func (*Tx) ExecTx

func (tx *Tx) ExecTx(ctx context.Context, query string, args ...any) error

ExecTx executes a SQL statement (see sql.DB.ExecContext).

func (*Tx) Now

func (tx *Tx) Now() time.Time

Now returns the time the transaction was started, hence returning a consistent time value across the whole transaction lifecycle.

func (*Tx) QueryRowTx

func (tx *Tx) QueryRowTx(ctx context.Context, query string, args ...any) (*sql.Row, error)

QueryRowTx queries a single row (see sql.DB.QueryRowContext).

func (*Tx) QueryTx

func (tx *Tx) QueryTx(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryTx executes a database query (see sql.DB.QueryContext).

type Type

type Type string

Type represents the database type.

func (Type) String

func (name Type) String() string

Directories

Path Synopsis
Package memory provides a memory based database configuration.
Package memory provides a memory based database configuration.
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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL