bug fixed and improved IsTableExist

This commit is contained in:
Lunny Xiao 2014-05-06 14:59:58 +08:00
parent 74a165e1fd
commit caffa2447f
3 changed files with 21 additions and 4 deletions

View File

@ -784,13 +784,19 @@ func (engine *Engine) IsTableEmpty(bean interface{}) (bool, error) {
// If a table is exist // If a table is exist
func (engine *Engine) IsTableExist(bean interface{}) (bool, error) { func (engine *Engine) IsTableExist(bean interface{}) (bool, error) {
v := rValue(bean) v := rValue(bean)
if v.Type().Kind() != reflect.Struct { var tableName string
if v.Type().Kind() == reflect.String {
tableName = bean.(string)
} else if v.Type().Kind() == reflect.Struct {
table := engine.autoMapType(v)
tableName = table.Name
} else {
return false, errors.New("bean should be a struct or struct's point") return false, errors.New("bean should be a struct or struct's point")
} }
table := engine.autoMapType(v)
session := engine.NewSession() session := engine.NewSession()
defer session.Close() defer session.Close()
has, err := session.isTableExist(table.Name) has, err := session.isTableExist(tableName)
return has, err return has, err
} }

View File

@ -88,6 +88,17 @@ func main() {
_, err = Orm.Insert(user) _, err = Orm.Insert(user)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return
}
isexist, err := Orm.IsTableExist("sync_user2")
if err != nil {
fmt.Println(err)
return
}
if !isexist {
fmt.Println("sync_user2 is not exist")
return
} }
} }
} }

View File

@ -927,7 +927,7 @@ func (statement *Statement) genGetSql(bean interface{}) (string, []interface{})
func (s *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) { func (s *Statement) genAddColumnStr(col *core.Column) (string, []interface{}) {
quote := s.Engine.Quote quote := s.Engine.Quote
sql := fmt.Sprintf("ALTER TABLE %v ADD COLUMN %v;", quote(s.TableName()), sql := fmt.Sprintf("ALTER TABLE %v ADD %v;", quote(s.TableName()),
col.String(s.Engine.dialect)) col.String(s.Engine.dialect))
return sql, []interface{}{} return sql, []interface{}{}
} }