Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExecAtomics ¶
func ExecAtomics(ctx context.Context, conn Connection, atomics ...Atomic) (err error)
ExecAtomics opens a transaction and call ExecAtomicsWithTx with it.
func ExecAtomicsWithTx ¶
ExecAtomicsWithTx executes a list of atomic operations within a single transaction. It takes a context and a list of atomic operations, and returns a new context for the next unit of work and an error if the work failed.
Types ¶
type Atomic ¶
Atomic represents a unit of work that can be executed atomically within a transaction. It takes a context and a database connection, and returns a new context for the next unit of work and an error if the work failed.
type Connection ¶
type Connection interface {
Begin() (*sql.Tx, error)
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
Close() error
Conn(ctx context.Context) (*sql.Conn, error)
Driver() driver.Driver
Ping() error
PingContext(ctx context.Context) error
SetConnMaxIdleTime(d time.Duration)
SetConnMaxLifetime(d time.Duration)
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
Stats() sql.DBStats
DB
}
Connection is a database connection interface that provides methods for managing database connections, transactions, and executing queries. It wraps sql.DB functionality to allow for easier testing and dependency injection.
Connection contains everything that *sql.DB provides.
func Open ¶
func Open(driverName, dataSourceName string) (Connection, error)
Open creates a new database connection using the provided driver name and data source name. It returns a Conn interface that can be used to interact with the database.
type DB ¶
type DB interface {
Exec(query string, args ...any) (sql.Result, error)
ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
Prepare(query string) (*sql.Stmt, error)
PrepareContext(ctx context.Context, query string) (*sql.Stmt, 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
}
DB defines the core database operations interface shared by both Conn and Tx. It provides methods for executing queries and preparing statements, allowing for consistent database interactions regardless of whether they're performed within a transaction or not.
type Row ¶
Row represents an interface for scanning a single row of results from a database query into program variables.
type Rows ¶
type Rows interface {
Close() error
Columns() ([]string, error)
ColumnTypes() ([]*sql.ColumnType, error)
Err() error
Next() bool
NextResultSet() bool
Row
}
Rows represents an iterator interface for database query results, providing methods to navigate through multiple rows of data and access column information. It must be closed after use to prevent resource leaks.
type Stmt ¶
type Stmt interface {
Close() error
Exec(args ...any) (sql.Result, error)
ExecContext(ctx context.Context, args ...any) (sql.Result, error)
Query(args ...any) (*sql.Rows, error)
QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
QueryRow(args ...any) *sql.Row
QueryRowContext(ctx context.Context, args ...any) *sql.Row
}
Stmt represents a prepared statement interface that can be executed multiple times with different parameters. It provides methods for executing queries and commands while avoiding SQL injection and improving performance for repeated operations.
type Tx ¶
type Tx interface {
DB
Commit() error
Rollback() error
Stmt(stmt *sql.Stmt) *sql.Stmt
StmtContext(ctx context.Context, stmt *sql.Stmt) *sql.Stmt
}
Tx represents a database transaction interface that provides methods for executing queries within a transaction and controlling its outcome through commit or rollback operations.
Tx contains everything that *sql.Tx provides.