sqlx

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2025 License: MIT Imports: 8 Imported by: 0

README

SQLX

Go Reference

Package sqlx provides extra features for go's standard database/sql library.

Feature

  • The APIs is very concise.
  • Support "range-over-func" feature.
  • Conveniently map query results.

Usage

import "gitee.com/erdian718/sqlx"

func main() {
	db, err := sqlx.Open(...)
	if err != nil {
		panic(err)
	}
	defer db.Close()

	db.Query(...)
}

Note

  • Currently, only struct and struct pointer mapping are supported.

Documentation

Overview

Package sqlx provides extra features for go's standard database/sql library.

Index

Constants

View Source
const (
	LevelDefault         = sql.LevelDefault
	LevelReadUncommitted = sql.LevelReadUncommitted
	LevelReadCommitted   = sql.LevelReadCommitted
	LevelWriteCommitted  = sql.LevelWriteCommitted
	LevelRepeatableRead  = sql.LevelRepeatableRead
	LevelSnapshot        = sql.LevelSnapshot
	LevelSerializable    = sql.LevelSerializable
	LevelLinearizable    = sql.LevelLinearizable
)

Isolation levels.

Variables

View Source
var (
	ErrNotFound  = errors.New("sqlx: result is not found")
	ErrNotSingle = errors.New("sqlx: result is not single")
)

Errors.

Functions

func Named added in v0.6.0

func Named(name string, value any) sql.NamedArg

Named creates a named argument.

func Query

func Query[T any](db *DB, cmd string, args ...any) iter.Seq2[T, error]

Query executes a query that returns a sequence.

func QueryContext

func QueryContext[T any](ctx context.Context, db *DB, cmd string, args ...any) iter.Seq2[T, error]

QueryContext executes a query that returns a sequence.

func QuerySingle added in v0.5.0

func QuerySingle[T any](db *DB, cmd string, args ...any) (T, error)

QuerySingle executes a query that returns a single value.

func QuerySingleContext added in v0.7.0

func QuerySingleContext[T any](ctx context.Context, db *DB, cmd string, args ...any) (T, error)

QuerySingleContext executes a query that returns a single value.

Types

type DB

type DB sql.DB

DB is a database handle representing a pool of zero or more underlying connections. It's safe for concurrent use by multiple goroutines.

func NewDB added in v0.6.0

func NewDB(raw *sql.DB) *DB

NewDB creates a DB from sql.DB.

func Open

func Open(driverName string, dataSourceName string) (*DB, error)

Open opens a database specified by its database driver name and a driver-specific data source name.

func (*DB) Begin

func (db *DB) Begin(options *TxOptions) (*Tx, error)

Begin starts a transaction. The default isolation level is dependent on the driver.

func (*DB) BeginContext added in v0.6.0

func (db *DB) BeginContext(ctx context.Context, options *TxOptions) (*Tx, error)

Begin starts a transaction. The default isolation level is dependent on the driver.

func (*DB) Close

func (db *DB) Close() error

Close closes the database and prevents new queries from starting.

func (*DB) Exec added in v0.6.0

func (db *DB) Exec(cmd string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows.

func (*DB) ExecContext added in v0.6.0

func (db *DB) ExecContext(ctx context.Context, cmd string, args ...any) (sql.Result, error)

ExecContext executes a query without returning any rows.

func (*DB) GetRow added in v0.6.0

func (db *DB) GetRow(cmd string, args ...any) *sql.Row

GetRow executes a query that returns a single row.

func (*DB) GetRowContext added in v0.6.0

func (db *DB) GetRowContext(ctx context.Context, cmd string, args ...any) *sql.Row

GetRowContext executes a query that returns a single row.

func (*DB) Prepare added in v0.6.0

func (db *DB) Prepare(cmd string) (*Stmt, error)

Prepare creates a prepared statement for later queries or executions.

func (*DB) PrepareContext added in v0.6.0

func (db *DB) PrepareContext(ctx context.Context, cmd string) (*Stmt, error)

PrepareContext creates a prepared statement for later queries or executions.

func (*DB) Query added in v0.6.0

func (db *DB) Query(cmd string, result any, args ...any) error

Query executes a query and fills the slice result.

func (*DB) QueryContext added in v0.6.0

func (db *DB) QueryContext(ctx context.Context, cmd string, result any, args ...any) error

QueryContext executes a query and fills the slice result.

func (*DB) QueryRows added in v0.6.0

func (db *DB) QueryRows(cmd string, args ...any) (*sql.Rows, error)

QueryRows executes a query that returns rows.

func (*DB) QueryRowsContext added in v0.6.0

func (db *DB) QueryRowsContext(ctx context.Context, cmd string, args ...any) (*sql.Rows, error)

QueryRowsContext executes a query that returns rows.

func (*DB) QuerySingle added in v0.7.0

func (db *DB) QuerySingle(cmd string, result any, args ...any) error

QuerySingle executes a query and fills the non-slice result.

func (*DB) QuerySingleContext added in v0.7.0

func (db *DB) QuerySingleContext(ctx context.Context, cmd string, result any, args ...any) error

QuerySingleContext executes a query and fills the non-slice result.

func (*DB) Raw added in v0.6.0

func (db *DB) Raw() *sql.DB

Raw returns the sql.DB.

type IsolationLevel

type IsolationLevel = sql.IsolationLevel

IsolationLevel is the transaction isolation level.

type Stmt

type Stmt sql.Stmt

Stmt is a prepared statement.

func NewStmt added in v0.6.0

func NewStmt(raw *sql.Stmt) *Stmt

NewStmt creates a Stmt from sql.Stmt.

func (*Stmt) Close added in v0.6.0

func (s *Stmt) Close() error

Close closes the statement.

func (*Stmt) Exec

func (s *Stmt) Exec(args ...any) (sql.Result, error)

Exec executes a query without returning any rows.

func (*Stmt) ExecContext

func (s *Stmt) ExecContext(ctx context.Context, args ...any) (sql.Result, error)

ExecContext executes a query without returning any rows.

func (*Stmt) GetRow added in v0.6.0

func (s *Stmt) GetRow(args ...any) *sql.Row

GetRow executes a query that returns a single row.

func (*Stmt) GetRowContext added in v0.6.0

func (s *Stmt) GetRowContext(ctx context.Context, args ...any) *sql.Row

GetRowContext executes a query that returns a single row.

func (*Stmt) Query

func (s *Stmt) Query(result any, args ...any) error

Query executes a query and fills the slice result.

func (*Stmt) QueryContext

func (s *Stmt) QueryContext(ctx context.Context, result any, args ...any) error

QueryContext executes a query and fills the slice result.

func (*Stmt) QueryRows

func (s *Stmt) QueryRows(args ...any) (*sql.Rows, error)

QueryRows executes a query that returns rows.

func (*Stmt) QueryRowsContext

func (s *Stmt) QueryRowsContext(ctx context.Context, args ...any) (*sql.Rows, error)

QueryRowsContext executes a query that returns rows.

func (*Stmt) QuerySingle added in v0.5.0

func (s *Stmt) QuerySingle(result any, args ...any) error

QuerySingle executes a query and fills the non-slice result.

func (*Stmt) QuerySingleContext added in v0.7.0

func (s *Stmt) QuerySingleContext(ctx context.Context, result any, args ...any) error

QuerySingleContext executes a query and fills the non-slice result.

func (*Stmt) Raw added in v0.6.0

func (s *Stmt) Raw() *sql.Stmt

Raw returns the sql.Stmt.

type Tx

type Tx sql.Tx

Tx is an in-progress database transaction.

func NewTx added in v0.6.0

func NewTx(raw *sql.Tx) *Tx

NewTx creates a Tx from sql.Tx.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Exec added in v0.6.0

func (tx *Tx) Exec(cmd string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows.

func (*Tx) ExecContext added in v0.6.0

func (tx *Tx) ExecContext(ctx context.Context, cmd string, args ...any) (sql.Result, error)

ExecContext executes a query without returning any rows.

func (*Tx) GetRow added in v0.6.0

func (tx *Tx) GetRow(cmd string, args ...any) *sql.Row

GetRow executes a query that returns a single row.

func (*Tx) GetRowContext added in v0.6.0

func (tx *Tx) GetRowContext(ctx context.Context, cmd string, args ...any) *sql.Row

GetRowContext executes a query that returns a single row.

func (*Tx) Prepare added in v0.6.0

func (tx *Tx) Prepare(cmd string) (*Stmt, error)

Prepare creates a prepared statement for later queries or executions.

func (*Tx) PrepareContext added in v0.6.0

func (tx *Tx) PrepareContext(ctx context.Context, cmd string) (*Stmt, error)

PrepareContext creates a prepared statement for later queries or executions.

func (*Tx) Query added in v0.6.0

func (tx *Tx) Query(cmd string, result any, args ...any) error

Query executes a query and fills the slice result.

func (*Tx) QueryContext added in v0.6.0

func (tx *Tx) QueryContext(ctx context.Context, cmd string, result any, args ...any) error

QueryContext executes a query and fills the slice result.

func (*Tx) QueryRows added in v0.6.0

func (tx *Tx) QueryRows(cmd string, args ...any) (*sql.Rows, error)

QueryRows executes a query that returns rows.

func (*Tx) QueryRowsContext added in v0.6.0

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

QueryRowsContext executes a query that returns rows.

func (*Tx) QuerySingle added in v0.7.0

func (tx *Tx) QuerySingle(cmd string, result any, args ...any) error

QuerySingle executes a query and fills the non-slice result.

func (*Tx) QuerySingleContext added in v0.7.0

func (tx *Tx) QuerySingleContext(ctx context.Context, cmd string, result any, args ...any) error

QuerySingleContext executes a query and fills the non-slice result.

func (*Tx) Raw added in v0.6.0

func (tx *Tx) Raw() *sql.Tx

Raw returns the sql.Tx.

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback aborts the transaction.

func (*Tx) Stmt

func (tx *Tx) Stmt(stmt *Stmt) *Stmt

Stmt returns a transaction-specific prepared statement from an existing statement.

func (*Tx) StmtContext added in v0.6.0

func (tx *Tx) StmtContext(ctx context.Context, stmt *Stmt) *Stmt

StmtContext returns a transaction-specific prepared statement from an existing statement.

type TxOptions added in v0.6.0

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

TxOptions holds the transaction options.

func NewTxOptions added in v0.6.0

func NewTxOptions() *TxOptions

NewTxOptions creates the default options.

func (*TxOptions) Clone added in v0.6.0

func (options *TxOptions) Clone() *TxOptions

Clone clones the options.

func (*TxOptions) Isolation added in v0.6.0

func (options *TxOptions) Isolation(level IsolationLevel) *TxOptions

Isolation is the transaction isolation level.

func (*TxOptions) ReadOnly added in v0.6.0

func (options *TxOptions) ReadOnly(readOnly bool) *TxOptions

ReadOnly indicates whether the transaction is read-only.

Jump to

Keyboard shortcuts

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