@@ -1053,21 +1053,37 @@ func (g *CodeGenerator) addQueryExecStd(f *poet.File, q Query) {
10531053
10541054 // Fall back to RawStmt for slice queries (complex handling)
10551055 if q .Arg .HasSqlcSlices () {
1056- var body strings.Builder
1057- g .writeQueryExecStdCall (& body , q , "_, err :=" )
1056+ var stmts []poet.Stmt
1057+
1058+ // Query exec call (complex dynamic SQL handling)
1059+ var queryExec strings.Builder
1060+ g .writeQueryExecStdCall (& queryExec , q , "_, err :=" )
1061+ stmts = append (stmts , poet.RawStmt {Code : queryExec .String ()})
1062+
1063+ // if err != nil { err = fmt.Errorf(...) }
10581064 if g .tctx .WrapErrors {
1059- body .WriteString ("\t if err != nil {\n " )
1060- fmt .Fprintf (& body , "\t \t err = fmt.Errorf(\" query %s: %%w\" , err)\n " , q .MethodName )
1061- body .WriteString ("\t }\n " )
1065+ stmts = append (stmts , poet.If {
1066+ Cond : "err != nil" ,
1067+ Body : []poet.Stmt {
1068+ poet.Assign {
1069+ Left : []string {"err" },
1070+ Op : "=" ,
1071+ Right : []string {fmt .Sprintf (`fmt.Errorf("query %s: %%w", err)` , q .MethodName )},
1072+ },
1073+ },
1074+ })
10621075 }
1063- body .WriteString ("\t return err\n " )
1076+
1077+ // return err
1078+ stmts = append (stmts , poet.Return {Values : []string {"err" }})
1079+
10641080 f .Decls = append (f .Decls , poet.Func {
10651081 Comment : g .queryComments (q ),
10661082 Recv : & poet.Param {Name : "q" , Type : "Queries" , Pointer : true },
10671083 Name : q .MethodName ,
10681084 Params : params ,
10691085 Results : []poet.Param {{Type : "error" }},
1070- Stmts : []poet. Stmt {poet. RawStmt { Code : body . String ()}} ,
1086+ Stmts : stmts ,
10711087 })
10721088 return
10731089 }
@@ -1108,23 +1124,30 @@ func (g *CodeGenerator) addQueryExecRowsStd(f *poet.File, q Query) {
11081124
11091125 // Fall back to RawStmt for slice queries
11101126 if q .Arg .HasSqlcSlices () {
1111- var body strings.Builder
1112- g .writeQueryExecStdCall (& body , q , "result, err :=" )
1113- body .WriteString ("\t if err != nil {\n " )
1114- if g .tctx .WrapErrors {
1115- fmt .Fprintf (& body , "\t \t return 0, fmt.Errorf(\" query %s: %%w\" , err)\n " , q .MethodName )
1116- } else {
1117- body .WriteString ("\t \t return 0, err\n " )
1118- }
1119- body .WriteString ("\t }\n " )
1120- body .WriteString ("\t return result.RowsAffected()\n " )
1127+ var stmts []poet.Stmt
1128+
1129+ // Query exec call (complex dynamic SQL handling)
1130+ var queryExec strings.Builder
1131+ g .writeQueryExecStdCall (& queryExec , q , "result, err :=" )
1132+ stmts = append (stmts , poet.RawStmt {Code : queryExec .String ()})
1133+
1134+ // if err != nil { return 0, err }
1135+ errReturn := g .wrapErrorReturn (q , "0" )
1136+ stmts = append (stmts , poet.If {
1137+ Cond : "err != nil" ,
1138+ Body : []poet.Stmt {poet.Return {Values : errReturn }},
1139+ })
1140+
1141+ // return result.RowsAffected()
1142+ stmts = append (stmts , poet.Return {Values : []string {"result.RowsAffected()" }})
1143+
11211144 f .Decls = append (f .Decls , poet.Func {
11221145 Comment : g .queryComments (q ),
11231146 Recv : & poet.Param {Name : "q" , Type : "Queries" , Pointer : true },
11241147 Name : q .MethodName ,
11251148 Params : params ,
11261149 Results : []poet.Param {{Type : "int64" }, {Type : "error" }},
1127- Stmts : []poet. Stmt {poet. RawStmt { Code : body . String ()}} ,
1150+ Stmts : stmts ,
11281151 })
11291152 return
11301153 }
0 commit comments