ddl

package
v0.0.0-...-7687a33 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PRIMARY_KEY = "PRIMARY"
)

Variables

This section is empty.

Functions

func CreateModel

func CreateModel(ctx context.Context, dd DataDefiner, m *dal.Model) (err error)

CreateModel creates table with columns and indexes that match Model definition

func DefaultBoolean

func DefaultBoolean(set, value bool) string

func DefaultID

func DefaultID(set bool, value uint64) string

func DefaultJSON

func DefaultJSON(set bool, value any) (_ string, err error)

func DefaultNumber

func DefaultNumber(set bool, precision int, value float64) string

func DefaultValueCurrentTimestamp

func DefaultValueCurrentTimestamp(set bool) string

func DeleteModel

func DeleteModel(ctx context.Context, dd DataDefiner, m *dal.Model) (err error)

func EnsureIndexes

func EnsureIndexes(ctx context.Context, dd DataDefiner, ii ...*dal.Index) (err error)

func Exec

func Exec(ctx context.Context, db sqlx.ExtContext, ss ...any) (err error)

Exec is a utility for executing series of commands

Parameters can be string, Stringer interface or goqu's exp.SQLExpression

Any other type will result in panic

func GenCreateTableBody

func GenCreateTableBody(d dialect, t *Table) string

func GenPrimaryKey

func GenPrimaryKey(d dialect, pk *Index) string

func GenTableColumn

func GenTableColumn(d dialect, col *Column) string

func GetBool

func GetBool(ctx context.Context, db sqlx.QueryerContext, query exp.SQLExpression) (bool, error)

GetBool is a utility function to simplify getting a boolean value from a query result.

func ParseColumnTypes

func ParseColumnTypes(c *Column) (original, name string, meta []string)

func SQLExpression

func SQLExpression(e exp.LiteralExpression) exp.SQLExpression

func Structs

func Structs(ctx context.Context, db sqlx.QueryerContext, query exp.SQLExpression, t any) error

Structs is a utility function to simplify selecting data into slice of structs

func UpdateModel

func UpdateModel(ctx context.Context, dd DataDefiner, m *dal.Model) (err error)

UpdateModel alters existing table's columns and indexes to match Model definition

Types

type AddColumn

type AddColumn struct {
	Dialect dialect
	Table   string
	Column  *Column
}

func (*AddColumn) ToSQL

func (c *AddColumn) ToSQL() (sql string, aa []interface{}, err error)

type Column

type Column struct {
	Ident string
	Type  *ColumnType

	Default string

	// implementation variations
	Meta map[string]interface{}

	Comment string
}

func ConvertAttribute

func ConvertAttribute(attr *dal.Attribute, d driverDialect) (col *Column, err error)

type ColumnType

type ColumnType struct {
	Name string

	Null bool

	// implementation variations
	Meta map[string]interface{}
}

type CreateIndex

type CreateIndex struct {
	Dialect               dialect
	Index                 *Index
	OmitIfNotExistsClause bool
	OmitFieldLength       bool
}

func (*CreateIndex) String

func (t *CreateIndex) String() string

type CreateTable

type CreateTable struct {
	Dialect               dialect
	Table                 *Table
	OmitIfNotExistsClause bool
	SuffixClause          string
}

func (*CreateTable) String

func (t *CreateTable) String() string

type DataDefiner

type DataDefiner interface {
	ConvertModel(*dal.Model) (*Table, error)
	ConvertAttribute(attr *dal.Attribute) (*Column, error)

	// Tables(ctx context.Context) ([]*Table, error)
	TableLookup(context.Context, string) (*Table, error)
	TableCreate(context.Context, *Table) error
	TableDrop(context.Context, string) error

	ColumnAdd(context.Context, string, *Column) error
	ColumnDrop(context.Context, string, string) error
	ColumnRename(context.Context, string, string, string) error
	ColumnReType(context.Context, string, string, *ColumnType) error

	IndexLookup(context.Context, string, string) (*Index, error)
	IndexCreate(context.Context, string, *Index) error
	IndexDrop(context.Context, string, string) error
}

DataDefiner describes an interface for all DDL commands

type DropColumn

type DropColumn struct {
	Dialect dialect
	Table   string
	Column  string
}

func (*DropColumn) ToSQL

func (c *DropColumn) ToSQL() (sql string, aa []interface{}, err error)

type DropIndex

type DropIndex struct {
	Dialect    dialect
	Ident      string
	TableIdent string
}

func (*DropIndex) ToSQL

func (c *DropIndex) ToSQL() (sql string, aa []interface{}, err error)

type DropTable

type DropTable struct {
	Dialect dialect
	Table   string
}

func (*DropTable) ToSQL

func (c *DropTable) ToSQL() (sql string, aa []interface{}, err error)

type Index

type Index struct {
	TableIdent string
	Ident      string
	Type       string
	Fields     []*IndexField
	Unique     bool
	Predicate  string
	Comment    string

	// implementation variations
	Meta map[string]interface{}
}

Index describes structure of the SQL index

func ConvertIndex

func ConvertIndex(i *dal.Index, aa dal.AttributeSet, table string, d driverDialect) (idx *Index, err error)

ConvertIndex converts dal.Index to ddl.Index

type IndexField

type IndexField struct {
	// Expression or a single column
	Column string

	Length int

	// Wrap part in parentheses
	Expression string

	// Ascending or descending
	Sort  dal.IndexFieldSort
	Nulls dal.IndexFieldNulls

	Statistics *IndexFieldStatistics

	// implementation variations
	Meta map[string]interface{}
}

IndexField describes a single field (column or expression) of the SQL index

type IndexFieldStatistics

type IndexFieldStatistics struct {
	// Cardinality is an indicator that refers to the uniqueness
	// of all values in a column. Low cardinality means a lot
	// of duplicate values in that column. For example, a column
	// that stores the gender values has low cardinality.
	// In contrast, high cardinality means that there are many distinct values.
	Cardinality int64
}

type ReTypeColumn

type ReTypeColumn struct {
	Dialect dialect
	Table   string
	Column  string
	Type    *ColumnType
}

func (*ReTypeColumn) ToSQL

func (c *ReTypeColumn) ToSQL() (sql string, aa []interface{}, err error)

type RenameColumn

type RenameColumn struct {
	Dialect dialect
	Table   string
	Old     string
	New     string
}

func (*RenameColumn) ToSQL

func (c *RenameColumn) ToSQL() (sql string, aa []interface{}, err error)

type Table

type Table struct {
	Ident   string
	Columns []*Column
	Indexes []*Index
	Comment string

	Temporary bool

	// implementation variations
	Meta map[string]interface{}
}

Table describes structure of the SQL table

func ConvertModel

func ConvertModel(m *dal.Model, d driverDialect) (t *Table, err error)

ConvertModel is generic model converter

func (*Table) ColumnByIdent

func (t *Table) ColumnByIdent(i string) *Column

Jump to

Keyboard shortcuts

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