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