From 325e6c2ef19c89363353dbff54e47f1cb691c24d Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Wed, 2 Jul 2014 11:56:37 +0800 Subject: [PATCH] Fix tests with go v1.3 --- scope.go | 9 +++++---- scope_private.go | 9 ++++++--- sqlite3.go | 9 ++++----- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/scope.go b/scope.go index 77268a35..8571fef5 100644 --- a/scope.go +++ b/scope.go @@ -176,6 +176,9 @@ func (scope *Scope) AddToVars(value interface{}) string { } // TableName get table name +var pluralMapKeys = []*regexp.Regexp{regexp.MustCompile("ch$"), regexp.MustCompile("ss$"), regexp.MustCompile("sh$"), regexp.MustCompile("day$"), regexp.MustCompile("y$"), regexp.MustCompile("x$"), regexp.MustCompile("([^s])s?$")} +var pluralMapValues = []string{"ches", "sses", "shes", "days", "ies", "xes", "${1}s"} + func (scope *Scope) TableName() string { if scope.Search != nil && len(scope.Search.TableName) > 0 { return scope.Search.TableName @@ -201,11 +204,9 @@ func (scope *Scope) TableName() string { str := toSnake(data.Type().Name()) if !scope.db.parent.singularTable { - pluralMap := map[string]string{"ch": "ches", "ss": "sses", "sh": "shes", "day": "days", "y": "ies", "x": "xes", "s?": "s"} - for key, value := range pluralMap { - reg := regexp.MustCompile(key + "$") + for index, reg := range pluralMapKeys { if reg.MatchString(str) { - return reg.ReplaceAllString(str, value) + return reg.ReplaceAllString(str, pluralMapValues[index]) } } } diff --git a/scope_private.go b/scope_private.go index 4db4ebbe..6a46eb80 100644 --- a/scope_private.go +++ b/scope_private.go @@ -450,13 +450,16 @@ func (scope *Scope) removeIndex(indexName string) { } func (scope *Scope) autoMigrate() *Scope { - if !scope.Dialect().HasTable(scope, scope.TableName()) { + tableName := scope.TableName() + quotedTableName := scope.QuotedTableName() + + if !scope.Dialect().HasTable(scope, tableName) { scope.createTable() } else { for _, field := range scope.Fields() { - if !scope.Dialect().HasColumn(scope, scope.TableName(), field.DBName) { + if !scope.Dialect().HasColumn(scope, tableName, field.DBName) { if len(field.SqlTag) > 0 && !field.IsIgnored { - scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", scope.QuotedTableName(), field.DBName, field.SqlTag)).Exec() + scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", quotedTableName, field.DBName, field.SqlTag)).Exec() } } } diff --git a/sqlite3.go b/sqlite3.go index 400d811e..b454930b 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -62,13 +62,12 @@ func (s *sqlite3) Quote(key string) string { func (s *sqlite3) HasTable(scope *Scope, tableName string) bool { var count int - newScope := scope.New(nil) - newScope.Raw(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=%v", newScope.AddToVars(tableName))) - newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count) + scope.DB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='%v';", tableName)).Scan(&count) return count > 0 } func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) bool { - _, err := scope.DB().Exec(fmt.Sprintf("SELECT %v FROM %v LIMIT 1", columnName, tableName)) - return err == nil + var count int + scope.DB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = '%v' AND (sql LIKE '%%(\"%v\" %%' OR sql LIKE '%%,\"%v\" %%');\n", tableName, columnName, columnName)).Scan(&count) + return count > 0 }