sqlx

package
v1.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2024 License: MIT Imports: 18 Imported by: 12

README

sqlx

sqlx is a library which provides a set of extensions on go's standard database/sql library. The sqlx versions of sql.DB, sql.TX, sql.Stmt, et al. all leave the underlying interfaces untouched, so that their interfaces are a superset on the standard ones. This makes it relatively painless to integrate existing codebases using database/sql with sqlx.

Major additional concepts are:

  • Marshal rows into structs (with embedded struct support), maps, and slices
  • Named parameter support including prepared statements
  • Get and Select to go quickly from query to struct/slice

issues

Row headers can be ambiguous (SELECT 1 AS a, 2 AS a), and the result of Columns() does not fully qualify column names in queries like:

SELECT a.id, a.name, b.id, b.name FROM foos AS a JOIN foos AS b ON a.parent = b.id;

making a struct or map destination ambiguous. Use AS in your queries to give columns distinct names, rows.Scan to scan them manually, or SliceScan to get a slice of results.

usage

Below is an example which shows some common use cases for sqlx. Check sqlx_test.go for more usage.

package main

import (
    "database/sql"
    "fmt"
    "log"
    
    _ "github.com/lib/pq"
    "github.com/askasoft/pango/sqx/sqlx"
)

var schema = `
CREATE TABLE person (
    first_name text,
    last_name text,
    email text
);

CREATE TABLE place (
    country text,
    city text NULL,
    telcode integer
)`

type Person struct {
    FirstName string `db:"first_name"`
    LastName  string `db:"last_name"`
    Email     string
}

type Place struct {
    Country string
    City    sql.NullString
    TelCode int
}

func main() {
    // this Pings the database trying to connect
    // use sqlx.Open() for sql.Open() semantics
    db, err := sqlx.Connect("postgres", "user=foo dbname=bar sslmode=disable")
    if err != nil {
        log.Fatalln(err)
    }

    // exec the schema or fail; multi-statement Exec behavior varies between
    // database drivers;  pq will exec them all, sqlite3 won't, ymmv
    db.MustExec(schema)
    
    tx := db.MustBeginx()
    tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "Jason", "Moiron", "jmoiron@jmoiron.net")
    tx.MustExec("INSERT INTO person (first_name, last_name, email) VALUES ($1, $2, $3)", "John", "Doe", "johndoeDNE@gmail.net")
    tx.MustExec("INSERT INTO place (country, city, telcode) VALUES ($1, $2, $3)", "United States", "New York", "1")
    tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Hong Kong", "852")
    tx.MustExec("INSERT INTO place (country, telcode) VALUES ($1, $2)", "Singapore", "65")
    // Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
    tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "jane.citzen@example.com"})
    tx.Commit()

    // Query the database, storing results in a []Person (wrapped in []any)
    people := []Person{}
    db.Select(&people, "SELECT * FROM person ORDER BY first_name ASC")
    jason, john := people[0], people[1]

    fmt.Printf("%#v\n%#v", jason, john)
    // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}
    // Person{FirstName:"John", LastName:"Doe", Email:"johndoeDNE@gmail.net"}

    // You can also get a single result, a la QueryRow
    jason = Person{}
    err = db.Get(&jason, "SELECT * FROM person WHERE first_name=$1", "Jason")
    fmt.Printf("%#v\n", jason)
    // Person{FirstName:"Jason", LastName:"Moiron", Email:"jmoiron@jmoiron.net"}

    // if you have null fields and use SELECT *, you must use sql.Null* in your struct
    places := []Place{}
    err = db.Select(&places, "SELECT * FROM place ORDER BY telcode ASC")
    if err != nil {
        fmt.Println(err)
        return
    }
    usa, singsing, honkers := places[0], places[1], places[2]
    
    fmt.Printf("%#v\n%#v\n%#v\n", usa, singsing, honkers)
    // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
    // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}
    // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}

    // Loop through rows using only one struct
    place := Place{}
    rows, err := db.Queryx("SELECT * FROM place")
    for rows.Next() {
        err := rows.StructScan(&place)
        if err != nil {
            log.Fatalln(err)
        } 
        fmt.Printf("%#v\n", place)
    }
    // Place{Country:"United States", City:sql.NullString{String:"New York", Valid:true}, TelCode:1}
    // Place{Country:"Hong Kong", City:sql.NullString{String:"", Valid:false}, TelCode:852}
    // Place{Country:"Singapore", City:sql.NullString{String:"", Valid:false}, TelCode:65}

    // Named queries, using `:name` as the bindvar.  Automatic bindvar support
    // which takes into account the dbtype based on the driverName on sqlx.Open/Connect
    _, err = db.NamedExec(`INSERT INTO person (first_name,last_name,email) VALUES (:first,:last,:email)`, 
        map[string]any{
            "first": "Bin",
            "last": "Smuth",
            "email": "bensmith@allblacks.nz",
    })

    // Selects Mr. Smith from the database
    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:fn`, map[string]any{"fn": "Bin"})

    // Named queries can also use structs.  Their bind names follow the same rules
    // as the name -> db mapping, so struct fields are lowercased and the `db` tag
    // is taken into consideration.
    rows, err = db.NamedQuery(`SELECT * FROM person WHERE first_name=:first_name`, jason)
    
    
    // batch insert
    
    // batch insert with structs
    personStructs := []Person{
        {FirstName: "Ardie", LastName: "Savea", Email: "asavea@ab.co.nz"},
        {FirstName: "Sonny Bill", LastName: "Williams", Email: "sbw@ab.co.nz"},
        {FirstName: "Ngani", LastName: "Laumape", Email: "nlaumape@ab.co.nz"},
    }

    _, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
        VALUES (:first_name, :last_name, :email)`, personStructs)

    // batch insert with maps
    personMaps := []map[string]any{
        {"first_name": "Ardie", "last_name": "Savea", "email": "asavea@ab.co.nz"},
        {"first_name": "Sonny Bill", "last_name": "Williams", "email": "sbw@ab.co.nz"},
        {"first_name": "Ngani", "last_name": "Laumape", "email": "nlaumape@ab.co.nz"},
    }

    _, err = db.NamedExec(`INSERT INTO person (first_name, last_name, email)
        VALUES (:first_name, :last_name, :email)`, personMaps)
}

Documentation

Overview

Package sqlx provides general purpose extensions to database/sql.

It is intended to seamlessly wrap database/sql and provide convenience methods which are useful in the development of database driven applications. None of the underlying database/sql methods are changed. Instead all extended behavior is implemented through new methods defined on wrapper types.

Additions include scanning into structs, named query support, rebinding queries for different drivers, convenient shorthands for common error handling and more.

This is package is forked from github.com/jmoiron/sqlx

Index

Constants

View Source
const (
	BindUnknown  = sqx.BindUnknown
	BindQuestion = sqx.BindQuestion
	BindDollar   = sqx.BindDollar
	BindColon    = sqx.BindColon
	BindAt       = sqx.BindAt
)

Variables

View Source
var (
	ErrConnDone = sql.ErrConnDone
	ErrNoRows   = sql.ErrNoRows
	ErrTxDone   = sql.ErrTxDone
)
View Source
var (
	QuoteDefault   = sqx.QuoteDefault
	QuoteBackticks = sqx.QuoteBackticks
	QuoteBrackets  = sqx.QuoteBrackets
)
View Source
var NameMapper = ref.NewMapperFunc("db", str.SnakeCase)

NameMapper is used to map column names to struct field names. By default, it uses str.SnakeCase to snakecase struct field names. It can be set to whatever you want, but it is encouraged to be set before sqlx is used as name-to-field mappings are cached after first use on a type.

Functions

func Create added in v1.0.26

func Create(x Sqlx, query string, args ...any) (int64, error)

Create does a QueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() and returns Result.LastInsertId().

func CreateContext added in v1.0.26

func CreateContext(ctx context.Context, x Sqlx, query string, args ...any) (int64, error)

CreateContext does a QueryRowxContext() scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func Get

func Get(q Queryerx, dest any, query string, args ...any) error

Get does a QueryRowx() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func GetContext

func GetContext(ctx context.Context, q ContextQueryerx, dest any, query string, args ...any) error

GetContext does a QueryRowxContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func MapScan

func MapScan(r ColScanner, dest map[string]any) error

MapScan scans a single Row into the dest map[string]any. Use this to get results for SQL that might not be under your control (for instance, if you're building an interface for an SQL server that executes SQL from input). Please do not use this as a primary interface! This will modify the map sent to it in place, so reuse the same map with care. Columns which occur more than once in the result will overwrite each other!

func Named

func Named(query string, arg any) (string, []any, error)

Named takes a query using named parameters and an argument and returns a new query with a list of args that can be executed by a database. The return value uses the `?` bindvar.

func NamedCreate added in v1.0.26

func NamedCreate(x Sqlx, query string, arg any) (int64, error)

NamedCreate does a NamedQueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func NamedCreateContext added in v1.0.26

func NamedCreateContext(ctx context.Context, x Sqlx, query string, arg any) (int64, error)

NamedCreateContext does a NamedQueryRow() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), does a NamedExecContext() and returns Result.LastInsertId().

func NamedGet added in v1.0.26

func NamedGet(q NamedQueryer, dest any, query string, arg any) error

NamedGet does a NamedQueryRow() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func NamedGetContext added in v1.0.26

func NamedGetContext(ctx context.Context, q ContextNamedQueryer, dest any, query string, arg any) error

NamedGetContext does a NamedQueryRowContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func NamedSelect added in v1.0.26

func NamedSelect(q NamedQueryer, dest any, query string, arg any) error

NamedSelect executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func NamedSelectContext added in v1.0.26

func NamedSelectContext(ctx context.Context, q ContextNamedQueryer, dest any, query string, arg any) error

NamedSelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func Select

func Select(q Queryerx, dest any, query string, args ...any) error

Select executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func SelectContext

func SelectContext(ctx context.Context, q ContextQueryerx, dest any, query string, args ...any) error

SelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func SliceScan

func SliceScan(r ColScanner) ([]any, error)

SliceScan a row, returning a []any with values similar to MapScan. This function is primarily intended for use where the number of columns is not known. Because you can pass an []any directly to Scan, it's recommended that you do that as it will not have to allocate new slices per row.

func StructScan

func StructScan(rows iRows, dest any) error

StructScan all rows from an sql.Rows or an sqlx.Rows into the dest slice. StructScan will scan in the entire rows result, so if you do not want to allocate structs for the entire result, use Queryx and see sqlx.Rows.StructScan. If rows is sqlx.Rows, it will use its mapper, otherwise it will use the default.

func Transaction

func Transaction(db Beginxer, fc func(tx *Tx) error) (err error)

Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs they are rolled back.

func Transactionx

func Transactionx(ctx context.Context, db BeginTxxer, opts *sql.TxOptions, fc func(tx *Tx) error) (err error)

Transactionx start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs they are rolled back.

Types

type BeginTxxer

type BeginTxxer interface {
	BeginTxx(context.Context, *sql.TxOptions) (*Tx, error)
}

type Beginxer

type Beginxer interface {
	Beginx() (*Tx, error)
}

type BindNamed added in v1.0.26

type BindNamed interface {
	BindNamed(string, any) (string, []any, error)
}

type Binder

type Binder = sqx.Binder

type Build added in v1.0.26

type Build interface {
	Builder() *Builder
}

type Builder added in v1.0.26

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

Builder a simple sql builder NOTE: the arguments are strict to it's order

func (*Builder) Build added in v1.0.26

func (b *Builder) Build() (string, []any)

func (*Builder) Columns added in v1.0.26

func (b *Builder) Columns(cols ...string) *Builder

func (*Builder) Count added in v1.0.26

func (b *Builder) Count(cols ...string) *Builder

Count shortcut for SELECT COUNT(*)

func (*Builder) CountDistinct added in v1.0.26

func (b *Builder) CountDistinct(cols ...string) *Builder

Count shortcut for SELECT COUNT(distinct *)

func (*Builder) Delete added in v1.0.26

func (b *Builder) Delete(tb string) *Builder

func (*Builder) Distinct added in v1.0.26

func (b *Builder) Distinct() *Builder

Distinct set distinct keyword only for SELECT

func (*Builder) From added in v1.0.26

func (b *Builder) From(tb string) *Builder

func (*Builder) In added in v1.0.26

func (b *Builder) In(col string, val any) *Builder

func (*Builder) Insert added in v1.0.26

func (b *Builder) Insert(tb string) *Builder

func (*Builder) Join added in v1.0.26

func (b *Builder) Join(query string, args ...any) *Builder

Join specify Join query and conditions

sqb.Join("JOIN emails ON emails.user_id = users.id AND emails.email = ?", "abc@example.org")

func (*Builder) Limit added in v1.0.26

func (b *Builder) Limit(limit int) *Builder

func (*Builder) Names added in v1.0.26

func (b *Builder) Names(cols ...string) *Builder

Names add named columns and values. Example:

sqb.Insert("a").Names("id", "name", "value") // INSERT INTO a (id, name, value) VALUES (:id, :name,, :value)
sqb.Update("a").Names("name", "value").Where("id = :id") // UPDATE a SET name = :name, value = :value WHERE id = :id

func (*Builder) NotIn added in v1.0.26

func (b *Builder) NotIn(col string, val any) *Builder

func (*Builder) Offset added in v1.0.26

func (b *Builder) Offset(offset int) *Builder

func (*Builder) Omits added in v1.0.26

func (b *Builder) Omits(cols ...string) *Builder

func (*Builder) Order added in v1.0.26

func (b *Builder) Order(order string, desc ...bool) *Builder

func (*Builder) Params added in v1.0.26

func (b *Builder) Params() []any

func (*Builder) Reset added in v1.0.26

func (b *Builder) Reset() *Builder

func (*Builder) Returns added in v1.0.26

func (b *Builder) Returns(cols ...string) *Builder

Returns add RETURNING cols... if `cols` is not specified, RETURNING *

func (*Builder) SQL added in v1.0.26

func (b *Builder) SQL() string

func (*Builder) Select added in v1.0.26

func (b *Builder) Select(cols ...string) *Builder

Select add select columns if `cols` is not specified, default select "*"

func (*Builder) Setc added in v1.0.26

func (b *Builder) Setc(col string, arg any) *Builder

func (*Builder) Setx added in v1.0.26

func (b *Builder) Setx(col string, val string, args ...any) *Builder

func (*Builder) StructNames added in v1.0.26

func (b *Builder) StructNames(a any, omits ...string) *Builder

StructNames add named columns and values for Struct. Example:

type User struct {
	ID    int64
	Name  string
	Value string
}
u := &User{}
sqb.Insert("users").StructNames(u) // INSERT INTO users (id, name, value) VALUES (:id, :name, :value)
sqb.Update("users").StructNames(u, "id").Where("id = :id") // UPDATE users SET name = :name, value = :value WHERE id = :id

func (*Builder) StructPrefixSelect added in v1.0.26

func (b *Builder) StructPrefixSelect(a any, prefix string, omits ...string) *Builder

StructPrefixSelect add columns for Struct. Example:

type User struct {
	ID    int64
	Name  string
	Value string
}
u := &User{}
sqb.StructPrefixSelect(u, "u.") // SELECT u.id, u.name, value FROM ...

func (*Builder) StructSelect added in v1.0.26

func (b *Builder) StructSelect(a any, omits ...string) *Builder

StructSelect add columns for Struct. Example:

type User struct {
	ID    int64
	Name  string
	Value string
}
u := &User{}
sqb.StructSelect(u) // SELECT id, name, value FROM ...

func (*Builder) Update added in v1.0.26

func (b *Builder) Update(tb string) *Builder

func (*Builder) Values added in v1.0.26

func (b *Builder) Values(vals ...string) *Builder

func (*Builder) Where added in v1.0.26

func (b *Builder) Where(q string, args ...any) *Builder

type ColScanner

type ColScanner interface {
	Columns() ([]string, error)
	Scan(dest ...any) error
	Err() error
}

ColScanner is an interface used by MapScan and SliceScan

type Conn

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

Conn is a wrapper around sql.Conn with extra functionality

func (*Conn) BeginTx added in v1.0.26

func (c *Conn) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*Conn) BeginTxx

func (c *Conn) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTxx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginxContext is canceled.

func (*Conn) Beginx added in v1.0.26

func (c *Conn) Beginx() (*Tx, error)

Beginx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx.

func (*Conn) BindNamed added in v1.0.26

func (c *Conn) BindNamed(query string, arg any) (string, []any, error)

BindNamed binds a query using the DB driver's bindvar type.

func (*Conn) Binder

func (ext *Conn) Binder() Binder

Binder returns the binder by driverName passed to the Open function for this DB.

func (*Conn) Builder added in v1.0.26

func (ext *Conn) Builder() *Builder

Builder returns a new sql builder

func (*Conn) Close added in v1.0.26

func (c *Conn) Close() error

Close returns the connection to the connection pool. All operations after a Close will return with ErrConnDone. Close is safe to call concurrently with other operations and will block until all other operations finish. It may be useful to first cancel any used context and then call close directly after.

func (*Conn) Conn added in v1.0.26

func (c *Conn) Conn() *sql.Conn

Conn returns the underlying *sql.Conn

func (*Conn) Create added in v1.0.26

func (c *Conn) Create(query string, args ...any) (int64, error)

Create does a QueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() and returns Result.LastInsertId().

func (*Conn) CreateContext added in v1.0.26

func (c *Conn) CreateContext(ctx context.Context, query string, args ...any) (int64, error)

CreateContext does a QueryRowxContext() scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func (*Conn) DriverName

func (ext *Conn) DriverName() string

DriverName returns the driverName passed to the Open function for this DB.

func (*Conn) Exec added in v1.0.26

func (c *Conn) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Conn) ExecContext added in v1.0.26

func (c *Conn) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Conn) Get added in v1.0.26

func (c *Conn) Get(dest any, query string, args ...any) error

Get does a QueryRowx() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Conn) GetContext

func (c *Conn) GetContext(ctx context.Context, dest any, query string, args ...any) error

GetContext does a QueryRowxContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Conn) IsUnsafe

func (ext *Conn) IsUnsafe() bool

IsUnsafe returns the unsafe

func (*Conn) Mapper

func (ext *Conn) Mapper() *ref.Mapper

Mapper returns the mapper

func (*Conn) NamedCreate added in v1.0.26

func (c *Conn) NamedCreate(query string, arg any) (int64, error)

NamedCreate does a NamedQueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func (*Conn) NamedCreateContext added in v1.0.26

func (c *Conn) NamedCreateContext(ctx context.Context, query string, arg any) (int64, error)

NamedCreateContext does a NamedQueryRow() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), does a NamedExecContext() and returns Result.LastInsertId().

func (*Conn) NamedExec added in v1.0.26

func (c *Conn) NamedExec(query string, arg any) (sql.Result, error)

NamedExec using this Conn. Any named placeholder parameters are replaced with fields from arg.

func (*Conn) NamedExecContext added in v1.0.26

func (c *Conn) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)

NamedExecContext using this Conn. Any named placeholder parameters are replaced with fields from arg.

func (*Conn) NamedGet added in v1.0.26

func (c *Conn) NamedGet(dest any, query string, arg any) error

NamedGet does a NamedQueryRow() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Conn) NamedGetContext added in v1.0.26

func (c *Conn) NamedGetContext(ctx context.Context, dest any, query string, arg any) error

NamedGetContext does a NamedQueryRowContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Conn) NamedQuery added in v1.0.26

func (c *Conn) NamedQuery(query string, arg any) (*Rows, error)

NamedQuery using this Conn. Any named placeholder parameters are replaced with fields from arg.

func (*Conn) NamedQueryContext added in v1.0.26

func (c *Conn) NamedQueryContext(ctx context.Context, query string, arg any) (*Rows, error)

NamedQueryContext using this Conn. Any named placeholder parameters are replaced with fields from arg.

func (*Conn) NamedQueryRow added in v1.0.26

func (c *Conn) NamedQueryRow(query string, arg any) *Row

NamedQueryRow using this Conn. Any named placeholder parameters are replaced with fields from arg.

func (*Conn) NamedQueryRowContext added in v1.0.26

func (c *Conn) NamedQueryRowContext(ctx context.Context, query string, arg any) *Row

NamedQueryRowContext using the Conn. Any named placeholder parameters are replaced with fields from arg.

func (*Conn) NamedSelect added in v1.0.26

func (c *Conn) NamedSelect(dest any, query string, arg any) error

NamedSelect executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func (*Conn) NamedSelectContext added in v1.0.26

func (c *Conn) NamedSelectContext(ctx context.Context, dest any, query string, arg any) error

NamedSelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func (*Conn) Ping added in v1.0.26

func (c *Conn) Ping() error

Ping verifies the connection to the database is still alive.

func (*Conn) PingContext added in v1.0.26

func (c *Conn) PingContext(ctx context.Context) error

PingContext verifies the connection to the database is still alive.

func (*Conn) Prepare added in v1.0.26

func (c *Conn) Prepare(query string) (*sql.Stmt, error)

Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.

The provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Conn) PrepareContext added in v1.0.26

func (c *Conn) PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)

PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.

The provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Conn) Preparex added in v1.0.26

func (c *Conn) Preparex(query string) (*Stmt, error)

Preparex returns an sqlx.Stmt instead of a sql.Stmt.

The provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Conn) PreparexContext

func (c *Conn) PreparexContext(ctx context.Context, query string) (*Stmt, error)

PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.

The provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Conn) Query added in v1.0.26

func (c *Conn) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Conn) QueryContext added in v1.0.26

func (c *Conn) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Conn) QueryRow added in v1.0.26

func (c *Conn) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Conn) QueryRowContext added in v1.0.26

func (c *Conn) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Conn) QueryRowx added in v1.0.26

func (c *Conn) QueryRowx(query string, args ...any) *Row

QueryRowx queries the database and returns an *sqlx.Row. Any placeholder parameters are replaced with supplied args.

func (*Conn) QueryRowxContext

func (c *Conn) QueryRowxContext(ctx context.Context, query string, args ...any) *Row

QueryRowxContext queries the database and returns an *sqlx.Row. Any placeholder parameters are replaced with supplied args.

func (*Conn) Queryx added in v1.0.26

func (c *Conn) Queryx(query string, args ...any) (*Rows, error)

Queryx queries the database and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args.

func (*Conn) QueryxContext

func (c *Conn) QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)

QueryxContext queries the database and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args.

func (*Conn) Quote

func (ext *Conn) Quote(s string) string

Quote quote string 's' with quote marks [2]rune, return (m[0] + s + m[1])

func (*Conn) Quoter

func (ext *Conn) Quoter() Quoter

Quoter returns the quoter by driverName passed to the Open function for this DB.

func (*Conn) Raw added in v1.0.26

func (c *Conn) Raw(f func(driverConn any) error) error

Raw executes f exposing the underlying driver connection for the duration of f. The driverConn must not be used outside of f.

Once f returns and err is not driver.ErrBadConn, the Conn will continue to be usable until Conn.Close is called.

func (*Conn) Rebind

func (ext *Conn) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bindvar type.

func (*Conn) Select added in v1.0.26

func (c *Conn) Select(dest any, query string, args ...any) error

Select executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func (*Conn) SelectContext

func (c *Conn) SelectContext(ctx context.Context, dest any, query string, args ...any) error

SelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func (*Conn) SupportLastInsertID

func (ext *Conn) SupportLastInsertID() bool

SupportRetuning check sql driver support "RETUNING"

func (*Conn) Transactionx

func (c *Conn) Transactionx(ctx context.Context, opts *sql.TxOptions, fc func(tx *Tx) error) (err error)

Transactionx start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs they are rolled back.

type ContextMixer added in v1.0.26

type ContextMixer interface {
	GetContext(ctx context.Context, dest any, query string, args ...any) error
	SelectContext(ctx context.Context, dest any, query string, args ...any) error
	CreateContext(ctx context.Context, query string, args ...any) (int64, error)
}

type ContextNamedExecer added in v1.0.26

type ContextNamedExecer interface {
	NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)
}

type ContextNamedQueryer added in v1.0.26

type ContextNamedQueryer interface {
	NamedQueryContext(ctx context.Context, query string, arg any) (*Rows, error)
	NamedQueryRowContext(ctx context.Context, query string, arg any) *Row
}

type ContextPreparerx added in v1.0.26

type ContextPreparerx interface {
	PreparexContext(ctx context.Context, query string) (*Stmt, error)
}

type ContextQueryerx added in v1.0.26

type ContextQueryerx interface {
	QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)
	QueryRowxContext(ctx context.Context, query string, args ...any) *Row
}

type DB

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

DB is a wrapper around sql.DB which keeps track of the driverName upon Open, used mostly to automatically bind named queries using the right bindvars.

func Connect

func Connect(driverName, dataSourceName string, trace ...Trace) (*DB, error)

Connect to a database and verify with a ping.

func ConnectContext

func ConnectContext(ctx context.Context, driverName, dataSourceName string) (*DB, error)

ConnectContext to a database and verify with a ping.

func MustConnect

func MustConnect(driverName, dataSourceName string) *DB

MustConnect connects to a database and panics on error.

func MustOpen

func MustOpen(driverName, dataSourceName string) *DB

MustOpen is the same as sql.Open, but returns an *sqlx.DB instead and panics on error.

func NewDB

func NewDB(db *sql.DB, driverName string, trace ...Trace) *DB

NewDB returns a new sqlx DB wrapper for a pre-existing *sql.DB. The driverName of the original database is required for named query support.

func Open

func Open(driverName, dataSourceName string, trace ...Trace) (*DB, error)

Open is the same as sql.Open, but returns an *sqlx.DB instead.

func (*DB) Begin added in v1.0.26

func (db *DB) Begin() (*sql.Tx, error)

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

func (*DB) BeginTx added in v1.0.26

func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a transaction.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.

The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.

func (*DB) BeginTxx

func (db *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*Tx, error)

BeginTxx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginxContext is canceled.

func (*DB) Beginx

func (db *DB) Beginx() (*Tx, error)

Beginx begins a transaction and returns an *sqlx.Tx instead of an *sql.Tx.

func (*DB) BindNamed

func (db *DB) BindNamed(query string, arg any) (string, []any, error)

BindNamed binds a query using the DB driver's bindvar type.

func (*DB) Binder

func (ext *DB) Binder() Binder

Binder returns the binder by driverName passed to the Open function for this DB.

func (*DB) Builder added in v1.0.26

func (ext *DB) Builder() *Builder

Builder returns a new sql builder

func (*DB) Close added in v1.0.26

func (db *DB) Close() error

Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish.

It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.

func (*DB) Conn added in v1.0.26

func (db *DB) Conn(ctx context.Context) (*sql.Conn, error)

Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session.

Every Conn must be returned to the database pool after use by calling Conn.Close.

func (*DB) Connx

func (db *DB) Connx(ctx context.Context) (*Conn, error)

Connx returns an *sqlx.Conn instead of an *sql.Conn.

func (*DB) Create added in v1.0.26

func (db *DB) Create(query string, args ...any) (int64, error)

Create does a QueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() and returns Result.LastInsertId().

func (*DB) CreateContext added in v1.0.26

func (db *DB) CreateContext(ctx context.Context, query string, args ...any) (int64, error)

CreateContext does a QueryRowxContext() scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func (*DB) DB added in v1.0.26

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

DB returns the underlying *sql.DB

func (*DB) Driver added in v1.0.26

func (db *DB) Driver() driver.Driver

Driver returns the database's underlying driver.

func (*DB) DriverName

func (ext *DB) DriverName() string

DriverName returns the driverName passed to the Open function for this DB.

func (*DB) Exec added in v1.0.26

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

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*DB) ExecContext added in v1.0.26

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

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*DB) Get

func (db *DB) Get(dest any, query string, args ...any) error

Get does a QueryRowx() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*DB) GetContext

func (db *DB) GetContext(ctx context.Context, dest any, query string, args ...any) error

GetContext does a QueryRowxContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*DB) IsUnsafe

func (ext *DB) IsUnsafe() bool

IsUnsafe returns the unsafe

func (*DB) Mapper

func (ext *DB) Mapper() *ref.Mapper

Mapper returns the mapper

func (*DB) MapperFunc

func (db *DB) MapperFunc(mf func(string) string)

MapperFunc sets a new mapper for this db using the default sqlx struct tag and the provided mapper function.

func (*DB) MustBeginTx

func (db *DB) MustBeginTx(ctx context.Context, opts *sql.TxOptions) *Tx

MustBeginTx starts a transaction, and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx.

The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to MustBeginContext is canceled.

func (*DB) MustBeginx added in v1.0.26

func (db *DB) MustBeginx() *Tx

MustBeginx starts a transaction, and panics on error. Returns an *sqlx.Tx instead of an *sql.Tx.

func (*DB) MustExec

func (db *DB) MustExec(query string, args ...any) sql.Result

MustExec (panic) runs MustExec using this database. Any placeholder parameters are replaced with supplied args.

func (*DB) MustExecContext

func (db *DB) MustExecContext(ctx context.Context, query string, args ...any) sql.Result

MustExecContext (panic) runs MustExec using this database. Any placeholder parameters are replaced with supplied args.

func (*DB) NamedCreate added in v1.0.26

func (db *DB) NamedCreate(query string, arg any) (int64, error)

NamedCreate does a NamedQueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func (*DB) NamedCreateContext added in v1.0.26

func (db *DB) NamedCreateContext(ctx context.Context, query string, arg any) (int64, error)

NamedCreateContext does a NamedQueryRow() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), does a NamedExecContext() and returns Result.LastInsertId().

func (*DB) NamedExec

func (db *DB) NamedExec(query string, arg any) (sql.Result, error)

NamedExec using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedExecContext

func (db *DB) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)

NamedExecContext using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedGet added in v1.0.26

func (db *DB) NamedGet(dest any, query string, arg any) error

NamedGet does a NamedQueryRow() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*DB) NamedGetContext added in v1.0.26

func (db *DB) NamedGetContext(ctx context.Context, dest any, query string, arg any) error

NamedGetContext does a NamedQueryRowContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*DB) NamedQuery

func (db *DB) NamedQuery(query string, arg any) (*Rows, error)

NamedQuery using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedQueryContext

func (db *DB) NamedQueryContext(ctx context.Context, query string, arg any) (*Rows, error)

NamedQueryContext using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedQueryRow

func (db *DB) NamedQueryRow(query string, arg any) *Row

NamedQueryRow using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedQueryRowContext added in v1.0.26

func (db *DB) NamedQueryRowContext(ctx context.Context, query string, arg any) *Row

NamedQueryRowContext using the DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedSelect added in v1.0.26

func (db *DB) NamedSelect(dest any, query string, arg any) error

NamedSelect executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func (*DB) NamedSelectContext added in v1.0.26

func (db *DB) NamedSelectContext(ctx context.Context, dest any, query string, arg any) error

NamedSelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func (*DB) Ping added in v1.0.26

func (db *DB) Ping() error

Ping verifies a connection to the database is still alive, establishing a connection if necessary.

func (*DB) PingContext added in v1.0.26

func (db *DB) PingContext(ctx context.Context) error

PingContext verifies a connection to the database is still alive, establishing a connection if necessary.

func (*DB) Prepare added in v1.0.26

func (db *DB) Prepare(query string) (*sql.Stmt, error)

func (*DB) PrepareContext added in v1.0.26

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

PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement.

func (*DB) PrepareNamed

func (db *DB) PrepareNamed(query string) (*NamedStmt, error)

PrepareNamed returns an sqlx.NamedStmt

func (*DB) PrepareNamedContext

func (db *DB) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)

PrepareNamedContext returns an sqlx.NamedStmt

func (*DB) Preparex

func (db *DB) Preparex(query string) (*Stmt, error)

Preparex returns an sqlx.Stmt instead of a sql.Stmt

func (*DB) PreparexContext

func (db *DB) PreparexContext(ctx context.Context, query string) (*Stmt, error)

PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.

The provided context is used for the preparation of the statement, not for the execution of the statement.

func (*DB) Query added in v1.0.26

func (db *DB) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryContext added in v1.0.26

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

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryRow added in v1.0.26

func (db *DB) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*DB) QueryRowContext added in v1.0.26

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

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*DB) QueryRowx

func (db *DB) QueryRowx(query string, args ...any) *Row

QueryRowx queries the database and returns an *sqlx.Row. Any placeholder parameters are replaced with supplied args.

func (*DB) QueryRowxContext

func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...any) *Row

QueryRowxContext queries the database and returns an *sqlx.Row. Any placeholder parameters are replaced with supplied args.

func (*DB) Queryx

func (db *DB) Queryx(query string, args ...any) (*Rows, error)

Queryx queries the database and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args.

func (*DB) QueryxContext

func (db *DB) QueryxContext(ctx context.Context, query string, args ...any) (*Rows, error)

QueryxContext queries the database and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args.

func (*DB) Quote

func (ext *DB) Quote(s string) string

Quote quote string 's' with quote marks [2]rune, return (m[0] + s + m[1])

func (*DB) Quoter

func (ext *DB) Quoter() Quoter

Quoter returns the quoter by driverName passed to the Open function for this DB.

func (*DB) Rebind

func (ext *DB) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bindvar type.

func (*DB) Select

func (db *DB) Select(dest any, query string, args ...any) error

Select executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func (*DB) SelectContext

func (db *DB) SelectContext(ctx context.Context, dest any, query string, args ...any) error

SelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func (*DB) SetConnMaxIdleTime added in v1.0.26

func (db *DB) SetConnMaxIdleTime(d time.Duration)

SetConnMaxIdleTime sets the maximum amount of time a connection may be idle.

Expired connections may be closed lazily before reuse.

If d <= 0, connections are not closed due to a connection's idle time.

func (*DB) SetConnMaxLifetime added in v1.0.26

func (db *DB) SetConnMaxLifetime(d time.Duration)

SetConnMaxLifetime sets the maximum amount of time a connection may be reused.

Expired connections may be closed lazily before reuse.

If d <= 0, connections are not closed due to a connection's age.

func (*DB) SetMaxIdleConns added in v1.0.26

func (db *DB) SetMaxIdleConns(n int)

SetMaxIdleConns sets the maximum number of connections in the idle connection pool.

If MaxOpenConns is greater than 0 but less than the new MaxIdleConns, then the new MaxIdleConns will be reduced to match the MaxOpenConns limit.

If n <= 0, no idle connections are retained.

The default max idle connections is currently 2. This may change in a future release.

func (*DB) SetMaxOpenConns added in v1.0.26

func (db *DB) SetMaxOpenConns(n int)

SetMaxOpenConns sets the maximum number of open connections to the database.

If MaxIdleConns is greater than 0 and the new MaxOpenConns is less than MaxIdleConns, then MaxIdleConns will be reduced to match the new MaxOpenConns limit.

If n <= 0, then there is no limit on the number of open connections. The default is 0 (unlimited).

func (*DB) Stats added in v1.0.26

func (db *DB) Stats() sql.DBStats

Stats returns database statistics.

func (*DB) SupportLastInsertID

func (ext *DB) SupportLastInsertID() bool

SupportRetuning check sql driver support "RETUNING"

func (*DB) Transaction

func (db *DB) Transaction(fc func(tx *Tx) error) (err error)

Transaction start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs they are rolled back.

func (*DB) Transactionx

func (db *DB) Transactionx(ctx context.Context, opts *sql.TxOptions, fc func(tx *Tx) error) (err error)

Transactionx start a transaction as a block, return error will rollback, otherwise to commit. Transaction executes an arbitrary number of commands in fc within a transaction. On success the changes are committed; if an error occurs they are rolled back.

func (*DB) Unsafe

func (db *DB) Unsafe() *DB

Unsafe returns a version of DB which will silently succeed to scan when columns in the SQL result have no fields in the destination struct. sqlx.Stmt and sqlx.Tx which are created from this DB will inherit its safety behavior.

type Mixer added in v1.0.26

type Mixer interface {
	Get(dest any, query string, args ...any) error
	Select(dest any, query string, args ...any) error
	Create(query string, args ...any) (int64, error)
}

type NamedExecer

type NamedExecer interface {
	NamedExec(query string, arg any) (sql.Result, error)
}

type NamedQueryer

type NamedQueryer interface {
	NamedQuery(query string, arg any) (*Rows, error)
	NamedQueryRow(query string, arg any) *Row
}

type NamedStmt

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

NamedStmt is a prepared statement that executes named queries. Prepare it how you would execute a NamedQuery, but pass in a struct or map when executing.

func (*NamedStmt) Close

func (ns *NamedStmt) Close() error

Close closes the named statement.

func (*NamedStmt) Exec

func (ns *NamedStmt) Exec(arg any) (sql.Result, error)

Exec executes a named statement using the struct passed. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) ExecContext

func (ns *NamedStmt) ExecContext(ctx context.Context, arg any) (sql.Result, error)

ExecContext executes a named statement using the struct passed. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) Get

func (ns *NamedStmt) Get(dest any, arg any) error

Get using this NamedStmt Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) GetContext

func (ns *NamedStmt) GetContext(ctx context.Context, dest any, arg any) error

GetContext using this NamedStmt Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) IsUnsafe

func (ns *NamedStmt) IsUnsafe() bool

IsUnsafe return unsafe

func (*NamedStmt) MustExec

func (ns *NamedStmt) MustExec(arg any) sql.Result

MustExec execs a NamedStmt, panicing on error Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) MustExecContext

func (ns *NamedStmt) MustExecContext(ctx context.Context, arg any) sql.Result

MustExecContext execs a NamedStmt, panicing on error Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) Query

func (ns *NamedStmt) Query(arg any) (*sql.Rows, error)

Query executes a named statement using the struct argument, returning rows. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) QueryContext

func (ns *NamedStmt) QueryContext(ctx context.Context, arg any) (*sql.Rows, error)

QueryContext executes a named statement using the struct argument, returning rows. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) QueryRow

func (ns *NamedStmt) QueryRow(arg any) *Row

QueryRow executes a named statement against the database. Because sqlx cannot create a *sql.Row with an error condition pre-set for binding errors, sqlx returns a *sqlx.Row instead. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) QueryRowContext

func (ns *NamedStmt) QueryRowContext(ctx context.Context, arg any) *Row

QueryRowContext executes a named statement against the database. Because sqlx cannot create a *sql.Row with an error condition pre-set for binding errors, sqlx returns a *sqlx.Row instead. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) QueryRowx

func (ns *NamedStmt) QueryRowx(arg any) *Row

QueryRowx this NamedStmt. Because of limitations with QueryRow, this is an alias for QueryRow. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) QueryRowxContext

func (ns *NamedStmt) QueryRowxContext(ctx context.Context, arg any) *Row

QueryRowxContext this NamedStmt. Because of limitations with QueryRow, this is an alias for QueryRow. Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) Queryx

func (ns *NamedStmt) Queryx(arg any) (*Rows, error)

Queryx using this NamedStmt Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) QueryxContext

func (ns *NamedStmt) QueryxContext(ctx context.Context, arg any) (*Rows, error)

QueryxContext using this NamedStmt Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) Select

func (ns *NamedStmt) Select(dest any, arg any) error

Select using this NamedStmt Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) SelectContext

func (ns *NamedStmt) SelectContext(ctx context.Context, dest any, arg any) error

SelectContext using this NamedStmt Any named placeholder parameters are replaced with fields from arg.

func (*NamedStmt) Unsafe

func (ns *NamedStmt) Unsafe() *NamedStmt

Unsafe creates an unsafe version of the NamedStmt

type Preparerx

type Preparerx interface {
	Preparex(query string) (*Stmt, error)
}

type Queryerx

type Queryerx interface {
	Queryx(query string, args ...any) (*Rows, error)
	QueryRowx(query string, args ...any) *Row
}

type Quoter added in v1.0.26

type Quoter = sqx.Quoter

type Result

type Result = sql.Result

type Row

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

Row is a reimplementation of sql.Row in order to gain access to the underlying sql.Rows.Columns() data, necessary for StructScan.

func (*Row) Binder

func (ext *Row) Binder() Binder

Binder returns the binder by driverName passed to the Open function for this DB.

func (*Row) Builder added in v1.0.26

func (ext *Row) Builder() *Builder

Builder returns a new sql builder

func (*Row) ColumnTypes

func (r *Row) ColumnTypes() ([]*sql.ColumnType, error)

ColumnTypes returns the underlying sql.Rows.ColumnTypes(), or the deferred error

func (*Row) Columns

func (r *Row) Columns() ([]string, error)

Columns returns the underlying sql.Rows.Columns(), or the deferred error usually returned by Row.Scan()

func (*Row) DriverName

func (ext *Row) DriverName() string

DriverName returns the driverName passed to the Open function for this DB.

func (*Row) Err

func (r *Row) Err() error

Err returns the error encountered while scanning.

func (*Row) IsUnsafe

func (ext *Row) IsUnsafe() bool

IsUnsafe returns the unsafe

func (*Row) MapScan

func (r *Row) MapScan(dest map[string]any) error

MapScan using this Rows.

func (*Row) Mapper

func (ext *Row) Mapper() *ref.Mapper

Mapper returns the mapper

func (*Row) Quote

func (ext *Row) Quote(s string) string

Quote quote string 's' with quote marks [2]rune, return (m[0] + s + m[1])

func (*Row) Quoter

func (ext *Row) Quoter() Quoter

Quoter returns the quoter by driverName passed to the Open function for this DB.

func (*Row) Rebind

func (ext *Row) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bindvar type.

func (*Row) Scan

func (r *Row) Scan(dest ...any) error

Scan is a fixed implementation of sql.Row.Scan, which does not discard the underlying error from the internal rows object if it exists.

func (*Row) SliceScan

func (r *Row) SliceScan() ([]any, error)

SliceScan using this Rows.

func (*Row) StructScan

func (r *Row) StructScan(dest any) error

StructScan a single Row into dest.

func (*Row) SupportLastInsertID

func (ext *Row) SupportLastInsertID() bool

SupportRetuning check sql driver support "RETUNING"

type Rows

type Rows struct {
	*sql.Rows
	// contains filtered or unexported fields
}

Rows is a wrapper around sql.Rows which caches costly reflect operations during a looped StructScan

func (*Rows) Binder

func (ext *Rows) Binder() Binder

Binder returns the binder by driverName passed to the Open function for this DB.

func (*Rows) Builder added in v1.0.26

func (ext *Rows) Builder() *Builder

Builder returns a new sql builder

func (*Rows) DriverName

func (ext *Rows) DriverName() string

DriverName returns the driverName passed to the Open function for this DB.

func (*Rows) IsUnsafe

func (ext *Rows) IsUnsafe() bool

IsUnsafe returns the unsafe

func (*Rows) MapScan

func (r *Rows) MapScan(dest map[string]any) error

MapScan using this Rows.

func (*Rows) Mapper

func (ext *Rows) Mapper() *ref.Mapper

Mapper returns the mapper

func (*Rows) Quote

func (ext *Rows) Quote(s string) string

Quote quote string 's' with quote marks [2]rune, return (m[0] + s + m[1])

func (*Rows) Quoter

func (ext *Rows) Quoter() Quoter

Quoter returns the quoter by driverName passed to the Open function for this DB.

func (*Rows) Rebind

func (ext *Rows) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bindvar type.

func (*Rows) SliceScan

func (r *Rows) SliceScan() ([]any, error)

SliceScan using this Rows.

func (*Rows) StructScan

func (r *Rows) StructScan(dest any) error

StructScan is like sql.Rows.Scan, but scans a single Row into a single Struct. Use this and iterate over Rows manually when the memory load of Select() might be prohibitive. *Rows.StructScan caches the reflect work of matching up column positions to fields to avoid that overhead per scan, which means it is not safe to run StructScan on the same Rows instance with different struct types.

func (*Rows) SupportLastInsertID

func (ext *Rows) SupportLastInsertID() bool

SupportRetuning check sql driver support "RETUNING"

type Sqltx added in v1.0.26

type Sqltx interface {
	Sqlx
	Transactioner
}

type Stmt

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

Stmt is an sqlx wrapper around sql.Stmt with extra functionality

func (*Stmt) Binder

func (ext *Stmt) Binder() Binder

Binder returns the binder by driverName passed to the Open function for this DB.

func (*Stmt) Builder added in v1.0.26

func (ext *Stmt) Builder() *Builder

Builder returns a new sql builder

func (*Stmt) Close added in v1.0.26

func (s *Stmt) Close() error

Close closes the statement.

func (*Stmt) DriverName

func (ext *Stmt) DriverName() string

DriverName returns the driverName passed to the Open function for this DB.

func (*Stmt) Exec added in v1.0.26

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

Exec executes a prepared statement with the given arguments and returns a Result summarizing the effect of the statement.

func (*Stmt) ExecContext added in v1.0.26

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

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Stmt) Get

func (s *Stmt) Get(dest any, args ...any) error

Get using the prepared statement. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Stmt) GetContext

func (s *Stmt) GetContext(ctx context.Context, dest any, args ...any) error

GetContext using the prepared statement. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Stmt) IsUnsafe

func (ext *Stmt) IsUnsafe() bool

IsUnsafe returns the unsafe

func (*Stmt) Mapper

func (ext *Stmt) Mapper() *ref.Mapper

Mapper returns the mapper

func (*Stmt) MustExec

func (s *Stmt) MustExec(args ...any) sql.Result

MustExec (panic) using this statement. Note that the query portion of the error output will be blank, as Stmt does not expose its query. Any placeholder parameters are replaced with supplied args.

func (*Stmt) MustExecContext

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

MustExecContext (panic) using this statement. Note that the query portion of the error output will be blank, as Stmt does not expose its query. Any placeholder parameters are replaced with supplied args.

func (*Stmt) Query added in v1.0.26

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

Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.

func (*Stmt) QueryContext added in v1.0.26

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

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Stmt) QueryRowx

func (s *Stmt) QueryRowx(args ...any) *Row

QueryRowx using this statement. Any placeholder parameters are replaced with supplied args.

func (*Stmt) QueryRowxContext

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

QueryRowxContext using this statement. Any placeholder parameters are replaced with supplied args.

func (*Stmt) Queryx

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

Queryx using this statement. Any placeholder parameters are replaced with supplied args.

func (*Stmt) QueryxContext

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

QueryxContext using this statement. Any placeholder parameters are replaced with supplied args.

func (*Stmt) Quote

func (ext *Stmt) Quote(s string) string

Quote quote string 's' with quote marks [2]rune, return (m[0] + s + m[1])

func (*Stmt) Quoter

func (ext *Stmt) Quoter() Quoter

Quoter returns the quoter by driverName passed to the Open function for this DB.

func (*Stmt) Rebind

func (ext *Stmt) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bindvar type.

func (*Stmt) Select

func (s *Stmt) Select(dest any, args ...any) error

Select using the prepared statement. Any placeholder parameters are replaced with supplied args.

func (*Stmt) SelectContext

func (s *Stmt) SelectContext(ctx context.Context, dest any, args ...any) error

SelectContext using the prepared statement. Any placeholder parameters are replaced with supplied args.

func (*Stmt) Stmt added in v1.0.26

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

Stmt returns the underlying *sql.Stmt

func (*Stmt) SupportLastInsertID

func (ext *Stmt) SupportLastInsertID() bool

SupportRetuning check sql driver support "RETUNING"

func (*Stmt) Unsafe

func (s *Stmt) Unsafe() *Stmt

Unsafe returns a version of Stmt which will silently succeed to scan when columns in the SQL result have no fields in the destination struct.

type Supporter

type Supporter interface {
	SupportLastInsertID() bool
}

type Trace added in v1.0.26

type Trace func(start time.Time, sql string, rows int64, err error)

type Transactioner added in v1.0.26

type Transactioner interface {
	Transaction(func(tx *Tx) error) error
}

type Transactionerx added in v1.0.26

type Transactionerx interface {
	Transactionx(ctx context.Context, opts *sql.TxOptions, fc func(tx *Tx) error) error
}

type Tx

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

Tx is an sqlx wrapper around sql.Tx with extra functionality

func (*Tx) BindNamed

func (tx *Tx) BindNamed(query string, arg any) (string, []any, error)

BindNamed binds a query within a transaction's bindvar type.

func (*Tx) Binder

func (ext *Tx) Binder() Binder

Binder returns the binder by driverName passed to the Open function for this DB.

func (*Tx) Builder added in v1.0.26

func (ext *Tx) Builder() *Builder

Builder returns a new sql builder

func (*Tx) Commit added in v1.0.26

func (tx *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Create added in v1.0.26

func (tx *Tx) Create(query string, args ...any) (int64, error)

Create does a QueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() and returns Result.LastInsertId().

func (*Tx) CreateContext added in v1.0.26

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

CreateContext does a QueryRowxContext() scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func (*Tx) DriverName

func (ext *Tx) DriverName() string

DriverName returns the driverName passed to the Open function for this DB.

func (*Tx) Exec added in v1.0.26

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

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Tx) ExecContext added in v1.0.26

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

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Tx) Get

func (tx *Tx) Get(dest any, query string, args ...any) error

Get does a QueryRowx() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Tx) GetContext

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

GetContext does a QueryRowxContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Tx) IsUnsafe

func (ext *Tx) IsUnsafe() bool

IsUnsafe returns the unsafe

func (*Tx) Mapper

func (ext *Tx) Mapper() *ref.Mapper

Mapper returns the mapper

func (*Tx) MustExec

func (tx *Tx) MustExec(query string, args ...any) sql.Result

MustExec runs MustExec within a transaction. Any placeholder parameters are replaced with supplied args.

func (*Tx) MustExecContext

func (tx *Tx) MustExecContext(ctx context.Context, query string, args ...any) sql.Result

MustExecContext runs MustExecContext within a transaction. Any placeholder parameters are replaced with supplied args.

func (*Tx) NamedCreate added in v1.0.26

func (tx *Tx) NamedCreate(query string, arg any) (int64, error)

NamedCreate does a NamedQueryRowx() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), do a Exec() return Result.LastInsertId().

func (*Tx) NamedCreateContext added in v1.0.26

func (tx *Tx) NamedCreateContext(ctx context.Context, query string, arg any) (int64, error)

NamedCreateContext does a NamedQueryRow() and scans the resulting row returns the last inserted ID. If the db supports LastInsertId(), does a NamedExecContext() and returns Result.LastInsertId().

func (*Tx) NamedExec

func (tx *Tx) NamedExec(query string, arg any) (sql.Result, error)

NamedExec a named query within a transaction. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedExecContext

func (tx *Tx) NamedExecContext(ctx context.Context, query string, arg any) (sql.Result, error)

NamedExecContext using this Tx. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedGet added in v1.0.26

func (tx *Tx) NamedGet(dest any, query string, arg any) error

NamedGet does a NamedQueryRow() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Tx) NamedGetContext added in v1.0.26

func (tx *Tx) NamedGetContext(ctx context.Context, dest any, query string, arg any) error

NamedGetContext does a NamedQueryRowContext() and scans the resulting row to dest. If dest is scannable, the result must only have one column. Otherwise, StructScan is used. Get will return ErrNoRows like row.Scan would. Any placeholder parameters are replaced with supplied args. An error is returned if the result set is empty.

func (*Tx) NamedQuery

func (tx *Tx) NamedQuery(query string, arg any) (*Rows, error)

NamedQuery within a transaction. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedQueryContext added in v1.0.26

func (tx *Tx) NamedQueryContext(ctx context.Context, query string, arg any) (*Rows, error)

NamedQueryContext using this Tx. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedQueryRow

func (tx *Tx) NamedQueryRow(query string, arg any) *Row

NamedQueryRow within a transaction. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedQueryRowContext added in v1.0.26

func (tx *Tx) NamedQueryRowContext(ctx context.Context, query string, arg any) *Row

NamedQueryRowContext within a transaction. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedSelect added in v1.0.26

func (tx *Tx) NamedSelect(dest any, query string, arg any) error

NamedSelect executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func (*Tx) NamedSelectContext added in v1.0.26

func (tx *Tx) NamedSelectContext(ctx context.Context, dest any, query string, arg any) error

NamedSelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func (*Tx) NamedStmt

func (tx *Tx) NamedStmt(stmt *NamedStmt) *NamedStmt

NamedStmt returns a version of the prepared statement which runs within a transaction.

func (*Tx) NamedStmtContext

func (tx *Tx) NamedStmtContext(ctx context.Context, stmt *NamedStmt) *NamedStmt

NamedStmtContext returns a version of the prepared statement which runs within a transaction.

func (*Tx) Prepare added in v1.0.26

func (tx *Tx) Prepare(query string) (*sql.Stmt, error)

Prepare creates a prepared statement for use within a transaction.

The returned statement operates within the transaction and will be closed when the transaction has been committed or rolled back.

To use an existing prepared statement on this transaction, see Tx.Stmt.

func (*Tx) PrepareContext added in v1.0.26

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

PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement.

func (*Tx) PrepareNamed

func (tx *Tx) PrepareNamed(query string) (*NamedStmt, error)

PrepareNamed returns an sqlx.NamedStmt

func (*Tx) PrepareNamedContext

func (tx *Tx) PrepareNamedContext(ctx context.Context, query string) (*NamedStmt, error)

PrepareNamedContext returns an sqlx.NamedStmt

func (*Tx) Preparex

func (tx *Tx) Preparex(query string) (*Stmt, error)

Preparex a statement within a transaction.

func (*Tx) PreparexContext

func (tx *Tx) PreparexContext(ctx context.Context, query string) (*Stmt, error)

PreparexContext returns an sqlx.Stmt instead of a sql.Stmt.

The provided context is used for the preparation of the statement, not for the execution of the statement.

func (*Tx) Query added in v1.0.26

func (tx *Tx) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Tx) QueryContext added in v1.0.26

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

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*Tx) QueryRow added in v1.0.26

func (tx *Tx) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Tx) QueryRowContext added in v1.0.26

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

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Tx) QueryRowx

func (tx *Tx) QueryRowx(query string, args ...any) *Row

QueryRowx within a transaction. Any placeholder parameters are replaced with supplied args.

func (*Tx) QueryRowxContext

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

QueryRowxContext within a transaction and context. Any placeholder parameters are replaced with supplied args.

func (*Tx) Queryx

func (tx *Tx) Queryx(query string, args ...any) (*Rows, error)

Queryx within a transaction. Any placeholder parameters are replaced with supplied args.

func (*Tx) QueryxContext

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

QueryxContext within a transaction and context. Any placeholder parameters are replaced with supplied args.

func (*Tx) Quote

func (ext *Tx) Quote(s string) string

Quote quote string 's' with quote marks [2]rune, return (m[0] + s + m[1])

func (*Tx) Quoter

func (ext *Tx) Quoter() Quoter

Quoter returns the quoter by driverName passed to the Open function for this DB.

func (*Tx) Rebind

func (ext *Tx) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bindvar type.

func (*Tx) Rollback added in v1.0.26

func (tx *Tx) Rollback() error

Rollback aborts the transaction.

func (*Tx) Select

func (tx *Tx) Select(dest any, query string, args ...any) error

Select executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied args.

func (*Tx) SelectContext

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

SelectContext executes a query, and StructScans each row into dest, which must be a slice. If the slice elements are scannable, then the result set must have only one column. Otherwise, StructScan is used. The *sql.Rows are closed automatically. Any placeholder parameters are replaced with supplied arg.

func (*Tx) Stmt added in v1.0.26

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

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

func (*Tx) StmtContext added in v1.0.26

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

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

func (*Tx) Stmtx

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

Stmtx returns a version of the prepared statement which runs within a transaction. Provided stmt can be either *sql.Stmt or *sqlx.Stmt.

func (*Tx) StmtxContext

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

StmtxContext returns a version of the prepared statement which runs within a transaction. Provided stmt can be either *sql.Stmt or *sqlx.Stmt.

func (*Tx) SupportLastInsertID

func (ext *Tx) SupportLastInsertID() bool

SupportRetuning check sql driver support "RETUNING"

func (*Tx) Tx added in v1.0.26

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

Tx returns the underlying *sql.Tx

func (*Tx) Unsafe

func (tx *Tx) Unsafe() *Tx

Unsafe returns a version of Tx which will silently succeed to scan when columns in the SQL result have no fields in the destination struct.

Jump to

Keyboard shortcuts

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