From fe122250eaba2991b34f288eb235e3fcc2a9e590 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Mon, 27 May 2024 08:29:56 +0800 Subject: [PATCH] refactor: remove dal subcommand --- cmd/ormat/command/dal.go | 151 -------------- cmd/ormat/command/root.go | 1 - cmd/ormat/command/template.go | 78 ------- cmd/ormat/command/template/dal_gorm.tpl | 235 ---------------------- cmd/ormat/command/template/dal_option.tpl | 71 ------- cmd/ormat/command/template/dal_query.tpl | 36 ---- cmd/ormat/command/template/dal_rapier.tpl | 202 ------------------- 7 files changed, 774 deletions(-) delete mode 100644 cmd/ormat/command/dal.go delete mode 100644 cmd/ormat/command/template.go delete mode 100644 cmd/ormat/command/template/dal_gorm.tpl delete mode 100644 cmd/ormat/command/template/dal_option.tpl delete mode 100644 cmd/ormat/command/template/dal_query.tpl delete mode 100644 cmd/ormat/command/template/dal_rapier.tpl diff --git a/cmd/ormat/command/dal.go b/cmd/ormat/command/dal.go deleted file mode 100644 index b801538..0000000 --- a/cmd/ormat/command/dal.go +++ /dev/null @@ -1,151 +0,0 @@ -package command - -import ( - "bytes" - "cmp" - "errors" - "fmt" - "log/slog" - "os" - "path/filepath" - "strings" - - "github.com/spf13/cobra" - "github.com/things-go/ens" - "github.com/things-go/ens/utils" -) - -type dalOpt struct { - source - OutputDir string - PackageName string // 包名 - ModelImportPath string // required, model导入路径 - RepoImportPath string // required, repository导入路径 - DalImportPath string // required, dal导入路径 - CustomTemplate string // 自定义模板 - ens.Option -} - -type dalCmd struct { - cmd *cobra.Command - dalOpt -} - -func newDakCmd() *dalCmd { - root := &dalCmd{} - - cmd := &cobra.Command{ - Use: "dal", - Short: "Generate dal from database", - Example: "ormat dal", - RunE: func(*cobra.Command, []string) error { - if root.CustomTemplate == "builtin-rapier" && root.RepoImportPath == "" { - return errors.New("使用builtin-rapier时repository导入路径, 不能为空") - } - schemaes, err := getSchema(&root.source) - if err != nil { - return err - } - daltpl, err := GetUsedTemplate(root.CustomTemplate) - if err != nil { - return err - } - buf := bytes.Buffer{} - packageName := cmp.Or(root.PackageName, utils.GetPkgName(root.OutputDir)) - queryImportPath := strings.Join([]string{root.DalImportPath, "query"}, "/") - dalOptionFilename := joinFilename(root.OutputDir, "dal_option", ".go") - _, err = os.Stat(dalOptionFilename) - if !(err == nil || os.IsExist(err)) { - err = dalOptionTpl.Execute(&buf, Dal{Package: packageName}) - if err != nil { - return err - } - err = WriteFile(dalOptionFilename, buf.Bytes()) - if err != nil { - return fmt.Errorf("dal_option: %v", err) - } - slog.Info("👉 " + dalOptionFilename) - } else { - slog.Warn("🐛 dal_option.go already exists, skipping") - } - dal := Dal{ - Package: packageName, - Imports: []string{root.ModelImportPath, queryImportPath, root.RepoImportPath}, - ModelPrefix: utils.PkgName(root.ModelImportPath) + ".", - QueryPrefix: "query.", - RepoPrefix: utils.PkgName(root.RepoImportPath) + ".", - Entity: nil, - } - dalQuery := Dal{ - Package: "query", - Imports: []string{}, - ModelPrefix: utils.PkgName(root.ModelImportPath) + ".", - QueryPrefix: "", - RepoPrefix: "", - Entity: nil, - } - - for _, entity := range schemaes.Entities { - dalFilename := joinFilename(root.OutputDir, entity.Name, ".go") - _, err = os.Stat(dalFilename) - if err == nil || os.IsExist(err) { - slog.Warn("🐛 " + entity.Name + " already exists, skipping") - continue - } - dal.Entity = entity - buf.Reset() - err = daltpl.Execute(&buf, dal) - if err != nil { - return err - } - - err = WriteFile(dalFilename, buf.Bytes()) - if err != nil { - return fmt.Errorf("%v: %v", entity.Name, err) - } - - buf.Reset() - dalQuery.Entity = entity - err = dalQueryTpl.Execute(&buf, dalQuery) - if err != nil { - return err - } - dalQueryFilename := joinFilename(filepath.Join(root.OutputDir, "query"), entity.Name, ".go") - err = WriteFile(dalQueryFilename, buf.Bytes()) - if err != nil { - return err - } - slog.Info("👉 " + dalFilename) - slog.Info("👉 " + dalQueryFilename) - } - - slog.Info("😄 generate success !!!") - return nil - }, - } - // input file - cmd.Flags().StringSliceVarP(&root.InputFile, "input", "i", nil, "input file") - cmd.Flags().StringVarP(&root.Schema, "schema", "s", "file+mysql", "parser file driver, [file+mysql,file+tidb](仅input时有效)") - // database url - cmd.Flags().StringVarP(&root.URL, "url", "u", "", "mysql://root:123456@127.0.0.1:3306/test") - cmd.Flags().StringSliceVarP(&root.Tables, "table", "t", nil, "only out custom table") - cmd.Flags().StringSliceVarP(&root.Exclude, "exclude", "e", nil, "exclude table pattern") - - cmd.Flags().StringVarP(&root.OutputDir, "out", "o", "./dal", "out directory") - cmd.Flags().StringVar(&root.PackageName, "package", "", "package name") - cmd.Flags().StringVar(&root.CustomTemplate, "template", "builtin-rapier", "use custom template except [builtin-rapier, builtin-gorm]") - cmd.Flags().StringVar(&root.ModelImportPath, "modelImportPath", "", "model导入路径") - cmd.Flags().StringVar(&root.DalImportPath, "dalImportPath", "", "dal导入路径") - cmd.Flags().StringVar(&root.RepoImportPath, "repoImportPath", "", "repository导入路径") - - cmd.Flags().BoolVar(&root.EnableInt, "enableInt", false, "使能int8,uint8,int16,uint16,int32,uint32输出为int,uint") - cmd.Flags().BoolVar(&root.EnableBoolInt, "enableBoolInt", false, "使能bool输出int") - cmd.Flags().BoolVar(&root.DisableNullToPoint, "disableNullToPoint", false, "禁用字段为null时输出指针类型,将输出为sql.Nullxx") - cmd.Flags().StringSliceVar(&root.EscapeName, "escapeName", nil, "escape name list") - - cmd.MarkFlagsOneRequired("url", "input") - cmd.MarkFlagRequired("modelImportPath") - cmd.MarkFlagRequired("dalImportPath") - root.cmd = cmd - return root -} diff --git a/cmd/ormat/command/root.go b/cmd/ormat/command/root.go index f38cce4..7d1e4c6 100644 --- a/cmd/ormat/command/root.go +++ b/cmd/ormat/command/root.go @@ -41,7 +41,6 @@ func NewRootCmd() *RootCmd { newModelCmd().cmd, newProtoCmd().cmd, newRapierCmd().cmd, - newDakCmd().cmd, ) root.cmd = cmd return root diff --git a/cmd/ormat/command/template.go b/cmd/ormat/command/template.go deleted file mode 100644 index 559faba..0000000 --- a/cmd/ormat/command/template.go +++ /dev/null @@ -1,78 +0,0 @@ -package command - -import ( - "embed" - "errors" - "text/template" - - "github.com/things-go/ens" - "github.com/things-go/ens/utils" -) - -//go:embed template/*.tpl -var Static embed.FS - -var TemplateFuncs = template.FuncMap{ - "add": func(a, b int) int { return a + b }, - "snakecase": func(s string) string { return utils.SnakeCase(s) }, - "kebabcase": func(s string) string { return utils.Kebab(s) }, - "pascalcase": func(s string) string { return utils.PascalCase(s) }, - "smallcamelcase": func(s string) string { return utils.SmallCamelCase(s) }, -} -var ( - tpl = template.Must(template.New("components"). - Funcs(TemplateFuncs). - ParseFS(Static, "template/*.tpl")) - dalRapierTpl = tpl.Lookup("dal_rapier.tpl") - dalGormTpl = tpl.Lookup("dal_gorm.tpl") - dalQueryTpl = tpl.Lookup("dal_query.tpl") - dalOptionTpl = tpl.Lookup("dal_option.tpl") -) - -type Dal struct { - Package string - Imports []string - ModelPrefix string - QueryPrefix string - RepoPrefix string - Entity *ens.EntityDescriptor -} - -type DalQuery struct { - PackageName string - Imports []string - ModelQualifier string - Entity ens.EntityDescriptor -} - -func GetUsedTemplate(t string) (*template.Template, error) { - switch t { - case "builtin-gorm": - return dalGormTpl, nil - case "builtin-rapier": - return dalRapierTpl, nil - default: - t, err := ParseTemplateFromFile(t) - if err != nil { - return nil, err - } - return t, nil - } -} - -func ParseTemplateFromFile(filename string) (*template.Template, error) { - if filename == "" { - return nil, errors.New("required template filename") - } - tt, err := template.New("custom"). - Funcs(TemplateFuncs). - ParseFiles(filename) - if err != nil { - return nil, err - } - ts := tt.Templates() - if len(ts) == 0 { - return nil, errors.New("not found any template") - } - return ts[0], nil -} diff --git a/cmd/ormat/command/template/dal_gorm.tpl b/cmd/ormat/command/template/dal_gorm.tpl deleted file mode 100644 index 7fafaa1..0000000 --- a/cmd/ormat/command/template/dal_gorm.tpl +++ /dev/null @@ -1,235 +0,0 @@ -package {{.Package}} - -import ( - "context" - - "gorm.io/gorm" - -{{- range $e := .Imports}} - {{- if ne $e ""}} - "{{$e}}" - {{- end}} -{{- end}} -) - -{{- $e := .Entity}} -{{- $stName := pascalcase $e.Name}} -{{- $mdPrefix := .ModelPrefix}} -{{- $queryPrefix := .QueryPrefix}} -{{- $mdName := printf "%s%s" $mdPrefix $stName}} - -var _ {{$stName}}Dal = (*{{$stName}})(nil) - -type {{$stName}}Dal interface { - Create(ctx context.Context, v ...*{{$mdName}}) (int64, error) - Delete(ctx context.Context, id ...int64) (int64, error) - UpdateFull(ctx context.Context, v *{{$mdName}}) (int64, error) - UpdatePartial(ctx context.Context, v *{{$queryPrefix}}Update{{$stName}}ByPartial) (int64, error) - Get(ctx context.Context, id int64) (*{{$mdName}}, error) - GetByFilter(ctx context.Context, q *{{$queryPrefix}}Get{{$stName}}ByFilter) (*{{$mdName}}, error) - ExistByFilter(ctx context.Context, q *{{$queryPrefix}}Exist{{$stName}}ByFilter) (bool, error) - Count(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) (int64, error) - List(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, error) - ListPage(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, int64, error) - PluckIdByFilter(ctx context.Context, q *{{$queryPrefix}}PluckId{{$stName}}ByFilter) ([]int64, error) -} - -type {{$stName}} struct { - db *gorm.DB -} - -func New{{$stName}}(db *gorm.DB) {{$stName}}Dal { - return {{$stName}} { - db: db, - } -} - -func (b {{$stName}}) Create(ctx context.Context, v ...*{{$mdName}}) (int64, error) { - res := b.db.Create(v) - return res.RowsAffected, res.Error -} - -func (b {{$stName}}) Delete(ctx context.Context, id ...int64) (int64, error) { - res := b.db.Model(&{{$mdName}}{}). - Scopes(func(db *gorm.DB) *gorm.DB { - if len(id) == 1 { - db = db.Where("id = ?", id[0]) - }else { - db = db.Where("id IN ?", id) - } - return db - }). - Delete(&{{$mdName}}{}) - return res.RowsAffected, res.Error -} - - -func (b {{$stName}}) UpdateFull(ctx context.Context, v *{{$mdName}}) (int64, error) { - res := b.db.Model(&{{$mdName}}{}). - Select("*"). - Where("id = ?", v.Id). - Updates(v) - return res.RowsAffected, res.Error -} - -func (b {{$stName}}) UpdatePartial(ctx context.Context, v *{{$queryPrefix}}Update{{$stName}}ByPartial) (int64, error) { - up := make(map[string]any, {{add (len $stName) 8}}) -{{- range $f := $e.Fields}} - {{- if ne $f.GoName "Id"}} - if v.{{$f.GoName}} != nil { - up["{{$f.ColumnName}}"] = *v.{{$f.GoName}} - } - {{- end}} -{{- end}} - if len(up) == 0 { - return 0, nil - } - res := b.db.Model(&{{$mdName}}{}). - Where("id = ?", v.Id). - Updates(v) - return res.RowsAffected, res.Error -} - -func (b {{$stName}}) Get(ctx context.Context, id int64) (*{{$mdName}}, error) { - var row {{$mdName}} - - err := b.db.Model(&{{$mdName}}{}). - Where("id = ?", id). - Take(&row).Error - if err != nil { - return nil, err - } - return &row, nil -} - -func (b {{$stName}}) GetByFilter(ctx context.Context, q *{{$queryPrefix}}Get{{$stName}}ByFilter) (*{{$mdName}}, error) { - var row {{$mdName}} - - err := b.db.Model(&{{$mdName}}{}). - Scopes(func(db *gorm.DB) *gorm.DB { - {{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where("{{$f.ColumnName}} = ?", q.{{$f.GoName}}) - } - {{- end}} - return db - }). - Take(&row).Error - if err != nil { - return nil, err - } - return &row, nil -} - -func (b {{$stName}}) ExistByFilter(ctx context.Context, q *{{$queryPrefix}}Exist{{$stName}}ByFilter) (existed bool, err error) { - err = b.db.Model(&{{$mdName}}{}). - Select("1"). - Scopes(func(db *gorm.DB) *gorm.DB { - {{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where("{{$f.ColumnName}} = ?", q.{{$f.GoName}}) - } - {{- end}} - return db - }). - Scan(&existed).Error - return existed, err -} - -func (b {{$stName}}) Count(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) (total int64, err error) { - err = b.db.Model(&{{$mdName}}{}). - Scopes(list{{$stName}}Filter(q)). - Count(&total).Error - return total, err -} - -func (b {{$stName}}) List(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, error) { - var rows []*{{$mdName}} - - err := b.db.Model(&{{$mdName}}{}). - Scopes(list{{$stName}}Filter(q), Limit(q.Page, q.PerPage)). - Find(&rows).Error - return rows, err -} - -func (b {{$stName}}) ListPage(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, int64, error) { - var total int64 - var rows []*{{$mdName}} - - db := b.db.Model(&{{$mdName}}{}). - Scopes(list{{$stName}}Filter(q)) - - err := db.Count(&total).Error - if err != nil { - return nil, 0, err - } - if total > 0 { - err = db.Scopes(Pagination(q.Page, q.PerPage)). - Find(&rows).Error - if err != nil { - return nil, 0, err - } - } - return rows, total, nil -} - -func (b {{$stName}}) PluckIdByFilter(ctx context.Context, q *{{$queryPrefix}}PluckId{{$stName}}ByFilter) ([]int64, error) { - var rows []int64 - - err := b.db.Model(&{{$mdName}}{}). - Scopes(func(db *gorm.DB) *gorm.DB { - {{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where("{{$f.ColumnName}} = ?", q.{{$f.GoName}}) - } - {{- end}} - return db - }). - Pluck("id", &rows).Error - return rows, err -} - - -func list{{$stName}}Filter(q *{{$queryPrefix}}List{{$stName}}ByFilter) func(db *gorm.DB) *gorm.DB { - return func(db *gorm.DB) *gorm.DB { -{{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where("{{$f.ColumnName}} = ?", q.{{$f.GoName}}) - } -{{- end}} - return db - } -} - diff --git a/cmd/ormat/command/template/dal_option.tpl b/cmd/ormat/command/template/dal_option.tpl deleted file mode 100644 index 4b472cd..0000000 --- a/cmd/ormat/command/template/dal_option.tpl +++ /dev/null @@ -1,71 +0,0 @@ -package {{.Package}} - -import ( - "gorm.io/gorm" - "gorm.io/gorm/clause" -) - -var ( - // DefaultPerPage 默认页大小 - DefaultPerPage = int64(50) - // DefaultPageSize 默认最大页大小 - DefaultMaxPerPage = int64(500) -) - -// Pagination 分页器 -// 分页索引: page >= 1 -// 分页大小: perPage >= 1 && <= DefaultMaxPerPage -func Pagination(page, perPage int64, maxPerPages ...int64) func(db *gorm.DB) *gorm.DB { - maxPerPage := DefaultMaxPerPage - if len(maxPerPages) > 0 && maxPerPages[0] > 0 { - maxPerPage = maxPerPages[0] - } - if page < 1 { - page = 1 - } - switch { - case perPage < 1: - perPage = DefaultPerPage - case perPage > maxPerPage: - perPage = maxPerPage - default: // do nothing - } - limit, offset := int(perPage), int(perPage*(page-1)) - l := clause.Limit{ - Limit: &limit, - Offset: offset, - } - return func(db *gorm.DB) *gorm.DB { - return db.Clauses(l) - } -} - -// 限制器 -// offset = perPage * (page - 1) -// limit = perPage -// if limit > 0: use limit -// if offset > 0: use offset -func Limit(page, perPage int) func(*gorm.DB) *gorm.DB { - offset := 0 - if page > 0 { - offset = perPage * (page - 1) - } - limit := perPage - return func(db *gorm.DB) *gorm.DB { - if offset > 0 || limit > 0 { - l := clause.Limit{ - Limit: new(int), - Offset: offset, - } - if offset > 0 { - l.Offset = offset - } - if limit > 0 { - l.Limit = &limit - } - return db.Clauses(l) - } else { - return db - } - } -} \ No newline at end of file diff --git a/cmd/ormat/command/template/dal_query.tpl b/cmd/ormat/command/template/dal_query.tpl deleted file mode 100644 index 727f157..0000000 --- a/cmd/ormat/command/template/dal_query.tpl +++ /dev/null @@ -1,36 +0,0 @@ -package {{.Package}} - -{{- $e := .Entity}} -{{- $stName := pascalcase $e.Name}} - -type Update{{$stName}}ByPartial struct { -{{- range $f := $e.Fields}} - {{$f.GoName}} {{if ne $f.GoName "Id"}}*{{- end}}{{$f.Type.Ident}} `json:"{{smallcamelcase $f.ColumnName}}"` -{{- end}} -} - -type Get{{$stName}}ByFilter struct { -{{- range $f := $e.Fields}} - {{$f.GoName}} {{$f.Type.Ident}} `json:"{{smallcamelcase $f.ColumnName}}"` -{{- end}} -} - -type Exist{{$stName}}ByFilter struct { -{{- range $f := $e.Fields}} - {{$f.GoName}} {{$f.Type.Ident}} `json:"{{smallcamelcase $f.ColumnName}}"` -{{- end}} -} - -type List{{$stName}}ByFilter struct { -{{- range $f := $e.Fields}} - {{$f.GoName}} {{$f.Type.Ident}} `json:"{{smallcamelcase $f.ColumnName}}"` -{{- end}} - Page int64 `json:"page"` - PerPage int64 `json:"perPage"` -} - -type PluckId{{$stName}}ByFilter struct { -{{- range $f := $e.Fields}} - {{$f.GoName}} {{$f.Type.Ident}} `json:"{{smallcamelcase $f.ColumnName}}"` -{{- end}} -} \ No newline at end of file diff --git a/cmd/ormat/command/template/dal_rapier.tpl b/cmd/ormat/command/template/dal_rapier.tpl deleted file mode 100644 index 853a5b5..0000000 --- a/cmd/ormat/command/template/dal_rapier.tpl +++ /dev/null @@ -1,202 +0,0 @@ -package {{.Package}} - -import ( - "context" - - "gorm.io/gorm" - rapier "github.com/thinkgos/gorm-rapier" - -{{- range $e := .Imports}} - {{- if ne $e ""}} - "{{$e}}" - {{- end}} -{{- end}} -) - -{{- $e := .Entity}} -{{- $stName := pascalcase $e.Name}} -{{- $mdPrefix := .ModelPrefix}} -{{- $queryPrefix := .QueryPrefix}} -{{- $repoPrefix := .RepoPrefix}} -{{- $mdName := printf "%s%s" $mdPrefix $stName}} - -var _ {{$stName}}Dal = (*{{$stName}})(nil) -var _ = rapier.NewExecutor[{{$stName}}] - -type {{$stName}}Dal interface { - Create(ctx context.Context, v ...*{{$mdName}}) (int64, error) - Delete(ctx context.Context, id ...int64) (int64, error) - UpdateFull(ctx context.Context, v *{{$mdName}}) (int64, error) - UpdatePartial(ctx context.Context, v *{{$queryPrefix}}Update{{$stName}}ByPartial) (int64, error) - Get(ctx context.Context, id int64) (*{{$mdName}}, error) - GetByFilter(ctx context.Context, q *{{$queryPrefix}}Get{{$stName}}ByFilter) (*{{$mdName}}, error) - ExistByFilter(ctx context.Context, q *{{$queryPrefix}}Exist{{$stName}}ByFilter) (bool, error) - Count(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) (int64, error) - List(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, error) - ListPage(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, int64, error) - PluckIdByFilter(ctx context.Context, q *{{$queryPrefix}}PluckId{{$stName}}ByFilter) ([]int64, error) -} - -type {{$stName}} struct { - db *gorm.DB -} - -func New{{$stName}}(db *gorm.DB) {{$stName}}Dal { - return {{$stName}} { - db: db, - } -} - -func (b {{$stName}}) Create(ctx context.Context, v ...*{{$mdName}}) (int64, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Create(v...) -} - -func (b {{$stName}}) Delete(ctx context.Context, id ...int64) (int64, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Where(ref.Id.In(id...)). - Delete() -} - - -func (b {{$stName}}) UpdateFull(ctx context.Context, v *{{$mdName}}) (int64, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Select("*"). - Where(ref.Id.Eq(v.Id)). - Updates(v) -} - -func (b {{$stName}}) UpdatePartial(ctx context.Context, v *{{$queryPrefix}}Update{{$stName}}ByPartial) (int64, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - up := make([]rapier.AssignExpr, {{add (len $stName) 8}}) -{{- range $f := $e.Fields}} - {{- if ne $f.GoName "Id"}} - if v.{{$f.GoName}} != nil { - up = append(up, ref.{{$f.GoName}}.Value(*v.{{$f.GoName}})) - } - {{- end}} -{{- end}} - if len(up) == 0 { - return 0, nil - } - return ref.New_Executor(b.db).Model(). - Where(ref.Id.Eq(v.Id)). - UpdatesExpr(up...) -} - -func (b {{$stName}}) Get(ctx context.Context, id int64) (*{{$mdName}}, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - SelectExpr(ref.Select_Expr()...). - Where(ref.Id.Eq(id)). - TakeOne() -} - -func (b {{$stName}}) GetByFilter(ctx context.Context, q *{{$queryPrefix}}Get{{$stName}}ByFilter) (*{{$mdName}}, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Scopes(func(db *gorm.DB) *gorm.DB { - {{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where(ref.{{$f.GoName}}.Eq(q.{{$f.GoName}})) - } - {{- end}} - return db - }). - TakeOne() -} - -func (b {{$stName}}) ExistByFilter(ctx context.Context, q *{{$queryPrefix}}Exist{{$stName}}ByFilter) (existed bool, err error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Scopes(func(db *gorm.DB) *gorm.DB { - {{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where("{{$f.ColumnName}} = ?", q.{{$f.GoName}}) - } - {{- end}} - return db - }). - Exist() -} - -func (b {{$stName}}) Count(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) (total int64, err error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Scopes(list{{$stName}}Filter(ref, q)). - Count() -} - - -func (b {{$stName}}) List(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Scopes(list{{$stName}}Filter(ref, q), Limit(q.Page, q.PerPage)). - FindAll() -} - -func (b {{$stName}}) ListPage(ctx context.Context, q *{{$queryPrefix}}List{{$stName}}ByFilter) ([]*{{$mdName}}, int64, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Scopes(list{{$stName}}Filter(ref, q)). - FindAllPaginate(q.Page, q.PerPage) -} - -func (b {{$stName}}) PluckIdByFilter(ctx context.Context, q *{{$queryPrefix}}PluckId{{$stName}}ByFilter) ([]int64, error) { - ref := {{$repoPrefix}}Ref_{{$stName}}() - return ref.New_Executor(b.db).Model(). - Scopes(func(db *gorm.DB) *gorm.DB { - {{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where("{{$f.ColumnName}} = ?", q.{{$f.GoName}}) - } - {{- end}} - return db - }). - PluckExprInt64(ref.Id) -} - -func list{{$stName}}Filter(ref *{{$repoPrefix}}Announce_Native, q *{{$queryPrefix}}List{{$stName}}ByFilter) func(db *gorm.DB) *gorm.DB { - return func(db *gorm.DB) *gorm.DB { -{{- range $f := $e.Fields}} - {{- if eq $f.Type.Type 15 }} - if q.{{$f.GoName}} != "" { - {{- else if eq $f.Type.Type 18 }} - if !q.{{$f.GoName}}.IsZero() { - {{- else if eq $f.Type.Type 1 }} - { - {{- else }} - if q.{{$f.GoName}} != 0 { - {{- end}} - db = db.Where(ref.{{$f.GoName}}.Eq(q.{{$f.GoName}})) - } -{{- end}} - return db - } -} \ No newline at end of file