spec

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ActionCreate      // ActionCreate represents a create action.
	ActionRead        // ActionRead represents a read action.
	ActionUpdate      // ActionUpdate represents an update action.
	ActionDelete      // ActionDelete represents a delete action.
	ActionTransaction // ActionTransaction represents a transaction action.
)
View Source
const (
	// TypeNullLongLong is a type extension for mysql.TypeLongLong.
	TypeNullLongLong byte = 0xf0
	// TypeNullDecimal is a type extension for mysql.TypeDecimal.
	TypeNullDecimal byte = 0xf1
	// TypeNullString is a type extension for mysql.TypeString.
	TypeNullString byte = 0xf2
)
View Source
const WildCard = "*"

WildCard is a wildcard column.

Variables

View Source
var OpName = []string{
	And:        "And",
	Between:    "Between",
	Case:       "Case",
	EQ:         "Equal",
	Or:         "Or",
	GE:         "GE",
	GT:         "GT",
	In:         "In",
	LE:         "LE",
	LT:         "LT",
	Like:       "Like",
	NE:         "NE",
	Not:        "Not",
	NotBetween: "NotBetween",
	NotIn:      "NotIn",
	NotLike:    "NotLike",
}
View Source
var Operator = []string{
	And:        "AND",
	Between:    "BETWEEN",
	Case:       "CASE",
	EQ:         "=",
	Or:         "OR",
	GE:         ">=",
	GT:         ">",
	In:         "IN",
	LE:         "<=",
	LT:         "<",
	Like:       "LIKE",
	NE:         "!=",
	Not:        "NOT",
	NotBetween: "NOT BETWEEN",
	NotIn:      "NOT IN",
	NotLike:    "NOT LIKE",
}

Functions

func NewParameter

func NewParameter(column string, tp string, thirdPkg string) parameter.Parameter

NewParameter returns a new parameter.

Types

type Action

type Action int

Action represents an action.

type ByItem

type ByItem struct {
	// Column represents the column name.
	Column string
	// Desc returns true if order by Column desc.
	Desc bool

	// the below data are from table
	// ColumnInfo are the column info which are convert from Column.
	ColumnInfo Column
	// TableInfo is the table info.
	TableInfo *Table

	// the below data are from stmt
	// Comment represents a sql comment.
	Comment Comment
}

ByItem represents an order-by or group-by item.

func (*ByItem) IsValid

func (b *ByItem) IsValid() bool

type ByItems

type ByItems []*ByItem

ByItems returns the by items.

func (ByItems) IsValid

func (b ByItems) IsValid() bool

func (ByItems) ParameterStructure

func (b ByItems) ParameterStructure(identifier string) (string, error)

ParameterStructure returns the parameter type structure.

func (ByItems) ParameterStructureName

func (b ByItems) ParameterStructureName(identifier string) string

ParameterStructureName returns the parameter structure name.

func (ByItems) ParameterThirdImports

func (b ByItems) ParameterThirdImports() (string, error)

ParameterThirdImports returns the third package imports.

func (ByItems) Parameters

func (b ByItems) Parameters(pkg string) (string, error)

Parameters returns the parameter variables.

func (ByItems) SQL

func (b ByItems) SQL() (string, error)

SQL returns the clause condition strings.

type Clause

type Clause struct {
	// Column represents the column name.
	Column string
	// Left represents the left expr.
	Left *Clause
	// Right represents the right expr.
	Right *Clause
	// OP represents the operator.
	OP OP

	// the below data are from table
	// ColumnInfo are the column info which are convert from Column.
	ColumnInfo Column
	// TableInfo is the table info.
	TableInfo *Table

	// the below data are from stmt
	// Comment represents a sql comment.
	Comment Comment
}

Clause represents a where clause, having clause.

func (*Clause) IsValid

func (c *Clause) IsValid() bool

IsValid returns true if the statement is valid.

func (*Clause) ParameterStructure

func (c *Clause) ParameterStructure(identifier string) (string, error)

ParameterStructure returns the parameter type structure.

func (*Clause) ParameterStructureName

func (c *Clause) ParameterStructureName(identifier string) string

ParameterStructureName returns the parameter structure name.

func (*Clause) ParameterThirdImports

func (c *Clause) ParameterThirdImports() (string, error)

ParameterThirdImports returns the third package imports.

func (*Clause) Parameters

func (c *Clause) Parameters(pkg string) (string, error)

Parameters returns the parameter variables.

func (*Clause) SQL

func (c *Clause) SQL() (string, error)

SQL returns the clause condition strings.

type Column

type Column struct {
	// ColumnOption is a column option.
	ColumnOption
	// Name is the name of the column.
	Name string
	// TP is the type of the column.
	TP            byte
	AggregateCall bool
}

Column represents a column in a table.

func (Column) DataType

func (c Column) DataType() (parameter.Parameter, error)

DataType returns the Go type, third-package of the column.

func (Column) GoType

func (c Column) GoType() (string, error)

GoType returns the Go type of the column.

func (Column) HasComment

func (c Column) HasComment() bool

type ColumnOption

type ColumnOption struct {
	// AutoIncrement is true if the column allows auto increment.
	AutoIncrement bool
	// Comment is the comment of the column.
	Comment string
	// HasDefault is true if the column has default value.
	HasDefaultValue bool
	// TODO: Add default value
	// NotNull is true if the column is not null, false represents the column is null.
	NotNull bool
	// Unsigned is true if the column is unsigned.
	Unsigned bool
}

ColumnOption is a column option.

type Columns

type Columns []Column

func (Columns) GetColumn

func (cs Columns) GetColumn(name string) (Column, bool)

func (Columns) Has

func (cs Columns) Has(name string) bool

Has returns true if Columns has specified column.

type Comment

type Comment struct {
	// OriginText represents the original sql text.
	OriginText string
	// LineText is the text of the line comment.
	LineText []string
	// FuncNames represents the generated function names.
	FuncName string
}

Comment represents a sql comment.

type Constraint

type Constraint struct {
	// Index is a list of column names that are part of an index, the key of map
	//	// is the key name, the values are the column list.
	Index map[string][]string
	// PrimaryKey is a list of column names that are part of the primary key, the key of map
	// is the key name, the values are the column list.
	PrimaryKey map[string][]string
	// UniqueKey is a list of column names that are part of a unique ke, the key of map
	//	// is the key name, the values are the column list.
	UniqueKey map[string][]string
}

Constraint is a struct that contains the constraints of a table. ConstraintForeignKey,ConstraintFulltext,ConstraintCheck are ignored.

func NewConstraint

func NewConstraint() *Constraint

NewConstraint returns a new Constraint.

func (*Constraint) AppendIndex

func (c *Constraint) AppendIndex(key string, columns ...string)

AppendIndex appends a column to the unique key.

func (*Constraint) AppendPrimaryKey

func (c *Constraint) AppendPrimaryKey(key string, columns ...string)

AppendPrimaryKey appends a column to the primary key.

func (*Constraint) AppendUniqueKey

func (c *Constraint) AppendUniqueKey(key string, columns ...string)

AppendUniqueKey appends a column to the unique key.

func (*Constraint) IsEmpty

func (c *Constraint) IsEmpty() bool

IsEmpty returns true if the constraint is empty.

func (*Constraint) Merge

func (c *Constraint) Merge(constraint *Constraint)

Merge merges the constraint with another constraint.

type Context

type Context struct {
	Table       *Table
	InsertStmt  []*InsertStmt
	SelectStmt  []*SelectStmt
	UpdateStmt  []*UpdateStmt
	DeleteStmt  []*DeleteStmt
	Transaction []*Transaction
}

Context is sql table and query context.

func From

func From(dxl *DXL) ([]Context, error)

From creates context from table and dml.

type DDL

type DDL struct {
	// Table represents a table in the database.
	Table *Table
}

DDL represents a DDL statement.

func (*DDL) IsEmpty

func (d *DDL) IsEmpty() bool

IsEmpty returns true if the DDL is empty.

type DML

type DML interface {
	// SQLText returns the SQL text of the DML statement.
	SQLText() string
	// TableName returns the table of the DML statement.
	TableName() string
	// contains filtered or unexported methods
}

DML represents a DML statement.

type DXL

type DXL struct {
	// DDL represents a DDL statement.
	DDL []*DDL
	// DML represents a DML statement.
	DML []DML
}

DXL describes ddl and dml.

func (*DXL) Validate

func (dxl *DXL) Validate() error

Validate validates the ddl and dml.

type DeleteStmt

type DeleteStmt struct {
	// Action represents the db action.
	Action Action
	// Comment represents a sql comment.
	Comment
	// From represents the operation table name, do not support multiple tables.
	From string
	// Limit represents the limit clause.
	Limit *Limit
	// OrderBy represents the order by clause.
	OrderBy ByItems
	// SQL represents the original sql text.
	SQL string
	// Where represents the where clause.
	Where *Clause

	// the below data are from table
	// FromInfo is the table info which is convert from From.
	FromInfo *Table
}

DeleteStmt represents a delete statement.

func (*DeleteStmt) HasArg

func (d *DeleteStmt) HasArg() bool

func (*DeleteStmt) SQLText

func (d *DeleteStmt) SQLText() string

func (*DeleteStmt) TableName

func (d *DeleteStmt) TableName() string

type Field

type Field struct {
	ASName        string
	ColumnName    string
	TP            byte
	AggregateCall bool
}

Field represents a select filed.

func (Field) Name

func (f Field) Name() string

type Fields

type Fields []Field

type InsertStmt

type InsertStmt struct {
	// Action represents the db action.
	Action Action
	// Columns represents the operation columns.
	Columns []string
	// Comment represents a sql comment.
	Comment
	// SQL represents the original sql text.
	SQL string
	// Table represents the operation table name, do not support multiple tables.
	Table string

	// the below data are from table
	// ColumnInfo are the column info which are convert from Columns.
	ColumnInfo Columns
	// TableInfo is the table info which is convert from Table.
	TableInfo *Table
}

InsertStmt represents a insert statement.

func (*InsertStmt) HasArg

func (i *InsertStmt) HasArg() bool

func (*InsertStmt) SQLText

func (i *InsertStmt) SQLText() string

func (*InsertStmt) TableName

func (i *InsertStmt) TableName() string

type Limit

type Limit struct {
	// Count represents the limit count.
	Count int64
	// Offset represents the limit offset.
	Offset int64

	// the below data are from table
	// TableInfo is the table info.
	TableInfo *Table

	// the below data are from stmt
	// Comment represents a sql comment.
	Comment Comment
}

Limit represents a limit clause.

func (*Limit) IsValid

func (l *Limit) IsValid() bool

func (*Limit) LimitParameter

func (l *Limit) LimitParameter(pkg string) string

LimitParameter returns the parameter variables.

func (*Limit) Multiple

func (l *Limit) Multiple() bool

func (*Limit) OffsetParameter

func (l *Limit) OffsetParameter(pkg string) string

OffsetParameter returns the parameter variables.

func (*Limit) One

func (l *Limit) One() bool

func (*Limit) ParameterStructure

func (l *Limit) ParameterStructure() (string, error)

ParameterStructure returns the parameter type structure.

func (*Limit) ParameterStructureName

func (l *Limit) ParameterStructureName() string

ParameterStructureName returns the parameter structure name.

func (*Limit) ParameterThirdImports

func (l *Limit) ParameterThirdImports() (string, error)

ParameterThirdImports returns the third package imports.

func (*Limit) Parameters

func (l *Limit) Parameters(pkg string) (string, error)

Parameters returns the parameter variables.

func (*Limit) SQL

func (l *Limit) SQL() (string, error)

SQL returns the clause condition strings.

type OP

type OP int

OP is opcode type.

const (
	ColumnValue OP
	And
	Between
	Case
	EQ
	Or
	GE
	GT
	In
	LE
	LT
	Like
	NE
	Not
	NotBetween
	NotIn
	NotLike
	Parentheses
)

type SelectStmt

type SelectStmt struct {
	// Action represents the db action.
	Action Action
	// SelectSQL represents the select filed sql.
	SelectSQL string
	// Columns represents the operation columns.
	Columns Fields
	// Comment represents a sql comment.
	Comment
	// Distinct represents the select distinct flag.
	Distinct bool
	// From represents the operation table name, do not support multiple tables.
	From string
	// GroupBy represents the group by clause.
	GroupBy ByItems
	// Having represents the having clause.
	Having *Clause
	// Limit represents the limit clause.
	Limit *Limit
	// OrderBy represents the order by clause.
	OrderBy ByItems
	// SQL represents the original sql text.
	SQL string
	// Where represents the where clause.
	Where *Clause

	// the below data are from table
	// ColumnInfo are the column info which are convert from Columns.
	ColumnInfo Columns
	// FromInfo is the table info which is convert from From.
	FromInfo *Table
}

SelectStmt represents a select statement.

func (*SelectStmt) ContainsExtraColumns

func (s *SelectStmt) ContainsExtraColumns() bool

ContainsExtraColumns returns true if the select statement contains extra columns.

func (*SelectStmt) HasArg

func (s *SelectStmt) HasArg() bool

func (*SelectStmt) ReceiverName

func (s *SelectStmt) ReceiverName() string

func (*SelectStmt) ReceiverStructure

func (s *SelectStmt) ReceiverStructure(orm string) string

func (*SelectStmt) SQLText

func (s *SelectStmt) SQLText() string

func (*SelectStmt) TableName

func (s *SelectStmt) TableName() string

type Table

type Table struct {
	// Columns is the list of columns in the table.
	Columns Columns
	// Constraint is a struct that contains the constraints of a table.
	// ConstraintForeignKey,ConstraintFulltext,ConstraintCheck are ignored.
	Constraint Constraint
	// Schema is the name of the schema that the table belongs to.
	Schema string
	// Name is the name of the table.
	Name string
}

Table represents a table in the database.

func (*Table) ColumnList

func (t *Table) ColumnList() []string

ColumnList is a list of column names.

func (*Table) GetColumnByName

func (t *Table) GetColumnByName(name string) (Column, bool)

GetColumnByName returns the column with the given name.

func (*Table) HasOnePrimaryKey

func (t *Table) HasOnePrimaryKey() bool

HasOnePrimaryKey returns true if the table has one primary key.

func (*Table) IsPrimary

func (t *Table) IsPrimary(name string) bool

IsPrimary returns true if the column is part of the primary key.

func (*Table) PrimaryColumn

func (t *Table) PrimaryColumn() Column

PrimaryColumn returns the primary column.

func (*Table) PrimaryColumnList

func (t *Table) PrimaryColumnList() Columns

PrimaryColumnList is a list of column names that are part of the primary key.

type Transaction

type Transaction struct {
	// Action represents the db action.
	Action Action
	// Comment represents a sql comment.
	Comment
	// SQL represents the original sql text.
	SQL string
	// Statements represents the list of statement.
	Statements []DML

	// the below fields is convert from Statements
	Context
}

func (Transaction) HasArg

func (t Transaction) HasArg() bool

func (Transaction) SQLText

func (t Transaction) SQLText() string

func (Transaction) TableName

func (t Transaction) TableName() string

type Type

type Type byte

Type is the type of the column.

type UpdateStmt

type UpdateStmt struct {
	// Action represents the db action.
	Action Action
	// Columns represents the operation columns.
	Columns []string
	// Comment represents a sql comment.
	Comment
	// Limit represents the limit clause.
	Limit *Limit
	// OrderBy represents the order by clause.
	OrderBy ByItems
	// SQL represents the original sql text.
	SQL string
	// Table represents the operation table name, do not support multiple tables.
	Table string
	// Where represents the where clause.
	Where *Clause

	// the below data are from table
	// ColumnInfo are the column info which are convert from Columns.
	ColumnInfo Columns
	// TableInfo is the table info which is convert from Table.
	TableInfo *Table
}

UpdateStmt represents a update statement.

func (*UpdateStmt) HasArg

func (u *UpdateStmt) HasArg() bool

func (*UpdateStmt) SQLText

func (u *UpdateStmt) SQLText() string

func (*UpdateStmt) TableName

func (u *UpdateStmt) TableName() string

Jump to

Keyboard shortcuts

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