sqlx

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NoRows schema.ExecQuerier = (*noRows)(nil)

NoRows to be used by differs and planners without a connection.

Functions

func ApplyChanges

func ApplyChanges(ctx context.Context, changes []schema.Change, p execPlanner, opts ...migrate.PlanOption) error

ApplyChanges is a helper used by the different drivers to apply changes.

func DefaultValue

func DefaultValue(c *schema.Column) (string, bool)

DefaultValue returns the string represents the DEFAULT of a column.

func ExprLastIndex

func ExprLastIndex(expr string) int

ExprLastIndex scans the first expression in the given string until its end and returns its last index.

func Has

func Has(elements, target any) bool

Has finds the first element in the elements list that matches target, and if so, sets target to that attribute value and returns true.

func IsLiteralBool

func IsLiteralBool(s string) bool

IsLiteralBool reports if the given string is a valid literal bool.

func IsLiteralNumber

func IsLiteralNumber(s string) bool

IsLiteralNumber reports if the given string is a literal number.

func IsQuoted

func IsQuoted(s string, q ...byte) bool

IsQuoted reports if the given string is quoted with one of the given quotes (e.g. ', ", `).

func IsUint

func IsUint(s string) bool

IsUint reports whether the string represents an unsigned integer.

func LinkSchemaTables

func LinkSchemaTables(schemas []*schema.Schema)

LinkSchemaTables links foreign-key stub tables/columns to actual elements.

func MayWrap

func MayWrap(s string) string

MayWrap ensures the given string is wrapped with parentheses. Used by the different drivers to turn strings valid expressions.

func ModeInspectRealm

func ModeInspectRealm(o *schema.InspectRealmOption) schema.InspectMode

ModeInspectRealm returns the InspectMode or its default.

func ModeInspectSchema

func ModeInspectSchema(o *schema.InspectOptions) schema.InspectMode

ModeInspectSchema returns the InspectMode or its default.

func P

func P[T any](v T) *T

P returns a pointer to v.

func ReverseChanges

func ReverseChanges(c []schema.Change)

ReverseChanges reverses the order of the changes.

func SameSchema

func SameSchema(s1, s2 *schema.Schema) bool

SameSchema reports if the given schemas are the same. Objects can be different as they might reside in two different states (current and desired).

func ScanNullBool

func ScanNullBool(rows *sql.Rows) (sql.NullBool, error)

ScanNullBool scans one sql.NullBool record and closes the rows at the end.

func ScanOne

func ScanOne(rows *sql.Rows, dest ...any) (returnErr error)

ScanOne scans one record and closes the rows at the end.

func SchemaFKs

func SchemaFKs(s *schema.Schema, rows *sql.Rows) error

SchemaFKs scans the rows and adds the foreign-key to the schema table. Reference elements are added as stubs and should be linked manually by the caller.

func TypedSchemaFKs

func TypedSchemaFKs[T ScanStringer](s *schema.Schema, rows *sql.Rows, attr ...*FKAttrScanner) error

TypedSchemaFKs is a version of SchemaFKs that allows to specify the type of used to scan update and delete actions from the database.

func Unquote

func Unquote(s string) (string, error)

Unquote single or double quotes.

func V

func V[T any](p *T) (v T)

V returns the value p is pointing to. If p is nil, the zero value is returned.

func ValidString

func ValidString(s sql.NullString) bool

ValidString reports if the given string is not null and valid.

func ValuesEqual

func ValuesEqual(v1, v2 []string) bool

ValuesEqual checks if the 2 string slices are equal (including their order).

Types

type ApplyError

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

ApplyError is an error that exposes an information for getting how any changes were applied before encountering the failure.

func (*ApplyError) Applied

func (e *ApplyError) Applied() int

Applied reports how many changes were applied before getting an error. In case the first change was failed, Applied() returns 0.

func (*ApplyError) Error

func (e *ApplyError) Error() string

Error implements the error interface.

type Builder

type Builder struct {
	bytes.Buffer
	QuoteOpening byte    // quoting identifiers
	QuoteClosing byte    // quoting identifiers
	Schema       *string // schema qualifier
	Indent       string  // indentation string
	// contains filtered or unexported fields
}

A Builder provides a syntactic sugar API for writing SQL statements.

func (*Builder) Clone

func (b *Builder) Clone() *Builder

Clone returns a duplicate of the builder.

func (*Builder) Comma

func (b *Builder) Comma() *Builder

Comma writes a comma in case the buffer is not empty, or replaces the last char if it is a whitespace.

func (*Builder) Func

func (b *Builder) Func(f *schema.Func) *Builder

Func writes the function identifier to the builder, prefixed with the schema name if exists.

func (*Builder) FuncCall

func (b *Builder) FuncCall(f *schema.Func, args ...string) *Builder

FuncCall writes the function identifier to the builder as a function call,

func (*Builder) Ident

func (b *Builder) Ident(s string) *Builder

Ident writes the given string quoted as an SQL identifier.

func (*Builder) IndentIn

func (b *Builder) IndentIn() *Builder

IndentIn adds one indentation in.

func (*Builder) IndentOut

func (b *Builder) IndentOut() *Builder

IndentOut removed one indentation level.

func (*Builder) Int64

func (b *Builder) Int64(v int64) *Builder

Int64 writes the given value to the builder in base 10.

func (*Builder) MapComma

func (b *Builder) MapComma(x any, f func(i int, b *Builder)) *Builder

MapComma maps the slice x using the function f and joins the result with a comma separating between the written elements.

func (*Builder) MapCommaErr

func (b *Builder) MapCommaErr(x any, f func(i int, b *Builder) error) error

MapCommaErr is like MapComma, but returns an error if f returns an error.

func (*Builder) MapIndent

func (b *Builder) MapIndent(x any, f func(i int, b *Builder)) *Builder

MapIndent is like MapComma, but writes a new line before each element.

func (*Builder) MapIndentErr

func (b *Builder) MapIndentErr(x any, f func(i int, b *Builder) error) error

MapIndentErr is like MapCommaErr, but writes a new line before each element.

func (*Builder) NL

func (b *Builder) NL() *Builder

NL adds line break and prefix the new line with indentation in case indentation is enabled.

func (*Builder) P

func (b *Builder) P(phrases ...string) *Builder

P writes a list of phrases to the builder separated and suffixed with whitespace.

func (*Builder) Proc

func (b *Builder) Proc(p *schema.Proc) *Builder

Proc writes the procedure identifier to the builder, prefixed with the schema name if exists.

func (*Builder) ProcCall

func (b *Builder) ProcCall(p *schema.Proc, args ...string) *Builder

ProcCall writes the procedure identifier to the builder as a procedure call,

func (*Builder) Quote

func (b *Builder) Quote(prefix string, fn func(b *Builder)) *Builder

Quote wraps the given function with a single quote and a prefix

func (*Builder) RefTable

func (b *Builder) RefTable(childT, parentT *schema.Table) *Builder

RefTable writes the referenced/parent table identifier to the builder. Unlike the Table method, RefTable prefix the table with its schema qualifier if the "Schema" is set to nil, or if the schema is set to empty (schema-scope), and the parent table is in a different schema. For example:

CREATE TABLE child (
	id INT PRIMARY KEY,
	parent_id1 INT REFERENCES other1.parent(id)
	parent_id2 INT REFERENCES other2.parent(id)
);

This case is possible only if the child table (and its schema) was loaded with baseline schema (contains other1, other2) or with external references to resources not managed by this scope.

func (*Builder) SchemaResource

func (b *Builder) SchemaResource(s *schema.Schema, name string) *Builder

SchemaResource writes the schema resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) String

func (b *Builder) String() string

String overrides the Buffer.String method and ensure no spaces pad the returned statement.

func (*Builder) Table

func (b *Builder) Table(t *schema.Table) *Builder

Table writes the table identifier to the builder, prefixed with the schema name if exists.

func (*Builder) TableColumn

func (b *Builder) TableColumn(t *schema.Table, c *schema.Column) *Builder

TableColumn writes the table's resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) TableResource

func (b *Builder) TableResource(t *schema.Table, r any) *Builder

TableResource writes the table resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) View

func (b *Builder) View(v *schema.View) *Builder

View writes the view identifier to the builder, prefixed with the schema name if exists.

func (*Builder) ViewResource

func (b *Builder) ViewResource(v *schema.View, r any) *Builder

ViewResource writes the view resource identifier to the builder, prefixed with the schema name if exists.

func (*Builder) Wrap

func (b *Builder) Wrap(f func(b *Builder)) *Builder

Wrap wraps the written string with parentheses.

func (*Builder) WrapErr

func (b *Builder) WrapErr(f func(b *Builder) error) error

WrapErr wraps the written string with parentheses

func (*Builder) WrapIndent

func (b *Builder) WrapIndent(f func(b *Builder)) *Builder

WrapIndent is like Wrap but with extra level of indentation.

func (*Builder) WrapIndentErr

func (b *Builder) WrapIndentErr(f func(b *Builder) error) error

WrapIndentErr is like WrapErr but with extra level of indentation.

type ExecQueryCloser

type ExecQueryCloser interface {
	schema.ExecQuerier
	io.Closer
}

ExecQueryCloser is the interface that groups Close with the schema.ExecQuerier methods.

func SingleConn

func SingleConn(ctx context.Context, conn schema.ExecQuerier) (ExecQueryCloser, error)

SingleConn returns a closable single connection from the given ExecQuerier. If the ExecQuerier is already bound to a single connection (e.g. Tx, Conn), the connection will return as-is with a NopCloser.

type FKAttrScanner

type FKAttrScanner struct {
	Columns  func() []any
	ScanFunc func(*schema.ForeignKey, []any) error
}

FKAttrScanner allows extending foreign-keys scanning with extra attributes.

type ScanStringer

type ScanStringer interface {
	fmt.Stringer
	sql.Scanner
}

ScanStringer groups the fmt.Stringer and sql.Scanner interfaces.

Jump to

Keyboard shortcuts

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