From ee1d0a5ed744e81447c5f92aac9ad9ee7e70e728 Mon Sep 17 00:00:00 2001 From: wispem-wantex Date: Sun, 15 Feb 2026 20:48:50 +0000 Subject: [PATCH] codegen (refactor): move foreign key failure check lambda function generator to a helper func --- pkg/codegen/modelgenerate/generate_model.go | 67 ++++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/pkg/codegen/modelgenerate/generate_model.go b/pkg/codegen/modelgenerate/generate_model.go index e4c4101..de31ad6 100644 --- a/pkg/codegen/modelgenerate/generate_model.go +++ b/pkg/codegen/modelgenerate/generate_model.go @@ -82,37 +82,11 @@ func GenerateModelAST(table schema.Table) *ast.GenDecl { } } -// GenerateSaveItemFunc produces an AST for the SaveXyz() function of the model. -// E.g., a table with `table.TypeName = "foods"` will produce a "SaveFood()" function. -func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl { - insertCols := make([]string, 0, len(tbl.Columns)) - insertVals := make([]string, 0, len(tbl.Columns)) - updatePairs := make([]string, 0, len(tbl.Columns)) - - hasCreatedAt, hasUpdatedAt := tbl.HasAutoTimestamps() - - // Assemble data for building SQL "insert" and "update" strings - for _, col := range tbl.Columns { - if col.Name == "rowid" { - continue - } - insertCols = append(insertCols, col.Name) - val := ":" + col.Name - if col.IsNullableForeignKey() { - val = fmt.Sprintf("nullif(%s, 0)", val) - } - insertVals = append(insertVals, val) - // created_at should not be updated after creation - if col.Name == "created_at" && hasCreatedAt { - continue - } - updatePairs = append(updatePairs, col.Name+"="+val) - } - insertStmt := fmt.Sprintf("\n\t\t insert into %s (%s)\n\t\t values (%s)\n\t\t", tbl.TableName, strings.Join(insertCols, ", "), strings.Join(insertVals, ", ")) - updateStmt := fmt.Sprintf("\n\t\t update %s\n\t\t set %s\n\t\t where rowid = :rowid\n\t\t", tbl.TableName, strings.Join(updatePairs, ",\n\t\t ")) - +// buildFKCheckLambda builds the `checkForeignKeyFailures := func(err error) error { ... }` AST. +// Returns the assignment statement and whether any FK columns were found. +func buildFKCheckLambda(tbl schema.Table) (*ast.AssignStmt, bool) { hasFks := false - checkForeignKeyFailuresAssignment := &ast.AssignStmt{ + stmt := &ast.AssignStmt{ Lhs: []ast.Expr{ast.NewIdent("checkForeignKeyFailures")}, Tok: token.DEFINE, Rhs: []ast.Expr{ @@ -228,6 +202,39 @@ func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl { }, }, } + return stmt, hasFks +} + +// GenerateSaveItemFunc produces an AST for the SaveXyz() function of the model. +// E.g., a table with `table.TypeName = "foods"` will produce a "SaveFood()" function. +func GenerateSaveItemFunc(tbl schema.Table) *ast.FuncDecl { + insertCols := make([]string, 0, len(tbl.Columns)) + insertVals := make([]string, 0, len(tbl.Columns)) + updatePairs := make([]string, 0, len(tbl.Columns)) + + hasCreatedAt, hasUpdatedAt := tbl.HasAutoTimestamps() + + // Assemble data for building SQL "insert" and "update" strings + for _, col := range tbl.Columns { + if col.Name == "rowid" { + continue + } + insertCols = append(insertCols, col.Name) + val := ":" + col.Name + if col.IsNullableForeignKey() { + val = fmt.Sprintf("nullif(%s, 0)", val) + } + insertVals = append(insertVals, val) + // created_at should not be updated after creation + if col.Name == "created_at" && hasCreatedAt { + continue + } + updatePairs = append(updatePairs, col.Name+"="+val) + } + insertStmt := fmt.Sprintf("\n\t\t insert into %s (%s)\n\t\t values (%s)\n\t\t", tbl.TableName, strings.Join(insertCols, ", "), strings.Join(insertVals, ", ")) + updateStmt := fmt.Sprintf("\n\t\t update %s\n\t\t set %s\n\t\t where rowid = :rowid\n\t\t", tbl.TableName, strings.Join(updatePairs, ",\n\t\t ")) + + checkForeignKeyFailuresAssignment, hasFks := buildFKCheckLambda(tbl) funcBody := &ast.BlockStmt{ List: func() []ast.Stmt {