Merge branch 'master' into feature/cacher-tag

This commit is contained in:
Nash Tsai 2014-08-28 23:22:11 +08:00
commit 524993f44d
7 changed files with 45 additions and 15 deletions

View File

@ -405,12 +405,14 @@ func (engine *Engine) DumpAll(w io.Writer) error {
} else if col.SQLType.IsText() || col.SQLType.IsTime() { } else if col.SQLType.IsText() || col.SQLType.IsTime() {
var v = fmt.Sprintf("%s", d) var v = fmt.Sprintf("%s", d)
temp += ", '" + strings.Replace(v, "'", "''", -1) + "'" temp += ", '" + strings.Replace(v, "'", "''", -1) + "'"
} else if col.SQLType.IsBlob() /**/ { } else if col.SQLType.IsBlob() {
if reflect.TypeOf(d).Kind() == reflect.Slice { if reflect.TypeOf(d).Kind() == reflect.Slice {
temp += fmt.Sprintf(", %s", engine.dialect.FormatBytes(d.([]byte))) temp += fmt.Sprintf(", %s", engine.dialect.FormatBytes(d.([]byte)))
} else if reflect.TypeOf(d).Kind() == reflect.String { } else if reflect.TypeOf(d).Kind() == reflect.String {
temp += fmt.Sprintf(", '%s'", d.(string)) temp += fmt.Sprintf(", '%s'", d.(string))
} }
} else if col.SQLType.IsNumeric() {
temp += fmt.Sprintf(", %s", string(d.([]byte)))
} else { } else {
s := fmt.Sprintf("%v", d) s := fmt.Sprintf("%v", d)
if strings.Contains(s, ":") || strings.Contains(s, "-") { if strings.Contains(s, ":") || strings.Contains(s, "-") {
@ -1187,19 +1189,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

@ -168,7 +168,7 @@ where a.object_id=object_id('` + tableName + `')`
} }
} }
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

@ -60,7 +60,7 @@ func (db *mysql) SqlType(c *core.Column) string {
for v, _ := range c.EnumOptions { for v, _ := range c.EnumOptions {
opts += fmt.Sprintf(",'%v'", v) opts += fmt.Sprintf(",'%v'", v)
} }
res += strings.TrimLeft(opts,",") res += strings.TrimLeft(opts, ",")
res += ")" res += ")"
case core.Set: //mysql set case core.Set: //mysql set
res = core.Set res = core.Set
@ -69,7 +69,7 @@ func (db *mysql) SqlType(c *core.Column) string {
for v, _ := range c.SetOptions { for v, _ := range c.SetOptions {
opts += fmt.Sprintf(",'%v'", v) opts += fmt.Sprintf(",'%v'", v)
} }
res += strings.TrimLeft(opts,",") res += strings.TrimLeft(opts, ",")
res += ")" res += ")"
default: default:
res = t res = t
@ -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 {
@ -223,7 +228,7 @@ func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column
col.IsAutoIncrement = true col.IsAutoIncrement = true
} }
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

@ -158,7 +158,7 @@ func (db *oracle) GetColumns(tableName string) ([]string, map[string]*core.Colum
col.Length = dataLen col.Length = dataLen
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

@ -177,7 +177,8 @@ 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 {
maxLen, err = strconv.Atoi(*maxLenStr) maxLen, err = strconv.Atoi(*maxLenStr)
@ -196,6 +197,10 @@ WHERE c.relkind = 'r'::char AND c.relname = $1 AND f.attnum > 0 ORDER BY f.attnu
} }
} }
if colDefault != nil && strings.HasPrefix(*colDefault, "nextval(") {
col.IsAutoIncrement = true
}
col.Nullable = (isNullable == "YES") col.Nullable = (isNullable == "YES")
switch dataType { switch dataType {
@ -220,7 +225,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

@ -653,6 +653,7 @@ func (session *Session) cacheGet(bean interface{}, sqlStr string, args ...interf
if v, ok := data[session.Statement.RefTable.PrimaryKeys[0]]; !ok { if v, ok := data[session.Statement.RefTable.PrimaryKeys[0]]; !ok {
return false, ErrCacheFailed return false, ErrCacheFailed
} else { } else {
// TODO https://github.com/go-xorm/xorm/issues/144, PK may not always be int64
id, err = strconv.ParseInt(string(v), 10, 64) id, err = strconv.ParseInt(string(v), 10, 64)
if err != nil { if err != nil {
return false, err return false, err
@ -998,9 +999,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

View File

@ -84,6 +84,21 @@ func (db *sqlite3) TableCheckSql(tableName string) (string, []interface{}) {
return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args return "SELECT name FROM sqlite_master WHERE type='table' and name = ?", args
} }
func (db *sqlite3) DropIndexSql(tableName string, index *core.Index) string {
quote := db.Quote
//var unique string
var idxName string = index.Name
if !strings.HasPrefix(idxName, "UQE_") &&
!strings.HasPrefix(idxName, "IDX_") {
if index.Type == core.UniqueType {
idxName = fmt.Sprintf("UQE_%v_%v", tableName, index.Name)
} else {
idxName = fmt.Sprintf("IDX_%v_%v", tableName, index.Name)
}
}
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
}
/*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) { /*func (db *sqlite3) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
args := []interface{}{tableName} args := []interface{}{tableName}
sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))" sql := "SELECT name FROM sqlite_master WHERE type='table' and name = ? and ((sql like '%`" + colName + "`%') or (sql like '%[" + colName + "]%'))"