mirror of https://github.com/go-gorm/gorm.git
Fix tests with go v1.3
This commit is contained in:
parent
1c1df2318c
commit
325e6c2ef1
9
scope.go
9
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])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue