diff --git a/dal/model/accounts.gen.go b/dal/model/accounts.gen.go new file mode 100644 index 00000000..21b4a90e --- /dev/null +++ b/dal/model/accounts.gen.go @@ -0,0 +1,28 @@ +// 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 model + +import ( + "time" + + "gorm.io/gorm" +) + +const TableNameAccountInfo = "accounts" + +// AccountInfo mapped from table +type AccountInfo struct { + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` + UserID int64 `gorm:"column:user_id" json:"user_id"` + Number string `gorm:"column:number" json:"number"` +} + +// TableName AccountInfo's table name +func (*AccountInfo) TableName() string { + return TableNameAccountInfo +} diff --git a/dal/model/users.gen.go b/dal/model/users.gen.go new file mode 100644 index 00000000..2a5b572d --- /dev/null +++ b/dal/model/users.gen.go @@ -0,0 +1,33 @@ +// 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 model + +import ( + "time" + + "gorm.io/gorm" +) + +const TableNameUser = "users" + +// User mapped from table +type User struct { + ID int64 `gorm:"column:id;primaryKey;autoIncrement:true" json:"id"` + CreatedAt time.Time `gorm:"column:created_at" json:"created_at"` + UpdatedAt time.Time `gorm:"column:updated_at" json:"updated_at"` + DeletedAt gorm.DeletedAt `gorm:"column:deleted_at" json:"deleted_at"` + Name string `gorm:"column:name" json:"name"` + Age int64 `gorm:"column:age" json:"age"` + Birthday time.Time `gorm:"column:birthday" json:"birthday"` + Area string `gorm:"column:area;not null;comment:数据地址" json:"area"` // 数据地址 + CompanyID int64 `gorm:"column:company_id" json:"company_id"` + ManagerID int64 `gorm:"column:manager_id" json:"manager_id"` + Active bool `gorm:"column:active" json:"active"` +} + +// TableName User's table name +func (*User) TableName() string { + return TableNameUser +} diff --git a/dal/query/accounts.gen.go b/dal/query/accounts.gen.go new file mode 100644 index 00000000..9b3d3ab0 --- /dev/null +++ b/dal/query/accounts.gen.go @@ -0,0 +1,349 @@ +// 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" + + "gorm.io/playground/dal/model" +) + +func newAccountInfo(db *gorm.DB, opts ...gen.DOOption) accountInfo { + _accountInfo := accountInfo{} + + _accountInfo.accountInfoDo.UseDB(db, opts...) + _accountInfo.accountInfoDo.UseModel(&model.AccountInfo{}) + + tableName := _accountInfo.accountInfoDo.TableName() + _accountInfo.ALL = field.NewAsterisk(tableName) + _accountInfo.ID = field.NewInt64(tableName, "id") + _accountInfo.CreatedAt = field.NewTime(tableName, "created_at") + _accountInfo.UpdatedAt = field.NewTime(tableName, "updated_at") + _accountInfo.DeletedAt = field.NewField(tableName, "deleted_at") + _accountInfo.UserID = field.NewInt64(tableName, "user_id") + _accountInfo.Number = field.NewString(tableName, "number") + + _accountInfo.fillFieldMap() + + return _accountInfo +} + +type accountInfo struct { + accountInfoDo accountInfoDo + + ALL field.Asterisk + ID field.Int64 + CreatedAt field.Time + UpdatedAt field.Time + DeletedAt field.Field + UserID field.Int64 + Number field.String + + fieldMap map[string]field.Expr +} + +func (a accountInfo) Table(newTableName string) *accountInfo { + a.accountInfoDo.UseTable(newTableName) + return a.updateTableName(newTableName) +} + +func (a accountInfo) As(alias string) *accountInfo { + a.accountInfoDo.DO = *(a.accountInfoDo.As(alias).(*gen.DO)) + return a.updateTableName(alias) +} + +func (a *accountInfo) updateTableName(table string) *accountInfo { + a.ALL = field.NewAsterisk(table) + a.ID = field.NewInt64(table, "id") + a.CreatedAt = field.NewTime(table, "created_at") + a.UpdatedAt = field.NewTime(table, "updated_at") + a.DeletedAt = field.NewField(table, "deleted_at") + a.UserID = field.NewInt64(table, "user_id") + a.Number = field.NewString(table, "number") + + a.fillFieldMap() + + return a +} + +func (a *accountInfo) WithContext(ctx context.Context) *accountInfoDo { + return a.accountInfoDo.WithContext(ctx) +} + +func (a accountInfo) TableName() string { return a.accountInfoDo.TableName() } + +func (a accountInfo) Alias() string { return a.accountInfoDo.Alias() } + +func (a accountInfo) Columns(cols ...field.Expr) gen.Columns { return a.accountInfoDo.Columns(cols...) } + +func (a *accountInfo) GetFieldByName(fieldName string) (field.OrderExpr, bool) { + _f, ok := a.fieldMap[fieldName] + if !ok || _f == nil { + return nil, false + } + _oe, ok := _f.(field.OrderExpr) + return _oe, ok +} + +func (a *accountInfo) fillFieldMap() { + a.fieldMap = make(map[string]field.Expr, 6) + a.fieldMap["id"] = a.ID + a.fieldMap["created_at"] = a.CreatedAt + a.fieldMap["updated_at"] = a.UpdatedAt + a.fieldMap["deleted_at"] = a.DeletedAt + a.fieldMap["user_id"] = a.UserID + a.fieldMap["number"] = a.Number +} + +func (a accountInfo) clone(db *gorm.DB) accountInfo { + a.accountInfoDo.ReplaceConnPool(db.Statement.ConnPool) + return a +} + +func (a accountInfo) replaceDB(db *gorm.DB) accountInfo { + a.accountInfoDo.ReplaceDB(db) + return a +} + +type accountInfoDo struct{ gen.DO } + +func (a accountInfoDo) Debug() *accountInfoDo { + return a.withDO(a.DO.Debug()) +} + +func (a accountInfoDo) WithContext(ctx context.Context) *accountInfoDo { + return a.withDO(a.DO.WithContext(ctx)) +} + +func (a accountInfoDo) ReadDB() *accountInfoDo { + return a.Clauses(dbresolver.Read) +} + +func (a accountInfoDo) WriteDB() *accountInfoDo { + return a.Clauses(dbresolver.Write) +} + +func (a accountInfoDo) Session(config *gorm.Session) *accountInfoDo { + return a.withDO(a.DO.Session(config)) +} + +func (a accountInfoDo) Clauses(conds ...clause.Expression) *accountInfoDo { + return a.withDO(a.DO.Clauses(conds...)) +} + +func (a accountInfoDo) Returning(value interface{}, columns ...string) *accountInfoDo { + return a.withDO(a.DO.Returning(value, columns...)) +} + +func (a accountInfoDo) Not(conds ...gen.Condition) *accountInfoDo { + return a.withDO(a.DO.Not(conds...)) +} + +func (a accountInfoDo) Or(conds ...gen.Condition) *accountInfoDo { + return a.withDO(a.DO.Or(conds...)) +} + +func (a accountInfoDo) Select(conds ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.Select(conds...)) +} + +func (a accountInfoDo) Where(conds ...gen.Condition) *accountInfoDo { + return a.withDO(a.DO.Where(conds...)) +} + +func (a accountInfoDo) Order(conds ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.Order(conds...)) +} + +func (a accountInfoDo) Distinct(cols ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.Distinct(cols...)) +} + +func (a accountInfoDo) Omit(cols ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.Omit(cols...)) +} + +func (a accountInfoDo) Join(table schema.Tabler, on ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.Join(table, on...)) +} + +func (a accountInfoDo) LeftJoin(table schema.Tabler, on ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.LeftJoin(table, on...)) +} + +func (a accountInfoDo) RightJoin(table schema.Tabler, on ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.RightJoin(table, on...)) +} + +func (a accountInfoDo) Group(cols ...field.Expr) *accountInfoDo { + return a.withDO(a.DO.Group(cols...)) +} + +func (a accountInfoDo) Having(conds ...gen.Condition) *accountInfoDo { + return a.withDO(a.DO.Having(conds...)) +} + +func (a accountInfoDo) Limit(limit int) *accountInfoDo { + return a.withDO(a.DO.Limit(limit)) +} + +func (a accountInfoDo) Offset(offset int) *accountInfoDo { + return a.withDO(a.DO.Offset(offset)) +} + +func (a accountInfoDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *accountInfoDo { + return a.withDO(a.DO.Scopes(funcs...)) +} + +func (a accountInfoDo) Unscoped() *accountInfoDo { + return a.withDO(a.DO.Unscoped()) +} + +func (a accountInfoDo) Create(values ...*model.AccountInfo) error { + if len(values) == 0 { + return nil + } + return a.DO.Create(values) +} + +func (a accountInfoDo) CreateInBatches(values []*model.AccountInfo, batchSize int) error { + return a.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 (a accountInfoDo) Save(values ...*model.AccountInfo) error { + if len(values) == 0 { + return nil + } + return a.DO.Save(values) +} + +func (a accountInfoDo) First() (*model.AccountInfo, error) { + if result, err := a.DO.First(); err != nil { + return nil, err + } else { + return result.(*model.AccountInfo), nil + } +} + +func (a accountInfoDo) Take() (*model.AccountInfo, error) { + if result, err := a.DO.Take(); err != nil { + return nil, err + } else { + return result.(*model.AccountInfo), nil + } +} + +func (a accountInfoDo) Last() (*model.AccountInfo, error) { + if result, err := a.DO.Last(); err != nil { + return nil, err + } else { + return result.(*model.AccountInfo), nil + } +} + +func (a accountInfoDo) Find() ([]*model.AccountInfo, error) { + result, err := a.DO.Find() + return result.([]*model.AccountInfo), err +} + +func (a accountInfoDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.AccountInfo, err error) { + buf := make([]*model.AccountInfo, 0, batchSize) + err = a.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 (a accountInfoDo) FindInBatches(result *[]*model.AccountInfo, batchSize int, fc func(tx gen.Dao, batch int) error) error { + return a.DO.FindInBatches(result, batchSize, fc) +} + +func (a accountInfoDo) Attrs(attrs ...field.AssignExpr) *accountInfoDo { + return a.withDO(a.DO.Attrs(attrs...)) +} + +func (a accountInfoDo) Assign(attrs ...field.AssignExpr) *accountInfoDo { + return a.withDO(a.DO.Assign(attrs...)) +} + +func (a accountInfoDo) Joins(fields ...field.RelationField) *accountInfoDo { + for _, _f := range fields { + a = *a.withDO(a.DO.Joins(_f)) + } + return &a +} + +func (a accountInfoDo) Preload(fields ...field.RelationField) *accountInfoDo { + for _, _f := range fields { + a = *a.withDO(a.DO.Preload(_f)) + } + return &a +} + +func (a accountInfoDo) FirstOrInit() (*model.AccountInfo, error) { + if result, err := a.DO.FirstOrInit(); err != nil { + return nil, err + } else { + return result.(*model.AccountInfo), nil + } +} + +func (a accountInfoDo) FirstOrCreate() (*model.AccountInfo, error) { + if result, err := a.DO.FirstOrCreate(); err != nil { + return nil, err + } else { + return result.(*model.AccountInfo), nil + } +} + +func (a accountInfoDo) FindByPage(offset int, limit int) (result []*model.AccountInfo, count int64, err error) { + result, err = a.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 = a.Offset(-1).Limit(-1).Count() + return +} + +func (a accountInfoDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) { + count, err = a.Count() + if err != nil { + return + } + + err = a.Offset(offset).Limit(limit).Scan(result) + return +} + +func (a accountInfoDo) Scan(result interface{}) (err error) { + return a.DO.Scan(result) +} + +func (a accountInfoDo) Delete(models ...*model.AccountInfo) (result gen.ResultInfo, err error) { + return a.DO.Delete(models) +} + +func (a *accountInfoDo) withDO(do gen.Dao) *accountInfoDo { + a.DO = *do.(*gen.DO) + return a +} diff --git a/dal/query/accounts.gen_test.go b/dal/query/accounts.gen_test.go new file mode 100644 index 00000000..b75f3766 --- /dev/null +++ b/dal/query/accounts.gen_test.go @@ -0,0 +1,145 @@ +// 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" + "fmt" + "testing" + + "gorm.io/gen" + "gorm.io/gen/field" + "gorm.io/gorm/clause" + "gorm.io/playground/dal/model" +) + +func init() { + InitializeDB() + err := _gen_test_db.AutoMigrate(&model.AccountInfo{}) + if err != nil { + fmt.Printf("Error: AutoMigrate(&model.AccountInfo{}) fail: %s", err) + } +} + +func Test_accountInfoQuery(t *testing.T) { + accountInfo := newAccountInfo(_gen_test_db) + accountInfo = *accountInfo.As(accountInfo.TableName()) + _do := accountInfo.WithContext(context.Background()).Debug() + + primaryKey := field.NewString(accountInfo.TableName(), clause.PrimaryKey) + _, err := _do.Unscoped().Where(primaryKey.IsNotNull()).Delete() + if err != nil { + t.Error("clean table fail:", err) + return + } + + _, ok := accountInfo.GetFieldByName("") + if ok { + t.Error("GetFieldByName(\"\") from accountInfo success") + } + + err = _do.Create(&model.AccountInfo{}) + if err != nil { + t.Error("create item in table fail:", err) + } + + err = _do.Save(&model.AccountInfo{}) + if err != nil { + t.Error("create item in table fail:", err) + } + + err = _do.CreateInBatches([]*model.AccountInfo{{}, {}}, 10) + if err != nil { + t.Error("create item in table fail:", err) + } + + _, err = _do.Select(accountInfo.ALL).Take() + if err != nil { + t.Error("Take() on table fail:", err) + } + + _, err = _do.First() + if err != nil { + t.Error("First() on table fail:", err) + } + + _, err = _do.Last() + if err != nil { + t.Error("First() on table fail:", err) + } + + _, err = _do.Where(primaryKey.IsNotNull()).FindInBatch(10, func(tx gen.Dao, batch int) error { return nil }) + if err != nil { + t.Error("FindInBatch() on table fail:", err) + } + + err = _do.Where(primaryKey.IsNotNull()).FindInBatches(&[]*model.AccountInfo{}, 10, func(tx gen.Dao, batch int) error { return nil }) + if err != nil { + t.Error("FindInBatches() on table fail:", err) + } + + _, err = _do.Select(accountInfo.ALL).Where(primaryKey.IsNotNull()).Order(primaryKey.Desc()).Find() + if err != nil { + t.Error("Find() on table fail:", err) + } + + _, err = _do.Distinct(primaryKey).Take() + if err != nil { + t.Error("select Distinct() on table fail:", err) + } + + _, err = _do.Select(accountInfo.ALL).Omit(primaryKey).Take() + if err != nil { + t.Error("Omit() on table fail:", err) + } + + _, err = _do.Group(primaryKey).Find() + if err != nil { + t.Error("Group() on table fail:", err) + } + + _, err = _do.Scopes(func(dao gen.Dao) gen.Dao { return dao.Where(primaryKey.IsNotNull()) }).Find() + if err != nil { + t.Error("Scopes() on table fail:", err) + } + + _, _, err = _do.FindByPage(0, 1) + if err != nil { + t.Error("FindByPage() on table fail:", err) + } + + _, err = _do.ScanByPage(&model.AccountInfo{}, 0, 1) + if err != nil { + t.Error("ScanByPage() on table fail:", err) + } + + _, err = _do.Attrs(primaryKey).Assign(primaryKey).FirstOrInit() + if err != nil { + t.Error("FirstOrInit() on table fail:", err) + } + + _, err = _do.Attrs(primaryKey).Assign(primaryKey).FirstOrCreate() + if err != nil { + t.Error("FirstOrCreate() on table fail:", err) + } + + var _a _another + var _aPK = field.NewString(_a.TableName(), "id") + + err = _do.Join(&_a, primaryKey.EqCol(_aPK)).Scan(map[string]interface{}{}) + if err != nil { + t.Error("Join() on table fail:", err) + } + + err = _do.LeftJoin(&_a, primaryKey.EqCol(_aPK)).Scan(map[string]interface{}{}) + if err != nil { + t.Error("LeftJoin() on table fail:", err) + } + + _, err = _do.Not().Or().Clauses().Take() + if err != nil { + t.Error("Not/Or/Clauses on table fail:", err) + } +} diff --git a/dal/query/gen.go b/dal/query/gen.go new file mode 100644 index 00000000..570363c3 --- /dev/null +++ b/dal/query/gen.go @@ -0,0 +1,111 @@ +// 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) + AccountInfo *accountInfo + User *user +) + +func SetDefault(db *gorm.DB, opts ...gen.DOOption) { + *Q = *Use(db, opts...) + AccountInfo = &Q.AccountInfo + User = &Q.User +} + +func Use(db *gorm.DB, opts ...gen.DOOption) *Query { + return &Query{ + db: db, + AccountInfo: newAccountInfo(db, opts...), + User: newUser(db, opts...), + } +} + +type Query struct { + db *gorm.DB + + AccountInfo accountInfo + User user +} + +func (q *Query) Available() bool { return q.db != nil } + +func (q *Query) clone(db *gorm.DB) *Query { + return &Query{ + db: db, + AccountInfo: q.AccountInfo.clone(db), + User: q.User.clone(db), + } +} + +func (q *Query) ReadDB() *Query { + return q.ReplaceDB(q.db.Clauses(dbresolver.Read)) +} + +func (q *Query) WriteDB() *Query { + return q.ReplaceDB(q.db.Clauses(dbresolver.Write)) +} + +func (q *Query) ReplaceDB(db *gorm.DB) *Query { + return &Query{ + db: db, + AccountInfo: q.AccountInfo.replaceDB(db), + User: q.User.replaceDB(db), + } +} + +type queryCtx struct { + AccountInfo *accountInfoDo + User *userDo +} + +func (q *Query) WithContext(ctx context.Context) *queryCtx { + return &queryCtx{ + AccountInfo: q.AccountInfo.WithContext(ctx), + User: q.User.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 { + tx := q.db.Begin(opts...) + return &QueryTx{Query: q.clone(tx), Error: tx.Error} +} + +type QueryTx struct { + *Query + Error error +} + +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 +} diff --git a/dal/query/gen_test.go b/dal/query/gen_test.go new file mode 100644 index 00000000..28de5b50 --- /dev/null +++ b/dal/query/gen_test.go @@ -0,0 +1,119 @@ +// 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" + "fmt" + "reflect" + "sync" + "testing" + + "gorm.io/driver/sqlite" + "gorm.io/gorm" +) + +type Input struct { + Args []interface{} +} + +type Expectation struct { + Ret []interface{} +} + +type TestCase struct { + Input + Expectation +} + +const _gen_test_db_name = "gen_test.db" + +var _gen_test_db *gorm.DB +var _gen_test_once sync.Once + +func init() { + InitializeDB() + _gen_test_db.AutoMigrate(&_another{}) +} + +func InitializeDB() { + _gen_test_once.Do(func() { + var err error + _gen_test_db, err = gorm.Open(sqlite.Open(_gen_test_db_name), &gorm.Config{}) + if err != nil { + panic(fmt.Errorf("open sqlite %q fail: %w", _gen_test_db_name, err)) + } + }) +} + +func assert(t *testing.T, methodName string, res, exp interface{}) { + if !reflect.DeepEqual(res, exp) { + t.Errorf("%v() gotResult = %v, want %v", methodName, res, exp) + } +} + +type _another struct { + ID uint64 `gorm:"primaryKey"` +} + +func (*_another) TableName() string { return "another_for_unit_test" } + +func Test_Available(t *testing.T) { + if !Use(_gen_test_db).Available() { + t.Errorf("query.Available() == false") + } +} + +func Test_WithContext(t *testing.T) { + query := Use(_gen_test_db) + if !query.Available() { + t.Errorf("query Use(_gen_test_db) fail: query.Available() == false") + } + + type Content string + var key, value Content = "gen_tag", "unit_test" + qCtx := query.WithContext(context.WithValue(context.Background(), key, value)) + + for _, ctx := range []context.Context{ + qCtx.AccountInfo.UnderlyingDB().Statement.Context, + qCtx.User.UnderlyingDB().Statement.Context, + } { + if v := ctx.Value(key); v != value { + t.Errorf("get value from context fail, expect %q, got %q", value, v) + } + } +} + +func Test_Transaction(t *testing.T) { + query := Use(_gen_test_db) + if !query.Available() { + t.Errorf("query Use(_gen_test_db) fail: query.Available() == false") + } + + err := query.Transaction(func(tx *Query) error { return nil }) + if err != nil { + t.Errorf("query.Transaction execute fail: %s", err) + } + + tx := query.Begin() + + err = tx.SavePoint("point") + if err != nil { + t.Errorf("query tx SavePoint fail: %s", err) + } + err = tx.RollbackTo("point") + if err != nil { + t.Errorf("query tx RollbackTo fail: %s", err) + } + err = tx.Commit() + if err != nil { + t.Errorf("query tx Commit fail: %s", err) + } + + err = query.Begin().Rollback() + if err != nil { + t.Errorf("query tx Rollback fail: %s", err) + } +} diff --git a/dal/query/users.gen.go b/dal/query/users.gen.go new file mode 100644 index 00000000..0849694c --- /dev/null +++ b/dal/query/users.gen.go @@ -0,0 +1,367 @@ +// 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" + + "gorm.io/playground/dal/model" +) + +func newUser(db *gorm.DB, opts ...gen.DOOption) user { + _user := user{} + + _user.userDo.UseDB(db, opts...) + _user.userDo.UseModel(&model.User{}) + + tableName := _user.userDo.TableName() + _user.ALL = field.NewAsterisk(tableName) + _user.ID = field.NewInt64(tableName, "id") + _user.CreatedAt = field.NewTime(tableName, "created_at") + _user.UpdatedAt = field.NewTime(tableName, "updated_at") + _user.DeletedAt = field.NewField(tableName, "deleted_at") + _user.Name = field.NewString(tableName, "name") + _user.Age = field.NewInt64(tableName, "age") + _user.Birthday = field.NewTime(tableName, "birthday") + _user.Area = field.NewString(tableName, "area") + _user.CompanyID = field.NewInt64(tableName, "company_id") + _user.ManagerID = field.NewInt64(tableName, "manager_id") + _user.Active = field.NewBool(tableName, "active") + + _user.fillFieldMap() + + return _user +} + +type user struct { + userDo userDo + + ALL field.Asterisk + ID field.Int64 + CreatedAt field.Time + UpdatedAt field.Time + DeletedAt field.Field + Name field.String + Age field.Int64 + Birthday field.Time + Area field.String // 数据地址 + CompanyID field.Int64 + ManagerID field.Int64 + Active field.Bool + + fieldMap map[string]field.Expr +} + +func (u user) Table(newTableName string) *user { + u.userDo.UseTable(newTableName) + return u.updateTableName(newTableName) +} + +func (u user) As(alias string) *user { + u.userDo.DO = *(u.userDo.As(alias).(*gen.DO)) + return u.updateTableName(alias) +} + +func (u *user) updateTableName(table string) *user { + u.ALL = field.NewAsterisk(table) + u.ID = field.NewInt64(table, "id") + u.CreatedAt = field.NewTime(table, "created_at") + u.UpdatedAt = field.NewTime(table, "updated_at") + u.DeletedAt = field.NewField(table, "deleted_at") + u.Name = field.NewString(table, "name") + u.Age = field.NewInt64(table, "age") + u.Birthday = field.NewTime(table, "birthday") + u.Area = field.NewString(table, "area") + u.CompanyID = field.NewInt64(table, "company_id") + u.ManagerID = field.NewInt64(table, "manager_id") + u.Active = field.NewBool(table, "active") + + u.fillFieldMap() + + return u +} + +func (u *user) WithContext(ctx context.Context) *userDo { return u.userDo.WithContext(ctx) } + +func (u user) TableName() string { return u.userDo.TableName() } + +func (u user) Alias() string { return u.userDo.Alias() } + +func (u user) Columns(cols ...field.Expr) gen.Columns { return u.userDo.Columns(cols...) } + +func (u *user) GetFieldByName(fieldName string) (field.OrderExpr, bool) { + _f, ok := u.fieldMap[fieldName] + if !ok || _f == nil { + return nil, false + } + _oe, ok := _f.(field.OrderExpr) + return _oe, ok +} + +func (u *user) fillFieldMap() { + u.fieldMap = make(map[string]field.Expr, 11) + u.fieldMap["id"] = u.ID + u.fieldMap["created_at"] = u.CreatedAt + u.fieldMap["updated_at"] = u.UpdatedAt + u.fieldMap["deleted_at"] = u.DeletedAt + u.fieldMap["name"] = u.Name + u.fieldMap["age"] = u.Age + u.fieldMap["birthday"] = u.Birthday + u.fieldMap["area"] = u.Area + u.fieldMap["company_id"] = u.CompanyID + u.fieldMap["manager_id"] = u.ManagerID + u.fieldMap["active"] = u.Active +} + +func (u user) clone(db *gorm.DB) user { + u.userDo.ReplaceConnPool(db.Statement.ConnPool) + return u +} + +func (u user) replaceDB(db *gorm.DB) user { + u.userDo.ReplaceDB(db) + return u +} + +type userDo struct{ gen.DO } + +func (u userDo) Debug() *userDo { + return u.withDO(u.DO.Debug()) +} + +func (u userDo) WithContext(ctx context.Context) *userDo { + return u.withDO(u.DO.WithContext(ctx)) +} + +func (u userDo) ReadDB() *userDo { + return u.Clauses(dbresolver.Read) +} + +func (u userDo) WriteDB() *userDo { + return u.Clauses(dbresolver.Write) +} + +func (u userDo) Session(config *gorm.Session) *userDo { + return u.withDO(u.DO.Session(config)) +} + +func (u userDo) Clauses(conds ...clause.Expression) *userDo { + return u.withDO(u.DO.Clauses(conds...)) +} + +func (u userDo) Returning(value interface{}, columns ...string) *userDo { + return u.withDO(u.DO.Returning(value, columns...)) +} + +func (u userDo) Not(conds ...gen.Condition) *userDo { + return u.withDO(u.DO.Not(conds...)) +} + +func (u userDo) Or(conds ...gen.Condition) *userDo { + return u.withDO(u.DO.Or(conds...)) +} + +func (u userDo) Select(conds ...field.Expr) *userDo { + return u.withDO(u.DO.Select(conds...)) +} + +func (u userDo) Where(conds ...gen.Condition) *userDo { + return u.withDO(u.DO.Where(conds...)) +} + +func (u userDo) Order(conds ...field.Expr) *userDo { + return u.withDO(u.DO.Order(conds...)) +} + +func (u userDo) Distinct(cols ...field.Expr) *userDo { + return u.withDO(u.DO.Distinct(cols...)) +} + +func (u userDo) Omit(cols ...field.Expr) *userDo { + return u.withDO(u.DO.Omit(cols...)) +} + +func (u userDo) Join(table schema.Tabler, on ...field.Expr) *userDo { + return u.withDO(u.DO.Join(table, on...)) +} + +func (u userDo) LeftJoin(table schema.Tabler, on ...field.Expr) *userDo { + return u.withDO(u.DO.LeftJoin(table, on...)) +} + +func (u userDo) RightJoin(table schema.Tabler, on ...field.Expr) *userDo { + return u.withDO(u.DO.RightJoin(table, on...)) +} + +func (u userDo) Group(cols ...field.Expr) *userDo { + return u.withDO(u.DO.Group(cols...)) +} + +func (u userDo) Having(conds ...gen.Condition) *userDo { + return u.withDO(u.DO.Having(conds...)) +} + +func (u userDo) Limit(limit int) *userDo { + return u.withDO(u.DO.Limit(limit)) +} + +func (u userDo) Offset(offset int) *userDo { + return u.withDO(u.DO.Offset(offset)) +} + +func (u userDo) Scopes(funcs ...func(gen.Dao) gen.Dao) *userDo { + return u.withDO(u.DO.Scopes(funcs...)) +} + +func (u userDo) Unscoped() *userDo { + return u.withDO(u.DO.Unscoped()) +} + +func (u userDo) Create(values ...*model.User) error { + if len(values) == 0 { + return nil + } + return u.DO.Create(values) +} + +func (u userDo) CreateInBatches(values []*model.User, batchSize int) error { + return u.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 (u userDo) Save(values ...*model.User) error { + if len(values) == 0 { + return nil + } + return u.DO.Save(values) +} + +func (u userDo) First() (*model.User, error) { + if result, err := u.DO.First(); err != nil { + return nil, err + } else { + return result.(*model.User), nil + } +} + +func (u userDo) Take() (*model.User, error) { + if result, err := u.DO.Take(); err != nil { + return nil, err + } else { + return result.(*model.User), nil + } +} + +func (u userDo) Last() (*model.User, error) { + if result, err := u.DO.Last(); err != nil { + return nil, err + } else { + return result.(*model.User), nil + } +} + +func (u userDo) Find() ([]*model.User, error) { + result, err := u.DO.Find() + return result.([]*model.User), err +} + +func (u userDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.User, err error) { + buf := make([]*model.User, 0, batchSize) + err = u.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 (u userDo) FindInBatches(result *[]*model.User, batchSize int, fc func(tx gen.Dao, batch int) error) error { + return u.DO.FindInBatches(result, batchSize, fc) +} + +func (u userDo) Attrs(attrs ...field.AssignExpr) *userDo { + return u.withDO(u.DO.Attrs(attrs...)) +} + +func (u userDo) Assign(attrs ...field.AssignExpr) *userDo { + return u.withDO(u.DO.Assign(attrs...)) +} + +func (u userDo) Joins(fields ...field.RelationField) *userDo { + for _, _f := range fields { + u = *u.withDO(u.DO.Joins(_f)) + } + return &u +} + +func (u userDo) Preload(fields ...field.RelationField) *userDo { + for _, _f := range fields { + u = *u.withDO(u.DO.Preload(_f)) + } + return &u +} + +func (u userDo) FirstOrInit() (*model.User, error) { + if result, err := u.DO.FirstOrInit(); err != nil { + return nil, err + } else { + return result.(*model.User), nil + } +} + +func (u userDo) FirstOrCreate() (*model.User, error) { + if result, err := u.DO.FirstOrCreate(); err != nil { + return nil, err + } else { + return result.(*model.User), nil + } +} + +func (u userDo) FindByPage(offset int, limit int) (result []*model.User, count int64, err error) { + result, err = u.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 = u.Offset(-1).Limit(-1).Count() + return +} + +func (u userDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) { + count, err = u.Count() + if err != nil { + return + } + + err = u.Offset(offset).Limit(limit).Scan(result) + return +} + +func (u userDo) Scan(result interface{}) (err error) { + return u.DO.Scan(result) +} + +func (u userDo) Delete(models ...*model.User) (result gen.ResultInfo, err error) { + return u.DO.Delete(models) +} + +func (u *userDo) withDO(do gen.Dao) *userDo { + u.DO = *do.(*gen.DO) + return u +} diff --git a/dal/query/users.gen_test.go b/dal/query/users.gen_test.go new file mode 100644 index 00000000..a1e686a7 --- /dev/null +++ b/dal/query/users.gen_test.go @@ -0,0 +1,145 @@ +// 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" + "fmt" + "testing" + + "gorm.io/gen" + "gorm.io/gen/field" + "gorm.io/gorm/clause" + "gorm.io/playground/dal/model" +) + +func init() { + InitializeDB() + err := _gen_test_db.AutoMigrate(&model.User{}) + if err != nil { + fmt.Printf("Error: AutoMigrate(&model.User{}) fail: %s", err) + } +} + +func Test_userQuery(t *testing.T) { + user := newUser(_gen_test_db) + user = *user.As(user.TableName()) + _do := user.WithContext(context.Background()).Debug() + + primaryKey := field.NewString(user.TableName(), clause.PrimaryKey) + _, err := _do.Unscoped().Where(primaryKey.IsNotNull()).Delete() + if err != nil { + t.Error("clean table fail:", err) + return + } + + _, ok := user.GetFieldByName("") + if ok { + t.Error("GetFieldByName(\"\") from user success") + } + + err = _do.Create(&model.User{}) + if err != nil { + t.Error("create item in table fail:", err) + } + + err = _do.Save(&model.User{}) + if err != nil { + t.Error("create item in table fail:", err) + } + + err = _do.CreateInBatches([]*model.User{{}, {}}, 10) + if err != nil { + t.Error("create item in table fail:", err) + } + + _, err = _do.Select(user.ALL).Take() + if err != nil { + t.Error("Take() on table fail:", err) + } + + _, err = _do.First() + if err != nil { + t.Error("First() on table fail:", err) + } + + _, err = _do.Last() + if err != nil { + t.Error("First() on table fail:", err) + } + + _, err = _do.Where(primaryKey.IsNotNull()).FindInBatch(10, func(tx gen.Dao, batch int) error { return nil }) + if err != nil { + t.Error("FindInBatch() on table fail:", err) + } + + err = _do.Where(primaryKey.IsNotNull()).FindInBatches(&[]*model.User{}, 10, func(tx gen.Dao, batch int) error { return nil }) + if err != nil { + t.Error("FindInBatches() on table fail:", err) + } + + _, err = _do.Select(user.ALL).Where(primaryKey.IsNotNull()).Order(primaryKey.Desc()).Find() + if err != nil { + t.Error("Find() on table fail:", err) + } + + _, err = _do.Distinct(primaryKey).Take() + if err != nil { + t.Error("select Distinct() on table fail:", err) + } + + _, err = _do.Select(user.ALL).Omit(primaryKey).Take() + if err != nil { + t.Error("Omit() on table fail:", err) + } + + _, err = _do.Group(primaryKey).Find() + if err != nil { + t.Error("Group() on table fail:", err) + } + + _, err = _do.Scopes(func(dao gen.Dao) gen.Dao { return dao.Where(primaryKey.IsNotNull()) }).Find() + if err != nil { + t.Error("Scopes() on table fail:", err) + } + + _, _, err = _do.FindByPage(0, 1) + if err != nil { + t.Error("FindByPage() on table fail:", err) + } + + _, err = _do.ScanByPage(&model.User{}, 0, 1) + if err != nil { + t.Error("ScanByPage() on table fail:", err) + } + + _, err = _do.Attrs(primaryKey).Assign(primaryKey).FirstOrInit() + if err != nil { + t.Error("FirstOrInit() on table fail:", err) + } + + _, err = _do.Attrs(primaryKey).Assign(primaryKey).FirstOrCreate() + if err != nil { + t.Error("FirstOrCreate() on table fail:", err) + } + + var _a _another + var _aPK = field.NewString(_a.TableName(), "id") + + err = _do.Join(&_a, primaryKey.EqCol(_aPK)).Scan(map[string]interface{}{}) + if err != nil { + t.Error("Join() on table fail:", err) + } + + err = _do.LeftJoin(&_a, primaryKey.EqCol(_aPK)).Scan(map[string]interface{}{}) + if err != nil { + t.Error("LeftJoin() on table fail:", err) + } + + _, err = _do.Not().Or().Clauses().Take() + if err != nil { + t.Error("Not/Or/Clauses on table fail:", err) + } +} diff --git a/db.go b/db.go index ccab03ed..9c7fea9d 100644 --- a/db.go +++ b/db.go @@ -43,7 +43,8 @@ func init() { func OpenTestConnection() (db *gorm.DB, err error) { dbDSN := os.Getenv("GORM_DSN") - switch os.Getenv("GORM_DIALECT") { + dialect := os.Getenv("GORM_DIALECT") + switch dialect { case "mysql": log.Println("testing mysql...") if dbDSN == "" { diff --git a/gen.go b/gen.go index c0f87398..2709a8de 100644 --- a/gen.go +++ b/gen.go @@ -12,10 +12,10 @@ func generate() { WithUnitTest: true, }) - // g.UseDB(dal.DB) + g.UseDB(DB) // g.ApplyBasic(Company{}, Language{}) // Associations - g.ApplyBasic(g.GenerateModel("user"), g.GenerateModelAs("account", "AccountInfo")) + g.ApplyBasic(g.GenerateModel("users"), g.GenerateModelAs("accounts", "AccountInfo")) g.Execute() } diff --git a/go.mod b/go.mod index f194634f..f833a456 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,6 @@ require ( golang.org/x/crypto v0.29.0 // indirect golang.org/x/mod v0.22.0 // indirect golang.org/x/sync v0.9.0 // indirect - golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.20.0 // indirect golang.org/x/tools v0.27.0 // indirect gorm.io/datatypes v1.2.4 // indirect @@ -38,4 +37,4 @@ require ( gorm.io/plugin/dbresolver v1.5.3 // indirect ) -replace gorm.io/gorm => ./gorm +//replace gorm.io/gorm => ./gorm diff --git a/main_test.go b/main_test.go index 60a388f7..868fd82f 100644 --- a/main_test.go +++ b/main_test.go @@ -1,6 +1,11 @@ package main import ( + "context" + "fmt" + "gorm.io/datatypes" + "gorm.io/gen" + "gorm.io/playground/dal/query" "testing" ) @@ -18,3 +23,16 @@ func TestGORM(t *testing.T) { t.Errorf("Failed, got error: %v", err) } } +func TestGen(t *testing.T) { + RunMigrations() + generate() + query.SetDefault(DB) + list, err := query.User.WithContext(context.Background()).Where(gen.Cond(datatypes.JSONArrayQuery("area").Contains([]int{1, 2, 3}))...).Find() + if err != nil { + // SELECT * FROM `users` WHERE JSON_CONTAINS(`area`,JSON_ARRAY((1,2,3))) AND `users`.`deleted_at` IS NULL + // Error 1241 (21000): Operand should contain 1 column(s) + t.Errorf("Failed, got error: %v", err) + } else { + fmt.Printf("%#v\n", len(list)) + } +} diff --git a/models.go b/models.go index 692a6842..be75c982 100644 --- a/models.go +++ b/models.go @@ -19,6 +19,7 @@ type User struct { Account Account Pets []*Pet Toys []Toy `gorm:"polymorphic:Owner"` + Area []int `gorm:"column:area;type:json;not null;comment:数据地址" json:"area"` CompanyID *int Company Company ManagerID *uint