xorm/dialect.go

168 lines
3.3 KiB
Go
Raw Normal View History

2014-04-08 12:57:04 +00:00
package core
import (
2014-04-11 07:38:09 +00:00
"fmt"
2014-04-08 12:57:04 +00:00
"strings"
"time"
)
2014-04-11 13:05:08 +00:00
type DbType string
2014-04-08 12:57:04 +00:00
type Uri struct {
2014-04-11 13:05:08 +00:00
DbType DbType
2014-04-08 12:57:04 +00:00
Proto string
Host string
Port string
DbName string
User string
Passwd string
Charset string
Laddr string
Raddr string
Timeout time.Duration
}
// a dialect is a driver's wrapper
type Dialect interface {
Init(*Uri, string, string) error
URI() *Uri
2014-04-11 13:05:08 +00:00
DBType() DbType
2014-04-08 12:57:04 +00:00
SqlType(t *Column) string
SupportInsertMany() bool
QuoteStr() string
2014-04-13 07:37:25 +00:00
AndStr() string
EqStr() string
2014-04-11 07:38:09 +00:00
RollBackStr() string
DropTableSql(tableName string) string
2014-04-08 12:57:04 +00:00
AutoIncrStr() string
SupportEngine() bool
SupportCharset() bool
IndexOnTable() bool
2014-04-13 07:37:25 +00:00
ShowCreateNull() bool
2014-04-08 12:57:04 +00:00
IndexCheckSql(tableName, idxName string) (string, []interface{})
TableCheckSql(tableName string) (string, []interface{})
ColumnCheckSql(tableName, colName string) (string, []interface{})
GetColumns(tableName string) ([]string, map[string]*Column, error)
GetTables() ([]*Table, error)
GetIndexes(tableName string) (map[string]*Index, error)
CreateTableSql(table *Table, tableName, storeEngine, charset string) string
Filters() []Filter
DriverName() string
DataSourceName() string
}
type Base struct {
dialect Dialect
driverName string
dataSourceName string
*Uri
}
func (b *Base) Init(dialect Dialect, uri *Uri, drivername, dataSourceName string) error {
b.dialect = dialect
b.driverName, b.dataSourceName = drivername, dataSourceName
b.Uri = uri
return nil
}
func (b *Base) URI() *Uri {
return b.Uri
}
2014-04-11 13:05:08 +00:00
func (b *Base) DBType() DbType {
2014-04-08 12:57:04 +00:00
return b.Uri.DbType
}
func (b *Base) DriverName() string {
return b.driverName
}
2014-04-13 07:37:25 +00:00
func (b *Base) ShowCreateNull() bool {
return true
}
2014-04-08 12:57:04 +00:00
func (b *Base) DataSourceName() string {
return b.dataSourceName
}
func (b *Base) Quote(c string) string {
return b.dialect.QuoteStr() + c + b.dialect.QuoteStr()
}
2014-04-13 07:37:25 +00:00
func (b *Base) AndStr() string {
return "AND"
}
func (b *Base) EqStr() string {
return "="
}
2014-04-11 07:38:09 +00:00
func (db *Base) RollBackStr() string {
return "ROLL BACK"
}
func (db *Base) DropTableSql(tableName string) string {
return fmt.Sprintf("DROP TABLE IF EXISTS `%s`", tableName)
}
2014-04-08 12:57:04 +00:00
func (b *Base) CreateTableSql(table *Table, tableName, storeEngine, charset string) string {
var sql string
sql = "CREATE TABLE IF NOT EXISTS "
if tableName == "" {
tableName = table.Name
}
sql += b.Quote(tableName) + " ("
pkList := table.PrimaryKeys
for _, colName := range table.ColumnsSeq() {
col := table.GetColumn(colName)
if col.IsPrimaryKey && len(pkList) == 1 {
sql += col.String(b.dialect)
} else {
sql += col.StringNoPk(b.dialect)
}
sql = strings.TrimSpace(sql)
sql += ", "
}
if len(pkList) > 1 {
sql += "PRIMARY KEY ( "
sql += b.Quote(strings.Join(pkList, b.Quote(",")))
sql += " ), "
}
sql = sql[:len(sql)-2] + ")"
if b.dialect.SupportEngine() && storeEngine != "" {
sql += " ENGINE=" + storeEngine
}
if b.dialect.SupportCharset() {
if charset == "" {
charset = b.dialect.URI().Charset
}
sql += " DEFAULT CHARSET " + charset
}
sql += ";"
return sql
}
var (
2014-04-11 13:05:08 +00:00
dialects = map[DbType]Dialect{}
2014-04-08 12:57:04 +00:00
)
2014-04-11 13:05:08 +00:00
func RegisterDialect(dbName DbType, dialect Dialect) {
2014-04-08 12:57:04 +00:00
if dialect == nil {
panic("core: Register dialect is nil")
}
2014-04-11 13:05:08 +00:00
dialects[dbName] = dialect // !nashtsai! allow override dialect
2014-04-08 12:57:04 +00:00
}
2014-04-11 13:05:08 +00:00
func QueryDialect(dbName DbType) Dialect {
2014-04-08 12:57:04 +00:00
return dialects[dbName]
}