diff --git a/field/export.go b/field/export.go index 22df37c0..6f2ea364 100644 --- a/field/export.go +++ b/field/export.go @@ -48,90 +48,90 @@ func NewAsterisk(table string, opts ...Option) Asterisk { // NewInt create new Int func NewInt(table, column string, opts ...Option) Int { - return Int{GenericsInt[int]{GenericsField: GenericsField[int]{expr{col: toColumn(table, column, opts...)}}}} + return Int{NewGenericsInt[int](expr{col: toColumn(table, column, opts...)})} } // NewInt8 create new Int8 func NewInt8(table, column string, opts ...Option) Int8 { - return Int8{GenericsInt[int8]{GenericsField: GenericsField[int8]{expr{col: toColumn(table, column, opts...)}}}} + return Int8{NewGenericsInt[int8](expr{col: toColumn(table, column, opts...)})} } // NewInt16 ... func NewInt16(table, column string, opts ...Option) Int16 { - return Int16{GenericsInt[int16]{GenericsField: GenericsField[int16]{expr{col: toColumn(table, column, opts...)}}}} + return Int16{NewGenericsInt[int16](expr{col: toColumn(table, column, opts...)})} } // NewInt32 ... func NewInt32(table, column string, opts ...Option) Int32 { - return Int32{GenericsInt[int32]{GenericsField: GenericsField[int32]{expr{col: toColumn(table, column, opts...)}}}} + return Int32{NewGenericsInt[int32](expr{col: toColumn(table, column, opts...)})} } // NewInt64 ... func NewInt64(table, column string, opts ...Option) Int64 { - return Int64{GenericsInt[int64]{GenericsField: GenericsField[int64]{expr{col: toColumn(table, column, opts...)}}}} + return Int64{NewGenericsInt[int64](expr{col: toColumn(table, column, opts...)})} } // NewUint ... func NewUint(table, column string, opts ...Option) Uint { - return Uint{GenericsInt[uint]{GenericsField: GenericsField[uint]{expr{col: toColumn(table, column, opts...)}}}} + return Uint{NewGenericsInt[uint](expr{col: toColumn(table, column, opts...)})} } // NewUint8 ... func NewUint8(table, column string, opts ...Option) Uint8 { - return Uint8{GenericsInt[uint8]{GenericsField: GenericsField[uint8]{expr{col: toColumn(table, column, opts...)}}}} + return Uint8{NewGenericsInt[uint8](expr{col: toColumn(table, column, opts...)})} } // NewUint16 ... func NewUint16(table, column string, opts ...Option) Uint16 { - return Uint16{GenericsInt[uint16]{GenericsField: GenericsField[uint16]{expr{col: toColumn(table, column, opts...)}}}} + return Uint16{NewGenericsInt[uint16](expr{col: toColumn(table, column, opts...)})} } // NewUint32 ... func NewUint32(table, column string, opts ...Option) Uint32 { - return Uint32{GenericsInt[uint32]{GenericsField: GenericsField[uint32]{expr{col: toColumn(table, column, opts...)}}}} + return Uint32{NewGenericsInt[uint32](expr{col: toColumn(table, column, opts...)})} } // NewUint64 ... func NewUint64(table, column string, opts ...Option) Uint64 { - return Uint64{GenericsInt[uint64]{GenericsField: GenericsField[uint64]{expr{col: toColumn(table, column, opts...)}}}} + return Uint64{NewGenericsInt[uint64](expr{col: toColumn(table, column, opts...)})} } // ======================== float ======================= // NewFloat32 ... func NewFloat32(table, column string, opts ...Option) Float32 { - return Float32{GenericsInt[float32]{GenericsField: GenericsField[float32]{expr{col: toColumn(table, column, opts...)}}}} + return Float32{NewGenericsInt[float32](expr{col: toColumn(table, column, opts...)})} } // NewFloat64 ... func NewFloat64(table, column string, opts ...Option) Float64 { - return Float64{GenericsInt[float64]{GenericsField: GenericsField[float64]{expr{col: toColumn(table, column, opts...)}}}} + return Float64{NewGenericsInt[float64](expr{col: toColumn(table, column, opts...)})} } // ======================== string ======================= // NewString ... func NewString(table, column string, opts ...Option) String { - return String{GenericsString[string]{GenericsField: GenericsField[string]{expr{col: toColumn(table, column, opts...)}}}} + return String{NewGenericsString[string](expr{col: toColumn(table, column, opts...)})} } // NewBytes ... func NewBytes(table, column string, opts ...Option) Bytes { - return Bytes{GenericsString[[]byte]{GenericsField: GenericsField[[]byte]{expr{col: toColumn(table, column, opts...)}}}} + return Bytes{NewGenericsString[[]byte](expr{col: toColumn(table, column, opts...)})} } // ======================== bool ======================= // NewBool ... func NewBool(table, column string, opts ...Option) Bool { - return Bool{GenericsField[bool]{expr{col: toColumn(table, column, opts...)}}} + return Bool{NewGenerics[bool](expr{col: toColumn(table, column, opts...)})} } // ======================== time ======================= // NewTime ... func NewTime(table, column string, opts ...Option) Time { - return Time{GenericsField[time.Time]{expr{col: toColumn(table, column, opts...)}}} + return Time{NewGenerics[time.Time](expr{col: toColumn(table, column, opts...)})} } func toColumn(table, column string, opts ...Option) clause.Column { diff --git a/field/expr.go b/field/expr.go index 1298eec6..638836ef 100644 --- a/field/expr.go +++ b/field/expr.go @@ -173,31 +173,34 @@ func (e expr) IsNotNull() Expr { } func (e expr) Count() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}})}}} + return Int{NewGenericsInt[int](e.setE(clause.Expr{SQL: "COUNT(?)", Vars: []interface{}{e.RawExpr()}}))} } func (e expr) Distinct() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{e.setE(clause.Expr{SQL: "DISTINCT ?", Vars: []interface{}{e.RawExpr()}})}}} + return Int{NewGenericsInt[int](e.setE(clause.Expr{SQL: "DISTINCT ?", Vars: []interface{}{e.RawExpr()}}))} + } func (e expr) Length() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{e.setE(clause.Expr{SQL: "LENGTH(?)", Vars: []interface{}{e.RawExpr()}})}}} + return Int{NewGenericsInt[int](e.setE(clause.Expr{SQL: "LENGTH(?)", Vars: []interface{}{e.RawExpr()}}))} } func (e expr) Max() Float64 { - return Float64{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[float64]{e.setE(clause.Expr{SQL: "MAX(?)", Vars: []interface{}{e.RawExpr()}})}}} + return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "MAX(?)", Vars: []interface{}{e.RawExpr()}}))} } func (e expr) Min() Float64 { - return Float64{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[float64]{e.setE(clause.Expr{SQL: "MIN(?)", Vars: []interface{}{e.RawExpr()}})}}} + return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "MIN(?)", Vars: []interface{}{e.RawExpr()}}))} + } func (e expr) Avg() Float64 { - return Float64{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[float64]{e.setE(clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.RawExpr()}})}}} + return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "AVG(?)", Vars: []interface{}{e.RawExpr()}}))} + } func (e expr) Abs() Float64 { - return Float64{GenericsInt: GenericsInt[float64]{GenericsField: GenericsField[float64]{e.setE(clause.Expr{SQL: "ABS(?)", Vars: []interface{}{e.RawExpr()}})}}} + return Float64{NewGenericsInt[float64](e.setE(clause.Expr{SQL: "ABS(?)", Vars: []interface{}{e.RawExpr()}}))} } func (e expr) Null() AssignExpr { diff --git a/field/float.go b/field/float.go index 7cbe2fb2..849b886d 100644 --- a/field/float.go +++ b/field/float.go @@ -7,7 +7,7 @@ type Float64 struct { // Floor ... func (field Float64) Floor() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{field.floor()}}} + return Int{NewGenericsInt[int](field.floor())} } // Float32 float32 type field @@ -17,5 +17,5 @@ type Float32 struct { // Floor ... func (field Float32) Floor() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{field.floor()}}} + return Int{NewGenericsInt[int](field.floor())} } diff --git a/field/generics.go b/field/generics.go index 3119380b..3dba7e51 100644 --- a/field/generics.go +++ b/field/generics.go @@ -13,6 +13,10 @@ type ScanValuer interface { Value() (driver.Value, error) // driver.Valuer } +func NewGenerics[T any](e expr) GenericsField[T] { + return GenericsField[T]{e} +} + // GenericsField a generics field struct type GenericsField[T any] struct{ expr } @@ -84,6 +88,10 @@ func (field GenericsField[T]) toSlice(values ...T) []interface{} { return slice } +func NewGenericsInt[T any](e expr) GenericsInt[T] { + return GenericsInt[T]{GenericsField: NewGenerics[T](e)} +} + // GenericsInt int type field type GenericsInt[T any] struct { GenericsField[T] @@ -169,6 +177,10 @@ func (field GenericsInt[T]) Zero() AssignExpr { return field.value(0) } +func NewGenericsString[T any](e expr) GenericsString[T] { + return GenericsString[T]{GenericsField: NewGenerics[T](e)} +} + // GenericsString string type field type GenericsString[T any] struct { GenericsField[T] diff --git a/field/time.go b/field/time.go index 128a59be..2d6aadde 100644 --- a/field/time.go +++ b/field/time.go @@ -44,12 +44,12 @@ func (field Time) Date() Time { // DateDiff equal to DATADIFF(self, value) func (field Time) DateDiff(value time.Time) Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "DATEDIFF(?,?)", Vars: []interface{}{field.RawExpr(), value}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "DATEDIFF(?,?)", Vars: []interface{}{field.RawExpr(), value}}})} } // DateFormat equal to DATE_FORMAT(self, value) func (field Time) DateFormat(value string) String { - return String{GenericsString: GenericsString[string]{GenericsField: GenericsField[string]{expr{e: clause.Expr{SQL: "DATE_FORMAT(?,?)", Vars: []interface{}{field.RawExpr(), value}}}}}} + return String{NewGenericsString[string](expr{e: clause.Expr{SQL: "DATE_FORMAT(?,?)", Vars: []interface{}{field.RawExpr(), value}}})} } // Now return result of NOW() @@ -69,61 +69,62 @@ func (field Time) CurTime() Time { // DayName equal to DAYNAME(self) func (field Time) DayName() String { - return String{GenericsString: GenericsString[string]{GenericsField: GenericsField[string]{expr{e: clause.Expr{SQL: "DAYNAME(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return String{NewGenericsString[string](expr{e: clause.Expr{SQL: "DAYNAME(?)", Vars: []interface{}{field.RawExpr()}}})} + } // MonthName equal to MONTHNAME(self) func (field Time) MonthName() String { - return String{GenericsString: GenericsString[string]{GenericsField: GenericsField[string]{expr{e: clause.Expr{SQL: "MONTHNAME(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return String{NewGenericsString[string](expr{e: clause.Expr{SQL: "MONTHNAME(?)", Vars: []interface{}{field.RawExpr()}}})} } func (field Time) Year() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "YEAR(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "YEAR(?)", Vars: []interface{}{field.RawExpr()}}})} } // Month equal to MONTH(self) func (field Time) Month() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "MONTH(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "MONTH(?)", Vars: []interface{}{field.RawExpr()}}})} } // Day equal to DAY(self) func (field Time) Day() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "DAY(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "DAY(?)", Vars: []interface{}{field.RawExpr()}}})} } // Hour equal to HOUR(self) func (field Time) Hour() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "HOUR(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "HOUR(?)", Vars: []interface{}{field.RawExpr()}}})} } // Minute equal to MINUTE(self) func (field Time) Minute() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "MINUTE(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "MINUTE(?)", Vars: []interface{}{field.RawExpr()}}})} } // Second equal to SECOND(self) func (field Time) Second() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "SECOND(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "SECOND(?)", Vars: []interface{}{field.RawExpr()}}})} } // MicroSecond equal to MICROSECOND(self) func (field Time) MicroSecond() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "MICROSECOND(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "MICROSECOND(?)", Vars: []interface{}{field.RawExpr()}}})} } // DayOfWeek equal to DAYOFWEEK(self) func (field Time) DayOfWeek() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "DAYOFWEEK(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "DAYOFWEEK(?)", Vars: []interface{}{field.RawExpr()}}})} } // DayOfMonth equal to DAYOFMONTH(self) func (field Time) DayOfMonth() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "DAYOFMONTH(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "DAYOFMONTH(?)", Vars: []interface{}{field.RawExpr()}}})} } // DayOfYear equal to DAYOFYEAR(self) func (field Time) DayOfYear() Int { - return Int{GenericsInt: GenericsInt[int]{GenericsField: GenericsField[int]{expr{e: clause.Expr{SQL: "DAYOFYEAR(?)", Vars: []interface{}{field.RawExpr()}}}}}} + return Int{NewGenericsInt[int](expr{e: clause.Expr{SQL: "DAYOFYEAR(?)", Vars: []interface{}{field.RawExpr()}}})} } // FromDays equal to FROM_DAYS(self) diff --git a/tests/.expect/dal_3/query/banks.gen.go b/tests/.expect/dal_3/query/banks.gen.go index 863cd584..a248fad6 100644 --- a/tests/.expect/dal_3/query/banks.gen.go +++ b/tests/.expect/dal_3/query/banks.gen.go @@ -7,20 +7,16 @@ 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/gorm" "gorm.io/gen/tests/.gen/dal_3/model" ) func newBank(db *gorm.DB, opts ...gen.DOOption) bank { _bank := bank{} + _bank.bankDo.RealDO = &_bank.bankDo _bank.bankDo.UseDB(db, opts...) _bank.bankDo.UseModel(&model.Bank{}) @@ -106,295 +102,16 @@ func (b bank) replaceDB(db *gorm.DB) bank { return b } -type bankDo struct{ gen.DO } - -type IBankDo interface { - gen.SubQuery - Debug() IBankDo - WithContext(ctx context.Context) IBankDo - WithResult(fc func(tx gen.Dao)) gen.ResultInfo - ReplaceDB(db *gorm.DB) - ReadDB() IBankDo - WriteDB() IBankDo - As(alias string) gen.Dao - Session(config *gorm.Session) IBankDo - Columns(cols ...field.Expr) gen.Columns - Clauses(conds ...clause.Expression) IBankDo - Not(conds ...gen.Condition) IBankDo - Or(conds ...gen.Condition) IBankDo - Select(conds ...field.Expr) IBankDo - Where(conds ...gen.Condition) IBankDo - Order(conds ...field.Expr) IBankDo - Distinct(cols ...field.Expr) IBankDo - Omit(cols ...field.Expr) IBankDo - Join(table schema.Tabler, on ...field.Expr) IBankDo - LeftJoin(table schema.Tabler, on ...field.Expr) IBankDo - RightJoin(table schema.Tabler, on ...field.Expr) IBankDo - Group(cols ...field.Expr) IBankDo - Having(conds ...gen.Condition) IBankDo - Limit(limit int) IBankDo - Offset(offset int) IBankDo - Count() (count int64, err error) - Scopes(funcs ...func(gen.Dao) gen.Dao) IBankDo - Unscoped() IBankDo - Create(values ...*model.Bank) error - CreateInBatches(values []*model.Bank, batchSize int) error - Save(values ...*model.Bank) error - First() (*model.Bank, error) - Take() (*model.Bank, error) - Last() (*model.Bank, error) - Find() ([]*model.Bank, error) - FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Bank, err error) - FindInBatches(result *[]*model.Bank, batchSize int, fc func(tx gen.Dao, batch int) error) error - Pluck(column field.Expr, dest interface{}) error - Delete(...*model.Bank) (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) IBankDo - Assign(attrs ...field.AssignExpr) IBankDo - Joins(fields ...field.RelationField) IBankDo - Preload(fields ...field.RelationField) IBankDo - FirstOrInit() (*model.Bank, error) - FirstOrCreate() (*model.Bank, error) - FindByPage(offset int, limit int) (result []*model.Bank, 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) IBankDo - UnderlyingDB() *gorm.DB - schema.Tabler -} - -func (b bankDo) Debug() IBankDo { - return b.withDO(b.DO.Debug()) -} - -func (b bankDo) WithContext(ctx context.Context) IBankDo { - return b.withDO(b.DO.WithContext(ctx)) -} - -func (b bankDo) ReadDB() IBankDo { - return b.Clauses(dbresolver.Read) -} - -func (b bankDo) WriteDB() IBankDo { - return b.Clauses(dbresolver.Write) -} - -func (b bankDo) Session(config *gorm.Session) IBankDo { - return b.withDO(b.DO.Session(config)) -} - -func (b bankDo) Clauses(conds ...clause.Expression) IBankDo { - return b.withDO(b.DO.Clauses(conds...)) -} - -func (b bankDo) Returning(value interface{}, columns ...string) IBankDo { - return b.withDO(b.DO.Returning(value, columns...)) -} - -func (b bankDo) Not(conds ...gen.Condition) IBankDo { - return b.withDO(b.DO.Not(conds...)) -} - -func (b bankDo) Or(conds ...gen.Condition) IBankDo { - return b.withDO(b.DO.Or(conds...)) -} - -func (b bankDo) Select(conds ...field.Expr) IBankDo { - return b.withDO(b.DO.Select(conds...)) -} - -func (b bankDo) Where(conds ...gen.Condition) IBankDo { - return b.withDO(b.DO.Where(conds...)) -} - -func (b bankDo) Order(conds ...field.Expr) IBankDo { - return b.withDO(b.DO.Order(conds...)) -} - -func (b bankDo) Distinct(cols ...field.Expr) IBankDo { - return b.withDO(b.DO.Distinct(cols...)) -} - -func (b bankDo) Omit(cols ...field.Expr) IBankDo { - return b.withDO(b.DO.Omit(cols...)) -} - -func (b bankDo) Join(table schema.Tabler, on ...field.Expr) IBankDo { - return b.withDO(b.DO.Join(table, on...)) -} - -func (b bankDo) LeftJoin(table schema.Tabler, on ...field.Expr) IBankDo { - return b.withDO(b.DO.LeftJoin(table, on...)) -} - -func (b bankDo) RightJoin(table schema.Tabler, on ...field.Expr) IBankDo { - return b.withDO(b.DO.RightJoin(table, on...)) -} - -func (b bankDo) Group(cols ...field.Expr) IBankDo { - return b.withDO(b.DO.Group(cols...)) -} - -func (b bankDo) Having(conds ...gen.Condition) IBankDo { - return b.withDO(b.DO.Having(conds...)) -} - -func (b bankDo) Limit(limit int) IBankDo { - return b.withDO(b.DO.Limit(limit)) -} - -func (b bankDo) Offset(offset int) IBankDo { - return b.withDO(b.DO.Offset(offset)) -} - -func (b bankDo) Scopes(funcs ...func(gen.Dao) gen.Dao) IBankDo { - return b.withDO(b.DO.Scopes(funcs...)) -} - -func (b bankDo) Unscoped() IBankDo { - return b.withDO(b.DO.Unscoped()) -} - -func (b bankDo) Create(values ...*model.Bank) error { - if len(values) == 0 { - return nil - } - return b.DO.Create(values) -} - -func (b bankDo) CreateInBatches(values []*model.Bank, batchSize int) error { - return b.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 (b bankDo) Save(values ...*model.Bank) error { - if len(values) == 0 { - return nil - } - return b.DO.Save(values) -} - -func (b bankDo) First() (*model.Bank, error) { - if result, err := b.DO.First(); err != nil { - return nil, err - } else { - return result.(*model.Bank), nil - } +type bankDo struct { + gen.GenericsDo[IBankDo, *model.Bank] } - -func (b bankDo) Take() (*model.Bank, error) { - if result, err := b.DO.Take(); err != nil { - return nil, err - } else { - return result.(*model.Bank), nil - } -} - -func (b bankDo) Last() (*model.Bank, error) { - if result, err := b.DO.Last(); err != nil { - return nil, err - } else { - return result.(*model.Bank), nil - } -} - -func (b bankDo) Find() ([]*model.Bank, error) { - result, err := b.DO.Find() - return result.([]*model.Bank), err -} - -func (b bankDo) FindInBatch(batchSize int, fc func(tx gen.Dao, batch int) error) (results []*model.Bank, err error) { - buf := make([]*model.Bank, 0, batchSize) - err = b.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 (b bankDo) FindInBatches(result *[]*model.Bank, batchSize int, fc func(tx gen.Dao, batch int) error) error { - return b.DO.FindInBatches(result, batchSize, fc) -} - -func (b bankDo) Attrs(attrs ...field.AssignExpr) IBankDo { - return b.withDO(b.DO.Attrs(attrs...)) -} - -func (b bankDo) Assign(attrs ...field.AssignExpr) IBankDo { - return b.withDO(b.DO.Assign(attrs...)) -} - -func (b bankDo) Joins(fields ...field.RelationField) IBankDo { - for _, _f := range fields { - b = *b.withDO(b.DO.Joins(_f)) - } - return &b -} - -func (b bankDo) Preload(fields ...field.RelationField) IBankDo { - for _, _f := range fields { - b = *b.withDO(b.DO.Preload(_f)) - } - return &b -} - -func (b bankDo) FirstOrInit() (*model.Bank, error) { - if result, err := b.DO.FirstOrInit(); err != nil { - return nil, err - } else { - return result.(*model.Bank), nil - } -} - -func (b bankDo) FirstOrCreate() (*model.Bank, error) { - if result, err := b.DO.FirstOrCreate(); err != nil { - return nil, err - } else { - return result.(*model.Bank), nil - } -} - -func (b bankDo) FindByPage(offset int, limit int) (result []*model.Bank, count int64, err error) { - result, err = b.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 = b.Offset(-1).Limit(-1).Count() - return -} - -func (b bankDo) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) { - count, err = b.Count() - if err != nil { - return - } - - err = b.Offset(offset).Limit(limit).Scan(result) - return -} - -func (b bankDo) Scan(result interface{}) (err error) { - return b.DO.Scan(result) -} - -func (b bankDo) Delete(models ...*model.Bank) (result gen.ResultInfo, err error) { - return b.DO.Delete(models) +type IBankDo interface { + gen.IGenericsDo[IBankDo, *model.Bank] } -func (b *bankDo) withDO(do gen.Dao) *bankDo { - b.DO = *do.(*gen.DO) - return b +func (r *bankDo) GetInstance(do gen.Dao) IBankDo { + _r := &bankDo{} + _r.DO = *do.(*gen.DO) + _r.RealDO = _r + return _r }