Rename the parameter to table_options and avoid introduction of new API function OpenWithTableSuffix

This commit is contained in:
Gabriel 2015-08-01 22:46:38 +00:00
parent 260000d00f
commit eef40a06ff
3 changed files with 10 additions and 15 deletions

View File

@ -113,7 +113,6 @@ 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
@ -137,12 +136,14 @@ db.SingularTable(true)
```go
// Create table
db.CreateTable(&User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").CreateTable(&User{})
// Drop table
db.DropTable(&User{})
// Automating Migration
db.AutoMigrate(&User{})
db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
db.AutoMigrate(&User{}, &Product{}, &Order{})
// Feel free to change your struct, AutoMigrate will keep your database up-to-date.
// AutoMigrate will ONLY add *new columns* and *new indexes*,

10
main.go
View File

@ -32,16 +32,11 @@ 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
@ -74,7 +69,6 @@ func OpenWithTableSuffix(dialect, tableSuffix string, args ...interface{}) (DB,
logger: defaultLogger,
callback: DefaultCallback,
source: source,
tableSuffix:tableSuffix,
values: map[string]interface{}{},
db: dbSql,
}
@ -376,7 +370,7 @@ func (s *DB) RecordNotFound() bool {
// Migrations
func (s *DB) CreateTable(value interface{}) *DB {
return s.clone().NewScope(value).Set("gorm:table_suffix", s.tableSuffix).createTable().db
return s.clone().NewScope(value).createTable().db
}
func (s *DB) DropTable(value interface{}) *DB {
@ -396,7 +390,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().Set("gorm:table_suffix", s.tableSuffix).autoMigrate().db
db = db.NewScope(value).NeedPtr().autoMigrate().db
}
return db
}

View File

@ -443,14 +443,14 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
}
/**
Return the table suffix string or an empty string if the table suffix does not exist
Return the table options string or an empty string if the table options does not exist
*/
func (scope *Scope) getTableSuffix() string{
tableSuffix, ok := scope.Get("gorm:table_suffix")
func (scope *Scope) getTableOptions() string{
tableOptions, ok := scope.Get("gorm:table_options")
if !ok {
return ""
}
return tableSuffix.(string)
return tableOptions.(string)
}
func (scope *Scope) createJoinTable(field *StructField) {
@ -469,7 +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) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableSuffix())).Error)
scope.Err(scope.NewDB().Exec(fmt.Sprintf("CREATE TABLE %v (%v) %s", scope.Quote(joinTable), strings.Join(sqlTypes, ","), scope.getTableOptions())).Error)
}
scope.NewDB().Table(joinTable).AutoMigrate(joinTableHandler)
}
@ -494,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) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableSuffix())).Exec()
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v %v) %s", scope.QuotedTableName(), strings.Join(tags, ","), primaryKeyStr, scope.getTableOptions())).Exec()
return scope
}