bug fixed

This commit is contained in:
Lunny Xiao 2015-02-28 17:03:28 +08:00
parent 8dcef74e9a
commit 11d36774e9
2 changed files with 49 additions and 13 deletions

View File

@ -281,6 +281,20 @@ func query2(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []m
return rows2Strings(rows)
}
func setColumnTime(bean interface{}, col *core.Column, t time.Time) {
v, _ := col.ValueOf(bean)
if v.CanSet() {
switch v.Type().Kind() {
case reflect.Struct:
v.Set(reflect.ValueOf(t))
case reflect.Int, reflect.Int64, reflect.Int32:
v.SetInt(t.Unix())
case reflect.Uint, reflect.Uint64, reflect.Uint32:
v.SetUint(uint64(t.Unix()))
}
}
}
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
colNames := make([]string, 0)
args := make([]interface{}, 0)
@ -342,15 +356,7 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
var colName = col.Name
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
col := table.GetColumn(colName)
v, _ := col.ValueOf(bean)
switch v.Type().Kind() {
case reflect.Struct:
v.Set(reflect.ValueOf(t))
case reflect.Int, reflect.Int64, reflect.Int32:
v.SetInt(t.Unix())
case reflect.Uint, reflect.Uint64, reflect.Uint32:
v.SetUint(uint64(t.Unix()))
}
setColumnTime(bean, col, t)
})
} else if col.IsVersion && session.Statement.checkVersion {
args = append(args, 1)

View File

@ -2059,7 +2059,14 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
}
}
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, session.Engine.NowTime(col.SQLType.Name))
val, t := session.Engine.NowTime2(col.SQLType.Name)
args = append(args, val)
var colName = col.Name
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
col := table.GetColumn(colName)
setColumnTime(bean, col, t)
})
} else {
arg, err := session.value2Interface(col, fieldValue)
if err != nil {
@ -2095,7 +2102,14 @@ func (session *Session) innerInsertMulti(rowsSlicePtr interface{}) (int64, error
}
}
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, session.Engine.NowTime(col.SQLType.Name))
val, t := session.Engine.NowTime2(col.SQLType.Name)
args = append(args, val)
var colName = col.Name
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
col := table.GetColumn(colName)
setColumnTime(bean, col, t)
})
} else {
arg, err := session.value2Interface(col, fieldValue)
if err != nil {
@ -3379,7 +3393,15 @@ func (session *Session) Update(bean interface{}, condiBean ...interface{}) (int6
if session.Statement.UseAutoTime && table.Updated != "" {
colNames = append(colNames, session.Engine.Quote(table.Updated)+" = ?")
args = append(args, session.Engine.NowTime(table.UpdatedColumn().SQLType.Name))
col := table.UpdatedColumn()
val, t := session.Engine.NowTime2(col.SQLType.Name)
args = append(args, val)
var colName = col.Name
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
col := table.GetColumn(colName)
setColumnTime(bean, col, t)
})
}
//for update action to like "column = column + ?"
@ -3659,7 +3681,15 @@ func (session *Session) Delete(bean interface{}) (int64, error) {
session.Statement.Params = append(session.Statement.Params, "")
paramsLen := len(session.Statement.Params)
copy(session.Statement.Params[1:paramsLen], session.Statement.Params[0:paramsLen-1])
session.Statement.Params[0] = session.Engine.NowTime(deletedColumn.SQLType.Name)
val, t := session.Engine.NowTime2(deletedColumn.SQLType.Name)
session.Statement.Params[0] = val
var colName = deletedColumn.Name
session.afterClosures = append(session.afterClosures, func(bean interface{}) {
col := table.GetColumn(colName)
setColumnTime(bean, col, t)
})
}
args = append(session.Statement.Params, args...)