sqx

package
v1.2.11 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2025 License: MIT Imports: 21 Imported by: 17

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	QuoteDefault   = Quoter{'"', '"'}
	QuoteBackticks = Quoter{'`', '`'}
	QuoteBrackets  = Quoter{'[', ']'}
)

Functions

func BindDriver added in v1.0.26

func BindDriver(driverName string, binder Binder)

BindDriver sets the Binder for driverName to binder.

func EndsLike

func EndsLike(s string, escape ...rune) string

StartsLike build a string for like suffix '%' + s The default escape char is backslach '\\'.

func EscapeLike

func EscapeLike(s string, escape ...rune) string

EscapeLike escape sql like string. The default escape char is backslach '\\'.

func EscapeString

func EscapeString(s string) string

At present, this method only turns single-quotes into doubled single-quotes ( <code>"McHale's Navy"</code> => <code>"McHale”s Navy"</code>). It does not handle the cases of percent (%) or underscore (_) for use in LIKE clauses. </p> see http://www.jguru.com/faq/view.jsp?EID=8881

func In added in v1.0.15

func In(col string, val any) (sql string, args []any)

func JSONScan added in v1.0.27

func JSONScan(value, dest any) error

func JSONValue added in v1.0.27

func JSONValue(value any) (driver.Value, error)

func MustExec added in v1.0.26

func MustExec(e Execer, query string, args ...any) sql.Result

MustExec execs the query using e and panics if there was an error. Any placeholder parameters are replaced with supplied args.

func MustExecContext added in v1.0.26

func MustExecContext(ctx context.Context, e ContextExecer, query string, args ...any) sql.Result

MustExecContext execs the query using e and panics if there was an error. Any placeholder parameters are replaced with supplied args.

func MustStmtExec added in v1.0.26

func MustStmtExec(e StmtExecer, args ...any) sql.Result

MustStmtExec execs the statement query using e and panics if there was an error. Any placeholder parameters are replaced with supplied args.

func MustStmtExecContext added in v1.0.26

func MustStmtExecContext(ctx context.Context, e ContextStmtExecer, query string, args ...any) sql.Result

MustStmtExecContext execs the statement query using e and panics if there was an error. Any placeholder parameters are replaced with supplied args.

func NotIn added in v1.0.15

func NotIn(col string, val any) (sql string, args []any)

func Question added in v1.0.15

func Question(n int) string

func Questions added in v1.0.15

func Questions(n int) []string

func QuoteDriver added in v1.0.15

func QuoteDriver(driverName string, quoter Quoter)

QuoteDriver sets the Quoter for driverName to quoter.

func StartsLike

func StartsLike(s string, escape ...rune) string

StartsLike build a string for like prefix s + '%' The default escape char is backslach '\\'.

func StringLike

func StringLike(s string, escape ...rune) string

StringLike build a string for like '%' + s + '%' The default escape char is backslach '\\'.

func Transaction added in v1.0.26

func Transaction(db Beginer, fc func(tx *sql.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 added in v1.0.26

func Transactionx(ctx context.Context, db BeginTxer, opts *sql.TxOptions, fc func(tx *sql.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 BeginTxer added in v1.0.26

type BeginTxer interface {
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}

BeginTxer is an interface used by Transaction

type Beginer added in v1.0.26

type Beginer interface {
	Begin() (*sql.Tx, error)
}

Beginer is an interface for Begin()

type Binder added in v1.0.26

type Binder int
const (
	BindUnknown Binder = iota
	BindQuestion
	BindDollar
	BindColon
	BindAt
)

func GetBinder added in v1.0.26

func GetBinder(driverName string) Binder

GetBinder returns the binder for a given database given a drivername.

func (Binder) Explain added in v1.0.26

func (b Binder) Explain(sql string, maxArgLength int, args ...any) string

Explain generate SQL string with given parameters. The generated SQL is expected to be used in logger, execute it might introduce a SQL injection vulnerability. If a string or binary argument's length exceeds `maxArgLength`, it will be cut and append with '...'.

func (Binder) Placeholder added in v1.0.26

func (b Binder) Placeholder(n int) string

Placeholder generate a place holder mark with No. n.

func (Binder) Rebind added in v1.0.26

func (b Binder) Rebind(sql string) string

Rebind a SQL from the default binder (QUESTION) to the target binder.

type Builder added in v1.0.15

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

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

func (*Builder) AND added in v1.2.9

func (b *Builder) AND() *Builder

AND switch to AND mode

func (*Builder) Btw added in v1.0.27

func (b *Builder) Btw(col string, vmin, vmax any) *Builder

func (*Builder) Build added in v1.0.15

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

Build returns (sql, parameters)

func (*Builder) Clone added in v1.2.9

func (b *Builder) Clone() *Builder

Clone returns a copy of the builder

func (*Builder) Columns added in v1.0.15

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

Columns add select columns

func (*Builder) Columnx added in v1.0.27

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

Columnx add select column and parameters

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.15

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

func (*Builder) Distinct added in v1.0.15

func (b *Builder) Distinct() *Builder

Distinct add 'DISTINCT' for SELECT

func (*Builder) Eq added in v1.0.27

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

func (*Builder) Explain added in v1.2.11

func (b *Builder) Explain(sql string, args ...any) string

func (*Builder) ForUpdate added in v1.2.8

func (b *Builder) ForUpdate() *Builder

ForUpdate add 'FOR UPDATE' for SELECT

func (*Builder) From added in v1.0.15

func (b *Builder) From(tb string, args ...any) *Builder

func (*Builder) Gt added in v1.0.27

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

func (*Builder) Gte added in v1.0.27

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

func (*Builder) ILike added in v1.0.27

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

func (*Builder) In added in v1.0.15

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

func (*Builder) Insert added in v1.0.15

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

func (*Builder) IsNull added in v1.0.27

func (b *Builder) IsNull(col 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) LP added in v1.2.9

func (b *Builder) LP() *Builder

LP append '(' to WHERE

func (*Builder) Like added in v1.0.27

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

func (*Builder) Limit added in v1.0.15

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

func (*Builder) Lt added in v1.0.27

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

func (*Builder) Lte added in v1.0.27

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

func (*Builder) Name added in v1.0.27

func (b *Builder) Name(col string) *Builder

Name add named column and value. Example:

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

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) VALUES (:id, :name)
sqb.Update("a").Names("name", "value").Where("id = :id") // UPDATE a SET name = :name, value = :value WHERE id = :id

func (*Builder) Nbtw added in v1.0.27

func (b *Builder) Nbtw(col string, vmin, vmax any) *Builder

func (*Builder) Neq added in v1.0.27

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

func (*Builder) NotILike added in v1.0.27

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

func (*Builder) NotIn added in v1.0.15

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

func (*Builder) NotLike added in v1.0.27

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

func (*Builder) NotNull added in v1.0.27

func (b *Builder) NotNull(col string) *Builder

func (*Builder) OR added in v1.2.9

func (b *Builder) OR() *Builder

OR switch to OR mode (default is AND mode)

func (*Builder) Offset added in v1.0.15

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

func (*Builder) Omits added in v1.0.26

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

Omits remove named columns and values

func (*Builder) Order added in v1.0.15

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

func (*Builder) Orders added in v1.2.8

func (b *Builder) Orders(order string, defaults ...string) *Builder

func (*Builder) Params added in v1.0.15

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

Params returns parameters

func (*Builder) RP added in v1.2.9

func (b *Builder) RP() *Builder

RP append ')' to WHERE

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

func (*Builder) SQL added in v1.0.15

func (b *Builder) SQL() string

SQL returns sql

func (*Builder) SQLWhere added in v1.2.9

func (b *Builder) SQLWhere() string

SQLWhere returns sql after WHERE

func (*Builder) Select added in v1.0.15

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) Update added in v1.0.15

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

func (*Builder) Values added in v1.0.15

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

func (*Builder) Where added in v1.0.15

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

type ContextExecer added in v1.0.26

type ContextExecer interface {
	ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error)
}

type ContextPinger added in v1.0.26

type ContextPinger interface {
	PingContext(ctx context.Context) error
}

type ContextPreparer added in v1.0.26

type ContextPreparer interface {
	PrepareContext(ctx context.Context, query string) (*sql.Stmt, error)
}

type ContextQueryer added in v1.0.26

type ContextQueryer interface {
	QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
}

type ContextRowQueryer added in v1.0.26

type ContextRowQueryer interface {
	QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row
}

type ContextStmtExecer added in v1.0.26

type ContextStmtExecer interface {
	ExecContext(ctx context.Context, args ...any) (sql.Result, error)
}

type ContextStmtQueryer added in v1.0.26

type ContextStmtQueryer interface {
	QueryContext(ctx context.Context, args ...any) (*sql.Rows, error)
}

type Execer added in v1.0.26

type Execer interface {
	Exec(query string, args ...any) (sql.Result, error)
}

type JSONArray added in v1.0.27

type JSONArray []any

func (*JSONArray) Scan added in v1.0.27

func (ja *JSONArray) Scan(value any) error

func (JSONArray) String added in v1.2.8

func (ja JSONArray) String() string

func (JSONArray) Value added in v1.0.27

func (ja JSONArray) Value() (driver.Value, error)

type JSONInt64Array added in v1.0.27

type JSONInt64Array []int64

func (*JSONInt64Array) Scan added in v1.0.27

func (jia *JSONInt64Array) Scan(value any) error

func (JSONInt64Array) String added in v1.2.8

func (jia JSONInt64Array) String() string

func (JSONInt64Array) Value added in v1.0.27

func (jia JSONInt64Array) Value() (driver.Value, error)

type JSONIntArray added in v1.0.27

type JSONIntArray []int

func (*JSONIntArray) Scan added in v1.0.27

func (jia *JSONIntArray) Scan(value any) error

func (JSONIntArray) String added in v1.2.8

func (jia JSONIntArray) String() string

func (JSONIntArray) Value added in v1.0.27

func (jia JSONIntArray) Value() (driver.Value, error)

type JSONObject added in v1.0.27

type JSONObject map[string]any

func (*JSONObject) Scan added in v1.0.27

func (jo *JSONObject) Scan(value any) error

func (JSONObject) String added in v1.2.8

func (jo JSONObject) String() string

func (JSONObject) Value added in v1.0.27

func (jo JSONObject) Value() (driver.Value, error)

type JSONStringArray added in v1.0.27

type JSONStringArray []string

func (*JSONStringArray) Scan added in v1.0.27

func (jsa *JSONStringArray) Scan(value any) error

func (JSONStringArray) String added in v1.2.8

func (jsa JSONStringArray) String() string

func (JSONStringArray) Value added in v1.0.27

func (jsa JSONStringArray) Value() (driver.Value, error)

type JSONStringObject added in v1.0.27

type JSONStringObject map[string]string

func (*JSONStringObject) Scan added in v1.0.27

func (jso *JSONStringObject) Scan(value any) error

func (JSONStringObject) String added in v1.2.8

func (jso JSONStringObject) String() string

func (JSONStringObject) Value added in v1.0.27

func (jso JSONStringObject) Value() (driver.Value, error)

type Pinger added in v1.0.26

type Pinger interface {
	Ping() error
}

type Preparer added in v1.0.26

type Preparer interface {
	Prepare(query string) (*sql.Stmt, error)
}

type Queryer added in v1.0.26

type Queryer interface {
	Query(query string, args ...any) (*sql.Rows, error)
}

type Quote

type Quote interface {
	Quote(s string) string
}

type Quoter added in v1.0.15

type Quoter []rune

func GetQuoter added in v1.0.15

func GetQuoter(driverName string) Quoter

GetQuoteer returns the quoter for a given database given a drivername.

func (Quoter) Quote added in v1.0.15

func (quoter Quoter) Quote(s string) string

Quote quote string 's' with quotes [2]rune. Returns (quoter[0] + s + quoter[1]), if 's' does not contains any "!\"#$%&'()*+,-/:;<=>?@[\\]^`{|}~" characters.

func (Quoter) Quotes added in v1.0.15

func (quoter Quoter) Quotes(ss ...string) []string

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

type Rebind added in v1.0.26

type Rebind interface {
	Rebind(string) string
}

type RowQueryer added in v1.0.26

type RowQueryer interface {
	QueryRow(query string, args ...any) *sql.Row
}

type Sql added in v1.0.26

type Sql interface {
	Queryer
	Execer
	Preparer
}

Sql the basic interface for sql.DB, sql.Tx

type SqlReader added in v1.0.17

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

func NewSqlReader added in v1.0.17

func NewSqlReader(r io.Reader) *SqlReader

func (*SqlReader) Error added in v1.0.17

func (sr *SqlReader) Error() error

func (*SqlReader) ReadSql added in v1.0.17

func (sr *SqlReader) ReadSql() (string, error)

type Sqlc added in v1.0.26

type Sqlc interface {
	ContextQueryer
	ContextExecer
	ContextPreparer
}

Sqlc the context interface for sql.DB, sql.Tx

type StmtExecer added in v1.0.26

type StmtExecer interface {
	Exec(args ...any) (sql.Result, error)
}

type StmtQueryer added in v1.0.26

type StmtQueryer interface {
	Query(args ...any) (*sql.Rows, error)
}

type Txer added in v1.0.26

type Txer interface {
	Commit() error
	Rollback() error
}

Directories

Path Synopsis
Package sqlx provides general purpose extensions to database/sql.
Package sqlx provides general purpose extensions to database/sql.

Jump to

Keyboard shortcuts

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