Skip to content

Commit

Permalink
add NullType
Browse files Browse the repository at this point in the history
  • Loading branch information
xormplus committed Jun 23, 2018
1 parent d12a513 commit 00e9bc4
Show file tree
Hide file tree
Showing 5 changed files with 648 additions and 72 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap1)
var valuesMap2 = make(map[string]interface{})
has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap2)

var valuesMap3 = make(map[string]xorm.Value)
has, err := db.Sql("select * from article where id=?", 2).Get(&valuesMap3)

//获得单条数据某个字段的值
var title string
has, err := db.Sql("select title from article where id=?", 2).Get(&title)
Expand Down
17 changes: 17 additions & 0 deletions session_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ func (session *Session) nocacheGet(beanKind reflect.Kind, table *core.Table, bea
return true, rows.Scan(&bean)
case *sql.NullInt64, *sql.NullBool, *sql.NullFloat64, *sql.NullString:
return true, rows.Scan(bean)
case *map[string]Value:
vv := reflect.ValueOf(bean)
if vv.Kind() != reflect.Ptr || vv.Elem().Kind() != reflect.Map {
return true, errors.New("dest should be a map's pointer")
}
vvv := vv.Elem()
tbean := make(map[string][]byte)
err = rows.ScanMap(&tbean)
if err != nil {
return true, err
}

for k, v := range tbean {
vvv.SetMapIndex(reflect.ValueOf(k), reflect.ValueOf(Value(v)))
}

return true, nil
}

switch beanKind {
Expand Down
70 changes: 0 additions & 70 deletions session_plus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"reflect"
"regexp"
"strings"
"time"

"github.com/Chronokeeper/anyxml"
"github.com/xormplus/core"
Expand Down Expand Up @@ -102,75 +101,6 @@ func (resultBean *ResultBean) XmlIndent(prefix string, indent string, recordTag
return resultBean.Has, string(resultByte), err
}

type Value []byte

func (v Value) Bytes() []byte {
return []byte(v)
}

func (v Value) String() string {
return string(v)
}

func (v Value) Bool() bool {
return Bool(v)
}

func (v Value) Int() int {
return Int(v)
}

func (v Value) Int8() int8 {
return Int8(v)
}

func (v Value) Int16() int16 {
return Int16(v)
}

func (v Value) Int32() int32 {
return Int32(v)
}

func (v Value) Int64() int64 {
return Int64(v)
}

func (v Value) Uint() uint {
return Uint(v)
}

func (v Value) Uint8() uint8 {
return Uint8(v)
}

func (v Value) Uint16() uint16 {
return Uint16(v)
}

func (v Value) Uint32() uint32 {
return Uint32(v)
}

func (v Value) Uint64() uint64 {
return Uint64(v)
}

func (v Value) Float32() float32 {
return Float32(v)
}

func (v Value) Float64() float64 {
return Float64(v)
}

func (v Value) Time(format string, TZLocation ...*time.Location) time.Time {
return Time(v, format, TZLocation...)
}
func (v Value) TimeDuration() time.Duration {
return TimeDuration(v)
}

type ResultMap struct {
Results []map[string]interface{}
Error error
Expand Down
3 changes: 1 addition & 2 deletions session_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,8 @@ func row2mapValue(rows *core.Rows, fields []string) (resultsMap map[string]Value

for ii, key := range fields {
rawValue := reflect.Indirect(reflect.ValueOf(scanResultContainers[ii]))
//if row is null then ignore
if rawValue.Interface() == nil {
result[key] = []byte{}
result[key] = nil
continue
}

Expand Down
Loading

0 comments on commit 00e9bc4

Please sign in to comment.