xorm/table.go

102 lines
2.4 KiB
Go
Raw Normal View History

2013-05-06 08:01:17 +00:00
package xorm
import (
2013-12-18 03:31:32 +00:00
"reflect"
"strings"
"time"
2014-01-07 09:33:27 +00:00
"github.com/lunny/xorm/core"
2013-05-06 08:01:17 +00:00
)
2013-12-18 14:55:39 +00:00
var (
c_EMPTY_STRING string
c_BOOL_DEFAULT bool
c_BYTE_DEFAULT byte
c_COMPLEX64_DEFAULT complex64
c_COMPLEX128_DEFAULT complex128
c_FLOAT32_DEFAULT float32
c_FLOAT64_DEFAULT float64
c_INT64_DEFAULT int64
c_UINT64_DEFAULT uint64
c_INT32_DEFAULT int32
c_UINT32_DEFAULT uint32
c_INT16_DEFAULT int16
c_UINT16_DEFAULT uint16
c_INT8_DEFAULT int8
c_UINT8_DEFAULT uint8
c_INT_DEFAULT int
c_UINT_DEFAULT uint
c_TIME_DEFAULT time.Time
)
2013-05-06 08:01:17 +00:00
2014-01-07 09:33:27 +00:00
func genCols(table *core.Table, session *Session, bean interface{}, useCol bool, includeQuote bool) ([]string, []interface{}, error) {
2013-12-18 03:31:32 +00:00
colNames := make([]string, 0)
args := make([]interface{}, 0)
2014-01-07 09:33:27 +00:00
for _, col := range table.Columns() {
2014-01-08 14:47:30 +00:00
lColName := strings.ToLower(col.Name)
2013-12-18 03:31:32 +00:00
if useCol && !col.IsVersion && !col.IsCreated && !col.IsUpdated {
2014-01-08 14:47:30 +00:00
if _, ok := session.Statement.columnMap[lColName]; !ok {
2013-12-18 03:31:32 +00:00
continue
}
}
2014-01-09 06:44:41 +00:00
if col.MapType == core.ONLYFROMDB {
continue
}
fieldValuePtr, err := col.ValueOf(bean)
if err != nil {
session.Engine.LogError(err)
2013-12-18 03:31:32 +00:00
continue
}
2014-01-09 06:44:41 +00:00
fieldValue := *fieldValuePtr
2013-12-18 03:31:32 +00:00
if col.IsAutoIncrement {
switch fieldValue.Type().Kind() {
case reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int, reflect.Int64:
if fieldValue.Int() == 0 {
continue
}
case reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint, reflect.Uint64:
if fieldValue.Uint() == 0 {
continue
}
case reflect.String:
if len(fieldValue.String()) == 0 {
continue
}
}
2013-12-18 03:31:32 +00:00
}
if session.Statement.ColumnStr != "" {
2014-01-08 14:47:30 +00:00
if _, ok := session.Statement.columnMap[lColName]; !ok {
2013-12-18 03:31:32 +00:00
continue
}
}
if session.Statement.OmitStr != "" {
2014-01-08 14:47:30 +00:00
if _, ok := session.Statement.columnMap[lColName]; ok {
2013-12-18 03:31:32 +00:00
continue
}
}
if (col.IsCreated || col.IsUpdated) && session.Statement.UseAutoTime {
args = append(args, time.Now())
} else if col.IsVersion && session.Statement.checkVersion {
args = append(args, 1)
} else {
arg, err := session.value2Interface(col, fieldValue)
if err != nil {
return colNames, args, err
}
args = append(args, arg)
}
if includeQuote {
colNames = append(colNames, session.Engine.Quote(col.Name)+" = ?")
} else {
colNames = append(colNames, col.Name)
}
}
return colNames, args, nil
2013-09-26 07:19:39 +00:00
}