Skip to content

Commit 8f4bd6d

Browse files
committed
modify value&pluck return type
1 parent 2365cb6 commit 8f4bd6d

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

orm_query.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,46 +109,46 @@ func (dba *Orm) _unionBuild(union, field string) (interface{}, error) {
109109
}
110110

111111
// Get : select more rows , relation limit set
112-
func (dba *Orm) Value(field string) (v t.T, err error) {
112+
func (dba *Orm) Value(field string) (v interface{}, err error) {
113113
dba.Limit(1)
114114
err = dba.Select()
115115
if err != nil {
116116
return
117117
}
118-
//var binder = dba.GetISession().GetIBinder()
119-
//switch binder.GetBindType() {
120-
//case OBJECT_MAP, OBJECT_MAP_SLICE, OBJECT_MAP_SLICE_T, OBJECT_MAP_T:
121-
// v = t.New(binder.GetBindResult().MapIndex(reflect.ValueOf(field)).Interface())
122-
//case OBJECT_STRUCT, OBJECT_STRUCT_SLICE:
123-
// bindResult := reflect.Indirect(binder.GetBindResult())
124-
// v = dba._valueFromStruct(bindResult, field)
125-
//}
118+
var binder = dba.GetISession().GetIBinder()
119+
switch binder.GetBindType() {
120+
case OBJECT_MAP, OBJECT_MAP_SLICE, OBJECT_MAP_SLICE_T, OBJECT_MAP_T:
121+
v = reflect.ValueOf(binder.GetBindResult()).MapIndex(reflect.ValueOf(field)).Interface()
122+
case OBJECT_STRUCT, OBJECT_STRUCT_SLICE:
123+
bindResult := reflect.Indirect(reflect.ValueOf(binder.GetBindResult()))
124+
v = dba._valueFromStruct(bindResult, field)
125+
}
126126
return
127127
}
128-
func (dba *Orm) _valueFromStruct(bindResult reflect.Value, field string) (v t.T) {
128+
func (dba *Orm) _valueFromStruct(bindResult reflect.Value, field string) (v interface{}) {
129129
ostype := bindResult.Type()
130130
for i := 0; i < ostype.NumField(); i++ {
131131
tag := ostype.Field(i).Tag.Get(TAGNAME)
132132
if tag == field || ostype.Field(i).Name == field {
133-
v = t.New(bindResult.FieldByName(ostype.Field(i).Name))
133+
v = bindResult.FieldByName(ostype.Field(i).Name).Interface()
134134
}
135135
}
136136
return
137137
}
138138

139139
// Pluck 获取一列数据, 第二个字段可以指定另一个字段的值作为这一列数据的key
140-
func (dba *Orm) Pluck(field string, fieldKey ...string) (v t.T, err error) {
140+
func (dba *Orm) Pluck(field string, fieldKey ...string) (v interface{}, err error) {
141141
err = dba.Select()
142142
if err != nil {
143143
return
144144
}
145145
var binder = dba.GetISession().GetIBinder()
146-
var resMap = make(t.MapStringInterface, 0)
147-
var resSlice = t.Slice{}
146+
var resMap = make(map[interface{}]interface{}, 0)
147+
var resSlice = make([]interface{}, 0)
148148

149149
switch binder.GetBindType() {
150150
case OBJECT_MAP, OBJECT_MAP_T, OBJECT_STRUCT: // row
151-
var key, val t.T
151+
var key, val interface{}
152152
if len(fieldKey) > 0 {
153153
key, err = dba.Value(fieldKey[0])
154154
if err != nil {
@@ -158,21 +158,20 @@ func (dba *Orm) Pluck(field string, fieldKey ...string) (v t.T, err error) {
158158
if err != nil {
159159
return
160160
}
161-
v = t.New(t.Map{key: val})
161+
resMap[key] = val
162162
} else {
163163
v, err = dba.Value(field)
164164
if err != nil {
165165
return
166166
}
167167
}
168-
return
169168
case OBJECT_MAP_SLICE, OBJECT_MAP_SLICE_T:
170169
for _, item := range t.New(binder.GetBindResultSlice().Interface()).Slice() {
171170
val := item.MapInterface()
172171
if len(fieldKey) > 0 {
173-
resMap[val[fieldKey[0]].String()] = val[field]
172+
resMap[val[fieldKey[0]].Interface()] = val[field].Interface()
174173
} else {
175-
resSlice = append(resSlice, val[field])
174+
resSlice = append(resSlice, val[field].Interface())
176175
}
177176
}
178177
case OBJECT_STRUCT_SLICE: // rows
@@ -182,16 +181,16 @@ func (dba *Orm) Pluck(field string, fieldKey ...string) (v t.T, err error) {
182181
if len(fieldKey) > 0 {
183182
mapkey := dba._valueFromStruct(val, fieldKey[0])
184183
mapVal := dba._valueFromStruct(val, field)
185-
resMap[mapkey.String()] = mapVal
184+
resMap[mapkey] = mapVal
186185
} else {
187186
resSlice = append(resSlice, dba._valueFromStruct(val, field))
188187
}
189188
}
190189
}
191190
if len(fieldKey) > 0 {
192-
v = t.New(t.New(resMap).Map())
191+
v = resMap
193192
} else {
194-
v = t.New(resSlice)
193+
v = resSlice
195194
}
196195
return
197196
}

orm_query_interface.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package gorose
22

3-
import "github.com/gohouse/t"
4-
53
type IOrmQuery interface {
64
// 获取数据, 依据传入的绑定对象, 选择查询一条或多条数据并绑定到传入对象上
75
// 当绑定对象传入的是string类型时, 返回多条结果集, 需要使用 Get() 来获取最终结果
@@ -11,10 +9,10 @@ type IOrmQuery interface {
119
// 获取多条结果并返回, 只有当传入的table对象是字符串时生效
1210
Get() ([]Data, error)
1311
// 如果你不需要完整的一行,可以使用 value 方法从结果中获取单个值,该方法会直接返回指定列的值:
14-
Value(field string) (v t.T, err error)
12+
Value(field string) (v interface{}, err error)
1513
// 如果想要获取包含单个列值的数组,可以使用 pluck 方法
1614
// 还可以在返回数组中为列值指定自定义键(该自定义键必须是该表的其它字段列名,否则会报错)
17-
Pluck(field string, fieldKey ...string) (v t.T, err error)
15+
Pluck(field string, fieldKey ...string) (v interface{}, err error)
1816
// 查询构建器还提供了多个聚合方法,如count, max, min, avg 和 sum,你可以在构造查询之后调用这些方法:
1917
Count(args ...string) (int64, error)
2018
Sum(sum string) (interface{}, error)

0 commit comments

Comments
 (0)