@@ -210,6 +210,14 @@ func (s *Stmt) addRelation(t1, t2 string, id interface{}) *Stmt {
210210 return s
211211}
212212
213+ // addOne2More 添加一对多关联条件
214+ func (s * Stmt ) addOne2More (t1 , t2 string , id interface {}) * Stmt {
215+ t1 = util .FieldEscape (t1 )
216+ t2 = util .FieldEscape (t2 )
217+ s .addWhere (fmt .Sprintf ("%s.%s_id=%d" , t1 , t2 , id ))
218+ return s
219+ }
220+
213221// Query 根据传入的result结构,生成查询sql,并返回执行结果, result 必需是一个指向切片的指针.
214222func (s * Stmt ) Query (result interface {}) error {
215223 if result == nil {
@@ -296,17 +304,24 @@ func (s *Stmt) Query(result interface{}) error {
296304 continue
297305 }
298306
299- if f .Tag .Get ("db_table" ) != "more" {
300- continue
301- }
302-
303307 lr := obj .Field (i ).Addr ().Interface ()
304308 id := reflect .ValueOf (refs [idx ]).Elem ().Interface ()
305309
306- //填充一对多结果,每次去查询
307- if err = NewStmt (s .db , util .FieldEscape (f .Name )).addRelation (f .Name , s .firstTable (), id ).Query (lr ); err != nil {
308- if errors .Cause (err ) != meta .ErrNotFound {
309- return errors .Trace (err )
310+ switch f .Tag .Get ("db_table" ) {
311+ case "more" :
312+
313+ //填充一对多结果,每次去查询
314+ if err = NewStmt (s .db , util .FieldEscape (f .Name )).addRelation (f .Name , s .firstTable (), id ).Query (lr ); err != nil {
315+ if errors .Cause (err ) != meta .ErrNotFound {
316+ return errors .Trace (err )
317+ }
318+ }
319+ case "one2more" :
320+ //填充一对多结果,每次去查询
321+ if err = NewStmt (s .db , util .FieldEscape (f .Name )).addOne2More (f .Name , s .firstTable (), id ).Query (lr ); err != nil {
322+ if errors .Cause (err ) != meta .ErrNotFound {
323+ return errors .Trace (err )
324+ }
310325 }
311326 }
312327 }
@@ -478,3 +493,12 @@ func (s *Stmt) Insert(data interface{}) (int64, error) {
478493
479494 return r .LastInsertId ()
480495}
496+
497+ //RawExec 保留的原始执行接口.
498+ func (s * Stmt ) Exec (query string , args ... interface {}) (int64 , error ) {
499+ rs , err := s .db .Exec (query , args ... )
500+ if err != nil {
501+ return - 1 , errors .Trace (err )
502+ }
503+ return rs .RowsAffected ()
504+ }
0 commit comments