Skip to content

Commit

Permalink
feat: fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed Jan 15, 2025
1 parent 18c4d30 commit c0fbf3f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 42 additions & 0 deletions generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,105 +82,130 @@ type GenericsDo[T IGenericsDo[T, E], E any] struct {
RealDO T
}

// Debug ...
func (b GenericsDo[T, E]) Debug() T {
return b.withDO(b.DO.Debug())
}

// WithContext ...
func (b GenericsDo[T, E]) WithContext(ctx context.Context) T {
return b.withDO(b.DO.WithContext(ctx))
}

// ReadDB ...
func (b GenericsDo[T, E]) ReadDB() T {
return b.Clauses(dbresolver.Read)
}

// WriteDB ...
func (b GenericsDo[T, E]) WriteDB() T {
return b.Clauses(dbresolver.Write)
}

// Session ...
func (b GenericsDo[T, E]) Session(config *gorm.Session) T {
return b.withDO(b.DO.Session(config))
}

// Clauses ...
func (b GenericsDo[T, E]) Clauses(conds ...clause.Expression) T {
return b.withDO(b.DO.Clauses(conds...))
}

// Returning ...
func (b GenericsDo[T, E]) Returning(value interface{}, columns ...string) T {
return b.withDO(b.DO.Returning(value, columns...))
}

// Not ...
func (b GenericsDo[T, E]) Not(conds ...Condition) T {
return b.withDO(b.DO.Not(conds...))
}

// Or ...
func (b GenericsDo[T, E]) Or(conds ...Condition) T {
return b.withDO(b.DO.Or(conds...))
}

// Select ...
func (b GenericsDo[T, E]) Select(conds ...field.Expr) T {
return b.withDO(b.DO.Select(conds...))
}

// Where ...
func (b GenericsDo[T, E]) Where(conds ...Condition) T {
return b.withDO(b.DO.Where(conds...))
}

// Order ...
func (b GenericsDo[T, E]) Order(conds ...field.Expr) T {
return b.withDO(b.DO.Order(conds...))
}

// Distinct ...
func (b GenericsDo[T, E]) Distinct(cols ...field.Expr) T {
return b.withDO(b.DO.Distinct(cols...))
}

// Omit ...
func (b GenericsDo[T, E]) Omit(cols ...field.Expr) T {
return b.withDO(b.DO.Omit(cols...))
}

// Join ...
func (b GenericsDo[T, E]) Join(table schema.Tabler, on ...field.Expr) T {
return b.withDO(b.DO.Join(table, on...))
}

// LeftJoin ...
func (b GenericsDo[T, E]) LeftJoin(table schema.Tabler, on ...field.Expr) T {
return b.withDO(b.DO.LeftJoin(table, on...))
}

// RightJoin ...
func (b GenericsDo[T, E]) RightJoin(table schema.Tabler, on ...field.Expr) T {
return b.withDO(b.DO.RightJoin(table, on...))
}

// Group ...
func (b GenericsDo[T, E]) Group(cols ...field.Expr) T {
return b.withDO(b.DO.Group(cols...))
}

// Having ...
func (b GenericsDo[T, E]) Having(conds ...Condition) T {
return b.withDO(b.DO.Having(conds...))
}

// Limit ...
func (b GenericsDo[T, E]) Limit(limit int) T {
return b.withDO(b.DO.Limit(limit))
}

// Offset ...
func (b GenericsDo[T, E]) Offset(offset int) T {
return b.withDO(b.DO.Offset(offset))
}

// Scopes ...
func (b GenericsDo[T, E]) Scopes(funcs ...func(Dao) Dao) T {
return b.withDO(b.DO.Scopes(funcs...))
}

// Unscoped ...
func (b GenericsDo[T, E]) Unscoped() T {
return b.withDO(b.DO.Unscoped())
}

// Create ...
func (b GenericsDo[T, E]) Create(values ...E) error {
if len(values) == 0 {
return nil
}
return b.DO.Create(values)
}

// CreateInBatches ...
func (b GenericsDo[T, E]) CreateInBatches(values []E, batchSize int) error {
return b.DO.CreateInBatches(values, batchSize)
}
Expand All @@ -194,6 +219,7 @@ func (b GenericsDo[T, E]) Save(values ...E) error {
return b.DO.Save(values)
}

// First ...
func (b GenericsDo[T, E]) First() (E, error) {
var e E
if result, err := b.DO.First(); err != nil {
Expand All @@ -203,6 +229,7 @@ func (b GenericsDo[T, E]) First() (E, error) {
}
}

// Take ...
func (b GenericsDo[T, E]) Take() (E, error) {
var e E
if result, err := b.DO.Take(); err != nil {
Expand All @@ -212,6 +239,7 @@ func (b GenericsDo[T, E]) Take() (E, error) {
}
}

// Last ...
func (b GenericsDo[T, E]) Last() (E, error) {
var e E
if result, err := b.DO.Last(); err != nil {
Expand All @@ -221,11 +249,13 @@ func (b GenericsDo[T, E]) Last() (E, error) {
}
}

// Find ...
func (b GenericsDo[T, E]) Find() ([]E, error) {
result, err := b.DO.Find()
return result.([]E), err
}

// FindInBatch ...
func (b GenericsDo[T, E]) FindInBatch(batchSize int, fc func(tx Dao, batch int) error) (results []E, err error) {
buf := make([]E, 0, batchSize)
err = b.DO.FindInBatches(&buf, batchSize, func(tx Dao, batch int) error {
Expand All @@ -235,18 +265,22 @@ func (b GenericsDo[T, E]) FindInBatch(batchSize int, fc func(tx Dao, batch int)
return results, err
}

// FindInBatches ...
func (b GenericsDo[T, E]) FindInBatches(result *[]E, batchSize int, fc func(tx Dao, batch int) error) error {
return b.DO.FindInBatches(result, batchSize, fc)
}

// Attrs ...
func (b GenericsDo[T, E]) Attrs(attrs ...field.AssignExpr) T {
return b.withDO(b.DO.Attrs(attrs...))
}

// Assign ...
func (b GenericsDo[T, E]) Assign(attrs ...field.AssignExpr) T {
return b.withDO(b.DO.Assign(attrs...))
}

// Joins ...
func (b GenericsDo[T, E]) Joins(fields ...field.RelationField) T {
var do Dao = &b.DO
for _, _f := range fields {
Expand All @@ -255,6 +289,7 @@ func (b GenericsDo[T, E]) Joins(fields ...field.RelationField) T {
return b.withDO(do)
}

// Preload ...
func (b GenericsDo[T, E]) Preload(fields ...field.RelationField) T {
var do Dao = &b.DO
for _, _f := range fields {
Expand All @@ -263,6 +298,7 @@ func (b GenericsDo[T, E]) Preload(fields ...field.RelationField) T {
return b.withDO(do)
}

// FirstOrInit ...
func (b GenericsDo[T, E]) FirstOrInit() (E, error) {
var e E
if result, err := b.DO.FirstOrInit(); err != nil {
Expand All @@ -272,6 +308,7 @@ func (b GenericsDo[T, E]) FirstOrInit() (E, error) {
}
}

// FirstOrCreate ...
func (b GenericsDo[T, E]) FirstOrCreate() (E, error) {
var e E
if result, err := b.DO.FirstOrCreate(); err != nil {
Expand All @@ -281,6 +318,7 @@ func (b GenericsDo[T, E]) FirstOrCreate() (E, error) {
}
}

// FindByPage ...
func (b GenericsDo[T, E]) FindByPage(offset int, limit int) (result []E, count int64, err error) {
result, err = b.Offset(offset).Limit(limit).Find()
if err != nil {
Expand All @@ -296,6 +334,7 @@ func (b GenericsDo[T, E]) FindByPage(offset int, limit int) (result []E, count i
return
}

// ScanByPage ...
func (b GenericsDo[T, E]) ScanByPage(result interface{}, offset int, limit int) (count int64, err error) {
count, err = b.Count()
if err != nil {
Expand All @@ -305,14 +344,17 @@ func (b GenericsDo[T, E]) ScanByPage(result interface{}, offset int, limit int)
return
}

// Scan ...
func (b GenericsDo[T, E]) Scan(result interface{}) (err error) {
return b.DO.Scan(result)
}

// Delete ...
func (b GenericsDo[T, E]) Delete(models ...E) (result ResultInfo, err error) {
return b.DO.Delete(models)
}

// ToSQL ...
func (b GenericsDo[T, E]) ToSQL(queryFn func(T) T) string {
b.db = b.db.Session(&gorm.Session{DryRun: true, SkipDefaultTransaction: true})
t := queryFn(b.withDO(&b.DO))
Expand Down
4 changes: 3 additions & 1 deletion internal/template/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const (
` + getFieldMethod + fillFieldMapMethod + cloneMethod + replaceMethod + relationship

// TableQueryIface table query interface
TableQueryIface = defineDoInterface
TableQueryIface = defineDoInterface

// TableGenericQueryIface table generic query interface
TableGenericQueryIface = defineGenericsDoInterface

//DefineGenericsMethodStruct generics do struct
Expand Down

0 comments on commit c0fbf3f

Please sign in to comment.