dialect

package
v0.2.14 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AlwaysFalseReverse always think it's not a reverse word
	AlwaysNoReserve = func(string) bool { return false }

	// AlwaysReverse always reverse the word
	AlwaysReserve = func(string) bool { return true }

	// CommanQuoteMark represnets the common quote mark
	CommanQuoteMark byte = '`'

	// CommonQuoter represetns a common quoter
	CommonQuoter = Quoter{CommanQuoteMark, CommanQuoteMark, AlwaysReserve}
)

Functions

This section is empty.

Types

type Dialect

type Dialect interface {
	Init(*URI) error
	URI() *URI

	// 给字符串中的双引号转义
	QuoteExpr(str string) string
	// ToSQLType 转为 sql 类型
	ToSQLType(typ reflect.Type) *types.SQLType
	// DataTypeOf 转为 sql 类型
	DataTypeOf(val reflect.Value) *types.SQLType
	// SQLType 转换为 SQL 数据类型
	SQLType(col *schema.Column) string
	// AutoIncrStr 自增字段标志字符串
	AutoIncrStr() string

	Quoter() Quoter

	TableNameWithSchema(tableName string) string

	// 从数据库中查询所有表结构
	GetTables(ex exec.SQLExecutor, ctx context.Context) ([]*schema.Table, error)
	// SQLTableExists 判断表是否存在SQL
	SQLTableExists(tableName string) (sql string, args []interface{})
	// SQLCreateTable 生成创建表结构的 SQL
	SQLCreateTable(table *schema.Table, tableName string) ([]string, error)
	// SQLDropTable 生成删除表的 SQL
	SQLDropTable(tableName string) (string, error)

	GetColumns(ex exec.SQLExecutor, ctx context.Context, tableName string) ([]string, map[string]*schema.Column, error)
	// SQLColumn 生成列相关的 SQL 定义
	SQLColumn(col *schema.Column, inlinePrimaryKey bool) (string, error)
	SQLAddColumn(tableName string, col *schema.Column) string
	SQLModifyColumn(tableName string, col *schema.Column) string
	SQLModifyColumnLength(tableName string, col *schema.Column) string

	GetIndexes(ex exec.SQLExecutor, ctx context.Context, tableName string) (map[string]*schema.Index, error)
	SQLCreateIndex(tableName string, index *schema.Index) string
	SQLDropIndex(tableName string, index *schema.Index) string
}

type Driver

type Driver interface {
	Parse(constants.DIALECT, string) (*URI, error)
	GetDialect() Dialect
}

type QuotePolicy

type QuotePolicy int

QuotePolicy describes quote handle policy

const (
	QuotePolicyAlways QuotePolicy = iota
	QuotePolicyNone
	QuotePolicyReserved
)

All QuotePolicies

type Quoter

type Quoter struct {
	Prefix     byte
	Suffix     byte
	IsReserved func(string) bool
}

Quoter represents a quoter to the SQL table name and column name

func (Quoter) IsEmpty

func (q Quoter) IsEmpty() bool

func (Quoter) Join

func (q Quoter) Join(a []string, sep string) string

func (Quoter) JoinWrite

func (q Quoter) JoinWrite(b *strings.Builder, a []string, sep string) error

func (Quoter) Quote

func (q Quoter) Quote(s string) string

func (Quoter) QuoteTo

func (q Quoter) QuoteTo(buf *strings.Builder, value string) error

QuoteTo quotes the table or column names. i.e. if the quotes are [ and ]

name -> [name]
`name` -> [name]
[name] -> [name]
schema.name -> [schema].[name]
`schema`.`name` -> [schema].[name]
`schema`.name -> [schema].[name]
schema.`name` -> [schema].[name]
[schema].name -> [schema].[name]
schema.[name] -> [schema].[name]
name AS a  ->  [name] AS a
schema.name AS a  ->  [schema].[name] AS a

func (Quoter) Replace

func (q Quoter) Replace(sql string) string

Replace replaces common quote(`) as the quotes on the sql

func (Quoter) Strings

func (q Quoter) Strings(s []string) []string

Strings quotes a slice of string

func (Quoter) Trim

func (q Quoter) Trim(s string) string

Trim removes quotes from s

type Regitser

type Regitser func(dialect constants.DIALECT, drv Driver)

type URI

type URI struct {
	DRIVER  constants.DRIVER
	Proto   string
	Host    string
	Port    string
	DBName  string
	User    string
	Passwd  string
	Charset string
	Laddr   string
	Raddr   string
	Timeout time.Duration
	Schema  string
}

URI represents an uri to visit database

func (*URI) SetSchema

func (uri *URI) SetSchema(schema string)

SetSchema set schema

func (*URI) SupportColumnVarchar2Text

func (uri *URI) SupportColumnVarchar2Text() bool

SupportColumnVarchar2Text 是否支持把现有列的数据类型从 varchar 扩展为 text

func (*URI) SupportColumnVarcharIncLength

func (uri *URI) SupportColumnVarcharIncLength() bool

SupportColumnVarcharIncLength 是否支持把现有列的数据类型从 varchar 增加长度

Jump to

Keyboard shortcuts

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