mirror of https://github.com/go-gorm/gorm.git
Add method HasTable, HasColumn to dialect
This commit is contained in:
parent
288c8e37af
commit
66e6c9af9f
|
@ -14,6 +14,8 @@ type Dialect interface {
|
||||||
PrimaryKeyTag(value reflect.Value, size int) string
|
PrimaryKeyTag(value reflect.Value, size int) string
|
||||||
ReturningStr(key string) string
|
ReturningStr(key string) string
|
||||||
Quote(key string) string
|
Quote(key string) string
|
||||||
|
HasTable(scope *Scope, tableName string) bool
|
||||||
|
HasColumn(scope *Scope, tableName string, columnName string) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDialect(driver string) Dialect {
|
func NewDialect(driver string) Dialect {
|
||||||
|
|
19
mysql.go
19
mysql.go
|
@ -68,10 +68,21 @@ func (s *mysql) Quote(key string) string {
|
||||||
return fmt.Sprintf("`%s`", key)
|
return fmt.Sprintf("`%s`", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *mysql) HasTable(tableName string) bool {
|
func (s *mysql) HasTable(scope *Scope, tableName string) bool {
|
||||||
return true
|
var count int
|
||||||
|
newScope := scope.New(nil)
|
||||||
|
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM INFORMATION_SCHEMA.tables where table_name = %v", newScope.AddToVars(tableName)))
|
||||||
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *mysql) HasColumn(tableName string, columnName string) bool {
|
func (s *mysql) HasColumn(scope *Scope, tableName string, columnName string) bool {
|
||||||
return true
|
var count int
|
||||||
|
newScope := scope.New(nil)
|
||||||
|
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM information_schema.columns WHERE table_name = %v AND column_name = %v",
|
||||||
|
newScope.AddToVars(tableName),
|
||||||
|
newScope.AddToVars(columnName),
|
||||||
|
))
|
||||||
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
|
return count > 0
|
||||||
}
|
}
|
||||||
|
|
19
postgres.go
19
postgres.go
|
@ -61,3 +61,22 @@ func (s *postgres) ReturningStr(key string) string {
|
||||||
func (s *postgres) Quote(key string) string {
|
func (s *postgres) Quote(key string) string {
|
||||||
return fmt.Sprintf("\"%s\"", key)
|
return fmt.Sprintf("\"%s\"", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *postgres) HasTable(scope *Scope, tableName string) bool {
|
||||||
|
var count int
|
||||||
|
newScope := scope.New(nil)
|
||||||
|
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM INFORMATION_SCHEMA.tables where table_name = %v", newScope.AddToVars(tableName)))
|
||||||
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *postgres) HasColumn(scope *Scope, tableName string, columnName string) bool {
|
||||||
|
var count int
|
||||||
|
newScope := scope.New(nil)
|
||||||
|
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM information_schema.columns WHERE table_name = %v AND column_name = %v",
|
||||||
|
newScope.AddToVars(tableName),
|
||||||
|
newScope.AddToVars(columnName),
|
||||||
|
))
|
||||||
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
|
|
@ -447,28 +447,14 @@ func (scope *Scope) removeIndex(indexName string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) autoMigrate() *Scope {
|
func (scope *Scope) autoMigrate() *Scope {
|
||||||
var tableName string
|
if !scope.Dialect().HasTable(scope, scope.TableName()) {
|
||||||
scope.Raw(fmt.Sprintf("SELECT table_name FROM INFORMATION_SCHEMA.tables where table_name = %v",
|
|
||||||
scope.AddToVars(scope.TableName())))
|
|
||||||
scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&tableName)
|
|
||||||
scope.SqlVars = []interface{}{}
|
|
||||||
|
|
||||||
// If table doesn't exist
|
|
||||||
if len(tableName) == 0 {
|
|
||||||
scope.createTable()
|
scope.createTable()
|
||||||
} else {
|
} else {
|
||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
var column, data string
|
if !scope.Dialect().HasColumn(scope, scope.TableName(), field.DBName) {
|
||||||
scope.Raw(fmt.Sprintf("SELECT column_name, data_type FROM information_schema.columns WHERE table_name = %v AND column_name = %v",
|
if len(field.SqlTag) > 0 && !field.IsIgnored {
|
||||||
scope.AddToVars(scope.TableName()),
|
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", scope.TableName(), field.DBName, field.SqlTag)).Exec()
|
||||||
scope.AddToVars(field.DBName),
|
}
|
||||||
))
|
|
||||||
scope.DB().QueryRow(scope.Sql, scope.SqlVars...).Scan(&column, &data)
|
|
||||||
scope.SqlVars = []interface{}{}
|
|
||||||
|
|
||||||
// If column doesn't exist
|
|
||||||
if len(column) == 0 && len(field.SqlTag) > 0 && !field.IsIgnored {
|
|
||||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", scope.TableName(), field.DBName, field.SqlTag)).Exec()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
25
sqlite3.go
25
sqlite3.go
|
@ -52,10 +52,29 @@ func (s *sqlite3) PrimaryKeyTag(value reflect.Value, size int) string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sqlite3) ReturningStr(key string) (str string) {
|
func (s *sqlite3) ReturningStr(key string) string {
|
||||||
return
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *sqlite3) Quote(key string) (str string) {
|
func (s *sqlite3) Quote(key string) string {
|
||||||
return fmt.Sprintf("\"%s\"", key)
|
return fmt.Sprintf("\"%s\"", key)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *sqlite3) HasTable(scope *Scope, tableName string) bool {
|
||||||
|
var count int
|
||||||
|
newScope := scope.New(nil)
|
||||||
|
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM INFORMATION_SCHEMA.tables where table_name = %v", newScope.AddToVars(tableName)))
|
||||||
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) bool {
|
||||||
|
var count int
|
||||||
|
newScope := scope.New(nil)
|
||||||
|
newScope.Raw(fmt.Sprintf("SELECT count(*) FROM information_schema.columns WHERE table_name = %v AND column_name = %v",
|
||||||
|
newScope.AddToVars(tableName),
|
||||||
|
newScope.AddToVars(columnName),
|
||||||
|
))
|
||||||
|
newScope.DB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||||
|
return count > 0
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue