Skip to content

Commit

Permalink
Beta:新增db.Scan(struct)方法,传入struct{}可以解析单条,类似Find方法,传入[]struct{}将会解析成多…
Browse files Browse the repository at this point in the history
…条,类似Get方法,注意需要传入指针值,例如传入:&User{},而不是:User{},不要用这个方法传入Map
  • Loading branch information
tobycroft committed Nov 13, 2023
1 parent 544a9e4 commit 79f10b5
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
60 changes: 59 additions & 1 deletion orm_query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gorose

import (
"errors"
"github.com/gohouse/t"
"math"
"reflect"
Expand All @@ -20,12 +21,69 @@ func (dba *Orm) Select() error {
if err != nil {
return err
}

// 执行查询
_, err = dba.GetISession().Query(sqlStr, args...)
return err
}

func (dba *Orm) Scan(scan_to_struct any) error {
dstVal := reflect.ValueOf(scan_to_struct)
sliceVal := reflect.Indirect(dstVal)
switch sliceVal.Kind() {
case reflect.Struct: // struct
dba.GetIBinder().SetBindType(OBJECT_STRUCT)
dba.GetIBinder().SetBindResult(scan_to_struct)
if len(dba.GetIBinder().GetBindFields()) == 0 {
dba.GetIBinder().SetBindFields(getTagName(dba.GetIBinder().GetBindResult(), TAGNAME))
}
switch dstVal.Kind() {
case reflect.Ptr, reflect.Struct:
break
default:
return errors.New("传入的对象有误,示例:var user User,传入 &user{}")
}
dba.Limit(1)
sqlStr, args, err := dba.BuildSql()
if err != nil {
return err
}
_, err = dba.GetISession().Query(sqlStr, args...)
return err

case reflect.Slice:
eltType := sliceVal.Type().Elem()
switch eltType.Kind() {
case reflect.Struct:
dba.GetIBinder().SetBindType(OBJECT_STRUCT_SLICE)
br := reflect.New(eltType)
dba.GetIBinder().SetBindResult(br.Interface())
dba.GetIBinder().SetBindResultSlice(sliceVal)
if len(dba.GetIBinder().GetBindFields()) == 0 {
dba.GetIBinder().SetBindFields(getTagName(dba.GetIBinder().GetBindResult(), TAGNAME))
}
switch dstVal.Kind() {
case reflect.Ptr, reflect.Struct:
break
default:
return errors.New("传入的对象有误,示例:var user User,传入 &user{}")
}
sqlStr, args, err := dba.BuildSql()
if err != nil {
return err
}
_, err = dba.GetISession().Query(sqlStr, args...)
return err

default:
return errors.New("传入[]struct{}将会解析成多条,类似Get方法,注意需要传入指针值,例如传入:&User{},而不是:User{},不要用这个方法传入Map")

}

default:
return errors.New("传入struct{}可以解析单条,类似Find方法,传入[]struct{}将会解析成多条,类似Get方法,注意需要传入指针值,例如传入:&User{},而不是:User{},不要用这个方法传入Map")
}
}

// First : select one row , relation limit set
func (dba *Orm) First() (result Data, err error) {
dba.GetIBinder().SetBindType(OBJECT_STRING)
Expand Down
2 changes: 2 additions & 0 deletions orm_query_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ type IOrmQuery interface {
// 获取数据, 依据传入的绑定对象, 选择查询一条或多条数据并绑定到传入对象上
// 当绑定对象传入的是string类型时, 返回多条结果集, 需要使用 Get() 来获取最终结果
Select() error
//Scan 方法传入struct{}可以解析单条,类似Find方法,输入[]struct{}将会解析成多条,类似Get方法
Scan(scan_to_struct any) error
// 获取一条结果并返回, 只有当传入的table对象是字符串时生效
First() (Data, error)
Find() (Data, error)
Expand Down

0 comments on commit 79f10b5

Please sign in to comment.