feat: add table vars to model provider
This commit is contained in:
@@ -6,6 +6,11 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
|
||||
{{ if gt (len .) 0 }}
|
||||
"{{ (index . 0).PkgName }}/database/table"
|
||||
{{ end }}
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
"go.ipao.vip/atom/contracts"
|
||||
@@ -33,7 +38,13 @@ func CondJoin(cond Cond, conds ...Cond) []Cond {
|
||||
return append([]Cond{cond}, conds...)
|
||||
}
|
||||
|
||||
// tables
|
||||
{{- range . }}
|
||||
var tbl{{.PascalTable}} = table.{{.PascalTable}}
|
||||
{{- end }}
|
||||
|
||||
|
||||
// models
|
||||
var db *sql.DB
|
||||
{{- range . }}
|
||||
func {{.PascalTable}}Model() *{{.PascalTable}} { return &{{.PascalTable}}{} }
|
||||
|
||||
@@ -130,22 +130,22 @@ func (m *{{.PascalTable}}) BatchForceDelete(ctx context.Context, ids []int64) er
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||
{{- if .HasUpdatedAt}}
|
||||
m.UpdatedAt = time.Now()
|
||||
{{- end}}
|
||||
// func (m *{{.PascalTable}}) Update(ctx context.Context) error {
|
||||
// {{- if .HasUpdatedAt}}
|
||||
// m.UpdatedAt = time.Now()
|
||||
// {{- end}}
|
||||
|
||||
stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns.Except({{.CamelTable}}UpdateExcludeColumns...)).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(Int(m.ID))).RETURNING(table.{{.PascalTable}}.AllColumns)
|
||||
m.log().WithField("func", "Update").Info(stmt.DebugSql())
|
||||
// stmt := table.{{.PascalTable}}.UPDATE(table.{{.PascalTable}}.MutableColumns.Except({{.CamelTable}}UpdateExcludeColumns...)).SET(m).WHERE(table.{{.PascalTable}}.ID.EQ(Int(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
|
||||
}
|
||||
// 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
|
||||
}
|
||||
// m.log().WithField("func", "Update").Infof("{{.PascalTable}} item updated successfully")
|
||||
// return nil
|
||||
// }
|
||||
|
||||
// GetByCond
|
||||
func (m *{{.PascalTable}}) GetByCond(ctx context.Context, conds ...Cond) (*{{.PascalTable}}, error) {
|
||||
|
||||
Reference in New Issue
Block a user