github.com/unionj-cloud/go-doudou/v2@v2.3.5/toolkit/gormgen/internal/template/struct.go (about)

     1  package template
     2  
     3  const (
     4  	// TableQueryStruct table query struct
     5  	TableQueryStruct = createMethod + `
     6  	type {{.QueryStructName}} struct {
     7  		{{.QueryStructName}}Do
     8  		` + fields + `
     9  	}
    10  	` + tableMethod + asMethond + updateFieldMethod + getFieldMethod + getFieldExprByName + fillFieldMapMethod + cloneMethod + replaceMethod + relationship + defineMethodStruct
    11  
    12  	// TableQueryStructWithContext table query struct with context
    13  	TableQueryStructWithContext = createMethod + `
    14  	type {{.QueryStructName}} struct {
    15  		{{.QueryStructName}}Do {{.QueryStructName}}Do
    16  		` + fields + `
    17  	}
    18  	` + tableMethod + asMethond + updateFieldMethod + `
    19  	
    20  	func ({{.S}} *{{.QueryStructName}}) WithContext(ctx context.Context) {{.ReturnObject}} { return {{.S}}.{{.QueryStructName}}Do.WithContext(ctx)}
    21  
    22  	func ({{.S}} {{.QueryStructName}}) TableName() string { return {{.S}}.{{.QueryStructName}}Do.TableName() } 
    23  
    24  	func ({{.S}} {{.QueryStructName}}) Alias() string { return {{.S}}.{{.QueryStructName}}Do.Alias() }
    25  
    26  	func ({{.S}} {{.QueryStructName}}) Columns(cols ...field.Expr) gormgen.Columns { return {{.S}}.{{.QueryStructName}}Do.Columns(cols...) }
    27  
    28  	` + getFieldMethod + getFieldExprByName + fillFieldMapMethod + cloneMethod + replaceMethod + relationship + defineMethodStruct
    29  
    30  	// TableQueryIface table query interface
    31  	TableQueryIface = defineDoInterface
    32  )
    33  
    34  const (
    35  	createMethod = `
    36  	func new{{.ModelStructName}}(db *gorm.DB, opts ...gormgen.DOOption) {{.QueryStructName}} {
    37  		_{{.QueryStructName}} := {{.QueryStructName}}{}
    38  	
    39  		_{{.QueryStructName}}.{{.QueryStructName}}Do.UseDB(db,opts...)
    40  		_{{.QueryStructName}}.{{.QueryStructName}}Do.UseModel(&{{.StructInfo.Package}}.{{.StructInfo.Type}}{})
    41  	
    42  		tableName := _{{.QueryStructName}}.{{.QueryStructName}}Do.TableName()
    43  		_{{$.QueryStructName}}.ALL = field.NewAsterisk(tableName)
    44  		{{range .Fields -}}
    45  		{{if not .IsRelation -}}
    46  			{{- if .ColumnName -}}_{{$.QueryStructName}}.{{.Name}} = field.New{{.GenType}}(tableName, "{{.ColumnName}}"){{- end -}}
    47  		{{- else -}}
    48  			_{{$.QueryStructName}}.{{.Relation.Name}} = {{$.QueryStructName}}{{.Relation.RelationshipName}}{{.Relation.Name}}{
    49  				db: db.Session(&gorm.Session{}),
    50  
    51  				{{.Relation.StructFieldInit}}
    52  			}
    53  		{{end}}
    54  		{{end}}
    55  
    56  		_{{$.QueryStructName}}.fillFieldMap()
    57  		
    58  		return _{{.QueryStructName}}
    59  	}
    60  	`
    61  	fields = `
    62  	ALL field.Asterisk
    63  	{{range .Fields -}}
    64  		{{if not .IsRelation -}}
    65  			{{if .MultilineComment -}}
    66  			/*
    67  {{.ColumnComment}}
    68      		*/
    69  			{{end -}}
    70  			{{- if .ColumnName -}}{{.Name}} field.{{.GenType}}{{if not .MultilineComment}}{{if .ColumnComment}}// {{.ColumnComment}}{{end}}{{end}}{{- end -}}
    71  		{{- else -}}
    72  			{{.Relation.Name}} {{$.QueryStructName}}{{.Relation.RelationshipName}}{{.Relation.Name}}
    73  		{{end}}
    74  	{{end}}
    75  
    76  	fieldMap  map[string]field.Expr
    77  `
    78  	tableMethod = `
    79  func ({{.S}} {{.QueryStructName}}) Table(newTableName string) *{{.QueryStructName}} { 
    80  	{{.S}}.{{.QueryStructName}}Do.UseTable(newTableName)
    81  	return {{.S}}.updateTableName(newTableName)
    82  }
    83  `
    84  
    85  	asMethond = `	
    86  func ({{.S}} {{.QueryStructName}}) As(alias string) *{{.QueryStructName}} { 
    87  	{{.S}}.{{.QueryStructName}}Do.DO = *({{.S}}.{{.QueryStructName}}Do.As(alias).(*gormgen.DO))
    88  	return {{.S}}.updateTableName(alias)
    89  }
    90  `
    91  	updateFieldMethod = `
    92  func ({{.S}} *{{.QueryStructName}}) updateTableName(table string) *{{.QueryStructName}} { 
    93  	{{.S}}.ALL = field.NewAsterisk(table)
    94  	{{range .Fields -}}
    95  	{{if not .IsRelation -}}
    96  		{{- if .ColumnName -}}{{$.S}}.{{.Name}} = field.New{{.GenType}}(table, "{{.ColumnName}}"){{- end -}}
    97  	{{end}}
    98  	{{end}}
    99  	
   100  	{{.S}}.fillFieldMap()
   101  
   102  	return {{.S}}
   103  }
   104  `
   105  
   106  	cloneMethod = `
   107  func ({{.S}} {{.QueryStructName}}) clone(db *gorm.DB) {{.QueryStructName}} {
   108  	{{.S}}.{{.QueryStructName}}Do.ReplaceConnPool(db.Statement.ConnPool)
   109  	return {{.S}}
   110  }
   111  `
   112  	replaceMethod = `
   113  func ({{.S}} {{.QueryStructName}}) replaceDB(db *gorm.DB) {{.QueryStructName}} {
   114  	{{.S}}.{{.QueryStructName}}Do.ReplaceDB(db)
   115  	return {{.S}}
   116  }
   117  `
   118  	getFieldMethod = `
   119  func ({{.S}} *{{.QueryStructName}}) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
   120  	_f, ok := {{.S}}.fieldMap[fieldName]
   121  	if !ok || _f == nil {
   122  		return nil, false
   123  	}
   124  	_oe,ok := _f.(field.OrderExpr)
   125  	return _oe,ok
   126  }
   127  `
   128  	getFieldExprByName = `
   129  func ({{.S}} *{{.QueryStructName}}) GetFieldExprByName(fieldName string) (field.Expr, bool) {
   130  	_f, ok := {{.S}}.fieldMap[fieldName]
   131  	if !ok || _f == nil {
   132  		return nil, false
   133  	}
   134  	return _f, ok
   135  }
   136  `
   137  	relationship = `{{range .Fields}}{{if .IsRelation}}` +
   138  		`{{- $relation := .Relation }}{{- $relationship := $relation.RelationshipName}}` +
   139  		relationStruct + relationTx +
   140  		`{{end}}{{end}}`
   141  	defineMethodStruct = `type {{.QueryStructName}}Do struct { gormgen.DO }`
   142  
   143  	fillFieldMapMethod = `
   144  func ({{.S}} *{{.QueryStructName}}) fillFieldMap() {
   145  	{{.S}}.fieldMap =  make(map[string]field.Expr, {{len .Fields}})
   146  	{{range .Fields -}}
   147  	{{if not .IsRelation -}}
   148  		{{- if .ColumnName -}}{{$.S}}.fieldMap["{{.ColumnName}}"] = {{$.S}}.{{.Name}}{{- end -}}
   149  	{{end}}
   150  	{{end -}}
   151  }
   152  `
   153  
   154  	defineDoInterface = `
   155  
   156  type I{{.ModelStructName}}Do interface {
   157  	gormgen.SubQuery
   158  	Debug() I{{.ModelStructName}}Do
   159  	WithContext(ctx context.Context) I{{.ModelStructName}}Do
   160  	WithResult(fc func(tx gormgen.Dao)) gormgen.ResultInfo
   161  	ReplaceDB(db *gorm.DB)
   162  	ReadDB() I{{.ModelStructName}}Do
   163  	WriteDB() I{{.ModelStructName}}Do
   164  	As(alias string) gormgen.Dao
   165  	Session(config *gorm.Session) I{{.ModelStructName}}Do
   166  	Columns(cols ...field.Expr) gormgen.Columns
   167  	Clauses(conds ...clause.Expression) I{{.ModelStructName}}Do
   168  	Not(conds ...gormgen.Condition) I{{.ModelStructName}}Do
   169  	Or(conds ...gormgen.Condition) I{{.ModelStructName}}Do
   170  	Select(conds ...field.Expr) I{{.ModelStructName}}Do
   171  	Where(conds ...gormgen.Condition) I{{.ModelStructName}}Do
   172  	Order(conds ...field.Expr) I{{.ModelStructName}}Do
   173  	Distinct(cols ...field.Expr) I{{.ModelStructName}}Do
   174  	Omit(cols ...field.Expr) I{{.ModelStructName}}Do
   175  	Join(table schema.Tabler, on ...field.Expr) I{{.ModelStructName}}Do
   176  	LeftJoin(table schema.Tabler, on ...field.Expr) I{{.ModelStructName}}Do
   177  	RightJoin(table schema.Tabler, on ...field.Expr) I{{.ModelStructName}}Do
   178  	Group(cols ...field.Expr) I{{.ModelStructName}}Do
   179  	Having(conds ...gormgen.Condition) I{{.ModelStructName}}Do
   180  	Limit(limit int) I{{.ModelStructName}}Do
   181  	Offset(offset int) I{{.ModelStructName}}Do
   182  	Count() (count int64, err error)
   183  	Scopes(funcs ...func(gormgen.Dao) gormgen.Dao) I{{.ModelStructName}}Do
   184  	Unscoped() I{{.ModelStructName}}Do
   185  	Create(values ...*{{.StructInfo.Package}}.{{.StructInfo.Type}}) error
   186  	CreateInBatches(values []*{{.StructInfo.Package}}.{{.StructInfo.Type}}, batchSize int) error
   187  	Save(values ...*{{.StructInfo.Package}}.{{.StructInfo.Type}}) error
   188  	First() (*{{.StructInfo.Package}}.{{.StructInfo.Type}}, error)
   189  	Take() (*{{.StructInfo.Package}}.{{.StructInfo.Type}}, error)
   190  	Last() (*{{.StructInfo.Package}}.{{.StructInfo.Type}}, error)
   191  	Find() ([]*{{.StructInfo.Package}}.{{.StructInfo.Type}}, error)
   192  	FindInBatch(batchSize int, fc func(tx gormgen.Dao, batch int) error) (results []*{{.StructInfo.Package}}.{{.StructInfo.Type}}, err error)
   193  	FindInBatches(result *[]*{{.StructInfo.Package}}.{{.StructInfo.Type}}, batchSize int, fc func(tx gormgen.Dao, batch int) error) error
   194  	Pluck(column field.Expr, dest interface{}) error
   195  	Delete(...*{{.StructInfo.Package}}.{{.StructInfo.Type}}) (info gormgen.ResultInfo, err error)
   196  	Update(column field.Expr, value interface{}) (info gormgen.ResultInfo, err error)
   197  	UpdateSimple(columns ...field.AssignExpr) (info gormgen.ResultInfo, err error)
   198  	Updates(value interface{}) (info gormgen.ResultInfo, err error)
   199  	UpdateColumn(column field.Expr, value interface{}) (info gormgen.ResultInfo, err error)
   200  	UpdateColumnSimple(columns ...field.AssignExpr) (info gormgen.ResultInfo, err error)
   201  	UpdateColumns(value interface{}) (info gormgen.ResultInfo, err error)
   202  	UpdateFrom(q gormgen.SubQuery) gormgen.Dao
   203  	Attrs(attrs ...field.AssignExpr) I{{.ModelStructName}}Do
   204  	Assign(attrs ...field.AssignExpr) I{{.ModelStructName}}Do
   205  	Joins(fields ...field.RelationField) I{{.ModelStructName}}Do
   206  	Preload(fields ...field.RelationField) I{{.ModelStructName}}Do
   207  	FirstOrInit() (*{{.StructInfo.Package}}.{{.StructInfo.Type}}, error)
   208  	FirstOrCreate() (*{{.StructInfo.Package}}.{{.StructInfo.Type}}, error)
   209  	FindByPage(offset int, limit int) (result []*{{.StructInfo.Package}}.{{.StructInfo.Type}}, count int64, err error)
   210  	ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
   211  	Scan(result interface{}) (err error)
   212  	Fetch(result interface{}) (err error)
   213  	Returning(value interface{}, columns ...string) I{{.ModelStructName}}Do
   214  	UnderlyingDB() *gorm.DB
   215  	schema.Tabler
   216  
   217  	{{range .Interfaces -}}
   218  	{{.FuncSign}}
   219  	{{end}}
   220  }
   221  `
   222  )
   223  
   224  const (
   225  	relationStruct = `
   226  type {{$.QueryStructName}}{{$relationship}}{{$relation.Name}} struct{
   227  	db *gorm.DB
   228  	
   229  	field.RelationField
   230  	
   231  	{{$relation.StructField}}
   232  }
   233  
   234  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}) Where(conds ...field.Expr) *{{$.QueryStructName}}{{$relationship}}{{$relation.Name}} {
   235  	if len(conds) == 0 {
   236  		return &a
   237  	}
   238  
   239  	exprs := make([]clause.Expression, 0, len(conds))
   240  	for _, cond := range conds {
   241  		exprs = append(exprs, cond.BeCond().(clause.Expression))
   242  	}
   243  	a.db = a.db.Clauses(clause.Where{Exprs: exprs})
   244  	return &a
   245  }
   246  
   247  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}) WithContext(ctx context.Context) *{{$.QueryStructName}}{{$relationship}}{{$relation.Name}} {
   248  	a.db = a.db.WithContext(ctx)
   249  	return &a
   250  }
   251  
   252  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}) Session(session *gorm.Session) *{{$.QueryStructName}}{{$relationship}}{{$relation.Name}} {
   253  	a.db = a.db.Session(session)
   254  	return &a
   255  }
   256  
   257  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}) Model(m *{{$.StructInfo.Package}}.{{$.StructInfo.Type}}) *{{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx {
   258  	return &{{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx{a.db.Model(m).Association(a.Name())}
   259  }
   260  
   261  `
   262  	relationTx = `
   263  type {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx struct{ tx *gorm.Association }
   264  
   265  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx) Find() (result {{if eq $relationship "HasMany" "ManyToMany"}}[]{{end}}*{{$relation.Type}}, err error) {
   266  	return result, a.tx.Find(&result)
   267  }
   268  
   269  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx) Append(values ...*{{$relation.Type}}) (err error) {
   270  	targetValues := make([]interface{}, len(values))
   271  	for i, v := range values {
   272  		targetValues[i] = v
   273  	}
   274  	return a.tx.Append(targetValues...)
   275  }
   276  
   277  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx) Replace(values ...*{{$relation.Type}}) (err error) {
   278  	targetValues := make([]interface{}, len(values))
   279  	for i, v := range values {
   280  		targetValues[i] = v
   281  	}
   282  	return a.tx.Replace(targetValues...)
   283  }
   284  
   285  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx) Delete(values ...*{{$relation.Type}}) (err error) {
   286  	targetValues := make([]interface{}, len(values))
   287  	for i, v := range values {
   288  		targetValues[i] = v
   289  	}
   290  	return a.tx.Delete(targetValues...)
   291  }
   292  
   293  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx) Clear() error {
   294  	return a.tx.Clear()
   295  }
   296  
   297  func (a {{$.QueryStructName}}{{$relationship}}{{$relation.Name}}Tx) Count() int64 {
   298  	return a.tx.Count()
   299  }
   300  `
   301  )