Skip to content

Commit

Permalink
feat: tosql
Browse files Browse the repository at this point in the history
  • Loading branch information
qqxhb committed May 6, 2024
1 parent 821a7e7 commit 23f5151
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions generics.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ type IGenericsDo[T any, E any] interface {
UnderlyingDB() *gorm.DB
schema.Tabler
GetInstance(do Dao) T
ToSQL(queryFn func(T) T) string
}

type GenericsDo[T IGenericsDo[T, E], E any] struct {

Check failure on line 75 in generics.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] generics.go#L75

exported: exported type GenericsDo should have comment or be unexported (revive)
Raw output
generics.go:75:6: exported: exported type GenericsDo should have comment or be unexported (revive)
type GenericsDo[T IGenericsDo[T, E], E any] struct {
     ^
Expand Down Expand Up @@ -311,6 +312,14 @@ func (b GenericsDo[T, E]) Delete(models ...E) (result ResultInfo, err error) {
return b.DO.Delete(models)
}

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.Debug()))
db := t.underlyingDB()
stmt := db.Statement
return db.Dialector.Explain(stmt.SQL.String(), stmt.Vars...)
}

func (b *GenericsDo[T, E]) withDO(do Dao) T {
return b.RealDO.GetInstance(do)
}

0 comments on commit 23f5151

Please sign in to comment.