codegen (refactor): move foreign key failure check lambda function generator to a helper func
All checks were successful
CI / build-docker (push) Successful in 6s
CI / build-docker-bootstrap (push) Has been skipped
CI / release-test (push) Successful in 25s

This commit is contained in:
wispem-wantex 2026-02-15 20:48:50 +00:00
parent 93b589d1b2
commit ee1d0a5ed7

View File

@ -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 {