2013-08-11 14:27:45 +00:00
|
|
|
package orm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// get table alias.
|
2013-09-16 01:48:04 +00:00
|
|
|
func getDbAlias(name string) *alias {
|
|
|
|
if al, ok := dataBaseCache.get(name); ok {
|
|
|
|
return al
|
|
|
|
} else {
|
|
|
|
panic(fmt.Errorf("unknown DataBase alias name %s", name))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// get pk column info.
|
2013-08-11 14:27:45 +00:00
|
|
|
func getExistPk(mi *modelInfo, ind reflect.Value) (column string, value interface{}, exist bool) {
|
|
|
|
fi := mi.fields.pk
|
|
|
|
|
|
|
|
v := ind.Field(fi.fieldIndex)
|
2013-08-30 04:32:05 +00:00
|
|
|
if fi.fieldType&IsPostiveIntegerField > 0 {
|
|
|
|
vu := v.Uint()
|
|
|
|
exist = vu > 0
|
|
|
|
value = vu
|
|
|
|
} else if fi.fieldType&IsIntegerField > 0 {
|
2013-08-11 14:27:45 +00:00
|
|
|
vu := v.Int()
|
|
|
|
exist = vu > 0
|
|
|
|
value = vu
|
|
|
|
} else {
|
|
|
|
vu := v.String()
|
|
|
|
exist = vu != ""
|
|
|
|
value = vu
|
|
|
|
}
|
|
|
|
|
|
|
|
column = fi.column
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-01-17 09:25:17 +00:00
|
|
|
// get fields description as flatted string.
|
2013-08-13 09:16:12 +00:00
|
|
|
func getFlatParams(fi *fieldInfo, args []interface{}, tz *time.Location) (params []interface{}) {
|
2013-08-11 14:27:45 +00:00
|
|
|
|
|
|
|
outFor:
|
|
|
|
for _, arg := range args {
|
|
|
|
val := reflect.ValueOf(arg)
|
|
|
|
|
|
|
|
if arg == nil {
|
|
|
|
params = append(params, arg)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
kind := val.Kind()
|
|
|
|
if kind == reflect.Ptr {
|
|
|
|
val = val.Elem()
|
|
|
|
kind = val.Kind()
|
|
|
|
arg = val.Interface()
|
|
|
|
}
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case reflect.String:
|
|
|
|
v := val.String()
|
2013-11-01 16:51:53 +00:00
|
|
|
if fi != nil {
|
|
|
|
if fi.fieldType == TypeDateField || fi.fieldType == TypeDateTimeField {
|
|
|
|
var t time.Time
|
|
|
|
var err error
|
|
|
|
if len(v) >= 19 {
|
|
|
|
s := v[:19]
|
|
|
|
t, err = time.ParseInLocation(format_DateTime, s, DefaultTimeLoc)
|
|
|
|
} else {
|
|
|
|
s := v
|
|
|
|
if len(v) > 10 {
|
|
|
|
s = v[:10]
|
|
|
|
}
|
2013-11-06 13:08:12 +00:00
|
|
|
t, err = time.ParseInLocation(format_Date, s, tz)
|
2013-11-01 16:51:53 +00:00
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
if fi.fieldType == TypeDateField {
|
|
|
|
v = t.In(tz).Format(format_Date)
|
|
|
|
} else {
|
|
|
|
v = t.In(tz).Format(format_DateTime)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
arg = v
|
2014-03-13 15:31:47 +00:00
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
arg = val.Int()
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
arg = val.Uint()
|
|
|
|
case reflect.Float32:
|
|
|
|
arg, _ = StrTo(ToStr(arg)).Float64()
|
|
|
|
case reflect.Float64:
|
|
|
|
arg = val.Float()
|
|
|
|
case reflect.Bool:
|
|
|
|
arg = val.Bool()
|
|
|
|
case reflect.Slice, reflect.Array:
|
|
|
|
if _, ok := arg.([]byte); ok {
|
|
|
|
continue outFor
|
2013-08-11 14:27:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
var args []interface{}
|
|
|
|
for i := 0; i < val.Len(); i++ {
|
|
|
|
v := val.Index(i)
|
2013-08-11 14:27:45 +00:00
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
var vu interface{}
|
|
|
|
if v.CanInterface() {
|
|
|
|
vu = v.Interface()
|
2013-08-11 14:27:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
if vu == nil {
|
|
|
|
continue
|
2013-08-11 14:27:45 +00:00
|
|
|
}
|
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
args = append(args, vu)
|
|
|
|
}
|
2013-08-11 14:27:45 +00:00
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
if len(args) > 0 {
|
|
|
|
p := getFlatParams(fi, args, tz)
|
|
|
|
params = append(params, p...)
|
|
|
|
}
|
|
|
|
continue outFor
|
|
|
|
case reflect.Struct:
|
|
|
|
if v, ok := arg.(time.Time); ok {
|
|
|
|
if fi != nil && fi.fieldType == TypeDateField {
|
|
|
|
arg = v.In(tz).Format(format_Date)
|
|
|
|
} else {
|
|
|
|
arg = v.In(tz).Format(format_DateTime)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
typ := val.Type()
|
|
|
|
name := getFullName(typ)
|
|
|
|
var value interface{}
|
|
|
|
if mmi, ok := modelCache.getByFN(name); ok {
|
|
|
|
if _, vu, exist := getExistPk(mmi, val); exist {
|
|
|
|
value = vu
|
2013-08-11 14:27:45 +00:00
|
|
|
}
|
2014-03-13 15:31:47 +00:00
|
|
|
}
|
|
|
|
arg = value
|
2013-08-11 14:27:45 +00:00
|
|
|
|
2014-03-13 15:31:47 +00:00
|
|
|
if arg == nil {
|
|
|
|
panic(fmt.Errorf("need a valid args value, unknown table or value `%s`", name))
|
2013-08-11 14:27:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-13 15:31:47 +00:00
|
|
|
|
2013-08-11 14:27:45 +00:00
|
|
|
params = append(params, arg)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|