mirror of https://github.com/go-gorm/gorm.git
Refactor check HasTable, HasColumn
This commit is contained in:
parent
da8fc53c86
commit
4e8d43dd4f
|
@ -86,26 +86,16 @@ func (s *commonDialect) databaseName(scope *Scope) string {
|
|||
|
||||
func (s *commonDialect) 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 AND table_schema = %v",
|
||||
newScope.AddToVars(tableName),
|
||||
newScope.AddToVars(s.databaseName(scope))))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_schema = ?", tableName, s.databaseName(scope)).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *commonDialect) 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_schema = %v AND table_name = %v AND column_name = %v",
|
||||
newScope.AddToVars(s.databaseName(scope)),
|
||||
newScope.AddToVars(tableName),
|
||||
newScope.AddToVars(columnName),
|
||||
))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?", s.databaseName(scope), tableName, columnName).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *commonDialect) RemoveIndex(scope *Scope, indexName string) {
|
||||
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
|
||||
scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName()))
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func init() {
|
|||
DB, err = gorm.Open("mysql", "gorm:gorm@/gorm?charset=utf8&parseTime=True")
|
||||
case "postgres":
|
||||
fmt.Println("testing postgres...")
|
||||
DB, err = gorm.Open("postgres", "user=gorm DB.ame=gorm sslmode=disable")
|
||||
DB, err = gorm.Open("postgres", "user=gorm DB.name=gorm sslmode=disable")
|
||||
case "mssql":
|
||||
fmt.Println("testing mssql...")
|
||||
DB, err = gorm.Open("mssql", "server=SERVER_HERE;database=rogue;user id=USER_HERE;password=PW_HERE;port=1433")
|
||||
|
|
|
@ -7,7 +7,7 @@ import (
|
|||
)
|
||||
|
||||
func runMigration() {
|
||||
if err := DB.DropTable(&User{}).Error; err != nil {
|
||||
if err := DB.DropTableIfExists(&User{}).Error; err != nil {
|
||||
fmt.Printf("Got error when try to delete table users, %+v\n", err)
|
||||
}
|
||||
|
||||
|
|
16
mssql.go
16
mssql.go
|
@ -88,26 +88,16 @@ func (s *mssql) databaseName(scope *Scope) string {
|
|||
|
||||
func (s *mssql) 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 AND table_catalog = %v",
|
||||
newScope.AddToVars(tableName),
|
||||
newScope.AddToVars(s.databaseName(scope))))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_catalog = ?", tableName, s.databaseName(scope)).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *mssql) 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_CATALOG = %v AND table_name = %v AND column_name = %v",
|
||||
newScope.AddToVars(s.databaseName(scope)),
|
||||
newScope.AddToVars(tableName),
|
||||
newScope.AddToVars(columnName),
|
||||
))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM information_schema.columns WHERE table_catalog = ? AND table_name = ? AND column_name = ?", s.databaseName(scope), tableName, columnName).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *mssql) RemoveIndex(scope *Scope, indexName string) {
|
||||
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
|
||||
scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName()))
|
||||
}
|
||||
|
|
16
mysql.go
16
mysql.go
|
@ -86,26 +86,16 @@ func (s *mysql) databaseName(scope *Scope) string {
|
|||
|
||||
func (s *mysql) 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 AND table_schema = %v",
|
||||
newScope.AddToVars(tableName),
|
||||
newScope.AddToVars(s.databaseName(scope))))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables where table_name = ? AND table_schema = ?", tableName, s.databaseName(scope)).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *mysql) 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_schema = %v AND table_name = %v AND column_name = %v",
|
||||
newScope.AddToVars(s.databaseName(scope)),
|
||||
newScope.AddToVars(tableName),
|
||||
newScope.AddToVars(columnName),
|
||||
))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM information_schema.columns WHERE table_schema = ? AND table_name = ? AND column_name = ?", s.databaseName(scope), tableName, columnName).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *mysql) RemoveIndex(scope *Scope, indexName string) {
|
||||
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
|
||||
scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName()))
|
||||
}
|
||||
|
|
13
postgres.go
13
postgres.go
|
@ -81,25 +81,18 @@ func (s *postgres) Quote(key string) string {
|
|||
|
||||
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 and table_type = 'BASE TABLE'", newScope.AddToVars(tableName)))
|
||||
newScope.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM INFORMATION_SCHEMA.tables WHERE table_name = ? AND table_type = 'BASE TABLE'", tableName).Row().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.SqlDB().QueryRow(newScope.Sql, newScope.SqlVars...).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM information_schema.columns WHERE table_name = ? AND column_name = ?", tableName, columnName).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *postgres) RemoveIndex(scope *Scope, indexName string) {
|
||||
scope.Raw(fmt.Sprintf("DROP INDEX %v", s.Quote(indexName))).Exec()
|
||||
scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v", indexName))
|
||||
}
|
||||
|
||||
var hstoreType = reflect.TypeOf(Hstore{})
|
||||
|
|
|
@ -471,7 +471,9 @@ func (scope *Scope) dropTable() *Scope {
|
|||
}
|
||||
|
||||
func (scope *Scope) dropTableIfExists() *Scope {
|
||||
scope.Raw(fmt.Sprintf("DROP TABLE IF EXISTS %v", scope.QuotedTableName())).Exec()
|
||||
if scope.Dialect().HasTable(scope, scope.TableName()) {
|
||||
scope.dropTable()
|
||||
}
|
||||
return scope
|
||||
}
|
||||
|
||||
|
|
|
@ -70,16 +70,16 @@ func (s *sqlite3) Quote(key string) string {
|
|||
|
||||
func (s *sqlite3) HasTable(scope *Scope, tableName string) bool {
|
||||
var count int
|
||||
scope.SqlDB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE type='table' AND name='%v';", tableName)).Scan(&count)
|
||||
scope.NewDB().Raw("SELECT count(*) FROM sqlite_master WHERE type='table' AND name=?", tableName).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *sqlite3) HasColumn(scope *Scope, tableName string, columnName string) bool {
|
||||
var count int
|
||||
scope.SqlDB().QueryRow(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = '%v' AND (sql LIKE '%%(\"%v\" %%' OR sql LIKE '%%,\"%v\" %%' OR sql LIKE '%%( %v %%' OR sql LIKE '%%, %v %%');\n", tableName, columnName, columnName, columnName, columnName)).Scan(&count)
|
||||
scope.NewDB().Raw(fmt.Sprintf("SELECT count(*) FROM sqlite_master WHERE tbl_name = ? AND (sql LIKE '%%(\"%v\" %%' OR sql LIKE '%%,\"%v\" %%' OR sql LIKE '%%( %v %%' OR sql LIKE '%%, %v %%');\n", columnName, columnName, columnName, columnName), tableName).Row().Scan(&count)
|
||||
return count > 0
|
||||
}
|
||||
|
||||
func (s *sqlite3) RemoveIndex(scope *Scope, indexName string) {
|
||||
scope.Raw(fmt.Sprintf("DROP INDEX %v", indexName)).Exec()
|
||||
scope.NewDB().Exec(fmt.Sprintf("DROP INDEX %v", indexName))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue