sqlx

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommentChange

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

CommentChange reports if the element comment was changed.

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 Has

func Has(attrs []schema.Attr, target interface{}) bool

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

func LinkSchemaTables

func LinkSchemaTables(schemas []*schema.Schema)

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

func ScanFKs

func ScanFKs(t *schema.Table, rows *sql.Rows) error

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

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

func VersionPermutations

func VersionPermutations(dialect, version string) []string

VersionPermutations returns permutations of the dialect version sorted from coarse to fine grained. For example:

VersionPermutations("mysql", "1.2.3") => ["mysql", "mysql 1", "mysql 1.2", "mysql 1.2.3"]

VersionPermutations will split the version number by ".", " ", "-" or "_", and rejoin them with ".". The output slice can be used by drivers to generate a list of permutations for searching for relevant overrides in schema element specs.

Types

type Builder

type Builder struct {
	bytes.Buffer
	QuoteChar byte
}

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

func (*Builder) Comma

func (b *Builder) Comma() *Builder

Comma writes a comma. If the current buffer ends with whitespace, it will be replaced instead.

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 interface{}, 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) 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 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) 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

	// ColumnChange returns the schema changes (if any) for migrating one column to the other.
	ColumnChange(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.Attr) 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 Normalizer

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

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