feat: update

This commit is contained in:
Rogee
2025-05-23 23:42:18 +08:00
parent b4cc2347e5
commit 906858dbd6
3 changed files with 47 additions and 38 deletions

View File

@@ -106,18 +106,12 @@ func Generate(tables []string, transformer Transformer) error {
} }
tableInfo := TableModelParam{ tableInfo := TableModelParam{
CamelTable: lo.CamelCase(table), CamelTable: lo.CamelCase(table),
PascalTable: lo.PascalCase(table), PascalTable: lo.PascalCase(table),
PkgName: gomod.GetModuleName(), PkgName: gomod.GetModuleName(),
} SoftDelete: strings.Contains(modelContent[table], "DeletedAt"),
if strings.Contains(table, "DeletedAt") { HasUpdatedAt: strings.Contains(modelContent[table], "UpdatedAt"),
tableInfo.SoftDelete = true HasCreatedAt: strings.Contains(modelContent[table], "CreatedAt"),
}
if strings.Contains(table, "UpdatedAt") {
tableInfo.HasUpdatedAt = true
}
if strings.Contains(table, "CreatedAt") {
tableInfo.HasCreatedAt = true
} }
items = append(items, tableInfo) items = append(items, tableInfo)

View File

@@ -15,8 +15,22 @@ import (
type Cond func(BoolExpression) BoolExpression type Cond func(BoolExpression) BoolExpression
func CondDefault() BoolExpression { func ExprCond(expr BoolExpression) Cond {
return BoolExp(Bool(true)) return func(cond BoolExpression) BoolExpression {
return cond.AND(expr)
}
}
func CondTrue(conds ...Cond) BoolExpression {
cond:= BoolExp(Bool(true))
for _, c := range conds {
cond = c(cond)
}
return cond
}
func CondJoin(cond Cond, conds ...Cond) []Cond {
return append([]Cond{cond}, conds...)
} }

View File

@@ -12,14 +12,18 @@ import (
) )
// conds // conds
{{- if .SoftDelete }} {{- if .SoftDelete }}
func (m *{{.PascalTable}}) NotDeleted() Cond { func (m *{{.PascalTable}}) CondNotDeleted() Cond {
return func(cond BoolExpression) BoolExpression { return func(cond BoolExpression) BoolExpression {
return cond.AND(table.Posts.DeletedAt.IS_NULL()) return cond.AND(table.{{.PascalTable}}.DeletedAt.IS_NULL())
} }
} }
{{- end}} {{- end}}
func (m *{{.PascalTable}}) CondID(id int64) Cond {
return func(cond BoolExpression) BoolExpression {
return cond.AND(table.{{.PascalTable}}.ID.EQ(Int(id)))
}
}
// funcs // funcs
func (m *{{.PascalTable}}) log() *log.Entry { func (m *{{.PascalTable}}) log() *log.Entry {
@@ -64,7 +68,7 @@ func (m *{{.PascalTable}}) BatchCreate(ctx context.Context, models []*{{.PascalT
{{- if .SoftDelete }} {{- if .SoftDelete }}
func (m *{{.PascalTable}}) Delete(ctx context.Context) error { func (m *{{.PascalTable}}) Delete(ctx context.Context) error {
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))) stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID)))
m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql()) m.log().WithField("func", "SoftDelete").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil { if err := stmt.QueryContext(ctx, db, m); err != nil {
@@ -82,7 +86,7 @@ func (m *{{.PascalTable}}) BatchDelete(ctx context.Context, ids []int64) error {
return Int64(id) return Int64(id)
}) })
stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.Set(TimestampzT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.IN(condIds...)) stmt := table.{{.PascalTable}}.UPDATE().SET(table.{{.PascalTable}}.DeletedAt.SET(TimestampT(time.Now()))).WHERE(table.{{.PascalTable}}.ID.IN(condIds...))
m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql()) m.log().WithField("func", "BatchSoftDelete").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil { if err := stmt.QueryContext(ctx, db, m); err != nil {
@@ -142,35 +146,32 @@ func (m *{{.PascalTable}}) Update(ctx context.Context) error {
m.log().WithField("func", "Update").Infof("{{.PascalTable}} item updated successfully") m.log().WithField("func", "Update").Infof("{{.PascalTable}} item updated successfully")
return nil return nil
} }
// GetByID
func (m *{{.PascalTable}}) GetByID(ctx context.Context, id int64, conds ...BoolExpression) (*{{.PascalTable}}, error) { // GetByCond
expr := table.{{.PascalTable}}.ID.EQ(Int(m.ID)) func (m *{{.PascalTable}}) GetByCond(ctx context.Context, conds ...Cond) (*{{.PascalTable}}, error) {
if len(conds) > 0 { cond := CondTrue(conds...)
for _, c := range conds {
expr = expr.AND(c) stmt := table.{{.PascalTable}}.SELECT(table.{{.PascalTable}}.AllColumns).WHERE(cond)
} m.log().WithField("func", "GetByCond").Info(stmt.DebugSql())
}
stmt := table.{{.PascalTable}}.SELECT(table.{{.PascalTable}}.AllColumns).WHERE(expr)
m.log().WithField("func", "GetByID").Info(stmt.DebugSql())
if err := stmt.QueryContext(ctx, db, m); err != nil { if err := stmt.QueryContext(ctx, db, m); err != nil {
m.log().WithField("func","GetByID").Errorf("error getting {{.PascalTable}} item by ID: %v", err) m.log().WithField("func","GetByCond").Errorf("error getting {{.PascalTable}} item by ID: %v", err)
return nil, err return nil, err
} }
m.log().WithField("func", "GetByID").Infof("{{.PascalTable}} item retrieved successfully") m.log().WithField("func", "GetByCond").Infof("{{.PascalTable}} item retrieved successfully")
return m, nil return m, nil
} }
// GetByID
func (m *{{.PascalTable}}) GetByID(ctx context.Context, id int64, conds ...Cond) (*{{.PascalTable}}, error) {
return m.GetByCond(ctx, CondJoin(m.CondID(id), conds...)...)
}
// Count // Count
func (m *{{.PascalTable}}) Count(ctx context.Context, conds ...BoolExpression) (int64, error) { func (m *{{.PascalTable}}) Count(ctx context.Context, conds ...Cond) (int64, error) {
cond := Bool(true) cond := CondTrue(conds...)
if len(conds) > 0 {
for _, c := range conds {
cond = cond.AND(c)
}
}
tbl := table.{{.PascalTable}} tbl := table.{{.PascalTable}}
stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond) stmt := tbl.SELECT(COUNT(tbl.ID).AS("count")).WHERE(cond)