sqlx

package
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2022 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyChanges added in v0.2.0

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 CheckChangesScope added in v0.6.0

func CheckChangesScope(changes []schema.Change) error

CheckChangesScope checks that changes can be applied on a schema scope (connection).

func CheckDiff added in v0.2.0

func CheckDiff(from, to *schema.Table, compare ...func(c1, c2 *schema.Check) bool) []schema.Change

CheckDiff computes the change diff between the 2 tables. A compare function is provided to check if a Check object was modified.

func CommentChange

func CommentChange(from, to []schema.Attr) schema.ChangeKind

CommentChange reports if the element comment was changed.

func CommentDiff added in v0.3.0

func CommentDiff(from, to []schema.Attr) schema.Change

CommentDiff computes the comment diff between the 2 attribute list. Note that, the implementation relies on the fact that both PostgreSQL and MySQL treat empty comment as "no comment" and a way to clear comments.

func DefaultValue added in v0.2.0

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

DefaultValue returns the string represents the DEFAULT of a column.

func DetachCycles

func DetachCycles(changes []schema.Change) ([]schema.Change, error)

DetachCycles takes a list of schema changes, and detaches references between changes if there is at least one circular reference in the changeset. More explicitly, it postpones fks creation, or deletes fks before deletes their tables.

func ExcludeRealm added in v0.6.0

func ExcludeRealm(r *schema.Realm, patterns []string) (*schema.Realm, error)

ExcludeRealm filters resources in the realm based on the given patterns.

func ExcludeSchema added in v0.6.0

func ExcludeSchema(s *schema.Schema, patterns []string) (*schema.Schema, error)

ExcludeSchema filters resources in the schema based on the given patterns.

func ExprLastIndex added in v0.3.8

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 added in v0.2.0

func IsLiteralBool(s string) bool

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

func IsLiteralNumber added in v0.2.0

func IsLiteralNumber(s string) bool

IsLiteralNumber reports if the given string is a literal number.

func IsQuoted added in v0.2.0

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

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

func LinkSchemaTables

func LinkSchemaTables(schemas []*schema.Schema)

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

func MayWrap added in v0.3.7

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 added in v0.3.8

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

ModeInspectRealm returns the InspectMode or its default.

func ModeInspectSchema added in v0.3.8

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

ModeInspectSchema returns the InspectMode or its default.

func P added in v0.6.4

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

P returns a pointer to v.

func ReverseChanges added in v0.4.1

func ReverseChanges(c []schema.Change)

ReverseChanges reverses the order of the changes.

func ScanNullBool added in v0.3.8

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

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

func ScanOne added in v0.3.1

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

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

func ScanStrings

func ScanStrings(rows *sql.Rows) ([]string, error)

ScanStrings scans sql.Rows into a slice of strings and closes it at the end.

func SchemaFKs added in v0.3.7

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 SingleQuote added in v0.3.0

func SingleQuote(s string) (string, error)

SingleQuote quotes the given string with single quote.

func Unquote added in v0.3.0

func Unquote(s string) (string, error)

Unquote single or double quotes.

func V added in v0.6.4

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 Builder

type Builder struct {
	bytes.Buffer
	QuoteChar byte    // quoting identifiers
	Schema    *string // schema qualifier
}

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

func (*Builder) Clone added in v0.2.0

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) Ident

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

Ident writes the given string quoted as an SQL identifier.

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 added in v0.2.0

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) P

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

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

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) Wrap

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

Wrap wraps the written string with parentheses.

type DevDriver added in v0.3.7

type DevDriver struct {
	// A Driver connected to the dev database.
	migrate.Driver

	// MaxNameLen configures the max length of object names in
	// the connected database (e.g. 64 in MySQL). Longer names
	// are trimmed and suffixed with their hash.
	MaxNameLen int

	// DropClause holds optional clauses that
	// can be added to the DropSchema change.
	DropClause []schema.Clause

	// PatchColumn allows providing a custom function to patch
	// columns that hold a schema reference.
	PatchColumn func(*schema.Schema, *schema.Column)
}

DevDriver is a driver that provides additional functionality to interact with the development database.

func (*DevDriver) NormalizeRealm added in v0.3.7

func (d *DevDriver) NormalizeRealm(ctx context.Context, r *schema.Realm) (nr *schema.Realm, err error)

NormalizeRealm implements the schema.Normalizer interface.

The implementation converts schema objects in "natural form" (e.g. HCL or DSL) to their "normal presentation" in the database, by creating them temporarily in a "dev database", and then inspects them from there.

func (*DevDriver) NormalizeSchema added in v0.3.7

func (d *DevDriver) NormalizeSchema(ctx context.Context, s *schema.Schema) (*schema.Schema, error)

NormalizeSchema returns the normal representation of the given database. See NormalizeRealm for more info.

type Diff

type Diff struct {
	DiffDriver
}

A Diff provides a generic schema.Differ for diffing schema elements.

The DiffDriver is required for supporting database/dialect specific diff capabilities, like diffing custom types or attributes.

func (*Diff) RealmDiff added in v0.2.0

func (d *Diff) RealmDiff(from, to *schema.Realm) ([]schema.Change, error)

RealmDiff implements the schema.Differ for Realm objects and returns a list of changes that need to be applied in order to move a database from the current state to the desired.

func (*Diff) SchemaDiff

func (d *Diff) SchemaDiff(from, to *schema.Schema) ([]schema.Change, error)

SchemaDiff implements the schema.Differ interface and returns a list of changes that need to be applied in order to move from one state to the other.

func (*Diff) TableDiff

func (d *Diff) TableDiff(from, to *schema.Table) ([]schema.Change, error)

TableDiff implements the schema.TableDiffer interface and returns a list of changes that need to be applied in order to move from one state to the other.

type DiffDriver

type DiffDriver interface {
	// SchemaAttrDiff returns a changeset for migrating schema attributes
	// from one state to the other. For example, changing schema collation.
	SchemaAttrDiff(from, to *schema.Schema) []schema.Change

	// TableAttrDiff returns a changeset for migrating table attributes from
	// one state to the other. For example, dropping or adding a `CHECK` constraint.
	TableAttrDiff(from, to *schema.Table) ([]schema.Change, error)

	// ColumnChange returns the schema changes (if any) for migrating one column to the other.
	ColumnChange(fromT *schema.Table, from, to *schema.Column) (schema.ChangeKind, error)

	// IndexAttrChanged reports if the index attributes were changed.
	// For example, an index type or predicate (for partial indexes).
	IndexAttrChanged(from, to []schema.Attr) bool

	// IndexPartAttrChanged reports if the index-part attributes were
	// changed. For example, an index-part collation.
	IndexPartAttrChanged(from, to *schema.IndexPart) bool

	// IsGeneratedIndexName reports if the index name was generated by the database
	// for unnamed INDEX or UNIQUE constraints. In such cases, the differ will look
	// for unnamed schema.Indexes on the desired state, before tagging the index as
	// a candidate for deletion.
	IsGeneratedIndexName(*schema.Table, *schema.Index) bool

	// ReferenceChanged reports if the foreign key referential action was
	// changed. For example, action was changed from RESTRICT to CASCADE.
	ReferenceChanged(from, to schema.ReferenceOption) bool
}

A DiffDriver wraps all required methods for diffing elements that may have database-specific diff logic. See sql/schema/mysql/diff.go for an implementation example.

type ExecQueryCloser added in v0.3.8

type ExecQueryCloser interface {
	schema.ExecQuerier
	io.Closer
}

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

func SingleConn added in v0.3.8

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 Normalizer

type Normalizer interface {
	Normalize(from, to *schema.Table) error
}

A Normalizer wraps the Normalize method for normalizing the from and to tables before running diffing. The "from" usually represents the inspected database state (current), and the second represents the desired state.

If the DiffDriver implements the Normalizer interface, TableDiff normalizes its table inputs before starting the diff process.

type UnsupportedTypeError

type UnsupportedTypeError struct {
	schema.Type
}

UnsupportedTypeError describes an unsupported type error.

func (UnsupportedTypeError) Error

func (e UnsupportedTypeError) Error() string

Jump to

Keyboard shortcuts

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