Skip to content

Commit 5b314a6

Browse files
committed
refactor(generator): convert Prepare/Close to structured poet statements
Replace manual string building with poet AST types for: - Prepare function: VarDecl, Assign, If with Return - Close function: VarDecl, nested If statements, Assign, Return The slice query fallback still uses RawStmt due to complex dynamic SQL handling requirements. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent b1a18ba commit 5b314a6

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

internal/codegen/golang/generator.go

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -146,41 +146,60 @@ func (g *CodeGenerator) addDBCodeStd(f *poet.File) {
146146

147147
// Prepare and Close functions for prepared queries
148148
if g.tctx.EmitPreparedQueries {
149-
var prepareBody strings.Builder
150-
prepareBody.WriteString("\tq := Queries{db: db}\n")
151-
prepareBody.WriteString("\tvar err error\n")
149+
// Build Prepare function statements
150+
var prepareStmts []poet.Stmt
151+
prepareStmts = append(prepareStmts, poet.Assign{
152+
Left: []string{"q"},
153+
Op: ":=",
154+
Right: []string{poet.StructLit{Type: "Queries", Fields: [][2]string{{"db", "db"}}}.Render()},
155+
})
156+
prepareStmts = append(prepareStmts, poet.VarDecl{Name: "err", Type: "error"})
152157
if len(g.tctx.GoQueries) == 0 {
153-
prepareBody.WriteString("\t_ = err\n")
158+
prepareStmts = append(prepareStmts, poet.Assign{Left: []string{"_"}, Op: "=", Right: []string{"err"}})
154159
}
155160
for _, query := range g.tctx.GoQueries {
156-
fmt.Fprintf(&prepareBody, "\tif q.%s, err = db.PrepareContext(ctx, %s); err != nil {\n", query.FieldName, query.ConstantName)
157-
fmt.Fprintf(&prepareBody, "\t\treturn nil, fmt.Errorf(\"error preparing query %s: %%w\", err)\n", query.MethodName)
158-
prepareBody.WriteString("\t}\n")
161+
prepareStmts = append(prepareStmts, poet.If{
162+
Init: fmt.Sprintf("q.%s, err = db.PrepareContext(ctx, %s)", query.FieldName, query.ConstantName),
163+
Cond: "err != nil",
164+
Body: []poet.Stmt{poet.Return{Values: []string{
165+
"nil",
166+
fmt.Sprintf(`fmt.Errorf("error preparing query %s: %%w", err)`, query.MethodName),
167+
}}},
168+
})
159169
}
160-
prepareBody.WriteString("\treturn &q, nil\n")
170+
prepareStmts = append(prepareStmts, poet.Return{Values: []string{"&q", "nil"}})
161171

162172
f.Decls = append(f.Decls, poet.Func{
163173
Name: "Prepare",
164174
Params: []poet.Param{{Name: "ctx", Type: "context.Context"}, {Name: "db", Type: "DBTX"}},
165175
Results: []poet.Param{{Type: "*Queries"}, {Type: "error"}},
166-
Stmts: []poet.Stmt{poet.RawStmt{Code: prepareBody.String()}},
176+
Stmts: prepareStmts,
167177
})
168178

169-
var closeBody strings.Builder
170-
closeBody.WriteString("\tvar err error\n")
179+
// Build Close function statements
180+
var closeStmts []poet.Stmt
181+
closeStmts = append(closeStmts, poet.VarDecl{Name: "err", Type: "error"})
171182
for _, query := range g.tctx.GoQueries {
172-
fmt.Fprintf(&closeBody, "\tif q.%s != nil {\n", query.FieldName)
173-
fmt.Fprintf(&closeBody, "\t\tif cerr := q.%s.Close(); cerr != nil {\n", query.FieldName)
174-
fmt.Fprintf(&closeBody, "\t\t\terr = fmt.Errorf(\"error closing %s: %%w\", cerr)\n", query.FieldName)
175-
closeBody.WriteString("\t\t}\n\t}\n")
183+
closeStmts = append(closeStmts, poet.If{
184+
Cond: fmt.Sprintf("q.%s != nil", query.FieldName),
185+
Body: []poet.Stmt{poet.If{
186+
Init: fmt.Sprintf("cerr := q.%s.Close()", query.FieldName),
187+
Cond: "cerr != nil",
188+
Body: []poet.Stmt{poet.Assign{
189+
Left: []string{"err"},
190+
Op: "=",
191+
Right: []string{fmt.Sprintf(`fmt.Errorf("error closing %s: %%w", cerr)`, query.FieldName)},
192+
}},
193+
}},
194+
})
176195
}
177-
closeBody.WriteString("\treturn err\n")
196+
closeStmts = append(closeStmts, poet.Return{Values: []string{"err"}})
178197

179198
f.Decls = append(f.Decls, poet.Func{
180199
Recv: &poet.Param{Name: "q", Type: "*Queries"},
181200
Name: "Close",
182201
Results: []poet.Param{{Type: "error"}},
183-
Stmts: []poet.Stmt{poet.RawStmt{Code: closeBody.String()}},
202+
Stmts: closeStmts,
184203
})
185204

186205
// Helper functions

0 commit comments

Comments
 (0)