Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var OutputTemplate = parseTemplateOrPanic("output_template", `
///////////////////////////////////////////////////////////
// THIS FILE IS AUTO GENERATED by gormgen, DON'T EDIT IT //
// ANY CHANGES DONE HERE WILL BE LOST //
///////////////////////////////////////////////////////////
package {{.PkgName}}
import (
"fmt"
"time"
"gitee.com/carlmax_my/console-core-go/pkg/mysql"
{{.RefStructPkgPath}}
"github.com/pkg/errors"
"gorm.io/gorm"
)
func NewModel() *{{.RefStructName}} {
return new({{.RefStructName}})
}
func NewQueryBuilder() *{{.QueryBuilderName}} {
return new({{.QueryBuilderName}})
}
func Create(db *gorm.DB, t *{{.RefStructName}}) (id {{.PrimaryKeyType}}, err error) {
if err = db.Create(t).Error; err != nil {
return {{.PrimaryKeyDef}}, errors.Wrap(err, "create err")
}
return t.Id, nil
}
type {{.QueryBuilderName}} struct {
order []string
where []struct {
prefix string
value interface{}
}
limit int
offset int
}
func (qb *{{.QueryBuilderName}}) buildQuery(db *gorm.DB) *gorm.DB {
ret := db
for _, where := range qb.where {
ret = ret.Where(where.prefix, where.value)
}
for _, order := range qb.order {
ret = ret.Order(order)
}
ret = ret.Limit(qb.limit).Offset(qb.offset)
return ret
}
func (qb *{{.QueryBuilderName}}) Updates(db *gorm.DB, m map[string]interface{}) (err error) {
db = db.Model(&{{.RefStructName}}{})
for _, where := range qb.where {
db.Where(where.prefix, where.value)
}
if err = db.Updates(m).Error; err != nil {
return errors.Wrap(err, "updates err")
}
return nil
}
func (qb *{{.QueryBuilderName}}) Delete(db *gorm.DB) (err error) {
for _, where := range qb.where {
db = db.Where(where.prefix, where.value)
}
if err = db.Delete(&{{.RefStructName}}{}).Error; err != nil {
return errors.Wrap(err, "delete err")
}
return nil
}
func (qb *{{.QueryBuilderName}}) Count(db *gorm.DB) (int64, error) {
var c int64
res := qb.buildQuery(db).Model(&{{.RefStructName}}{}).Count(&c)
if res.Error != nil && res.Error == gorm.ErrRecordNotFound {
c = 0
}
return c, res.Error
}
func (qb *{{.QueryBuilderName}}) First(db *gorm.DB) (*{{.RefStructName}}, error) {
ret := &{{.RefStructName}}{}
res := qb.buildQuery(db).First(ret)
if res.Error != nil && res.Error == gorm.ErrRecordNotFound {
ret = nil
}
return ret, res.Error
}
func (qb *{{.QueryBuilderName}}) QueryOne(db *gorm.DB) (*{{.RefStructName}}, error) {
qb.limit = 1
ret, err := qb.QueryAll(db)
if len(ret) > 0 {
return &ret[0], err
}
return nil, err
}
func (qb *{{.QueryBuilderName}}) QueryAll(db *gorm.DB) ([]{{.RefStructName}}, error) {
var ret []{{.RefStructName}}
err := qb.buildQuery(db).Find(&ret).Error
return ret, err
}
func (qb *{{.QueryBuilderName}}) Limit(limit int) *{{.QueryBuilderName}} {
qb.limit = limit
return qb
}
func (qb *{{.QueryBuilderName}}) Offset(offset int) *{{.QueryBuilderName}} {
qb.offset = offset
return qb
}
{{$queryBuilderName := .QueryBuilderName}}
{{range .OptionFields}}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}(p mysql.Predicate, value {{.FieldType}}) *{{$queryBuilderName}} {
qb.where = append(qb.where, struct {
prefix string
value interface{}
}{
fmt.Sprintf("%v %v ?", "{{.ColumnName}}", p),
value,
})
return qb
}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}In(value []{{.FieldType}}) *{{$queryBuilderName}} {
qb.where = append(qb.where, struct {
prefix string
value interface{}
}{
fmt.Sprintf("%v %v ?", "{{.ColumnName}}", "IN"),
value,
})
return qb
}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}NotIn(value []{{.FieldType}}) *{{$queryBuilderName}} {
qb.where = append(qb.where, struct {
prefix string
value interface{}
}{
fmt.Sprintf("%v %v ?", "{{.ColumnName}}", "NOT IN"),
value,
})
return qb
}
func (qb *{{$queryBuilderName}}) OrderBy{{call $.Helpers.Titlize .FieldName}}(asc bool) *{{$queryBuilderName}} {
order := "DESC"
if asc {
order = "ASC"
}
qb.order = append(qb.order, "{{.ColumnName}} " + order)
return qb
}
{{end}}
`)
View Source
var OutputTemplate_mongo = parseTemplateOrPanic("output_template_mongo", `
///////////////////////////////////////////////////////////
// THIS FILE IS AUTO GENERATED by gormgen, DON'T EDIT IT //
// ANY CHANGES DONE HERE WILL BE LOST //
///////////////////////////////////////////////////////////
package {{.PkgName}}
import (
"time"
"gitee.com/carlmax_my/console-core-go/pkg/mongo"
{{.RefStructPkgPath}}
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
)
var CollName = "{{.PkgName}}"
func NewModel() *{{.RefStructName}} {
return new({{.RefStructName}})
}
func NewQueryBuilder() *{{.QueryBuilderName}} {
return new({{.QueryBuilderName}})
}
func Create(col *Coll, t *{{.RefStructName}}) (id {{.PrimaryKeyType}}, err error) {
if err = col.InsertOne(t); err != nil {
return {{.PrimaryKeyDef}}, errors.Wrap(err, "create err")
}
return t.Id, nil
}
// region query builder
type {{.QueryBuilderName}} struct {
mongo.ModelQueryBuilder[{{.RefStructName}}]
}
{{$queryBuilderName := .QueryBuilderName}}
{{range .OptionFields}}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}(p mongo.Predicate, value {{.FieldType}}) *{{$queryBuilderName}} {
qb.AddWhere("{{.ColumnName}}", value, p)
return qb
}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}Eq(value {{.FieldType}}) *{{$queryBuilderName}} {
qb.AddWhere("{{.ColumnName}}", value, mongo.EqualPredicate)
return qb
}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}NEq(value {{.FieldType}}) *{{$queryBuilderName}} {
qb.AddWhere("{{.ColumnName}}", value, mongo.NotEqualPredicate)
return qb
}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}In(value []{{.FieldType}}) *{{$queryBuilderName}} {
qb.AddWhere("{{.ColumnName}}", value, mongo.InPredicate)
return qb
}
func (qb *{{$queryBuilderName}}) Where{{call $.Helpers.Titlize .FieldName}}NotIn(value []{{.FieldType}}) *{{$queryBuilderName}} {
qb.AddWhere("{{.ColumnName}}", value, mongo.NotInPredicate)
return qb
}
func (qb *{{$queryBuilderName}}) OrderBy{{call $.Helpers.Titlize .FieldName}}(asc bool) *{{$queryBuilderName}} {
qb.AddOrder("{{.ColumnName}}", asc)
return qb
}
{{end}}
// endregion query builder
// region coll
type Coll struct {
mongo.Collection[{{.RefStructName}}]
}
func NewColl(db mongo.MongoDB) *Coll {
c := &Coll{}
c.Collection = *mongo.NewColl[{{.RefStructName}}](db, mongo.SetColl(CollName))
return c
}
func (c *Coll) PatchById(id string, doc *{{.RefStructName}}Patch) error {
return c.UpdateOneByIdStr(&id, doc)
}
func (c *Coll) Patch(filter *bson.M, doc *{{.RefStructName}}Patch) error {
return c.UpdateOneByFilter(filter, doc)
}
{{$RefStructName := .RefStructName}}
{{range .OptionFields}}
func (c *Coll) FindBy{{call $.Helpers.Titlize .FieldName}}(value {{.FieldType}}) (*{{$RefStructName}}, error) {
filter := &bson.M{"{{.ColumnName}}": value}
return c.FindOneByFilter(filter)
}
func (c *Coll) FindAllBy{{call $.Helpers.Titlize .FieldName}}(value {{.FieldType}}) ([]{{$RefStructName}}, error) {
filter := &bson.M{"{{.ColumnName}}": value}
return c.FindAllByFilter(filter)
}
func (c *Coll) FindAllPagedBy{{call $.Helpers.Titlize .FieldName}}(value {{.FieldType}}, page int64, pageSize int64) ([]{{$RefStructName}}, error) {
filter := &bson.M{"{{.ColumnName}}": value}
return c.FindAllPagedByFilter(filter, page, pageSize)
}
{{if .CanPatch}}
func (c *Coll) Patch{{call $.Helpers.Titlize .FieldName}}ById(id string, value *{{.FieldType}}) error {
doc := new({{$RefStructName}}Patch)
doc.{{call $.Helpers.Titlize .FieldName}} = value
return c.UpdateOneByIdStr(&id, doc)
}
func (c *Coll) Patch{{call $.Helpers.Titlize .FieldName}}(filter *bson.M, value *{{.FieldType}}) error {
doc := new({{$RefStructName}}Patch)
doc.{{call $.Helpers.Titlize .FieldName}} = value
return c.UpdateOneByFilter(filter, doc)
}
{{end}}
{{end}}
// endregion coll
`)
Functions ¶
func SQLColumnToHumpStyle ¶
SQLColumnToHumpStyle sql转换成驼峰模式
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
The Generator is the one responsible for generating the code, adding the imports, formatting, and writing it to the file.
func NewGenerator ¶
func NewGenerator(inputFolder, outputFolder, pkgName string, template *template.Template) *Generator
NewGenerator function creates an instance of the generator given the name of the output file as an argument.
Click to show internal directories.
Click to hide internal directories.