feat: add table funcs
This commit is contained in:
@@ -16,6 +16,9 @@ import (
|
|||||||
//go:embed table.go.tpl
|
//go:embed table.go.tpl
|
||||||
var tableTpl string
|
var tableTpl string
|
||||||
|
|
||||||
|
//go:embed table_funcs.go.tpl
|
||||||
|
var tableFuncsTpl string
|
||||||
|
|
||||||
//go:embed table_test.go.tpl
|
//go:embed table_test.go.tpl
|
||||||
var tableTestTpl string
|
var tableTestTpl string
|
||||||
|
|
||||||
@@ -80,6 +83,7 @@ func Generate(tables []string, transformer Transformer) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
tableTpl := template.Must(template.New("model").Parse(string(tableTpl)))
|
tableTpl := template.Must(template.New("model").Parse(string(tableTpl)))
|
||||||
|
tableFuncsTpl := template.Must(template.New("model").Parse(string(tableFuncsTpl)))
|
||||||
tableTestTpl := template.Must(template.New("model").Parse(string(tableTestTpl)))
|
tableTestTpl := template.Must(template.New("model").Parse(string(tableTestTpl)))
|
||||||
providerTpl := template.Must(template.New("modelGen").Parse(string(providerTplStr)))
|
providerTpl := template.Must(template.New("modelGen").Parse(string(providerTplStr)))
|
||||||
|
|
||||||
@@ -132,6 +136,25 @@ func Generate(tables []string, transformer Transformer) error {
|
|||||||
if err := tableTestTpl.Execute(fd, tableInfo); err != nil {
|
if err := tableTestTpl.Execute(fd, tableInfo); err != nil {
|
||||||
return fmt.Errorf("failed to render model test template: %w", err)
|
return fmt.Errorf("failed to render model test template: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tableFuncsFile
|
||||||
|
tableFuncsFile := fmt.Sprintf("%s/%s.funcs.gen.go", baseDir, table)
|
||||||
|
// 如果 modelFuncsFile 已存在,则跳过
|
||||||
|
if _, err := os.Stat(tableFuncsFile); err == nil {
|
||||||
|
fmt.Printf("Model funcs file %s already exists. Skipping...\n", tableFuncsFile)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果 modelFuncsFile 不存在,则创建
|
||||||
|
fd, err = os.Create(tableFuncsFile)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to create model funcs file %s: %w", tableFuncsFile, err)
|
||||||
|
}
|
||||||
|
defer fd.Close()
|
||||||
|
|
||||||
|
if err := tableFuncsTpl.Execute(fd, tableInfo); err != nil {
|
||||||
|
return fmt.Errorf("failed to render model funcs template: %w", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 渲染总的 provider 文件
|
// 渲染总的 provider 文件
|
||||||
|
|||||||
119
pkg/ast/model/table_funcs.go.tpl
Normal file
119
pkg/ast/model/table_funcs.go.tpl
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
. "github.com/go-jet/jet/v2/postgres"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
func (m *{{.PascalTable}}) log() *log.Entry {
|
||||||
|
return log.WithField("model", "{{.PascalTable}}")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *{{.PascalTable}}) Create(ctx context.Context) error {
|
||||||
|
m.CreatedAt = time.Now()
|
||||||
|
stmt := table.Medias.INSERT(table.{{.PascalTable}}.MutableColumns).MODEL(m).RETURNING(table.Medias.AllColumns)
|
||||||
|
m.log().WithField("func","Create").Info( stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func","Create").Errorf("error creating {{.PascalTable}} item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func","Create").Infof("{{.PascalTable}} item created successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalTable}}) error {
|
||||||
|
stmt := table.{{.PascalTable}}.INSERT(table.{{.PascalTable}}.MutableColumns).MODELS(models)
|
||||||
|
m.log().WithField("func", "BatchCreate").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
|
m.log().WithField("func","Create").Errorf("error creating {{.PascalTable}} item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "BatchCreate").Infof("{{.PascalTable}} items created successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Delete
|
||||||
|
func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
|
||||||
|
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.EQ(m.ID))
|
||||||
|
m.log().WithField("func", "Delete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
|
m.log().WithField("func","Delete").Errorf("error deleting {{.PascalTable}} item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "Delete").Infof("{{.PascalTable}} item deleted successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
|
||||||
|
stmt := table.{{.PascalTable}}.DELETE().WHERE(table.{{.PascalTable}}.ID.IN(ids))
|
||||||
|
m.log().WithField("func", "BatchDelete").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if _, err := stmt.ExecContext(ctx, db); err != nil {
|
||||||
|
m.log().WithField("func","BatchDelete").Errorf("error deleting {{.PascalTable}} items: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "BatchDelete").Infof("{{.PascalTable}} items deleted successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||||
|
stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(m.ID)).RETURNING(table.{{.PascalTable}}.AllColumns)
|
||||||
|
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func","Update").Errorf("error updating {{.PascalTable}} item: %v", err)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "Update").Infof("{{.PascalTable}} item updated successfully")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
// GetByID
|
||||||
|
func (m *{{.PascalTable}}) GetByID(ctx context.Context, id int64) (*{{.PascalTable}}, error) {
|
||||||
|
stmt := table.{{.PascalTable}}.SELECT(table.{{.PascalTable}}.AllColumns).WHERE(table.{{.PascalTable}}.ID.EQ(id))
|
||||||
|
m.log().WithField("func", "GetByID").Info(stmt.DebugSql())
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, m); err != nil {
|
||||||
|
m.log().WithField("func","GetByID").Errorf("error getting {{.PascalTable}} item by ID: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
m.log().WithField("func", "GetByID").Infof("{{.PascalTable}} item retrieved successfully")
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Count
|
||||||
|
func (m *{{.PascalTable}}) Count(ctx context.Context, conds ...BoolExpression) (int64, error) {
|
||||||
|
cond := Bool(true)
|
||||||
|
if len(conds) > 0 {
|
||||||
|
for _, c := range conds {
|
||||||
|
cond = cond.AND(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tbl := table.{{.PascalTable}}
|
||||||
|
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)
|
||||||
|
m.log().Infof("sql: %s", stmt.DebugSql())
|
||||||
|
|
||||||
|
var count struct {
|
||||||
|
Count int64
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := stmt.QueryContext(ctx, db, &count); err != nil {
|
||||||
|
m.log().Errorf("error counting {{.PascalTable}} items: %v", err)
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return count.Count, nil
|
||||||
|
}
|
||||||
|
|
||||||
Reference in New Issue
Block a user