context

package
v0.0.0-...-015b1eb Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2017 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultDBName = "justsql"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnMeta

type ColumnMeta struct {
	*model.ColumnInfo

	Table *TableMeta

	Name       string
	PascalName string

	Offset int

	// Column field type.
	Type *types.FieldType
}

ColumnMeta contains meta data of a column.

func NewColumnMeta

func NewColumnMeta(ctx *Context, tableMeta *TableMeta, columnInfo *model.ColumnInfo) (*ColumnMeta, error)

func (*ColumnMeta) DefaultValue

func (c *ColumnMeta) DefaultValue() interface{}

func (*ColumnMeta) Elems

func (c *ColumnMeta) Elems() []string

func (*ColumnMeta) IsAutoInc

func (c *ColumnMeta) IsAutoInc() bool

func (*ColumnMeta) IsEnum

func (c *ColumnMeta) IsEnum() bool

func (*ColumnMeta) IsNotNULL

func (c *ColumnMeta) IsNotNULL() bool

func (*ColumnMeta) IsOnUpdateNow

func (c *ColumnMeta) IsOnUpdateNow() bool

func (*ColumnMeta) IsSet

func (c *ColumnMeta) IsSet() bool

type Context

type Context struct {
	// The embeded db.
	DB *EmbedDB

	// Default database name in embeded db.
	DBName string

	// Database name -> cached DBMeta
	CachedDBMeta map[string]*DBMeta
}

Context contains an embeded database (tidb), extracted database meta information.

func NewContext

func NewContext(storePath, dbName string) (*Context, error)

NewContext create new Context.

func (*Context) ClearCachedDBMeta

func (ctx *Context) ClearCachedDBMeta()

ClearCachedDBMeta clears all cached DBMeta.

func (*Context) GetDBMeta

func (ctx *Context) GetDBMeta(dbName string) (*DBMeta, error)

GetDBMeta get DBMeta of the given db name or use cached one.

func (*Context) UniqueTableName

func (ctx *Context) UniqueTableName(dbName, tableName string) string

UniqueTableName == github.com/pingcap/tidb/plan/resolver.go nameResolver.tableUniqueName

type DBMeta

type DBMeta struct {
	*model.DBInfo

	Name       string
	PascalName string

	Tables map[string]*TableMeta
}

DBMeta contains meta information of a database.

func NewDBMeta

func NewDBMeta(ctx *Context, dbInfo *model.DBInfo) (*DBMeta, error)

type DeleteStmtMeta

type DeleteStmtMeta struct {
	*ast.DeleteStmt
}

DeleteStmtMeta contains meta information of a DELETE statement.

func NewDeleteStmtMeta

func NewDeleteStmtMeta(ctx *Context, stmt *ast.DeleteStmt) (*DeleteStmtMeta, error)

NewDeleteStmtMeta create DeleteStmtMeta from *ast.DeleteStmt.

type EmbedDB

type EmbedDB struct {
	Store kv.Storage
	Sess  tidb.Session
}

Use TiDB as an embeded database to execute or parse SQLs.

func NewEmbedDB

func NewEmbedDB(storePath string) (*EmbedDB, error)

func (*EmbedDB) Compile

func (db *EmbedDB) Compile(stmt ast.StmtNode) (ast.Statement, error)

Compile stmt.

func (*EmbedDB) Ctx

func (db *EmbedDB) Ctx() context.Context

DB context.

func (*EmbedDB) Domain

func (db *EmbedDB) Domain() *domain.Domain

Get Tidb domain (storage space).

func (*EmbedDB) Execute

func (db *EmbedDB) Execute(src string) ([]ast.RecordSet, error)

Execute some SQLs.

func (*EmbedDB) MustExecute

func (db *EmbedDB) MustExecute(src string)

Execute some SQLs and Panic if error.

func (*EmbedDB) Parse

func (db *EmbedDB) Parse(src string) ([]ast.StmtNode, error)

Parse SQLs.

type FKMeta

type FKMeta struct {
	*model.FKInfo

	Table *TableMeta

	Name       string
	PascalName string

	ColNames     []string
	RefTableName string
	RefColNames  []string
}

FKMeta contains meta data of a foreign key.

func NewFKMeta

func NewFKMeta(ctx *Context, tableMeta *TableMeta, fkInfo *model.FKInfo) (*FKMeta, error)

func (*FKMeta) Columns

func (f *FKMeta) Columns() []*ColumnMeta

func (*FKMeta) RefColumns

func (f *FKMeta) RefColumns() []*ColumnMeta

func (*FKMeta) RefIndex

func (f *FKMeta) RefIndex() *IndexMeta

RefIndex find the first index in ref table that this foreign key use.

func (*FKMeta) RefTable

func (f *FKMeta) RefTable() *TableMeta

type FieldListMeta

type FieldListMeta struct {
	// Wildcard information.
	Wildcards []WildcardMeta

	// Map index of result field -> index in wildcards or -1 if the result field
	// is not in a wildcard.
	ResultFieldToWildcard []int
}

FieldListMeta contain field list meta information in SELECT statement.

func NewFieldListMeta

func NewFieldListMeta(ctx *Context, stmt *ast.SelectStmt, tableRefsMeta *TableRefsMeta) (*FieldListMeta, error)

NewFieldListMeta create FieldListMeta from the field list and table refs of a SELECT statement.

func (*FieldListMeta) WildcardColumnOffset

func (f *FieldListMeta) WildcardColumnOffset(i int) int

WildcardColumnOffset return the offset inside wildcard of the i-th result field or -1 if the result field is not inside wildcard.

func (*FieldListMeta) WildcardTableRefName

func (f *FieldListMeta) WildcardTableRefName(i int) string

WildcardTableRefName return the wildcard table ref name of the i-th result field or "" if the result field is not inside wildcard.

type IndexMeta

type IndexMeta struct {
	// Nil if it is created from PKHandler.
	*model.IndexInfo

	Table *TableMeta

	Name       string
	PascalName string

	Unique        bool
	Primary       bool
	ColumnIndices []int
}

IndexMeta contains meta data of an index.

func NewIndexMeta

func NewIndexMeta(ctx *Context, tableMeta *TableMeta, indexInfo *model.IndexInfo) (*IndexMeta, error)

func NewIndexMetaFromPKHandler

func NewIndexMetaFromPKHandler(ctx *Context, tableMeta *TableMeta) *IndexMeta

see: https://github.com/pingcap/tidb/issues/3746

func (*IndexMeta) Columns

func (i *IndexMeta) Columns() []*ColumnMeta

type InsertStmtMeta

type InsertStmtMeta struct {
	*ast.InsertStmt
}

InsertStmtMeta contains meta information of a INSERT statement.

func NewInsertStmtMeta

func NewInsertStmtMeta(ctx *Context, stmt *ast.InsertStmt) (*InsertStmtMeta, error)

NewInsertStmtMeta create InsertStmtMeta from *ast.InsertStmt.

type ResultFieldMeta

type ResultFieldMeta struct {
	*ast.ResultField

	// Field name, maybe contain non-identifier chars: "now()"
	Name string

	// Field type.
	Type *types.FieldType
}

ResultFieldMeta contains meta information of a result field.

func NewResultFieldMeta

func NewResultFieldMeta(ctx *Context, rf *ast.ResultField) (*ResultFieldMeta, error)

func (*ResultFieldMeta) Elems

func (rf *ResultFieldMeta) Elems() []string

func (*ResultFieldMeta) IsAutoInc

func (rf *ResultFieldMeta) IsAutoInc() bool

func (*ResultFieldMeta) IsEnum

func (rf *ResultFieldMeta) IsEnum() bool

func (*ResultFieldMeta) IsNotNULL

func (rf *ResultFieldMeta) IsNotNULL() bool

func (*ResultFieldMeta) IsOnUpdateNow

func (rf *ResultFieldMeta) IsOnUpdateNow() bool

func (*ResultFieldMeta) IsSet

func (rf *ResultFieldMeta) IsSet() bool

type SelectStmtMeta

type SelectStmtMeta struct {
	*ast.SelectStmt

	ResultFields []*ResultFieldMeta
	TableRefs    *TableRefsMeta
	FieldList    *FieldListMeta
}

SelectStmtMeta contains meta information of a SELECT statement.

func NewSelectStmtMeta

func NewSelectStmtMeta(ctx *Context, stmt *ast.SelectStmt) (*SelectStmtMeta, error)

NewSelectStmtMeta create SelectStmtMeta from *ast.SelectStmt.

func (*SelectStmtMeta) ExpandWildcard

func (s *SelectStmtMeta) ExpandWildcard(ctx *Context) (*SelectStmtMeta, error)

This function expand all wildcards ("*") in a SELECT statement and return a new equivalent one. This is useful since "SELECT * ..." may lead to unpredictable error when table is altered.

type TableMeta

type TableMeta struct {
	*model.TableInfo

	DB *DBMeta

	Name       string
	PascalName string

	Columns     []*ColumnMeta
	Indices     []*IndexMeta
	ForeignKeys []*FKMeta
	// contains filtered or unexported fields
}

TableMeta contains meta information of a table.

func NewTableMeta

func NewTableMeta(ctx *Context, dbMeta *DBMeta, tableInfo *model.TableInfo) (*TableMeta, error)

func (*TableMeta) AutoIncColumn

func (t *TableMeta) AutoIncColumn() *ColumnMeta

Return auto increment column if exists.

func (*TableMeta) ColumnByName

func (t *TableMeta) ColumnByName(name string) *ColumnMeta

Retrive column by its name.

func (*TableMeta) NonPrimaryColumns

func (t *TableMeta) NonPrimaryColumns() []*ColumnMeta

Return non-primary columns if exists.

func (*TableMeta) PrimaryColumns

func (t *TableMeta) PrimaryColumns() []*ColumnMeta

Return primary key columns if exists.

type TableRefsMeta

type TableRefsMeta struct {
	// List of table sources and its reference name. Reference name can be the following:
	//   - tbl_name
	//   - other_database.tbl_name
	//   - alias
	TableSources  []*ast.TableSource
	TableRefNames []string

	// Not nil if TableSources[i] is normal table.
	TableMetas []*TableMeta

	// Map table ref name to its index.
	// Similar to github.com/tidb/plan/resolver.go:resolverContext,
	// but only use one map to enforce name uniqueness for both normal tables and derived tables.
	TableRefNameMap map[string]int
}

TableRefsMeta contains meta information of table references (e.g. 'FROM' part of SELECT statement).

CASE 1:

MySQL allows a normal table and a derived table sharing a same name:
   "SELECT a.* FROM t AS a JOIN (SELECT 1) AS a;"
 "a.*" expands to all columns of the two tables.
 Also see comments of github.com/pingcap/tidb/plan/resolver.go:handleTableSource

CASE 2:

"SELECT user.* FROM user, mysql.user" is also allowed, "user.*" also expands to all
columns of the two tables. This may lead to confusion.

Due to the reasons above, JustSQL enforce table referernce names' uniqueness, even in different database ("mysql.user" and "user"). Thus the above SQLs should modified to something like:

"SELECT a1.*, a2.* FROM t AS a1 JOIN (SELECT 1) AS a2;
"SELECT u1.*, u2.* FROM user u1, mysql.user u2"

func NewTableRefsMeta

func NewTableRefsMeta(ctx *Context, refs *ast.TableRefsClause) (*TableRefsMeta, error)

func (*TableRefsMeta) GetResultFields

func (t *TableRefsMeta) GetResultFields(tableRefName string) []*ast.ResultField

GetResultFields return a list of result field of the table or nil if not exists.

func (*TableRefsMeta) TableMeta

func (t *TableRefsMeta) TableMeta(tableRefName string) *TableMeta

TableMeta return *TableMeta if tableRefName references a normal table or nil if tableRefName references a derived table.

type UpdateStmtMeta

type UpdateStmtMeta struct {
	*ast.UpdateStmt
}

UpdateStmtMeta contains meta information of a UPDATE statement.

func NewUpdateStmtMeta

func NewUpdateStmtMeta(ctx *Context, stmt *ast.UpdateStmt) (*UpdateStmtMeta, error)

NewUpdateStmtMeta create UpdateStmtMeta from *ast.UpdateStmt.

type WildcardMeta

type WildcardMeta struct {
	// Table ref name of wildcard.
	TableRefName string

	// Portion of the wildcard in result fields.
	ResultFieldOffset int
	ResultFieldNum    int
}

WildcardMeta contain information for a wildcard. NOTE: For unqualified wildcard "*", serveral WildcardMeta will be generated.

Jump to

Keyboard shortcuts

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