2013-09-26 07:19:39 +00:00
|
|
|
package xorm
|
|
|
|
|
|
|
|
import (
|
2013-12-26 18:14:30 +00:00
|
|
|
"fmt"
|
2013-12-18 03:31:32 +00:00
|
|
|
"reflect"
|
2013-12-26 18:14:30 +00:00
|
|
|
"strconv"
|
2013-12-18 03:31:32 +00:00
|
|
|
"strings"
|
2013-12-26 18:14:30 +00:00
|
|
|
"time"
|
2014-02-11 06:16:14 +00:00
|
|
|
|
|
|
|
"github.com/lunny/xorm/core"
|
2013-09-26 07:19:39 +00:00
|
|
|
)
|
|
|
|
|
2013-09-30 01:17:35 +00:00
|
|
|
func indexNoCase(s, sep string) int {
|
2013-12-18 03:31:32 +00:00
|
|
|
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
|
2013-09-26 07:19:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 01:17:35 +00:00
|
|
|
func splitNoCase(s, sep string) []string {
|
2013-12-18 03:31:32 +00:00
|
|
|
idx := indexNoCase(s, sep)
|
|
|
|
if idx < 0 {
|
|
|
|
return []string{s}
|
|
|
|
}
|
|
|
|
return strings.Split(s, s[idx:idx+len(sep)])
|
2013-09-26 07:19:39 +00:00
|
|
|
}
|
|
|
|
|
2013-09-30 01:17:35 +00:00
|
|
|
func splitNNoCase(s, sep string, n int) []string {
|
2013-12-18 03:31:32 +00:00
|
|
|
idx := indexNoCase(s, sep)
|
|
|
|
if idx < 0 {
|
|
|
|
return []string{s}
|
|
|
|
}
|
|
|
|
return strings.SplitN(s, s[idx:idx+len(sep)], n)
|
2013-09-26 07:19:39 +00:00
|
|
|
}
|
2013-09-30 01:17:35 +00:00
|
|
|
|
|
|
|
func makeArray(elem string, count int) []string {
|
2013-12-18 03:31:32 +00:00
|
|
|
res := make([]string, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
res[i] = elem
|
|
|
|
}
|
|
|
|
return res
|
2013-09-30 01:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func rType(bean interface{}) reflect.Type {
|
2013-12-18 03:31:32 +00:00
|
|
|
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
|
|
|
|
return reflect.TypeOf(sliceValue.Interface())
|
2013-09-30 01:17:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func structName(v reflect.Type) string {
|
2013-12-18 03:31:32 +00:00
|
|
|
for v.Kind() == reflect.Ptr {
|
|
|
|
v = v.Elem()
|
|
|
|
}
|
|
|
|
return v.Name()
|
2013-09-30 01:17:35 +00:00
|
|
|
}
|
2013-11-22 06:11:07 +00:00
|
|
|
|
|
|
|
func sliceEq(left, right []string) bool {
|
2013-12-18 03:31:32 +00:00
|
|
|
for _, l := range left {
|
|
|
|
var find bool
|
|
|
|
for _, r := range right {
|
|
|
|
if l == r {
|
|
|
|
find = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !find {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2013-11-22 06:11:07 +00:00
|
|
|
}
|
2013-12-26 18:14:30 +00:00
|
|
|
|
|
|
|
func value2Bytes(rawValue *reflect.Value) (data []byte, err error) {
|
|
|
|
|
|
|
|
aa := reflect.TypeOf((*rawValue).Interface())
|
|
|
|
vv := reflect.ValueOf((*rawValue).Interface())
|
|
|
|
|
|
|
|
var str string
|
|
|
|
switch aa.Kind() {
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
str = strconv.FormatInt(vv.Int(), 10)
|
|
|
|
data = []byte(str)
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
str = strconv.FormatUint(vv.Uint(), 10)
|
|
|
|
data = []byte(str)
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
str = strconv.FormatFloat(vv.Float(), 'f', -1, 64)
|
|
|
|
data = []byte(str)
|
|
|
|
case reflect.String:
|
|
|
|
str = vv.String()
|
|
|
|
data = []byte(str)
|
|
|
|
case reflect.Array, reflect.Slice:
|
|
|
|
switch aa.Elem().Kind() {
|
|
|
|
case reflect.Uint8:
|
|
|
|
data = rawValue.Interface().([]byte)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
|
|
|
}
|
|
|
|
//时间类型
|
|
|
|
case reflect.Struct:
|
2014-02-11 06:16:14 +00:00
|
|
|
if aa == core.TimeType {
|
2013-12-26 18:14:30 +00:00
|
|
|
str = rawValue.Interface().(time.Time).Format(time.RFC3339Nano)
|
|
|
|
data = []byte(str)
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
|
|
|
}
|
|
|
|
case reflect.Bool:
|
|
|
|
str = strconv.FormatBool(vv.Bool())
|
|
|
|
data = []byte(str)
|
|
|
|
case reflect.Complex128, reflect.Complex64:
|
|
|
|
str = fmt.Sprintf("%v", vv.Complex())
|
|
|
|
data = []byte(str)
|
|
|
|
/* TODO: unsupported types below
|
|
|
|
case reflect.Map:
|
|
|
|
case reflect.Ptr:
|
|
|
|
case reflect.Uintptr:
|
|
|
|
case reflect.UnsafePointer:
|
|
|
|
case reflect.Chan, reflect.Func, reflect.Interface:
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-11 06:59:04 +00:00
|
|
|
func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
|
2013-12-26 18:14:30 +00:00
|
|
|
fields, err := rows.Columns()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for rows.Next() {
|
|
|
|
result, err := row2map(rows, fields)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultsSlice = append(resultsSlice, result)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resultsSlice, nil
|
|
|
|
}
|