some comments, refactors improvements
This commit is contained in:
parent
2b34c682f1
commit
0f03658955
91
helpers.go
91
helpers.go
|
@ -5,6 +5,7 @@
|
||||||
package xorm
|
package xorm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -15,6 +16,77 @@ import (
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// str2PK convert string value to primary key value according to tp
|
||||||
|
func str2PK(s string, tp reflect.Type) (interface{}, error) {
|
||||||
|
var err error
|
||||||
|
var result interface{}
|
||||||
|
switch tp.Kind() {
|
||||||
|
case reflect.Int:
|
||||||
|
result, err = strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as int: " + err.Error())
|
||||||
|
}
|
||||||
|
case reflect.Int8:
|
||||||
|
x, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as int16: " + err.Error())
|
||||||
|
}
|
||||||
|
result = int8(x)
|
||||||
|
case reflect.Int16:
|
||||||
|
x, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as int16: " + err.Error())
|
||||||
|
}
|
||||||
|
result = int16(x)
|
||||||
|
case reflect.Int32:
|
||||||
|
x, err := strconv.Atoi(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as int32: " + err.Error())
|
||||||
|
}
|
||||||
|
result = int32(x)
|
||||||
|
case reflect.Int64:
|
||||||
|
result, err = strconv.ParseInt(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as int64: " + err.Error())
|
||||||
|
}
|
||||||
|
case reflect.Uint:
|
||||||
|
x, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as uint: " + err.Error())
|
||||||
|
}
|
||||||
|
result = uint(x)
|
||||||
|
case reflect.Uint8:
|
||||||
|
x, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as uint8: " + err.Error())
|
||||||
|
}
|
||||||
|
result = uint8(x)
|
||||||
|
case reflect.Uint16:
|
||||||
|
x, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as uint16: " + err.Error())
|
||||||
|
}
|
||||||
|
result = uint16(x)
|
||||||
|
case reflect.Uint32:
|
||||||
|
x, err := strconv.ParseUint(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as uint32: " + err.Error())
|
||||||
|
}
|
||||||
|
result = uint32(x)
|
||||||
|
case reflect.Uint64:
|
||||||
|
result, err = strconv.ParseUint(s, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("convert " + s + " as uint64: " + err.Error())
|
||||||
|
}
|
||||||
|
case reflect.String:
|
||||||
|
result = s
|
||||||
|
default:
|
||||||
|
panic("unsupported convert type")
|
||||||
|
}
|
||||||
|
result = reflect.ValueOf(result).Convert(tp).Interface()
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
func splitTag(tag string) (tags []string) {
|
func splitTag(tag string) (tags []string) {
|
||||||
tag = strings.TrimSpace(tag)
|
tag = strings.TrimSpace(tag)
|
||||||
var hasQuote = false
|
var hasQuote = false
|
||||||
|
@ -160,6 +232,19 @@ func structName(v reflect.Type) string {
|
||||||
return v.Name()
|
return v.Name()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func col2NewCols(columns ...string) []string {
|
||||||
|
newColumns := make([]string, 0, len(columns))
|
||||||
|
for _, col := range columns {
|
||||||
|
col = strings.Replace(col, "`", "", -1)
|
||||||
|
col = strings.Replace(col, `"`, "", -1)
|
||||||
|
ccols := strings.Split(col, ",")
|
||||||
|
for _, c := range ccols {
|
||||||
|
newColumns = append(newColumns, strings.TrimSpace(c))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return newColumns
|
||||||
|
}
|
||||||
|
|
||||||
func sliceEq(left, right []string) bool {
|
func sliceEq(left, right []string) bool {
|
||||||
if len(left) != len(right) {
|
if len(left) != len(right) {
|
||||||
return false
|
return false
|
||||||
|
@ -194,7 +279,7 @@ func reflect2value(rawValue *reflect.Value) (str string, err error) {
|
||||||
default:
|
default:
|
||||||
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
||||||
}
|
}
|
||||||
//时间类型
|
// time type
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if aa.ConvertibleTo(core.TimeType) {
|
if aa.ConvertibleTo(core.TimeType) {
|
||||||
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
|
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
|
||||||
|
@ -453,3 +538,7 @@ func genCols(table *core.Table, session *Session, bean interface{}, useCol bool,
|
||||||
}
|
}
|
||||||
return colNames, args, nil
|
return colNames, args, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func indexName(tableName, idxName string) string {
|
||||||
|
return fmt.Sprintf("IDX_%v_%v", tableName, idxName)
|
||||||
|
}
|
||||||
|
|
242
session.go
242
session.go
|
@ -960,7 +960,7 @@ func (session *Session) cacheFind(t reflect.Type, sqlStr string, rowsSlicePtr in
|
||||||
keyType := sliceValue.Type().Key()
|
keyType := sliceValue.Type().Key()
|
||||||
var ikey interface{}
|
var ikey interface{}
|
||||||
if len(key) == 1 {
|
if len(key) == 1 {
|
||||||
ikey, err = Str2PK(fmt.Sprintf("%v", key[0]), keyType)
|
ikey, err = str2PK(fmt.Sprintf("%v", key[0]), keyType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1123,77 +1123,6 @@ func (session *Session) Count(bean interface{}) (int64, error) {
|
||||||
return int64(total), err
|
return int64(total), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Str2PK convert string value to primary key value according to tp
|
|
||||||
func Str2PK(s string, tp reflect.Type) (interface{}, error) {
|
|
||||||
var err error
|
|
||||||
var result interface{}
|
|
||||||
switch tp.Kind() {
|
|
||||||
case reflect.Int:
|
|
||||||
result, err = strconv.Atoi(s)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as int: " + err.Error())
|
|
||||||
}
|
|
||||||
case reflect.Int8:
|
|
||||||
x, err := strconv.Atoi(s)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as int16: " + err.Error())
|
|
||||||
}
|
|
||||||
result = int8(x)
|
|
||||||
case reflect.Int16:
|
|
||||||
x, err := strconv.Atoi(s)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as int16: " + err.Error())
|
|
||||||
}
|
|
||||||
result = int16(x)
|
|
||||||
case reflect.Int32:
|
|
||||||
x, err := strconv.Atoi(s)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as int32: " + err.Error())
|
|
||||||
}
|
|
||||||
result = int32(x)
|
|
||||||
case reflect.Int64:
|
|
||||||
result, err = strconv.ParseInt(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as int64: " + err.Error())
|
|
||||||
}
|
|
||||||
case reflect.Uint:
|
|
||||||
x, err := strconv.ParseUint(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as uint: " + err.Error())
|
|
||||||
}
|
|
||||||
result = uint(x)
|
|
||||||
case reflect.Uint8:
|
|
||||||
x, err := strconv.ParseUint(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as uint8: " + err.Error())
|
|
||||||
}
|
|
||||||
result = uint8(x)
|
|
||||||
case reflect.Uint16:
|
|
||||||
x, err := strconv.ParseUint(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as uint16: " + err.Error())
|
|
||||||
}
|
|
||||||
result = uint16(x)
|
|
||||||
case reflect.Uint32:
|
|
||||||
x, err := strconv.ParseUint(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as uint32: " + err.Error())
|
|
||||||
}
|
|
||||||
result = uint32(x)
|
|
||||||
case reflect.Uint64:
|
|
||||||
result, err = strconv.ParseUint(s, 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.New("convert " + s + " as uint64: " + err.Error())
|
|
||||||
}
|
|
||||||
case reflect.String:
|
|
||||||
result = s
|
|
||||||
default:
|
|
||||||
panic("unsupported convert type")
|
|
||||||
}
|
|
||||||
result = reflect.ValueOf(result).Convert(tp).Interface()
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find retrieve records from table, condiBeans's non-empty fields
|
// Find retrieve records from table, condiBeans's non-empty fields
|
||||||
// are conditions. beans could be []Struct, []*Struct, map[int64]Struct
|
// are conditions. beans could be []Struct, []*Struct, map[int64]Struct
|
||||||
// map[int64]*Struct
|
// map[int64]*Struct
|
||||||
|
@ -1238,7 +1167,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
// !oinume! Add "<col> IS NULL" to WHERE whatever condiBean is given.
|
// !oinume! Add "<col> IS NULL" to WHERE whatever condiBean is given.
|
||||||
// See https://github.com/go-xorm/xorm/issues/179
|
// See https://github.com/go-xorm/xorm/issues/179
|
||||||
if col := table.DeletedColumn(); col != nil && !session.Statement.unscoped { // tag "deleted" is enabled
|
if col := table.DeletedColumn(); col != nil && !session.Statement.unscoped { // tag "deleted" is enabled
|
||||||
var colName string = session.Engine.Quote(col.Name)
|
var colName = session.Engine.Quote(col.Name)
|
||||||
if addedTableName {
|
if addedTableName {
|
||||||
var nm = session.Statement.TableName()
|
var nm = session.Statement.TableName()
|
||||||
if len(session.Statement.TableAlias) > 0 {
|
if len(session.Statement.TableAlias) > 0 {
|
||||||
|
@ -1327,8 +1256,6 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fieldsCount := len(fields)
|
|
||||||
|
|
||||||
var newElemFunc func() reflect.Value
|
var newElemFunc func() reflect.Value
|
||||||
if sliceElementType.Kind() == reflect.Ptr {
|
if sliceElementType.Kind() == reflect.Ptr {
|
||||||
newElemFunc = func() reflect.Value {
|
newElemFunc = func() reflect.Value {
|
||||||
|
@ -1354,15 +1281,15 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var newValue reflect.Value = newElemFunc()
|
var newValue = newElemFunc()
|
||||||
dataStruct := rValue(newValue.Interface())
|
dataStruct := rValue(newValue.Interface())
|
||||||
if dataStruct.Kind() != reflect.Struct {
|
if dataStruct.Kind() != reflect.Struct {
|
||||||
return errors.New("Expected a pointer to a struct")
|
return errors.New("Expected a pointer to a struct")
|
||||||
}
|
}
|
||||||
|
|
||||||
table := session.Engine.autoMapType(dataStruct)
|
return session.rows2Beans(rawRows, fields, len(fields), session.Engine.autoMapType(dataStruct), newElemFunc, sliceValueSetFunc)
|
||||||
return session.rows2Beans(rawRows, fields, fieldsCount, table, newElemFunc, sliceValueSetFunc)
|
}
|
||||||
} else {
|
|
||||||
resultsSlice, err := session.query(sqlStr, args...)
|
resultsSlice, err := session.query(sqlStr, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1384,7 +1311,7 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
var key interface{}
|
var key interface{}
|
||||||
// if there is only one pk, we can put the id as map key.
|
// if there is only one pk, we can put the id as map key.
|
||||||
if len(table.PrimaryKeys) == 1 {
|
if len(table.PrimaryKeys) == 1 {
|
||||||
key, err = Str2PK(string(results[table.PrimaryKeys[0]]), keyType)
|
key, err = str2PK(string(results[table.PrimaryKeys[0]]), keyType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1392,9 +1319,9 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
if keyType.Kind() != reflect.Slice {
|
if keyType.Kind() != reflect.Slice {
|
||||||
panic("don't support multiple primary key's map has non-slice key type")
|
panic("don't support multiple primary key's map has non-slice key type")
|
||||||
} else {
|
} else {
|
||||||
keys := core.PK{}
|
var keys core.PK = make([]interface{}, 0, len(table.PrimaryKeys))
|
||||||
for _, pk := range table.PrimaryKeys {
|
for _, pk := range table.PrimaryKeys {
|
||||||
skey, err := Str2PK(string(results[pk]), keyType)
|
skey, err := str2PK(string(results[pk]), keyType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -1410,11 +1337,11 @@ func (session *Session) Find(rowsSlicePtr interface{}, condiBean ...interface{})
|
||||||
sliceValue.SetMapIndex(reflect.ValueOf(key), reflect.Indirect(reflect.ValueOf(newValue.Interface())))
|
sliceValue.SetMapIndex(reflect.ValueOf(key), reflect.Indirect(reflect.ValueOf(newValue.Interface())))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test if database is ok
|
// Ping test if database is ok
|
||||||
func (session *Session) Ping() error {
|
func (session *Session) Ping() error {
|
||||||
defer session.resetStatement()
|
defer session.resetStatement()
|
||||||
if session.IsAutoClose {
|
if session.IsAutoClose {
|
||||||
|
@ -1435,6 +1362,7 @@ func (engine *Engine) tableName(beanOrTableName interface{}) (string, error) {
|
||||||
return "", errors.New("bean should be a struct or struct's point")
|
return "", errors.New("bean should be a struct or struct's point")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsTableExist if a table is exist
|
||||||
func (session *Session) IsTableExist(beanOrTableName interface{}) (bool, error) {
|
func (session *Session) IsTableExist(beanOrTableName interface{}) (bool, error) {
|
||||||
tableName, err := session.Engine.tableName(beanOrTableName)
|
tableName, err := session.Engine.tableName(beanOrTableName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1454,6 +1382,7 @@ func (session *Session) isTableExist(tableName string) (bool, error) {
|
||||||
return len(results) > 0, err
|
return len(results) > 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsTableEmpty if table have any records
|
||||||
func (session *Session) IsTableEmpty(bean interface{}) (bool, error) {
|
func (session *Session) IsTableEmpty(bean interface{}) (bool, error) {
|
||||||
v := rValue(bean)
|
v := rValue(bean)
|
||||||
t := v.Type()
|
t := v.Type()
|
||||||
|
@ -2601,76 +2530,16 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
|
||||||
} else if session.Statement.UseCascade {
|
} else if session.Statement.UseCascade {
|
||||||
table := session.Engine.autoMapType(*fieldValue)
|
table := session.Engine.autoMapType(*fieldValue)
|
||||||
if table != nil {
|
if table != nil {
|
||||||
|
// TODO: current only support 1 primary key
|
||||||
if len(table.PrimaryKeys) > 1 {
|
if len(table.PrimaryKeys) > 1 {
|
||||||
panic("unsupported composited primary key cascade")
|
panic("unsupported composited primary key cascade")
|
||||||
}
|
}
|
||||||
var pk = make(core.PK, len(table.PrimaryKeys))
|
var pk = make(core.PK, len(table.PrimaryKeys))
|
||||||
rawValueType := table.ColumnType(table.PKColumns()[0].FieldName)
|
rawValueType := table.ColumnType(table.PKColumns()[0].FieldName)
|
||||||
switch rawValueType.Kind() {
|
var err error
|
||||||
case reflect.Int64:
|
pk[0], err = str2PK(string(data), rawValueType)
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
return err
|
||||||
}
|
|
||||||
pk[0] = x
|
|
||||||
case reflect.Int:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = int(x)
|
|
||||||
case reflect.Int32:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = int32(x)
|
|
||||||
case reflect.Int16:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = int16(x)
|
|
||||||
case reflect.Int8:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = int8(x)
|
|
||||||
case reflect.Uint64:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = x
|
|
||||||
case reflect.Uint:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = uint(x)
|
|
||||||
case reflect.Uint32:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = uint32(x)
|
|
||||||
case reflect.Uint16:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = uint16(x)
|
|
||||||
case reflect.Uint8:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
pk[0] = uint8(x)
|
|
||||||
case reflect.String:
|
|
||||||
pk[0] = string(data)
|
|
||||||
default:
|
|
||||||
panic("unsupported primary key type cascade")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isPKZero(pk) {
|
if !isPKZero(pk) {
|
||||||
|
@ -2944,82 +2813,11 @@ func (session *Session) bytes2Value(col *core.Column, fieldValue *reflect.Value,
|
||||||
panic("unsupported composited primary key cascade")
|
panic("unsupported composited primary key cascade")
|
||||||
}
|
}
|
||||||
var pk = make(core.PK, len(table.PrimaryKeys))
|
var pk = make(core.PK, len(table.PrimaryKeys))
|
||||||
|
var err error
|
||||||
rawValueType := table.ColumnType(table.PKColumns()[0].FieldName)
|
rawValueType := table.ColumnType(table.PKColumns()[0].FieldName)
|
||||||
switch rawValueType.Kind() {
|
pk[0], err = str2PK(string(data), rawValueType)
|
||||||
case reflect.Int64:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
return err
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = x
|
|
||||||
case reflect.Int:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = int(x)
|
|
||||||
case reflect.Int32:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = int32(x)
|
|
||||||
case reflect.Int16:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = int16(x)
|
|
||||||
case reflect.Int8:
|
|
||||||
x, err := strconv.ParseInt(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = x
|
|
||||||
case reflect.Uint64:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = x
|
|
||||||
case reflect.Uint:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = uint(x)
|
|
||||||
case reflect.Uint32:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = uint32(x)
|
|
||||||
case reflect.Uint16:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = uint16(x)
|
|
||||||
case reflect.Uint8:
|
|
||||||
x, err := strconv.ParseUint(string(data), 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("arg %v as int: %s", key, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
pk[0] = uint8(x)
|
|
||||||
case reflect.String:
|
|
||||||
pk[0] = string(data)
|
|
||||||
default:
|
|
||||||
panic("unsupported primary key type cascade")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !isPKZero(pk) {
|
if !isPKZero(pk) {
|
||||||
|
|
69
statement.go
69
statement.go
|
@ -82,7 +82,7 @@ type Statement struct {
|
||||||
exprColumns map[string]exprParam
|
exprColumns map[string]exprParam
|
||||||
}
|
}
|
||||||
|
|
||||||
// init
|
// Init reset all the statment's fields
|
||||||
func (statement *Statement) Init() {
|
func (statement *Statement) Init() {
|
||||||
statement.RefTable = nil
|
statement.RefTable = nil
|
||||||
statement.Start = 0
|
statement.Start = 0
|
||||||
|
@ -207,7 +207,7 @@ func buildUpdates(engine *Engine, table *core.Table, bean interface{},
|
||||||
mustColumnMap map[string]bool, nullableMap map[string]bool,
|
mustColumnMap map[string]bool, nullableMap map[string]bool,
|
||||||
columnMap map[string]bool, update, unscoped bool) ([]string, []interface{}) {
|
columnMap map[string]bool, update, unscoped bool) ([]string, []interface{}) {
|
||||||
|
|
||||||
colNames := make([]string, 0)
|
var colNames = make([]string, 0)
|
||||||
var args = make([]interface{}, 0)
|
var args = make([]interface{}, 0)
|
||||||
for _, col := range table.Columns() {
|
for _, col := range table.Columns() {
|
||||||
if !includeVersion && col.IsVersion {
|
if !includeVersion && col.IsVersion {
|
||||||
|
@ -667,7 +667,7 @@ func buildConditions(engine *Engine, table *core.Table, bean interface{},
|
||||||
return colNames, args
|
return colNames, args
|
||||||
}
|
}
|
||||||
|
|
||||||
// return current tableName
|
// TableName return current tableName
|
||||||
func (statement *Statement) TableName() string {
|
func (statement *Statement) TableName() string {
|
||||||
if statement.AltTableName != "" {
|
if statement.AltTableName != "" {
|
||||||
return statement.AltTableName
|
return statement.AltTableName
|
||||||
|
@ -683,11 +683,6 @@ func (statement *Statement) TableName() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
|
||||||
ptrPkType = reflect.TypeOf(&core.PK{})
|
|
||||||
pkType = reflect.TypeOf(core.PK{})
|
|
||||||
)
|
|
||||||
|
|
||||||
// Id generate "where id = ? " statment or for composite key "where key1 = ? and key2 = ?"
|
// Id generate "where id = ? " statment or for composite key "where key1 = ? and key2 = ?"
|
||||||
func (statement *Statement) Id(id interface{}) *Statement {
|
func (statement *Statement) Id(id interface{}) *Statement {
|
||||||
idValue := reflect.ValueOf(id)
|
idValue := reflect.ValueOf(id)
|
||||||
|
@ -760,7 +755,7 @@ func (statement *Statement) getExpr() map[string]exprParam {
|
||||||
return statement.exprColumns
|
return statement.exprColumns
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Where column IN (?) " statment
|
// In generate "Where column IN (?) " statment
|
||||||
func (statement *Statement) In(column string, args ...interface{}) *Statement {
|
func (statement *Statement) In(column string, args ...interface{}) *Statement {
|
||||||
length := len(args)
|
length := len(args)
|
||||||
if length == 0 {
|
if length == 0 {
|
||||||
|
@ -794,7 +789,7 @@ func (statement *Statement) genInSql() (string, []interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
inStrs := make([]string, len(statement.inColumns), len(statement.inColumns))
|
inStrs := make([]string, len(statement.inColumns), len(statement.inColumns))
|
||||||
args := make([]interface{}, 0)
|
args := make([]interface{}, 0, len(statement.inColumns))
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
var i int
|
var i int
|
||||||
for _, params := range statement.inColumns {
|
for _, params := range statement.inColumns {
|
||||||
|
@ -824,19 +819,6 @@ func (statement *Statement) attachInSql() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func col2NewCols(columns ...string) []string {
|
|
||||||
newColumns := make([]string, 0)
|
|
||||||
for _, col := range columns {
|
|
||||||
col = strings.Replace(col, "`", "", -1)
|
|
||||||
col = strings.Replace(col, `"`, "", -1)
|
|
||||||
ccols := strings.Split(col, ",")
|
|
||||||
for _, c := range ccols {
|
|
||||||
newColumns = append(newColumns, strings.TrimSpace(c))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newColumns
|
|
||||||
}
|
|
||||||
|
|
||||||
func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
|
func (statement *Statement) col2NewColsWithQuote(columns ...string) []string {
|
||||||
newColumns := make([]string, 0)
|
newColumns := make([]string, 0)
|
||||||
for _, col := range columns {
|
for _, col := range columns {
|
||||||
|
@ -871,13 +853,13 @@ func (statement *Statement) ForUpdate() *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// replace select
|
// Select replace select
|
||||||
func (s *Statement) Select(str string) *Statement {
|
func (s *Statement) Select(str string) *Statement {
|
||||||
s.selectStr = str
|
s.selectStr = str
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "col1, col2" statement
|
// Cols generate "col1, col2" statement
|
||||||
func (statement *Statement) Cols(columns ...string) *Statement {
|
func (statement *Statement) Cols(columns ...string) *Statement {
|
||||||
cols := col2NewCols(columns...)
|
cols := col2NewCols(columns...)
|
||||||
for _, nc := range cols {
|
for _, nc := range cols {
|
||||||
|
@ -891,13 +873,13 @@ func (statement *Statement) Cols(columns ...string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update use only: update all columns
|
// AllCols update use only: update all columns
|
||||||
func (statement *Statement) AllCols() *Statement {
|
func (statement *Statement) AllCols() *Statement {
|
||||||
statement.useAllCols = true
|
statement.useAllCols = true
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update use only: must update columns
|
// MustCols update use only: must update columns
|
||||||
func (statement *Statement) MustCols(columns ...string) *Statement {
|
func (statement *Statement) MustCols(columns ...string) *Statement {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
for _, nc := range newColumns {
|
||||||
|
@ -906,7 +888,7 @@ func (statement *Statement) MustCols(columns ...string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// indicates that use bool fields as update contents and query contiditions
|
// UseBool indicates that use bool fields as update contents and query contiditions
|
||||||
func (statement *Statement) UseBool(columns ...string) *Statement {
|
func (statement *Statement) UseBool(columns ...string) *Statement {
|
||||||
if len(columns) > 0 {
|
if len(columns) > 0 {
|
||||||
statement.MustCols(columns...)
|
statement.MustCols(columns...)
|
||||||
|
@ -916,7 +898,7 @@ func (statement *Statement) UseBool(columns ...string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// do not use the columns
|
// Omit do not use the columns
|
||||||
func (statement *Statement) Omit(columns ...string) {
|
func (statement *Statement) Omit(columns ...string) {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
for _, nc := range newColumns {
|
||||||
|
@ -925,7 +907,7 @@ func (statement *Statement) Omit(columns ...string) {
|
||||||
statement.OmitStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
|
statement.OmitStr = statement.Engine.Quote(strings.Join(newColumns, statement.Engine.Quote(", ")))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update use only: update columns to null when value is nullable and zero-value
|
// Nullable Update use only: update columns to null when value is nullable and zero-value
|
||||||
func (statement *Statement) Nullable(columns ...string) {
|
func (statement *Statement) Nullable(columns ...string) {
|
||||||
newColumns := col2NewCols(columns...)
|
newColumns := col2NewCols(columns...)
|
||||||
for _, nc := range newColumns {
|
for _, nc := range newColumns {
|
||||||
|
@ -933,13 +915,13 @@ func (statement *Statement) Nullable(columns ...string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate LIMIT limit statement
|
// Top generate LIMIT limit statement
|
||||||
func (statement *Statement) Top(limit int) *Statement {
|
func (statement *Statement) Top(limit int) *Statement {
|
||||||
statement.Limit(limit)
|
statement.Limit(limit)
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate LIMIT start, limit statement
|
// Limit generate LIMIT start, limit statement
|
||||||
func (statement *Statement) Limit(limit int, start ...int) *Statement {
|
func (statement *Statement) Limit(limit int, start ...int) *Statement {
|
||||||
statement.LimitN = limit
|
statement.LimitN = limit
|
||||||
if len(start) > 0 {
|
if len(start) > 0 {
|
||||||
|
@ -948,7 +930,7 @@ func (statement *Statement) Limit(limit int, start ...int) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Order By order" statement
|
// OrderBy generate "Order By order" statement
|
||||||
func (statement *Statement) OrderBy(order string) *Statement {
|
func (statement *Statement) OrderBy(order string) *Statement {
|
||||||
if len(statement.OrderStr) > 0 {
|
if len(statement.OrderStr) > 0 {
|
||||||
statement.OrderStr += ", "
|
statement.OrderStr += ", "
|
||||||
|
@ -957,6 +939,7 @@ func (statement *Statement) OrderBy(order string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Desc generate `ORDER BY xx DESC`
|
||||||
func (statement *Statement) Desc(colNames ...string) *Statement {
|
func (statement *Statement) Desc(colNames ...string) *Statement {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
fmt.Fprintf(&buf, statement.OrderStr)
|
fmt.Fprintf(&buf, statement.OrderStr)
|
||||||
|
@ -969,7 +952,7 @@ func (statement *Statement) Desc(colNames ...string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Method Asc provide asc order by query condition, the input parameters are columns.
|
// Asc provide asc order by query condition, the input parameters are columns.
|
||||||
func (statement *Statement) Asc(colNames ...string) *Statement {
|
func (statement *Statement) Asc(colNames ...string) *Statement {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
fmt.Fprintf(&buf, statement.OrderStr)
|
fmt.Fprintf(&buf, statement.OrderStr)
|
||||||
|
@ -982,13 +965,13 @@ func (statement *Statement) Asc(colNames ...string) *Statement {
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
//The join_operator should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
// Join The joinOP should be one of INNER, LEFT OUTER, CROSS etc - this will be prepended to JOIN
|
||||||
func (statement *Statement) Join(join_operator string, tablename interface{}, condition string, args ...interface{}) *Statement {
|
func (statement *Statement) Join(joinOP string, tablename interface{}, condition string, args ...interface{}) *Statement {
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
if len(statement.JoinStr) > 0 {
|
if len(statement.JoinStr) > 0 {
|
||||||
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, join_operator)
|
fmt.Fprintf(&buf, "%v %v JOIN ", statement.JoinStr, joinOP)
|
||||||
} else {
|
} else {
|
||||||
fmt.Fprintf(&buf, "%v JOIN ", join_operator)
|
fmt.Fprintf(&buf, "%v JOIN ", joinOP)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch tablename.(type) {
|
switch tablename.(type) {
|
||||||
|
@ -1030,19 +1013,19 @@ func (statement *Statement) Join(join_operator string, tablename interface{}, co
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Group By keys" statement
|
// GroupBy generate "Group By keys" statement
|
||||||
func (statement *Statement) GroupBy(keys string) *Statement {
|
func (statement *Statement) GroupBy(keys string) *Statement {
|
||||||
statement.GroupByStr = keys
|
statement.GroupByStr = keys
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate "Having conditions" statement
|
// Having generate "Having conditions" statement
|
||||||
func (statement *Statement) Having(conditions string) *Statement {
|
func (statement *Statement) Having(conditions string) *Statement {
|
||||||
statement.HavingStr = fmt.Sprintf("HAVING %v", conditions)
|
statement.HavingStr = fmt.Sprintf("HAVING %v", conditions)
|
||||||
return statement
|
return statement
|
||||||
}
|
}
|
||||||
|
|
||||||
// Always disable struct tag "deleted"
|
// Unscoped always disable struct tag "deleted"
|
||||||
func (statement *Statement) Unscoped() *Statement {
|
func (statement *Statement) Unscoped() *Statement {
|
||||||
statement.unscoped = true
|
statement.unscoped = true
|
||||||
return statement
|
return statement
|
||||||
|
@ -1091,10 +1074,6 @@ func (statement *Statement) genCreateTableSQL() string {
|
||||||
statement.StoreEngine, statement.Charset)
|
statement.StoreEngine, statement.Charset)
|
||||||
}
|
}
|
||||||
|
|
||||||
func indexName(tableName, idxName string) string {
|
|
||||||
return fmt.Sprintf("IDX_%v_%v", tableName, idxName)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *Statement) genIndexSQL() []string {
|
func (s *Statement) genIndexSQL() []string {
|
||||||
var sqls []string = make([]string, 0)
|
var sqls []string = make([]string, 0)
|
||||||
tbName := s.TableName()
|
tbName := s.TableName()
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
package xorm
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/go-xorm/core"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ptrPkType = reflect.TypeOf(&core.PK{})
|
||||||
|
pkType = reflect.TypeOf(core.PK{})
|
||||||
|
)
|
Loading…
Reference in New Issue