Skip to content

Commit

Permalink
fix: Add DELETE hasMany func
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Aug 12, 2024
1 parent 2a02f01 commit f039b18
Show file tree
Hide file tree
Showing 4 changed files with 789 additions and 667 deletions.
170 changes: 86 additions & 84 deletions internal/arcgen/lang/go/generate_orm_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,100 +17,102 @@ func generateCREATEContent(astFile *ast.File, arcSrcSet *ARCSourceSet) {
tableInfo := arcSrc.extractFieldNamesAndColumnNames()
columnNames := tableInfo.Columns.ColumnNames()

// const Create{StructName}Query = `INSERT INTO {table_name} ({column_name1}, {column_name2}) VALUES ($1, $2)`
//
// func (orm *_ORM) Create{StructName}(ctx context.Context, queryer QueryerContext, s *{Struct}) error {
// LoggerFromContext(ctx).Debug(Create{StructName}Query)
// if _, err := queryerContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil {
// return fmt.Errorf("queryerContext.ExecContext: %w", orm.HandleError(err))
// }
// return nil
// }
funcName := "Create" + structName
queryName := funcName + "Query"
astFile.Decls = append(astFile.Decls,
&ast.GenDecl{
Tok: token.CONST,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{{Name: queryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames, 1) + ")`",
}},
{
// const Create{StructName}Query = `INSERT INTO {table_name} ({column_name1}, {column_name2}) VALUES ($1, $2)`
//
// func (orm *_ORM) Create{StructName}(ctx context.Context, queryer QueryerContext, s *{Struct}) error {
// LoggerFromContext(ctx).Debug(Create{StructName}Query)
// if _, err := queryerContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil {
// return fmt.Errorf("queryerContext.ExecContext: %w", orm.HandleError(err))
// }
// return nil
// }
funcName := "Create" + structName
queryName := funcName + "Query"
astFile.Decls = append(astFile.Decls,
&ast.GenDecl{
Tok: token.CONST,
Specs: []ast.Spec{
&ast.ValueSpec{
Names: []*ast.Ident{{Name: queryName}},
Values: []ast.Expr{&ast.BasicLit{
Kind: token.STRING,
Value: "`INSERT INTO " + tableName + " (" + strings.Join(columnNames, ", ") + ") VALUES (" + columnValuesPlaceholder(columnNames, 1) + ")`",
}},
},
},
},
},
&ast.FuncDecl{
Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: receiverName}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}}}},
Name: &ast.Ident{Name: funcName},
Type: &ast.FuncType{
Params: &ast.FieldList{List: []*ast.Field{
{Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}},
{Names: []*ast.Ident{{Name: queryerContextVarName}}, Type: &ast.Ident{Name: queryerContextTypeName}},
{Names: []*ast.Ident{{Name: "s"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: importName + "." + structName}}},
}},
Results: &ast.FieldList{List: []*ast.Field{
{Type: &ast.Ident{Name: "error"}},
}},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
// LoggerFromContext(ctx).Debug(queryName)
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.CallExpr{Fun: &ast.Ident{Name: "LoggerFromContext"}, Args: []ast.Expr{&ast.Ident{Name: "ctx"}}},
Sel: &ast.Ident{Name: "Debug"},
},
Args: []ast.Expr{&ast.Ident{Name: queryName}},
},
},
&ast.IfStmt{
// if _, err := queryerContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil {
Init: &ast.AssignStmt{
Lhs: []ast.Expr{&ast.Ident{Name: "_"}, &ast.Ident{Name: "err"}},
Tok: token.DEFINE,
Rhs: []ast.Expr{&ast.CallExpr{
&ast.FuncDecl{
Recv: &ast.FieldList{List: []*ast.Field{{Names: []*ast.Ident{{Name: receiverName}}, Type: &ast.StarExpr{X: &ast.Ident{Name: config.GoORMStructName()}}}}},
Name: &ast.Ident{Name: funcName},
Type: &ast.FuncType{
Params: &ast.FieldList{List: []*ast.Field{
{Names: []*ast.Ident{{Name: "ctx"}}, Type: &ast.Ident{Name: "context.Context"}},
{Names: []*ast.Ident{{Name: queryerContextVarName}}, Type: &ast.Ident{Name: queryerContextTypeName}},
{Names: []*ast.Ident{{Name: "s"}}, Type: &ast.StarExpr{X: &ast.Ident{Name: importName + "." + structName}}},
}},
Results: &ast.FieldList{List: []*ast.Field{
{Type: &ast.Ident{Name: "error"}},
}},
},
Body: &ast.BlockStmt{
List: []ast.Stmt{
&ast.ExprStmt{
// LoggerFromContext(ctx).Debug(queryName)
X: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: queryerContextVarName},
Sel: &ast.Ident{Name: "ExecContext"},
X: &ast.CallExpr{Fun: &ast.Ident{Name: "LoggerFromContext"}, Args: []ast.Expr{&ast.Ident{Name: "ctx"}}},
Sel: &ast.Ident{Name: "Debug"},
},
Args: append(
[]ast.Expr{
&ast.Ident{Name: "ctx"},
&ast.Ident{Name: queryName},
Args: []ast.Expr{&ast.Ident{Name: queryName}},
},
},
&ast.IfStmt{
// if _, err := queryerContext.ExecContext(ctx, Create{StructName}Query, s.{ColumnName1}, s.{ColumnName2}); err != nil {
Init: &ast.AssignStmt{
Lhs: []ast.Expr{&ast.Ident{Name: "_"}, &ast.Ident{Name: "err"}},
Tok: token.DEFINE,
Rhs: []ast.Expr{&ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: queryerContextVarName},
Sel: &ast.Ident{Name: "ExecContext"},
},
func() []ast.Expr {
var args []ast.Expr
for _, c := range tableInfo.Columns {
args = append(args, &ast.SelectorExpr{X: &ast.Ident{Name: "s"}, Sel: &ast.Ident{Name: c.FieldName}})
}
return args
}()...),
Args: append(
[]ast.Expr{
&ast.Ident{Name: "ctx"},
&ast.Ident{Name: queryName},
},
func() []ast.Expr {
var args []ast.Expr
for _, c := range tableInfo.Columns {
args = append(args, &ast.SelectorExpr{X: &ast.Ident{Name: "s"}, Sel: &ast.Ident{Name: c.FieldName}})
}
return args
}()...),
}},
},
// err != nil {
Cond: &ast.BinaryExpr{X: &ast.Ident{Name: "err"}, Op: token.NEQ, Y: &ast.Ident{Name: "nil"}},
Body: &ast.BlockStmt{List: []ast.Stmt{
// return fmt.Errorf("queryerContext.ExecContext: %w", err)
&ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{
Fun: &ast.SelectorExpr{X: &ast.Ident{Name: "fmt"}, Sel: &ast.Ident{Name: "Errorf"}},
Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(queryerContextVarName + ".ExecContext: %w")}, &ast.CallExpr{
Fun: &ast.SelectorExpr{X: &ast.Ident{Name: receiverName}, Sel: &ast.Ident{Name: "HandleError"}},
Args: []ast.Expr{&ast.Ident{Name: "ctx"}, &ast.Ident{Name: "err"}},
}},
}}},
}},
},
// err != nil {
Cond: &ast.BinaryExpr{X: &ast.Ident{Name: "err"}, Op: token.NEQ, Y: &ast.Ident{Name: "nil"}},
Body: &ast.BlockStmt{List: []ast.Stmt{
// return fmt.Errorf("queryerContext.ExecContext: %w", err)
&ast.ReturnStmt{Results: []ast.Expr{&ast.CallExpr{
Fun: &ast.SelectorExpr{X: &ast.Ident{Name: "fmt"}, Sel: &ast.Ident{Name: "Errorf"}},
Args: []ast.Expr{&ast.Ident{Name: strconv.Quote(queryerContextVarName + ".ExecContext: %w")}, &ast.CallExpr{
Fun: &ast.SelectorExpr{X: &ast.Ident{Name: receiverName}, Sel: &ast.Ident{Name: "HandleError"}},
Args: []ast.Expr{&ast.Ident{Name: "ctx"}, &ast.Ident{Name: "err"}},
}},
}}},
}},
},
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.Ident{Name: "nil"},
&ast.ReturnStmt{
Results: []ast.Expr{
&ast.Ident{Name: "nil"},
},
},
},
},
},
},
)
)
}
}
}
Loading

0 comments on commit f039b18

Please sign in to comment.