From a46d149579abf7dbd3c25724485fd3ec5cad392a Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Fri, 25 Apr 2014 07:20:23 +0800 Subject: [PATCH] remove package dialect for easier contribution --- dialect/dialect.go => dialect.go | 4 ++-- main.go | 6 ++---- dialect/mysql.go => mysql.go | 17 +++++++++++++---- dialect/postgres.go => postgres.go | 6 +++--- scope.go | 3 +-- dialect/sqlite3.go => sqlite3.go | 2 +- 6 files changed, 22 insertions(+), 16 deletions(-) rename dialect/dialect.go => dialect.go (89%) rename dialect/mysql.go => mysql.go (85%) rename dialect/postgres.go => postgres.go (92%) rename dialect/sqlite3.go => sqlite3.go (98%) diff --git a/dialect/dialect.go b/dialect.go similarity index 89% rename from dialect/dialect.go rename to dialect.go index a0f403c4..acec284a 100644 --- a/dialect/dialect.go +++ b/dialect.go @@ -1,4 +1,4 @@ -package dialect +package gorm import ( "reflect" @@ -16,7 +16,7 @@ type Dialect interface { Quote(key string) string } -func New(driver string) Dialect { +func NewDialect(driver string) Dialect { var d Dialect switch driver { case "postgres": diff --git a/main.go b/main.go index 8fca8612..0980c13b 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,6 @@ package gorm import ( "database/sql" - - "github.com/jinzhu/gorm/dialect" ) type DB struct { @@ -15,14 +13,14 @@ type DB struct { search *search logMode int logger logger - dialect dialect.Dialect + dialect Dialect tagIdentifier string singularTable bool } func Open(driver, source string) (DB, error) { var err error - db := DB{dialect: dialect.New(driver), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback} + db := DB{dialect: NewDialect(driver), tagIdentifier: "sql", logger: defaultLogger, callback: DefaultCallback} db.db, err = sql.Open(driver, source) db.parent = &db return db, err diff --git a/dialect/mysql.go b/mysql.go similarity index 85% rename from dialect/mysql.go rename to mysql.go index 9602bcc1..6a5ed6ef 100644 --- a/dialect/mysql.go +++ b/mysql.go @@ -1,7 +1,8 @@ -package dialect +package gorm import ( "fmt" + "reflect" ) @@ -59,10 +60,18 @@ func (s *mysql) PrimaryKeyTag(value reflect.Value, size int) string { } } -func (s *mysql) ReturningStr(key string) (str string) { - return +func (s *mysql) ReturningStr(key string) string { + return "" } -func (s *mysql) Quote(key string) (str string) { +func (s *mysql) Quote(key string) string { return fmt.Sprintf("`%s`", key) } + +func (s *mysql) HasTable(tableName string) bool { + return true +} + +func (s *mysql) HasColumn(tableName string, columnName string) bool { + return true +} diff --git a/dialect/postgres.go b/postgres.go similarity index 92% rename from dialect/postgres.go rename to postgres.go index 7b744fa4..7c5c61e4 100644 --- a/dialect/postgres.go +++ b/postgres.go @@ -1,4 +1,4 @@ -package dialect +package gorm import ( "fmt" @@ -54,10 +54,10 @@ func (s *postgres) PrimaryKeyTag(value reflect.Value, size int) string { } } -func (s *postgres) ReturningStr(key string) (str string) { +func (s *postgres) ReturningStr(key string) string { return fmt.Sprintf("RETURNING \"%v\"", key) } -func (s *postgres) Quote(key string) (str string) { +func (s *postgres) Quote(key string) string { return fmt.Sprintf("\"%s\"", key) } diff --git a/scope.go b/scope.go index 543c4cde..2a567058 100644 --- a/scope.go +++ b/scope.go @@ -3,7 +3,6 @@ package gorm import ( "errors" "fmt" - "github.com/jinzhu/gorm/dialect" "go/ast" "strings" "time" @@ -55,7 +54,7 @@ func (scope *Scope) Quote(str string) string { } // Dialect get dialect -func (scope *Scope) Dialect() dialect.Dialect { +func (scope *Scope) Dialect() Dialect { return scope.db.parent.dialect } diff --git a/dialect/sqlite3.go b/sqlite3.go similarity index 98% rename from dialect/sqlite3.go rename to sqlite3.go index ae54e603..cbeac9a3 100644 --- a/dialect/sqlite3.go +++ b/sqlite3.go @@ -1,4 +1,4 @@ -package dialect +package gorm import ( "fmt"