From 8848fc476dc93dfc4db337744462a1675f993f7f Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sun, 19 Jul 2015 22:37:08 +0000 Subject: [PATCH] Table suffix to create tables with InnoDB engine with mysql. Alter table is not affected yet, only create table and auto migration --- README.md | 1 + main.go | 10 ++++++++-- scope_private.go | 16 +++++++++++++--- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index ccab06db..5919b188 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,7 @@ import ( db, err := gorm.Open("postgres", "user=gorm dbname=gorm sslmode=disable") // db, err := gorm.Open("foundation", "dbname=gorm") // FoundationDB. // db, err := gorm.Open("mysql", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local") +// db, err := gorm.OpenWithTableSuffix("mysql", "ENGINE=InnoDB", "user:password@/dbname?charset=utf8&parseTime=True&loc=Local") // db, err := gorm.Open("sqlite3", "/tmp/gorm.db") // You can also use an existing database connection handle diff --git a/main.go b/main.go index aba51fc4..b2c625f5 100644 --- a/main.go +++ b/main.go @@ -32,11 +32,16 @@ type DB struct { dialect Dialect singularTable bool source string + tableSuffix string values map[string]interface{} joinTableHandlers map[string]JoinTableHandler } func Open(dialect string, args ...interface{}) (DB, error) { + return OpenWithTableSuffix(dialect, "", args) +} + +func OpenWithTableSuffix(dialect, tableSuffix string, args ...interface{}) (DB, error) { var db DB var err error @@ -69,6 +74,7 @@ func Open(dialect string, args ...interface{}) (DB, error) { logger: defaultLogger, callback: DefaultCallback, source: source, + tableSuffix:tableSuffix, values: map[string]interface{}{}, db: dbSql, } @@ -370,7 +376,7 @@ func (s *DB) RecordNotFound() bool { // Migrations func (s *DB) CreateTable(value interface{}) *DB { - return s.clone().NewScope(value).createTable().db + return s.clone().NewScope(value).Set("gorm:table_suffix", s.tableSuffix).createTable().db } func (s *DB) DropTable(value interface{}) *DB { @@ -390,7 +396,7 @@ func (s *DB) HasTable(value interface{}) bool { func (s *DB) AutoMigrate(values ...interface{}) *DB { db := s.clone() for _, value := range values { - db = db.NewScope(value).NeedPtr().autoMigrate().db + db = db.NewScope(value).NeedPtr().Set("gorm:table_suffix", s.tableSuffix).autoMigrate().db } return db } diff --git a/scope_private.go b/scope_private.go index 85f07e99..2ab340b2 100644 --- a/scope_private.go +++ b/scope_private.go @@ -442,6 +442,17 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope { return scope } +/** + Return the table suffix string or an empty string if the table suffix does not exist +*/ +func (scope *Scope) getTableSuffix() string{ + tableSuffix, ok := scope.Get("gorm:table_suffix") + if !ok { + return "" + } + return tableSuffix.(string) +} + func (scope *Scope) createJoinTable(field *StructField) { if relationship := field.Relationship; relationship != nil && relationship.JoinTableHandler != nil { joinTableHandler := relationship.JoinTableHandler @@ -458,8 +469,7 @@ func (scope *Scope) createJoinTable(field *StructField) { sqlTypes = append(sqlTypes, scope.Quote(dbName)+" "+primaryKeySqlType) } } - - scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v)", scope.Quote(joinTable), strings.Join(sqlTypes, ","))).Error) + scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableSuffix())).Error) } scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler) } @@ -484,7 +494,7 @@ func (scope *Scope) createTable() *Scope { if len(primaryKeys) > 0 { primaryKeyStr = fmt.Sprintf(", PRIMARY KEY (%v)", strings.Join(primaryKeys, ",")) } - scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v)", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr)).Exec() + scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableSuffix())).Exec() return scope }