Documentation ¶
Index ¶
- Constants
- func CamelCaseToUnderscore(s string) string
- func GetFuncNamePath(fn interface{}) string
- func ListDir(dirPth string, suffix string) (files []string, err error)
- func StoreTpl(key, content string)
- func SupportGenericType() bool
- func ToLower(s string) string
- func ToUpper(s string) string
- func UnderscoreToLowerCamelCase(s string) string
- func UnderscoreToUpperCamelCase(s string) string
- func WalkDir(dirPth, suffix string) (files []string, err error)
- type BaseConfig
- type ColumnInfo
- type DaoMeta
- type IBaseData
- type MetaType
- func GetMetaType(dbType gormx.DBType, columnType string) (metaType MetaType)
- func MySQLTrans(columnType string) (metaType MetaType)
- func OracleTrans(columnType string) (metaType MetaType)
- func PostgresTrans(columnType string) (metaType MetaType)
- func SQLServerTrans(columnType string) (metaType MetaType)
- type ModelMeta
- type PrimaryKeyInfo
- type Template
Constants ¶
View Source
const Dao = NotEditMark + `
package {{.DaoPackageName}}
import (
"context"
"{{.ModelModulePath}}"
)
type {{.ModelStructName}}Dao interface {
// SelectAll 查询所有记录
SelectAll(ctx context.Context, selectFields ...model.{{.ModelStructName}}Field) (records []*model.{{.ModelStructName}}, err error)
// SelectOneByPrimaryKey 通过主键查询记录
SelectOneByPrimaryKey(ctx context.Context, {{range .PrimaryKeyList}}{{.GoColumnName}} {{.GoColumnOriginType}}, {{end}}selectFields ...model.{{.ModelStructName}}Field) (record *model.{{.ModelStructName}}, err error)
// SelectRecordByCondition 通过指定条件查询记录
SelectRecordByCondition(ctx context.Context, condition *model.Condition, selectFields ...model.{{.ModelStructName}}Field) (records []*model.{{.ModelStructName}}, err error)
// SelectPageRecordByCondition 通过指定条件查询分页记录
SelectPageRecordByCondition(ctx context.Context, condition *model.Condition, pageParam *model.Pagination,
selectFields ...model.{{.ModelStructName}}Field) (records []*model.{{.ModelStructName}}, err error)
// CountByCondition 通过指定条件查询记录数量
CountByCondition(ctx context.Context, condition *model.Condition) (count int64, err error)
// DeleteByCondition 通过指定条件删除记录,返回删除记录数量
DeleteByCondition(ctx context.Context, condition *model.Condition) (affect int64, err error)
// DeleteByPrimaryKey 通过主键删除记录,返回删除记录数量
DeleteByPrimaryKey(ctx context.Context{{range .PrimaryKeyList}}, {{.GoColumnName}} {{.GoColumnOriginType}}{{end}}) (affect int64, err error)
// UpdateRecord 更新记录
UpdateRecord(ctx context.Context, record *model.{{.ModelStructName}}) (affect int64, err error)
// UpdateRecords 批量更新记录
UpdateRecords(ctx context.Context, records []*model.{{.ModelStructName}}) (affect int64, err error)
// UpdateByCondition 更新指定条件下的记录
UpdateByCondition(ctx context.Context, condition *model.Condition, updateField model.UpdateField) (affect int64, err error)
// UpdateByPrimaryKey 更新主键的记录
UpdateByPrimaryKey(ctx context.Context, {{range .PrimaryKeyList}}{{.GoColumnName}} {{.GoColumnOriginType}}, {{end}}updateField model.UpdateField) (affect int64, err error)
// Insert 插入记录
Insert(ctx context.Context, record *model.{{.ModelStructName}}) (affect int64, err error)
// BatchInsert 批量插入记录
BatchInsert(ctx context.Context, records []*model.{{.ModelStructName}}) (affect int64, err error)
// InsertOrUpdateOnDuplicateKey 插入记录,假如唯一键冲突则更新
InsertOrUpdateOnDuplicateKey(ctx context.Context, record *model.{{.ModelStructName}}) (affect int64, err error)
// BatchInsertOrUpdateOnDuplicateKey 批量插入记录,假如唯一键冲突则更新
BatchInsertOrUpdateOnDuplicateKey(ctx context.Context, records []*model.{{.ModelStructName}}) (affect int64, err error)
}
`
View Source
const DaoImpl = NotEditMark + `
package impl
import (
"context"
"strings"
"gorm.io/gorm/clause"
"{{.DaoModulePath}}"
"{{.ModelModulePath}}"
)
var {{.ModelLowerCamelName}}Dao {{.DaoPackageName}}.{{.ModelStructName}}Dao = &{{.ModelLowerCamelName}}DaoImpl{}
func Get{{.ModelStructName}}Dao() {{.DaoPackageName}}.{{.ModelStructName}}Dao {
return {{.ModelLowerCamelName}}Dao
}
type {{.ModelLowerCamelName}}DaoImpl struct{}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) SelectAll(ctx context.Context, selectFields ...model.{{.ModelStructName}}Field) (records []*model.{{.ModelStructName}}, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}})
if len(selectFields) > 0 {
columns := make([]string, 0)
for _, field := range selectFields {
columns = append(columns, string(field))
}
tx = tx.Select(strings.Join(columns, ","))
}
err = tx.Find(&records).Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) SelectOneByPrimaryKey(ctx context.Context, {{range .PrimaryKeyList}}{{.GoColumnName}} {{.GoColumnOriginType}}, {{end}}selectFields ...model.{{.ModelStructName}}Field) (record *model.{{.ModelStructName}}, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}})
if len(selectFields) > 0 {
columns := make([]string, 0)
for _, field := range selectFields {
columns = append(columns, string(field))
}
tx = tx.Select(strings.Join(columns, ","))
}
whereCondition := map[string]any{
{{ range .PrimaryKeyList -}}
"{{- .GoFieldName -}}": {{- .GoColumnName }},
{{ end }}
}
err = tx.Where(whereCondition).First(&record).Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) SelectRecordByCondition(ctx context.Context, condition *model.Condition, selectFields ...model.{{.ModelStructName}}Field) (records []*model.{{.ModelStructName}}, err error) {
if condition == nil {
return {{.ModelShortName}}.SelectAll(ctx, selectFields...)
}
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}})
if len(selectFields) > 0 {
columns := make([]string, 0)
for _, field := range selectFields {
columns = append(columns, string(field))
}
tx = tx.Select(strings.Join(columns, ","))
}
for _, strCondition := range condition.StringCondition {
tx = tx.Where(strCondition)
}
if len(condition.MapCondition) > 0 {
tx = tx.Where(condition.MapCondition)
}
for _, order := range condition.OrderByClause {
tx = tx.Order(order)
}
err = tx.Find(&records).Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) SelectPageRecordByCondition(ctx context.Context, condition *model.Condition, pageParam *model.Pagination,
selectFields ...model.{{.ModelStructName}}Field) (records []*model.{{.ModelStructName}}, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}})
if len(selectFields) > 0 {
columns := make([]string, 0)
for _, field := range selectFields {
columns = append(columns, string(field))
}
tx = tx.Select(strings.Join(columns, ","))
}
if condition != nil {
for _, strCondition := range condition.StringCondition {
tx = tx.Where(strCondition)
}
if len(condition.MapCondition) > 0 {
tx = tx.Where(condition.MapCondition)
}
for _, order := range condition.OrderByClause {
tx = tx.Order(order)
}
}
var count int64
if pageParam != nil {
tx = tx.Count(&count).Offset(int(pageParam.CalculateOffset())).Limit(int(pageParam.PageSize))
}
err = tx.Find(&records).Error
if pageParam != nil {
pageParam.Total = count
pageParam.CalculatePageCount()
}
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) CountByCondition(ctx context.Context, condition *model.Condition) (count int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}})
if condition != nil {
for _, strCondition := range condition.StringCondition {
tx = tx.Where(strCondition)
}
if len(condition.MapCondition) > 0 {
tx = tx.Where(condition.MapCondition)
}
}
err = tx.Count(&count).Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) DeleteByCondition(ctx context.Context, condition *model.Condition) (affect int64, err error) {
tx := DB().WithContext(ctx)
if condition != nil {
for _, strCondition := range condition.StringCondition {
tx = tx.Where(strCondition)
}
if len(condition.MapCondition) > 0 {
tx = tx.Where(condition.MapCondition)
}
}
tx = tx.Delete(&model.{{.ModelStructName}}{})
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) DeleteByPrimaryKey(ctx context.Context{{range .PrimaryKeyList}}, {{.GoColumnName}} {{.GoColumnOriginType}}{{end}}) (affect int64, err error) {
whereCondition := map[string]any{
{{ range .PrimaryKeyList -}}
"{{- .GoFieldName -}}": {{- .GoColumnName }},
{{ end }}
}
tx := DB().WithContext(ctx).Where(whereCondition).Delete(&model.{{.ModelStructName}}{})
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) UpdateRecord(ctx context.Context, record *model.{{.ModelStructName}}) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Save(record)
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) UpdateRecords(ctx context.Context, records []*model.{{.ModelStructName}}) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Save(records)
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) UpdateByCondition(ctx context.Context, condition *model.Condition, updateField model.UpdateField) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}})
if condition != nil {
for _, strCondition := range condition.StringCondition {
tx = tx.Where(strCondition)
}
if len(condition.MapCondition) > 0 {
tx = tx.Where(condition.MapCondition)
}
}
tx = tx.Updates(map[string]any(updateField))
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) UpdateByPrimaryKey(ctx context.Context, {{range .PrimaryKeyList}}{{.GoColumnName}} {{.GoColumnOriginType}}, {{end}}updateField model.UpdateField) (affect int64, err error) {
whereCondition := map[string]any{
{{ range .PrimaryKeyList -}}
"{{- .GoFieldName -}}": {{- .GoColumnName }},
{{ end }}
}
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Where(whereCondition)
tx = tx.Updates(map[string]any(updateField))
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) Insert(ctx context.Context, record *model.{{.ModelStructName}}) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Create(&record)
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) BatchInsert(ctx context.Context, records []*model.{{.ModelStructName}}) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Create(&records)
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) InsertOrUpdateOnDuplicateKey(ctx context.Context, record *model.{{.ModelStructName}}) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(&record)
affect = tx.RowsAffected
err = tx.Error
return
}
func ({{.ModelShortName}} {{.ModelLowerCamelName}}DaoImpl) BatchInsertOrUpdateOnDuplicateKey(ctx context.Context, records []*model.{{.ModelStructName}}) (affect int64, err error) {
tx := DB().WithContext(ctx).
Table(model.TableName{{.ModelStructName}}).
Clauses(clause.OnConflict{
UpdateAll: true,
}).Create(&records)
affect = tx.RowsAffected
err = tx.Error
return
}
`
View Source
const Database = NotEditMark + `
package impl
import "gorm.io/gorm"
var gormDB *gorm.DB
func SetGormDB(db *gorm.DB) {
if db == nil {
panic("db connection is nil")
}
gormDB = db
return
}
func DB() *gorm.DB {
if gormDB == nil {
panic("db connection is nil")
}
return gormDB
}
`
View Source
const EditMark = `
// Code generated by jasonlabz/gentol. You may edit it.
`
View Source
const Model = NotEditMark + `
package {{.ModelPackageName}}
import (
"database/sql"
"fmt"
"time"
"github.com/guregu/null"
"github.com/satori/go.uuid"
{{range .ImportPkgList}}{{.}} ` + "\n" + `{{end}}
)
var (
_ = time.Second
_ = sql.LevelDefault
_ = null.Bool{}
_ = uuid.UUID{}
)
{{if .TableName -}}
{{if eq .DBType "postgres" -}}
{{if and .SchemaName (ne .SchemaName "public") -}}
const TableName{{.ModelStructName}} = "{{if .SchemaQuota -}}\"{{.SchemaName}}\"{{- else}}{{.SchemaName}}{{- end}}.{{if .TableQuota -}}\"{{.TableName}}\"{{- else}}{{.TableName}}{{- end}}"
{{- else -}}
const TableName{{.ModelStructName}} = "{{if .TableQuota -}}\"{{.TableName}}\"{{- else}}{{.TableName}}{{- end}}"
{{ end }}
{{- else if eq .DBType "sqlserver" -}}
{{if ne .SchemaName "dbo" -}}
const TableName{{.ModelStructName}} = "{{.SchemaName}}.{{.TableName}}"
{{- else -}}
const TableName{{.ModelStructName}} = "{{.TableName}}"
{{end}}
{{- else}}
const TableName{{.ModelStructName}} = "{{.TableName}}"
{{- end}}
{{- end}}
type {{.TitleTableName}}Field string
// {{.ModelStructName}} struct is mapping to the {{.TableName}} table
type {{.ModelStructName}} struct {
{{range .ColumnList}}
{{if eq .GoColumnName "TableName" }}{{.GoColumnName}}_{{ else }}{{.GoColumnName}}{{ end }} {{.GoColumnType}} ` + "`{{.Tags}}` " +
"// Comment: {{if .Comment}}{{.Comment}}{{else}}no comment{{end}} " +
`{{end}}
}
func ({{.ModelShortName}} *{{.ModelStructName}}) TableName() string {
return "{{.TableName}}"
}
type {{.ModelStructName}}TableColumn struct {
{{range .ColumnList}}
{{- .GoColumnName}} {{.TitleTableName}}Field
{{end}}
}
type {{.ModelStructName}}Condition struct {
Condition
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) ColumnInfo() {{.ModelStructName}}TableColumn {
return {{.ModelStructName}}TableColumn{
{{range .ColumnList}}
{{- .GoColumnName}}: "{{.ColumnName}}",
{{end}}
}
}
{{range .ColumnList}}
{{if eq .GoColumnOriginType "string"}}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}IsLike(value string) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} like '%v'", value)
}
{{end}}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}IsNull() *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} is null")
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}IsNotNull() *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} is not null")
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}EqualTo(value {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} = {{.ValueFormat}}", value)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}NotEqualTo(value {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} <> {{.ValueFormat}}", value)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}GreaterThan(value {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} > {{.ValueFormat}}", value)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}GreaterThanOrEqualTo(value {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} >= {{.ValueFormat}}", value)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}LessThan(value {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} < {{.ValueFormat}}", value)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}LessThanOrEqualTo(value {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} <= {{.ValueFormat}}", value)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}Between(startValue, endValue {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} between {{.ValueFormat}} and {{.ValueFormat}}", startValue, endValue)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}NotBetween(startValue, endValue {{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} not between {{.ValueFormat}} and {{.ValueFormat}}", startValue, endValue)
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}In(inValues []{{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where(TransInCondition("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} in ",inValues))
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) {{.GoColumnName}}NotIn(inValues []{{.GoColumnOriginType}}) *{{.ModelStructName}}Condition {
return {{.ModelShortName}}.Where(TransInCondition("{{if .ColumnQuota -}}\"{{.ColumnName}}\"{{- else}}{{.ColumnName}}{{- end}} not in ",inValues))
}
{{end}}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) Where(query any, args ...any) *{{.ModelStructName}}Condition {
switch query.(type) {
case map[string]any:
mapCondition := query.(map[string]any)
if {{.ModelShortName}}.MapCondition == nil {
{{.ModelShortName}}.MapCondition =mapCondition
break
}
for key, val := range mapCondition {
{{.ModelShortName}}.MapCondition[key] = val
}
case string:
condition:=query.(string)
{{.ModelShortName}}.StringCondition = append({{.ModelShortName}}.StringCondition, fmt.Sprintf(condition, args...))
}
return {{.ModelShortName}}
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) OrderBy(orderByClause ...string) *{{.ModelStructName}}Condition {
{{.ModelShortName}}.OrderByClause = append({{.ModelShortName}}.OrderByClause, orderByClause...)
return {{.ModelShortName}}
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) GroupBy(groupByClause string) *{{.ModelStructName}}Condition {
{{.ModelShortName}}.GroupByClause = groupByClause
return {{.ModelShortName}}
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) Having(query string, args ...any) *{{.ModelStructName}}Condition {
{{.ModelShortName}}.HavingCondition = fmt.Sprintf(query, args...)
return {{.ModelShortName}}
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) Joins(query string, args ...any) *{{.ModelStructName}}Condition {
{{.ModelShortName}}.JoinCondition = append({{.ModelShortName}}.JoinCondition, fmt.Sprintf(query, args...))
return {{.ModelShortName}}
}
func ({{.ModelShortName}} *{{.ModelStructName}}Condition) Build() *Condition {
return &{{.ModelShortName}}.Condition
}
`
Model used as a variable because it cannot load template file after packed, params still can pass file
View Source
const ModelBase = NotEditMark + `
package {{.ModelPackageName}}
import (
"fmt"
"math"
"strings"
)
type ConditionBuilder interface {
Build() *Condition
}
type Condition struct {
JoinCondition []string
MapCondition map[string]any
StringCondition []string
GroupByClause string
HavingCondition string
OrderByClause []string
}
type UpdateField map[string]any
// Pagination 分页结构体(该分页只适合数据量很少的情况)
type Pagination struct {
Page int64 ` + "`json:\"page\"` // 当前页\n" +
"PageSize int64 " + "`json:\"page_size\"` // 每页多少条记录\n" +
"PageCount int64 " + "`json:\"page_count\"` // 一共多少页\n" +
"Total int64 " + "`json:\"total\"` // 一共多少条记录" + `
}
func (p *Pagination) CalculatePageCount() {
if p.Page == 0 || p.PageSize == 0 {
panic("error pagination param")
}
p.PageCount = int64(math.Ceil(float64(p.Total) / float64(p.PageSize)))
return
}
func (p *Pagination) CalculateOffset() (offset int64) {
if p.Page == 0 || p.PageSize == 0 {
panic("error pagination param")
}
offset = (p.Page - 1) * p.PageSize
return
}
func Values(value any) string {
switch value.(type) {
case int, int8, int16, int32, int64, bool, float32, float64:
return fmt.Sprintf("%v", value)
default:
return fmt.Sprintf("'%v'", value)
}
}
func TransInCondition[T any](prefix string, values []T) string {
res := make([]string, 0)
numbers := len(values) / 1000
for i := 0; i < numbers; i++ {
items := make([]string, 0)
for j := i * 1000; j < (i+1)*1000; j++ {
items = append(items, Values(values[j]))
}
res = append(res, fmt.Sprintf("%s (%s)", prefix, strings.Join(items, ",")))
}
items := make([]string, 0)
for i := numbers * 1000; i < numbers*1000+len(values)%1000; i++ {
items = append(items, Values(values[i]))
}
res = append(res, fmt.Sprintf("%s (%s)", prefix, strings.Join(items, ",")))
return strings.Join(res, " or ")
}
`
ModelBase base file (no overwrite if file is existed)
View Source
const ModelHook = EditMark + `
package {{.ModelPackageName}}
import (
"gorm.io/gorm"
)
// BeforeSave invoked before saving, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) BeforeSave(tx *gorm.DB) (err error) {
// TODO: something
return
}
// AfterSave invoked after saving, return an error if field is not populated.
func ({{.ModelShortName}} *{{.ModelStructName}}) AfterSave(tx *gorm.DB) (err error) {
// TODO: something
return
}
// BeforeCreate invoked before create, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) BeforeCreate(tx *gorm.DB) (err error) {
// TODO: something
return
}
// AfterCreate invoked after create, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) AfterCreate(tx *gorm.DB) (err error) {
// TODO: something
return
}
// BeforeUpdate invoked before update, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) BeforeUpdate(tx *gorm.DB) (err error) {
// TODO: something
return
}
// AfterUpdate invoked after update, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) AfterUpdate(tx *gorm.DB) (err error) {
// TODO: something
return
}
// BeforeDelete invoked before delete, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) BeforeDelete(tx *gorm.DB) (err error) {
// TODO: something
return
}
// AfterDelete invoked after delete, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) AfterDelete(tx *gorm.DB) (err error) {
// TODO: something
return
}
// AfterFind invoked after find, return an error.
func ({{.ModelShortName}} *{{.ModelStructName}}) AfterFind(tx *gorm.DB) (err error) {
// TODO: something
return
}
`
ModelHook hook file (no overwrite if file is existed), provide func BeforeCreate、AfterUpdate、BeforeDelete etc.
View Source
const NotEditMark = `` /* 158-byte string literal not displayed */
Variables ¶
This section is empty.
Functions ¶
func CamelCaseToUnderscore ¶
CamelCaseToUnderscore 驼峰单词转下划线单词
func SupportGenericType ¶
func SupportGenericType() bool
func UnderscoreToLowerCamelCase ¶
UnderscoreToLowerCamelCase 下划线单词转为小写驼峰单词
func UnderscoreToUpperCamelCase ¶
UnderscoreToUpperCamelCase 下划线单词转为大写驼峰单词
Types ¶
type BaseConfig ¶
type ColumnInfo ¶
type ColumnInfo struct { Index int UpperTableName string TitleTableName string GoColumnName string GoUpperColumnName string GoColumnType string // string GoColumnOriginType string // string ValueFormat string // string Tags string ModelPackageName string ModelStructName string ModelShortName string SchemaName string TableQuota bool TableName string ColumnQuota bool ColumnName string SQLNullableType string GureguNullableType string ColumnType string // varchar(64) DataBaseType string // varchar Length int64 // 64 IsPrimaryKey bool Unique bool AutoIncrement bool Nullable bool Comment string DefaultValue string }
type DaoMeta ¶
type DaoMeta struct { BaseConfig ModelModulePath string ModelPackageName string ModelStructName string DaoModulePath string DaoPackageName string PrimaryKeyList []*PrimaryKeyInfo ColumnList []*ColumnInfo }
func (*DaoMeta) GenRenderData ¶
type MetaType ¶
type MetaType struct { GoType string SQLNullableType string GureguNullableType string ValueFormat string }
func MySQLTrans ¶
func OracleTrans ¶
func PostgresTrans ¶
func SQLServerTrans ¶
type ModelMeta ¶
type ModelMeta struct { BaseConfig ModelPackageName string ModelStructName string ImportPkgList []string ColumnList []*ColumnInfo }
func (*ModelMeta) GenRenderData ¶
type PrimaryKeyInfo ¶
Click to show internal directories.
Click to hide internal directories.