Skip to content

Commit

Permalink
Register dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Feb 14, 2016
1 parent 421979c commit f4456e1
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 19 deletions.
27 changes: 12 additions & 15 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ type Dialect interface {
LastInsertIdReturningSuffix(tableName, columnName string) string
}

func NewDialect(driver string) Dialect {
var d Dialect
switch driver {
case "postgres":
d = &postgres{}
case "mysql":
d = &mysql{}
case "sqlite3":
d = &sqlite3{}
case "mssql":
d = &mssql{}
default:
fmt.Printf("`%v` is not officially supported, running under compatibility mode.\n", driver)
d = &commonDialect{}
var dialectsMap = map[string]Dialect{}

func newDialect(name string) Dialect {
if dialect, ok := dialectsMap[name]; ok {
return dialect
}
return d
fmt.Printf("`%v` is not officially supported, running under compatibility mode.\n", name)
return &commonDialect{}
}

// RegisterDialect register new dialect
func RegisterDialect(name string, dialect Dialect) {
dialectsMap[name] = dialect
}

// ParseFieldStructForDialect parse field struct for dialect
Expand Down
4 changes: 4 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (

type commonDialect struct{}

func init() {
RegisterDialect("common", &commonDialect{})
}

func (commonDialect) BindVar(i int) string {
return "$$" // ?
}
Expand Down
4 changes: 4 additions & 0 deletions dialect_mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type mssql struct {
commonDialect
}

func init() {
RegisterDialect("mssql", &mssql{})
}

func (mssql) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)

Expand Down
4 changes: 4 additions & 0 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ type mysql struct {
commonDialect
}

func init() {
RegisterDialect("mysql", &mysql{})
}

func (mysql) Quote(key string) string {
return fmt.Sprintf("`%s`", key)
}
Expand Down
4 changes: 4 additions & 0 deletions dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type postgres struct {
commonDialect
}

func init() {
RegisterDialect("postgres", &postgres{})
}

func (postgres) BindVar(i int) string {
return fmt.Sprintf("$%v", i)
}
Expand Down
5 changes: 5 additions & 0 deletions dialect_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ type sqlite3 struct {
commonDialect
}

func init() {
RegisterDialect("sqlite", &sqlite3{})
RegisterDialect("sqlite3", &sqlite3{})
}

// Get Data Type for Sqlite Dialect
func (sqlite3) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
Expand Down
5 changes: 1 addition & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ func Open(dialect string, args ...interface{}) (*DB, error) {
driver = value
source = args[1].(string)
}
if driver == "foundation" {
driver = "postgres" // FoundationDB speaks a postgres-compatible protocol.
}
dbSql, err = sql.Open(driver, source)
case sqlCommon:
source = reflect.Indirect(reflect.ValueOf(value)).FieldByName("dsn").String()
dbSql = value
}

db = DB{
dialect: NewDialect(dialect),
dialect: newDialect(dialect),
logger: defaultLogger,
callbacks: defaultCallback,
source: source,
Expand Down

0 comments on commit f4456e1

Please sign in to comment.