statement

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2025 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package statement is a wrapper around the parser with some added functionality.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotSupportedStatement   = errors.New("not a supported statement type")
	ErrNotAlterTable           = errors.New("not an ALTER TABLE statement")
	ErrMultipleSchemas         = errors.New("statement attempts to modify tables across multiple schemas")
	ErrNoStatements            = errors.New("could not find any compatible statements to execute")
	ErrMixMatchMultiStatements = errors.New("when performing atomic schema changes, all statements must be of type ALTER TABLE")
	ErrUnsafeForInplace        = errors.New("statement contains operations that are not safe for INPLACE algorithm")
	ErrMultipleAlterClauses    = errors.New("ALTER contains multiple clauses. Combinations of INSTANT and INPLACE operations cannot be detected safely. Consider executing these as separate ALTER statements")
	ErrAlterContainsUnique     = errors.New("ALTER contains adding a unique index")
)

Functions

func ByName

func ByName[T HasName](slice []T, name string) *T

ByName is a generic function that finds an element by name in any slice of types with Name field NOTE: This function assumes that names are unique within the slice! That will be true for "canonical" CREATE TABLE statements as returned by SHOW CREATE TABLE, but may not be true for arbitrary input.

Types

type AbstractStatement

type AbstractStatement struct {
	Schema    string // this will be empty unless the table name is fully qualified (ALTER TABLE test.t1 ...)
	Table     string // for statements that affect multiple tables (DROP TABLE t1, t2), only the first is set here!
	Alter     string // may be empty.
	Statement string
	StmtNode  *ast.StmtNode
}

func MustNew

func MustNew(statement string) []*AbstractStatement

MustNew is like New but panics if the statement cannot be parsed. It is used by tests.

func New

func New(statement string) ([]*AbstractStatement, error)

func (*AbstractStatement) AlgorithmInplaceConsideredSafe

func (a *AbstractStatement) AlgorithmInplaceConsideredSafe() error

AlgorithmInplaceConsideredSafe checks to see if all clauses of an ALTER statement are "safe". We consider an operation to be "safe" if it is "In Place" and "Only Modifies Metadata". See https://dev.mysql.com/doc/refman/8.0/en/innodb-online-ddl-operations.html for details. INPLACE DDL is not generally safe for online use in MySQL 8.0, because ADD INDEX can block replicas.

func (*AbstractStatement) AlterContainsAddUnique

func (a *AbstractStatement) AlterContainsAddUnique() error

AlterContainsAddUnique checks to see if any clauses of an ALTER contains add UNIQUE index. We use this to customize the error returned from checksum fails.

func (*AbstractStatement) AlterContainsUnsupportedClause

func (a *AbstractStatement) AlterContainsUnsupportedClause() error

AlterContainsUnsupportedClause checks to see if any clauses of an ALTER statement are unsupported by Spirit. These include clauses like ALGORITHM and LOCK, because they step on the toes of Spirit's own locking and algorithm selection.

func (*AbstractStatement) AsAlterTable

func (a *AbstractStatement) AsAlterTable() (*ast.AlterTableStmt, bool)

AsAlterTable is a helper function that simply wraps the type case so the caller doesn't have to import the ast package and use the cast syntax

func (*AbstractStatement) IsAlterTable

func (a *AbstractStatement) IsAlterTable() bool

func (*AbstractStatement) IsCreateTable

func (a *AbstractStatement) IsCreateTable() bool

func (*AbstractStatement) ParseCreateTable

func (a *AbstractStatement) ParseCreateTable() (*CreateTable, error)

func (*AbstractStatement) TrimAlter

func (a *AbstractStatement) TrimAlter() string

type Column

type Column struct {
	Raw        *ast.ColumnDef    `json:"-"`
	Name       string            `json:"name"`
	Type       string            `json:"type"`
	Length     *int              `json:"length,omitempty"`
	Precision  *int              `json:"precision,omitempty"`
	Scale      *int              `json:"scale,omitempty"`
	Unsigned   *bool             `json:"unsigned,omitempty"`
	EnumValues []string          `json:"enum_values,omitempty"` // Permitted values for ENUM type
	SetValues  []string          `json:"set_values,omitempty"`  // Permitted values for SET type
	Nullable   bool              `json:"nullable"`
	Default    *string           `json:"default,omitempty"`
	AutoInc    bool              `json:"auto_increment"`
	PrimaryKey bool              `json:"primary_key"`
	Unique     bool              `json:"unique"`
	Comment    *string           `json:"comment,omitempty"`
	Charset    *string           `json:"charset,omitempty"`
	Collation  *string           `json:"collation,omitempty"`
	Options    map[string]string `json:"options,omitempty"`
}

Column represents a table column definition

func (Column) GetName

func (c Column) GetName() string

type Columns

type Columns []Column

func (Columns) ByName

func (columns Columns) ByName(name string) *Column

type Constraint

type Constraint struct {
	Raw        *ast.Constraint      `json:"-"`
	Name       string               `json:"name"`
	Type       string               `json:"type"` // CHECK, FOREIGN KEY, etc.
	Columns    []string             `json:"columns,omitempty"`
	Expression *string              `json:"expression,omitempty"`
	References *ForeignKeyReference `json:"references,omitempty"`
	Definition *string              `json:"definition,omitempty"` // Generated definition string for compatibility
	Options    map[string]any       `json:"options,omitempty"`
}

Constraint represents a table constraint

func (Constraint) GetName

func (c Constraint) GetName() string

type Constraints

type Constraints []Constraint

func (Constraints) ByName

func (constraints Constraints) ByName(name string) *Constraint

func (Constraints) HasForeignKeys

func (constraints Constraints) HasForeignKeys() bool

type CreateTable

type CreateTable struct {
	Raw          *ast.CreateTableStmt `json:"-"`
	TableName    string               `json:"table_name"`
	Temporary    bool                 `json:"temporary"`
	IfNotExists  bool                 `json:"if_not_exists"`
	Columns      Columns              `json:"columns"`
	Indexes      Indexes              `json:"indexes"`
	Constraints  Constraints          `json:"constraints"`
	TableOptions *TableOptions        `json:"table_options,omitempty"`
	Partition    *PartitionOptions    `json:"partition,omitempty"`
}

CreateTable represents a parsed CREATE TABLE statement with structured data

func ParseCreateTable

func ParseCreateTable(sql string) (*CreateTable, error)

ParseCreateTable parses a CREATE TABLE statement and returns an analyzer This function is particularly designed to be used with the output of SHOW CREATE TABLE, which we consider to be the "canonical" form of a CREATE TABLE statement.

Because there's so much variation in the ways a human might write a CREATE TABLE statement, from index names being auto-generated to column attributes being turned into table options, you should consider use of this function on non-canonical CREATE statements to be experimental at best.

Note also that this parser does not attempt to validate the SQL beyond what the underlying parser does. For example, it will not check that a PRIMARY KEY column is NOT NULL, or that column names are unique, or that indexed columns exist.

func (*CreateTable) GetColumns

func (ct *CreateTable) GetColumns() Columns

func (*CreateTable) GetConstraints

func (ct *CreateTable) GetConstraints() Constraints

func (*CreateTable) GetCreateTable

func (ct *CreateTable) GetCreateTable() *CreateTable

func (*CreateTable) GetIndexes

func (ct *CreateTable) GetIndexes() Indexes

func (*CreateTable) GetPartition

func (ct *CreateTable) GetPartition() *PartitionOptions

func (*CreateTable) GetTableName

func (ct *CreateTable) GetTableName() string

func (*CreateTable) GetTableOptions

func (ct *CreateTable) GetTableOptions() map[string]any

type ForeignKeyReference

type ForeignKeyReference struct {
	Table   string   `json:"table"`
	Columns []string `json:"columns"`
}

ForeignKeyReference represents a foreign key reference

type HasName

type HasName interface {
	GetName() string
}

HasName is a type constraint for types that have a Name field

type Index

type Index struct {
	Raw          *ast.Constraint   `json:"-"`
	Name         string            `json:"name"`
	Type         string            `json:"type"` // PRIMARY, UNIQUE, INDEX, FULLTEXT, SPATIAL
	Columns      []string          `json:"columns"`
	Invisible    *bool             `json:"invisible,omitempty"`
	Using        *string           `json:"using,omitempty"` // BTREE, HASH, RTREE
	Comment      *string           `json:"comment,omitempty"`
	KeyBlockSize *uint64           `json:"key_block_size,omitempty"`
	ParserName   *string           `json:"parser_name,omitempty"`
	Options      map[string]string `json:"options,omitempty"`
}

Index represents an index definition

func (Index) GetName

func (i Index) GetName() string

type Indexes

type Indexes []Index

func (Indexes) ByName

func (indexes Indexes) ByName(name string) *Index

func (Indexes) HasInvisible

func (indexes Indexes) HasInvisible() bool

type PartitionDefinition

type PartitionDefinition struct {
	Name          string                   `json:"name"`
	Values        *PartitionValues         `json:"values,omitempty"` // VALUES LESS THAN or VALUES IN
	Comment       *string                  `json:"comment,omitempty"`
	Engine        *string                  `json:"engine,omitempty"`
	Options       map[string]any           `json:"options,omitempty"`
	SubPartitions []SubPartitionDefinition `json:"subpartitions,omitempty"`
}

PartitionDefinition represents a single partition definition

type PartitionOptions

type PartitionOptions struct {
	Type         string                `json:"type"`                   // RANGE, LIST, HASH, KEY, SYSTEM_TIME
	Expression   *string               `json:"expression,omitempty"`   // For HASH and RANGE
	Columns      []string              `json:"columns,omitempty"`      // For KEY, RANGE COLUMNS, LIST COLUMNS
	Linear       bool                  `json:"linear,omitempty"`       // For LINEAR HASH/KEY
	Partitions   uint64                `json:"partitions,omitempty"`   // Number of partitions
	Definitions  []PartitionDefinition `json:"definitions,omitempty"`  // Individual partition definitions
	SubPartition *SubPartitionOptions  `json:"subpartition,omitempty"` // Subpartitioning options
}

PartitionOptions represents table partitioning configuration

type PartitionValues

type PartitionValues struct {
	Type   string `json:"type"`   // "LESS_THAN", "IN", "MAXVALUE"
	Values []any  `json:"values"` // The actual values
}

PartitionValues represents the VALUES clause in partition definitions

type SubPartitionDefinition

type SubPartitionDefinition struct {
	Name    string         `json:"name"`
	Comment *string        `json:"comment,omitempty"`
	Engine  *string        `json:"engine,omitempty"`
	Options map[string]any `json:"options,omitempty"`
}

SubPartitionDefinition represents a single subpartition definition

type SubPartitionOptions

type SubPartitionOptions struct {
	Type       string   `json:"type"`                 // HASH, KEY
	Expression *string  `json:"expression,omitempty"` // For HASH
	Columns    []string `json:"columns,omitempty"`    // For KEY
	Linear     bool     `json:"linear,omitempty"`     // For LINEAR HASH/KEY
	Count      uint64   `json:"count,omitempty"`      // Number of subpartitions
}

SubPartitionOptions represents subpartitioning configuration

type TableOptions

type TableOptions struct {
	Engine        *string `json:"engine,omitempty"`
	Charset       *string `json:"charset,omitempty"`
	Collation     *string `json:"collation,omitempty"`
	Comment       *string `json:"comment,omitempty"`
	AutoIncrement *uint64 `json:"auto_increment,omitempty"`
	RowFormat     *string `json:"row_format,omitempty"`
}

TableOptions represents table-level options

Jump to

Keyboard shortcuts

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