auto generate query & models
This commit is contained in:
@@ -5,6 +5,8 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"atom/container"
|
"atom/container"
|
||||||
|
"errors"
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"go.uber.org/dig"
|
"go.uber.org/dig"
|
||||||
@@ -25,22 +27,40 @@ type GenQueryGenerator struct {
|
|||||||
// FilterWithNameAndRole(name, role string) ([]gen.T, error)
|
// FilterWithNameAndRole(name, role string) ([]gen.T, error)
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// genCmd represents the gen command
|
// modelCmd represents the gen command
|
||||||
var genCmd = &cobra.Command{
|
var modelCmd = &cobra.Command{
|
||||||
Use: "gen",
|
Use: "model",
|
||||||
Short: "gorm query generator",
|
Short: "gorm model&query generator",
|
||||||
Long: `gorm query generator. more info, see https://gorm.io/gen`,
|
Long: `gorm model&query generator. more info, see https://gorm.io/gen`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return container.Container.Invoke(func(gq GenQueryGenerator) error {
|
return container.Container.Invoke(func(gq GenQueryGenerator) error {
|
||||||
|
var tables []string
|
||||||
|
err := gq.DB.Raw("show tables").Scan(&tables).Error
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if len(tables) == 0 {
|
||||||
|
return errors.New("no tables in database, run migrate first")
|
||||||
|
}
|
||||||
|
|
||||||
g := gen.NewGenerator(gen.Config{
|
g := gen.NewGenerator(gen.Config{
|
||||||
OutPath: "providers/query",
|
OutPath: "database/query",
|
||||||
Mode: gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
|
OutFile: "query.gen.go",
|
||||||
|
ModelPkgPath: "database/models",
|
||||||
|
FieldSignable: true,
|
||||||
|
FieldWithTypeTag: true,
|
||||||
|
Mode: gen.WithDefaultQuery | gen.WithQueryInterface,
|
||||||
})
|
})
|
||||||
|
|
||||||
g.UseDB(gq.DB) // reuse your gorm db
|
g.UseDB(gq.DB) // reuse your gorm db
|
||||||
|
|
||||||
|
models := []interface{}{}
|
||||||
|
for _, table := range tables {
|
||||||
|
models = append(models, g.GenerateModel(table))
|
||||||
|
}
|
||||||
|
|
||||||
// Generate basic type-safe DAO API for struct `model.User` following conventions
|
// Generate basic type-safe DAO API for struct `model.User` following conventions
|
||||||
g.ApplyBasic()
|
g.ApplyBasic(models...)
|
||||||
|
|
||||||
// Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
|
// Generate Type Safe API with Dynamic SQL defined on Querier interface for `model.User` and `model.Company`
|
||||||
// g.ApplyInterface(func(Querier) {}, model.User{}, model.Company{})
|
// g.ApplyInterface(func(Querier) {}, model.User{}, model.Company{})
|
||||||
@@ -53,5 +73,5 @@ var genCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rootCmd.AddCommand(genCmd)
|
rootCmd.AddCommand(modelCmd)
|
||||||
}
|
}
|
||||||
17
database/models/migrations.gen.go
Normal file
17
database/models/migrations.gen.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
|
||||||
|
package models
|
||||||
|
|
||||||
|
const TableNameMigration = "migrations"
|
||||||
|
|
||||||
|
// Migration mapped from table <migrations>
|
||||||
|
type Migration struct {
|
||||||
|
ID string `gorm:"column:id;type:varchar(255);primaryKey" json:"id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TableName Migration's table name
|
||||||
|
func (*Migration) TableName() string {
|
||||||
|
return TableNameMigration
|
||||||
|
}
|
||||||
392
database/query/migrations.gen.go
Normal file
392
database/query/migrations.gen.go
Normal file
@@ -0,0 +1,392 @@
|
|||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
|
||||||
|
package query
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
"gorm.io/gorm/clause"
|
||||||
|
"gorm.io/gorm/schema"
|
||||||
|
|
||||||
|
"gorm.io/gen"
|
||||||
|
"gorm.io/gen/field"
|
||||||
|
|
||||||
|
"gorm.io/plugin/dbresolver"
|
||||||
|
|
||||||
|
"atom/database/models"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newMigration(db *gorm.DB, opts ...gen.DOOption) migration {
|
||||||
|
_migration := migration{}
|
||||||
|
|
||||||
|
_migration.migrationDo.UseDB(db, opts...)
|
||||||
|
_migration.migrationDo.UseModel(&models.Migration{})
|
||||||
|
|
||||||
|
tableName := _migration.migrationDo.TableName()
|
||||||
|
_migration.ALL = field.NewAsterisk(tableName)
|
||||||
|
_migration.ID = field.NewString(tableName, "id")
|
||||||
|
|
||||||
|
_migration.fillFieldMap()
|
||||||
|
|
||||||
|
return _migration
|
||||||
|
}
|
||||||
|
|
||||||
|
type migration struct {
|
||||||
|
migrationDo migrationDo
|
||||||
|
|
||||||
|
ALL field.Asterisk
|
||||||
|
ID field.String
|
||||||
|
|
||||||
|
fieldMap map[string]field.Expr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migration) Table(newTableName string) *migration {
|
||||||
|
m.migrationDo.UseTable(newTableName)
|
||||||
|
return m.updateTableName(newTableName)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migration) As(alias string) *migration {
|
||||||
|
m.migrationDo.DO = *(m.migrationDo.As(alias).(*gen.DO))
|
||||||
|
return m.updateTableName(alias)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *migration) updateTableName(table string) *migration {
|
||||||
|
m.ALL = field.NewAsterisk(table)
|
||||||
|
m.ID = field.NewString(table, "id")
|
||||||
|
|
||||||
|
m.fillFieldMap()
|
||||||
|
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *migration) WithContext(ctx context.Context) IMigrationDo {
|
||||||
|
return m.migrationDo.WithContext(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migration) TableName() string { return m.migrationDo.TableName() }
|
||||||
|
|
||||||
|
func (m migration) Alias() string { return m.migrationDo.Alias() }
|
||||||
|
|
||||||
|
func (m *migration) GetFieldByName(fieldName string) (field.OrderExpr, bool) {
|
||||||
|
_f, ok := m.fieldMap[fieldName]
|
||||||
|
if !ok || _f == nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
_oe, ok := _f.(field.OrderExpr)
|
||||||
|
return _oe, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *migration) fillFieldMap() {
|
||||||
|
m.fieldMap = make(map[string]field.Expr, 1)
|
||||||
|
m.fieldMap["id"] = m.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migration) clone(db *gorm.DB) migration {
|
||||||
|
m.migrationDo.ReplaceConnPool(db.Statement.ConnPool)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migration) replaceDB(db *gorm.DB) migration {
|
||||||
|
m.migrationDo.ReplaceDB(db)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
type migrationDo struct{ gen.DO }
|
||||||
|
|
||||||
|
type IMigrationDo interface {
|
||||||
|
gen.SubQuery
|
||||||
|
Debug() IMigrationDo
|
||||||
|
WithContext(ctx context.Context) IMigrationDo
|
||||||
|
WithResult(fc func(tx gen.Dao)) gen.ResultInfo
|
||||||
|
ReplaceDB(db *gorm.DB)
|
||||||
|
ReadDB() IMigrationDo
|
||||||
|
WriteDB() IMigrationDo
|
||||||
|
As(alias string) gen.Dao
|
||||||
|
Session(config *gorm.Session) IMigrationDo
|
||||||
|
Columns(cols ...field.Expr) gen.Columns
|
||||||
|
Clauses(conds ...clause.Expression) IMigrationDo
|
||||||
|
Not(conds ...gen.Condition) IMigrationDo
|
||||||
|
Or(conds ...gen.Condition) IMigrationDo
|
||||||
|
Select(conds ...field.Expr) IMigrationDo
|
||||||
|
Where(conds ...gen.Condition) IMigrationDo
|
||||||
|
Order(conds ...field.Expr) IMigrationDo
|
||||||
|
Distinct(cols ...field.Expr) IMigrationDo
|
||||||
|
Omit(cols ...field.Expr) IMigrationDo
|
||||||
|
Join(table schema.Tabler, on ...field.Expr) IMigrationDo
|
||||||
|
LeftJoin(table schema.Tabler, on ...field.Expr) IMigrationDo
|
||||||
|
RightJoin(table schema.Tabler, on ...field.Expr) IMigrationDo
|
||||||
|
Group(cols ...field.Expr) IMigrationDo
|
||||||
|
Having(conds ...gen.Condition) IMigrationDo
|
||||||
|
Limit(limit int) IMigrationDo
|
||||||
|
Offset(offset int) IMigrationDo
|
||||||
|
Count() (count int64, err error)
|
||||||
|
Scopes(funcs ...func(gen.Dao) gen.Dao) IMigrationDo
|
||||||
|
Unscoped() IMigrationDo
|
||||||
|
Create(values ...*models.Migration) error
|
||||||
|
CreateInBatches(values []*models.Migration, batchSize int) error
|
||||||
|
Save(values ...*models.Migration) error
|
||||||
|
First() (*models.Migration, error)
|
||||||
|
Take() (*models.Migration, error)
|
||||||
|
Last() (*models.Migration, error)
|
||||||
|
Find() ([]*models.Migration, error)
|
||||||
|
FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*models.Migration, err error)
|
||||||
|
FindInBatches(result *[]*models.Migration, batchSize int, fc func(tx gen.Dao, batch int) error) error
|
||||||
|
Pluck(column field.Expr, dest interface{}) error
|
||||||
|
Delete(...*models.Migration) (info gen.ResultInfo, err error)
|
||||||
|
Update(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
|
||||||
|
UpdateSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
|
||||||
|
Updates(value interface{}) (info gen.ResultInfo, err error)
|
||||||
|
UpdateColumn(column field.Expr, value interface{}) (info gen.ResultInfo, err error)
|
||||||
|
UpdateColumnSimple(columns ...field.AssignExpr) (info gen.ResultInfo, err error)
|
||||||
|
UpdateColumns(value interface{}) (info gen.ResultInfo, err error)
|
||||||
|
UpdateFrom(q gen.SubQuery) gen.Dao
|
||||||
|
Attrs(attrs ...field.AssignExpr) IMigrationDo
|
||||||
|
Assign(attrs ...field.AssignExpr) IMigrationDo
|
||||||
|
Joins(fields ...field.RelationField) IMigrationDo
|
||||||
|
Preload(fields ...field.RelationField) IMigrationDo
|
||||||
|
FirstOrInit() (*models.Migration, error)
|
||||||
|
FirstOrCreate() (*models.Migration, error)
|
||||||
|
FindByPage(offset int, limit int) (result []*models.Migration, count int64, err error)
|
||||||
|
ScanByPage(result interface{}, offset int, limit int) (count int64, err error)
|
||||||
|
Scan(result interface{}) (err error)
|
||||||
|
Returning(value interface{}, columns ...string) IMigrationDo
|
||||||
|
UnderlyingDB() *gorm.DB
|
||||||
|
schema.Tabler
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Debug() IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Debug())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) WithContext(ctx context.Context) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.WithContext(ctx))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) ReadDB() IMigrationDo {
|
||||||
|
return m.Clauses(dbresolver.Read)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) WriteDB() IMigrationDo {
|
||||||
|
return m.Clauses(dbresolver.Write)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Session(config *gorm.Session) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Session(config))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Clauses(conds ...clause.Expression) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Clauses(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Returning(value interface{}, columns ...string) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Returning(value, columns...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Not(conds ...gen.Condition) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Not(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Or(conds ...gen.Condition) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Or(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Select(conds ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Select(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Where(conds ...gen.Condition) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Where(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Exists(subquery interface{ UnderlyingDB() *gorm.DB }) IMigrationDo {
|
||||||
|
return m.Where(field.CompareSubQuery(field.ExistsOp, nil, subquery.UnderlyingDB()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Order(conds ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Order(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Distinct(cols ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Distinct(cols...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Omit(cols ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Omit(cols...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Join(table schema.Tabler, on ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Join(table, on...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) LeftJoin(table schema.Tabler, on ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.LeftJoin(table, on...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) RightJoin(table schema.Tabler, on ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.RightJoin(table, on...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Group(cols ...field.Expr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Group(cols...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Having(conds ...gen.Condition) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Having(conds...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Limit(limit int) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Limit(limit))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Offset(offset int) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Offset(offset))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Scopes(funcs...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Unscoped() IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Unscoped())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Create(values ...*models.Migration) error {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return m.DO.Create(values)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) CreateInBatches(values []*models.Migration, batchSize int) error {
|
||||||
|
return m.DO.CreateInBatches(values, batchSize)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save : !!! underlying implementation is different with GORM
|
||||||
|
// The method is equivalent to executing the statement: db.Clauses(clause.OnConflict{UpdateAll: true}).Create(values)
|
||||||
|
func (m migrationDo) Save(values ...*models.Migration) error {
|
||||||
|
if len(values) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return m.DO.Save(values)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) First() (*models.Migration, error) {
|
||||||
|
if result, err := m.DO.First(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return result.(*models.Migration), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Take() (*models.Migration, error) {
|
||||||
|
if result, err := m.DO.Take(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return result.(*models.Migration), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Last() (*models.Migration, error) {
|
||||||
|
if result, err := m.DO.Last(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return result.(*models.Migration), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Find() ([]*models.Migration, error) {
|
||||||
|
result, err := m.DO.Find()
|
||||||
|
return result.([]*models.Migration), err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*models.Migration, err error) {
|
||||||
|
buf := make([]*models.Migration, 0, batchSize)
|
||||||
|
err = m.DO.FindInBatches(&buf, batchSize, func(tx gen.Dao, batch int) error {
|
||||||
|
defer func() { results = append(results, buf...) }()
|
||||||
|
return fc(tx, batch)
|
||||||
|
})
|
||||||
|
return results, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) FindInBatches(result *[]*models.Migration, batchSize int, fc func(tx gen.Dao, batch int) error) error {
|
||||||
|
return m.DO.FindInBatches(result, batchSize, fc)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Attrs(attrs ...field.AssignExpr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Attrs(attrs...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Assign(attrs ...field.AssignExpr) IMigrationDo {
|
||||||
|
return m.withDO(m.DO.Assign(attrs...))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Joins(fields ...field.RelationField) IMigrationDo {
|
||||||
|
for _, _f := range fields {
|
||||||
|
m = *m.withDO(m.DO.Joins(_f))
|
||||||
|
}
|
||||||
|
return &m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Preload(fields ...field.RelationField) IMigrationDo {
|
||||||
|
for _, _f := range fields {
|
||||||
|
m = *m.withDO(m.DO.Preload(_f))
|
||||||
|
}
|
||||||
|
return &m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) FirstOrInit() (*models.Migration, error) {
|
||||||
|
if result, err := m.DO.FirstOrInit(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return result.(*models.Migration), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) FirstOrCreate() (*models.Migration, error) {
|
||||||
|
if result, err := m.DO.FirstOrCreate(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return result.(*models.Migration), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) FindByPage(offset int, limit int) (result []*models.Migration, count int64, err error) {
|
||||||
|
result, err = m.Offset(offset).Limit(limit).Find()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if size := len(result); 0 < limit && 0 < size && size < limit {
|
||||||
|
count = int64(size + offset)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err = m.Offset(-1).Limit(-1).Count()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
|
||||||
|
count, err = m.Count()
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = m.Offset(offset).Limit(limit).Scan(result)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Scan(result interface{}) (err error) {
|
||||||
|
return m.DO.Scan(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m migrationDo) Delete(models ...*models.Migration) (result gen.ResultInfo, err error) {
|
||||||
|
return m.DO.Delete(models)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *migrationDo) withDO(do gen.Dao) *migrationDo {
|
||||||
|
m.DO = *do.(*gen.DO)
|
||||||
|
return m
|
||||||
|
}
|
||||||
99
database/query/query.gen.go
Normal file
99
database/query/query.gen.go
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
// Code generated by gorm.io/gen. DO NOT EDIT.
|
||||||
|
|
||||||
|
package query
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
|
|
||||||
|
"gorm.io/gen"
|
||||||
|
|
||||||
|
"gorm.io/plugin/dbresolver"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Q = new(Query)
|
||||||
|
Migration *migration
|
||||||
|
)
|
||||||
|
|
||||||
|
func SetDefault(db *gorm.DB, opts ...gen.DOOption) {
|
||||||
|
*Q = *Use(db, opts...)
|
||||||
|
Migration = &Q.Migration
|
||||||
|
}
|
||||||
|
|
||||||
|
func Use(db *gorm.DB, opts ...gen.DOOption) *Query {
|
||||||
|
return &Query{
|
||||||
|
db: db,
|
||||||
|
Migration: newMigration(db, opts...),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query struct {
|
||||||
|
db *gorm.DB
|
||||||
|
|
||||||
|
Migration migration
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) Available() bool { return q.db != nil }
|
||||||
|
|
||||||
|
func (q *Query) clone(db *gorm.DB) *Query {
|
||||||
|
return &Query{
|
||||||
|
db: db,
|
||||||
|
Migration: q.Migration.clone(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) ReadDB() *Query {
|
||||||
|
return q.clone(q.db.Clauses(dbresolver.Read))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) WriteDB() *Query {
|
||||||
|
return q.clone(q.db.Clauses(dbresolver.Write))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) ReplaceDB(db *gorm.DB) *Query {
|
||||||
|
return &Query{
|
||||||
|
db: db,
|
||||||
|
Migration: q.Migration.replaceDB(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type queryCtx struct {
|
||||||
|
Migration IMigrationDo
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) WithContext(ctx context.Context) *queryCtx {
|
||||||
|
return &queryCtx{
|
||||||
|
Migration: q.Migration.WithContext(ctx),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) Transaction(fc func(tx *Query) error, opts ...*sql.TxOptions) error {
|
||||||
|
return q.db.Transaction(func(tx *gorm.DB) error { return fc(q.clone(tx)) }, opts...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Query) Begin(opts ...*sql.TxOptions) *QueryTx {
|
||||||
|
return &QueryTx{q.clone(q.db.Begin(opts...))}
|
||||||
|
}
|
||||||
|
|
||||||
|
type QueryTx struct{ *Query }
|
||||||
|
|
||||||
|
func (q *QueryTx) Commit() error {
|
||||||
|
return q.db.Commit().Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QueryTx) Rollback() error {
|
||||||
|
return q.db.Rollback().Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QueryTx) SavePoint(name string) error {
|
||||||
|
return q.db.SavePoint(name).Error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *QueryTx) RollbackTo(name string) error {
|
||||||
|
return q.db.RollbackTo(name).Error
|
||||||
|
}
|
||||||
@@ -6,5 +6,4 @@ import (
|
|||||||
_ "atom/providers/http"
|
_ "atom/providers/http"
|
||||||
_ "atom/providers/logger"
|
_ "atom/providers/logger"
|
||||||
_ "atom/providers/micro"
|
_ "atom/providers/micro"
|
||||||
_ "atom/providers/query"
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package query
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"atom/container"
|
"atom/container"
|
||||||
|
"atom/database/query"
|
||||||
"log"
|
"log"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -13,6 +14,6 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewQuery(db *gorm.DB) *Query {
|
func NewQuery(db *gorm.DB) *query.Query {
|
||||||
return Use(db)
|
return query.Use(db)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user