1 Star 0 Fork 5

金鳞 / xorm

forked from lunny / xorm 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
helpers.go 2.99 KB
一键复制 编辑 原始数据 按行查看 历史
NashTsai 提交于 2013-12-27 02:14 . add Session.row2Bean
package xorm
import (
"database/sql"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
func indexNoCase(s, sep string) int {
return strings.Index(strings.ToLower(s), strings.ToLower(sep))
}
func splitNoCase(s, sep string) []string {
idx := indexNoCase(s, sep)
if idx < 0 {
return []string{s}
}
return strings.Split(s, s[idx:idx+len(sep)])
}
func splitNNoCase(s, sep string, n int) []string {
idx := indexNoCase(s, sep)
if idx < 0 {
return []string{s}
}
return strings.SplitN(s, s[idx:idx+len(sep)], n)
}
func makeArray(elem string, count int) []string {
res := make([]string, count)
for i := 0; i < count; i++ {
res[i] = elem
}
return res
}
func rType(bean interface{}) reflect.Type {
sliceValue := reflect.Indirect(reflect.ValueOf(bean))
return reflect.TypeOf(sliceValue.Interface())
}
func structName(v reflect.Type) string {
for v.Kind() == reflect.Ptr {
v = v.Elem()
}
return v.Name()
}
func sliceEq(left, right []string) bool {
for _, l := range left {
var find bool
for _, r := range right {
if l == r {
find = true
break
}
}
if !find {
return false
}
}
return true
}
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:
if aa == reflect.TypeOf(c_TIME_DEFAULT) {
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
}
func rows2maps(rows *sql.Rows) (resultsSlice []map[string][]byte, err error) {
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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/tyt/xorm.git
git@gitee.com:tyt/xorm.git
tyt
xorm
xorm
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891