improved Sync2 method

This commit is contained in:
Lunny Xiao 2014-08-28 22:48:01 +08:00
parent 64790ba262
commit 0fa2433cf9
4 changed files with 19 additions and 11 deletions

View File

@ -1116,19 +1116,22 @@ func (engine *Engine) Sync2(beans ...interface{}) error {
} }
if oriCol != nil { if oriCol != nil {
if col.SQLType.Name != oriCol.SQLType.Name { expectedType := engine.dialect.SqlType(col)
if col.SQLType.Name == core.Text && //curType := oriCol.SQLType.Name
oriCol.SQLType.Name == core.Varchar { curType := engine.dialect.SqlType(oriCol)
if expectedType != curType {
if expectedType == core.Text &&
curType == core.Varchar {
// currently only support mysql // currently only support mysql
if engine.dialect.DBType() == core.MYSQL { if engine.dialect.DBType() == core.MYSQL {
_, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col)) _, err = engine.Exec(engine.dialect.ModifyColumnSql(table.Name, col))
} else { } else {
engine.LogWarnf("Table %s Column %s db type is %s, struct type is %s\n", engine.LogWarnf("Table %s Column %s db type is %s, struct type is %s\n",
table.Name, col.Name, oriCol.SQLType.Name, col.SQLType.Name) table.Name, col.Name, curType, expectedType)
} }
} else { } else {
engine.LogWarnf("Table %s Column %s db type is %s, struct type is %s", engine.LogWarnf("Table %s Column %s db type is %s, struct type is %s",
table.Name, col.Name, oriCol.SQLType.Name, col.SQLType.Name) table.Name, col.Name, curType, expectedType)
} }
} }
if col.Default != oriCol.Default { if col.Default != oriCol.Default {

View File

@ -78,6 +78,11 @@ func (db *mysql) SqlType(c *core.Column) string {
var hasLen1 bool = (c.Length > 0) var hasLen1 bool = (c.Length > 0)
var hasLen2 bool = (c.Length2 > 0) var hasLen2 bool = (c.Length2 > 0)
if res == core.BigInt && !hasLen1 && !hasLen2 {
c.Length = 20
hasLen1 = true
}
if hasLen2 { if hasLen2 {
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")" res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
} else if hasLen1 { } else if hasLen1 {

View File

@ -177,6 +177,7 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND f.attnum > 0 ORDER BY f.attnu
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
//fmt.Println(args, colName, isNullable, dataType, maxLenStr, colDefault, numPrecision, numRadix, isPK, isUnique) //fmt.Println(args, colName, isNullable, dataType, maxLenStr, colDefault, numPrecision, numRadix, isPK, isUnique)
var maxLen int var maxLen int
if maxLenStr != nil { if maxLenStr != nil {
@ -220,7 +221,7 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND f.attnum > 0 ORDER BY f.attnu
col.Length = maxLen col.Length = maxLen
if col.SQLType.IsText() { if col.SQLType.IsText() || col.SQLType.IsTime() {
if col.Default != "" { if col.Default != "" {
col.Default = "'" + col.Default + "'" col.Default = "'" + col.Default + "'"
} else { } else {

View File

@ -996,9 +996,8 @@ func (session *Session) Get(bean interface{}) (bool, error) {
err = session.row2Bean(rawRows, fields, len(fields), bean) err = session.row2Bean(rawRows, fields, len(fields), bean)
} }
return true, err return true, err
} else {
return false, nil
} }
return false, nil
} }
// Count counts the records. bean's non-empty fields // Count counts the records. bean's non-empty fields