This commit is contained in:
Edison.Hsu 2017-02-10 02:27:08 +00:00 committed by GitHub
commit 925e411904
3 changed files with 1174 additions and 0 deletions

View File

@ -1426,6 +1426,20 @@ func (engine *Engine) Query(sql string, paramStr ...interface{}) (resultsSlice [
return session.Query(sql, paramStr...) return session.Query(sql, paramStr...)
} }
// Query a raw sql and return records as []map[string]string
func (engine *Engine) QueryMapString(sql string, paramStr ...interface{}) (resultsSlice []map[string]string, err error) {
session := engine.NewSession()
defer session.Close()
return session.Query2(sql, paramStr...)
}
// Query a raw sql and return records as []map[string]interface{}
func (engine *Engine) QueryMapInterface(sql string, paramStr ...interface{}) (resultsSlice []map[string]interface{}, err error) {
session := engine.NewSession()
defer session.Close()
return session.QueryInterfaces(sql, paramStr...)
}
// Insert one or more records // Insert one or more records
func (engine *Engine) Insert(beans ...interface{}) (int64, error) { func (engine *Engine) Insert(beans ...interface{}) (int64, error) {
session := engine.NewSession() session := engine.NewSession()

View File

@ -279,6 +279,50 @@ func sliceEq(left, right []string) bool {
return true return true
} }
func reflect2truevalue(rawValue *reflect.Value) (str 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:
str = vv.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
str = vv.Uint()
case reflect.Float32, reflect.Float64:
str = vv.Float()
case reflect.String:
str = vv.String()
case reflect.Array, reflect.Slice:
switch aa.Elem().Kind() {
case reflect.Uint8:
data := rawValue.Interface().([]byte)
str = string(data)
default:
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
}
// time type
case reflect.Struct:
if aa.ConvertibleTo(core.TimeType) {
str = vv.Convert(core.TimeType).Interface().(time.Time).Format(time.RFC3339Nano)
} else {
err = fmt.Errorf("Unsupported struct type %v", vv.Type().Name())
}
case reflect.Bool:
str = vv.Bool()
case reflect.Complex128, reflect.Complex64:
str = fmt.Sprintf("%v", 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 reflect2value(rawValue *reflect.Value) (str string, err error) { func reflect2value(rawValue *reflect.Value) (str string, err error) {
aa := reflect.TypeOf((*rawValue).Interface()) aa := reflect.TypeOf((*rawValue).Interface())
vv := reflect.ValueOf((*rawValue).Interface()) vv := reflect.ValueOf((*rawValue).Interface())
@ -373,6 +417,22 @@ func rows2maps(rows *core.Rows) (resultsSlice []map[string][]byte, err error) {
return resultsSlice, nil return resultsSlice, nil
} }
func rows2interfaces(rows *core.Rows) (resultsSlice []map[string]interface{}, err error) {
fields, err := rows.Columns()
if err != nil {
return nil, err
}
for rows.Next() {
result, err := row2mapInterface(rows, fields)
if err != nil {
return nil, err
}
resultsSlice = append(resultsSlice, result)
}
return resultsSlice, nil
}
func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) { func row2map(rows *core.Rows, fields []string) (resultsMap map[string][]byte, err error) {
result := make(map[string][]byte) result := make(map[string][]byte)
scanResultContainers := make([]interface{}, len(fields)) scanResultContainers := make([]interface{}, len(fields))
@ -429,6 +489,35 @@ func row2mapStr(rows *core.Rows, fields []string) (resultsMap map[string]string,
return result, nil return result, nil
} }
func row2mapInterface(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 {
//fmt.Println("ignore ...", key, rawValue)
continue
}
if data, err := reflect2truevalue(&rawValue); err == nil {
result[key] = data
} else {
return nil, err // !nashtsai! REVIEW, should return err or just error log?
}
}
return result, nil
}
func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) { func txQuery2(tx *core.Tx, sqlStr string, params ...interface{}) (resultsSlice []map[string]string, err error) {
rows, err := tx.Query(sqlStr, params...) rows, err := tx.Query(sqlStr, params...)
if err != nil { if err != nil {

1071
session.go

File diff suppressed because it is too large Load Diff