123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- package xorm
- import (
- "fmt"
- "reflect"
- "time"
- "github.com/xormplus/core"
- )
- func reflect2objectWithDateFormat(rawValue *reflect.Value, dateFormat string) (value interface{}, err error) {
- aa := reflect.TypeOf((*rawValue).Interface())
- vv := reflect.ValueOf((*rawValue).Interface())
- switch aa.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- value = vv.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- value = vv.Uint()
- case reflect.Float32, reflect.Float64:
- value = vv.Float()
- case reflect.String:
- value = vv.String()
- case reflect.Array, reflect.Slice:
- switch aa.Elem().Kind() {
- case reflect.Uint8:
- data := rawValue.Interface().([]byte)
- value = string(data)
- default:
- err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
- }
- // time type
- case reflect.Struct:
- if aa.ConvertibleTo(core.TimeType) {
- value = vv.Convert(core.TimeType).Interface().(time.Time).Format(dateFormat)
- } else {
- err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
- }
- case reflect.Bool:
- value = vv.Bool()
- case reflect.Complex128, reflect.Complex64:
- value = vv.Complex()
- /* 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 value2ObjectWithDateFormat(rawValue *reflect.Value, dateFormat string) (data interface{}, err error) {
- data, err = reflect2objectWithDateFormat(rawValue, dateFormat)
- if err != nil {
- return
- }
- return
- }
- func rows2mapObjectsWithDateFormat(rows *core.Rows, dateFormat string) (resultsSlice []map[string]interface{}, err error) {
- fields, err := rows.Columns()
- if err != nil {
- return nil, err
- }
- for rows.Next() {
- result, err := rows2mapObjectWithDateFormat(rows, dateFormat, fields)
- if err != nil {
- return nil, err
- }
- resultsSlice = append(resultsSlice, result)
- }
- return resultsSlice, nil
- }
- func rows2mapObjectWithDateFormat(rows *core.Rows, dateFormat string, fields []string) (resultsMap map[string]interface{}, err error) {
- result := make(map[string]interface{})
- scanResultContainers := make([]interface{}, len(fields))
- for i := 0; i < len(fields); i++ {
- var scanResultContainer interface{}
- scanResultContainers[i] = &scanResultContainer
- }
- if err := rows.Scan(scanResultContainers...); err != nil {
- return nil, err
- }
- for ii, key := range fields {
- rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
- //if row is null then ignore
- if rawValue.Interface() == nil {
- continue
- }
- if data, err := value2ObjectWithDateFormat(&rawValue, dateFormat); err == nil {
- result[key] = data
- } else {
- return nil, err // !nashtsai! REVIEW, should return err or just error log?
- }
- }
- return result, nil
- }
- func txQueryByMap(tx *core.Tx, sqlStr string, params interface{}) (resultsSlice []map[string]interface{}, err error) {
- rows, err := tx.QueryMap(sqlStr, params)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- return rows2mapObjects(rows)
- }
- func txQuery3WithDateFormat(tx *core.Tx, dateFormat string, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
- rows, err := tx.Query(sqlStr, params...)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- return rows2mapObjectsWithDateFormat(rows, dateFormat)
- }
- func queryByMap(db *core.DB, sqlStr string, params interface{}) (resultsSlice []map[string]interface{}, err error) {
- s, err := db.Prepare(sqlStr)
- if err != nil {
- return nil, err
- }
- defer s.Close()
- rows, err := s.QueryMap(params)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- return rows2mapObjects(rows)
- }
- func query3WithDateFormat(db *core.DB, dateFormat string, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
- s, err := db.Prepare(sqlStr)
- if err != nil {
- return nil, err
- }
- defer s.Close()
- rows, err := s.Query(params...)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- return rows2mapObjectsWithDateFormat(rows, dateFormat)
- }
- func reflect2object(rawValue *reflect.Value) (value interface{}, err error) {
- aa := reflect.TypeOf((*rawValue).Interface())
- vv := reflect.ValueOf((*rawValue).Interface())
- switch aa.Kind() {
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- value = vv.Int()
- case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
- value = vv.Uint()
- case reflect.Float32, reflect.Float64:
- value = vv.Float()
- case reflect.String:
- value = vv.String()
- case reflect.Array, reflect.Slice:
- switch aa.Elem().Kind() {
- case reflect.Uint8:
- data := rawValue.Interface().([]byte)
- value = string(data)
- default:
- err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
- }
- // time type
- case reflect.Struct:
- if aa.ConvertibleTo(core.TimeType) {
- value = vv.Convert(core.TimeType).Interface().(time.Time)
- } else {
- err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
- }
- case reflect.Bool:
- value = vv.Bool()
- case reflect.Complex128, reflect.Complex64:
- value = vv.Complex()
- /* 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 value2Object(rawValue *reflect.Value) (data interface{}, err error) {
- data, err = reflect2object(rawValue)
- if err != nil {
- return
- }
- return
- }
- func rows2mapObjects(rows *core.Rows) (resultsSlice []map[string]interface{}, err error) {
- fields, err := rows.Columns()
- if err != nil {
- return nil, err
- }
- for rows.Next() {
- result, err := rows2mapObject(rows, fields)
- if err != nil {
- return nil, err
- }
- resultsSlice = append(resultsSlice, result)
- }
- return resultsSlice, nil
- }
- func rows2mapObject(rows *core.Rows, fields []string) (resultsMap map[string]interface{}, err error) {
- result := make(map[string]interface{})
- scanResultContainers := make([]interface{}, len(fields))
- for i := 0; i < len(fields); i++ {
- var scanResultContainer interface{}
- scanResultContainers[i] = &scanResultContainer
- }
- if err := rows.Scan(scanResultContainers...); err != nil {
- return nil, err
- }
- for ii, key := range fields {
- rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
- //if row is null then ignore
- if rawValue.Interface() == nil {
- continue
- }
- if data, err := value2Object(&rawValue); err == nil {
- result[key] = data
- } else {
- return nil, err // !nashtsai! REVIEW, should return err or just error log?
- }
- }
- return result, nil
- }
- func txQuery3(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
- rows, err := tx.Query(sqlStr, params...)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- return rows2mapObjects(rows)
- }
- func query3(db *core.DB, sqlStr string, params ...interface{}) (resultsSlice []map[string]interface{}, err error) {
- s, err := db.Prepare(sqlStr)
- if err != nil {
- return nil, err
- }
- defer s.Close()
- rows, err := s.Query(params...)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- return rows2mapObjects(rows)
- }
|