1
0
mirror of https://github.com/astaxie/beego.git synced 2024-06-02 04:53:30 +00:00
Beego/orm/db_utils.go

178 lines
4.2 KiB
Go
Raw Normal View History

2014-08-18 08:41:43 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// http://www.apache.org/licenses/LICENSE-2.0
2014-07-03 15:40:21 +00:00
//
2014-08-18 08:41:43 +00:00
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
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.
func getDbAlias(name string) *alias {
if al, ok := dataBaseCache.get(name); ok {
return al
}
2015-09-12 13:46:43 +00:00
panic(fmt.Errorf("unknown DataBase alias name %s", name))
}
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
2016-02-24 10:46:14 +00:00
v := ind.FieldByIndex(fi.fieldIndex)
2016-03-17 13:34:49 +00:00
if fi.fieldType&IsPositiveIntegerField > 0 {
2013-08-30 04:32:05 +00:00
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()
2016-03-17 13:34:49 +00:00
exist = true
2013-08-11 14:27:45 +00:00
value = vu
2017-01-11 10:51:32 +00:00
} else if fi.fieldType&IsRelField > 0 {
_, value, exist = getExistPk(fi.relModelInfo, reflect.Indirect(v))
2013-08-11 14:27:45 +00:00
} 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.
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
}
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 == TypeTimeField || fi.fieldType == TypeDateField || fi.fieldType == TypeDateTimeField {
2013-11-01 16:51:53 +00:00
var t time.Time
var err error
if len(v) >= 19 {
s := v[:19]
2015-09-12 13:46:43 +00:00
t, err = time.ParseInLocation(formatDateTime, s, DefaultTimeLoc)
} else if len(v) >= 10 {
2013-11-01 16:51:53 +00:00
s := v
if len(v) > 10 {
s = v[:10]
}
2015-09-12 13:46:43 +00:00
t, err = time.ParseInLocation(formatDate, s, tz)
} else {
s := v
if len(s) > 8 {
s = v[:8]
}
t, err = time.ParseInLocation(formatTime, s, tz)
2013-11-01 16:51:53 +00:00
}
if err == nil {
if fi.fieldType == TypeDateField {
2015-09-12 13:46:43 +00:00
v = t.In(tz).Format(formatDate)
} else if fi.fieldType == TypeDateTimeField {
2015-09-12 13:46:43 +00:00
v = t.In(tz).Format(formatDateTime)
} else {
v = t.In(tz).Format(formatTime)
2013-11-01 16:51:53 +00:00
}
}
}
}
arg = v
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
}
var args []interface{}
for i := 0; i < val.Len(); i++ {
v := val.Index(i)
2013-08-11 14:27:45 +00:00
var vu interface{}
if v.CanInterface() {
vu = v.Interface()
2013-08-11 14:27:45 +00:00
}
if vu == nil {
continue
2013-08-11 14:27:45 +00:00
}
args = append(args, vu)
}
2013-08-11 14:27:45 +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 {
2015-09-12 13:46:43 +00:00
arg = v.In(tz).Format(formatDate)
2016-08-30 12:40:46 +00:00
} else if fi != nil && fi.fieldType == TypeDateTimeField {
2015-09-12 13:46:43 +00:00
arg = v.In(tz).Format(formatDateTime)
} else if fi != nil && fi.fieldType == TypeTimeField {
arg = v.In(tz).Format(formatTime)
} else {
arg = v.In(tz).Format(formatDateTime)
}
} else {
typ := val.Type()
name := getFullName(typ)
var value interface{}
2016-08-30 16:07:19 +00:00
if mmi, ok := modelCache.getByFullName(name); ok {
if _, vu, exist := getExistPk(mmi, val); exist {
value = vu
2013-08-11 14:27:45 +00:00
}
}
arg = value
2013-08-11 14:27:45 +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
}
}
}
2013-08-11 14:27:45 +00:00
params = append(params, arg)
}
return
}