Skip to content

Commit

Permalink
add query options to count & exist
Browse files Browse the repository at this point in the history
  • Loading branch information
liming.dirac committed Feb 20, 2024
1 parent e27c77d commit f7d3885
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"

"github.com/dirac-lee/gdal/gutil/gsql"
"gorm.io/gorm"
"gorm.io/plugin/dbresolver"
Expand All @@ -17,8 +18,8 @@ type DAL interface {
Update(ctx context.Context, po any, where any, update any) (int64, error)
Find(ctx context.Context, po any, where any, options ...QueryOption) (err error)
First(ctx context.Context, po, where any, options ...QueryOption) error
Count(ctx context.Context, po any, where any) (int32, error)
Exist(ctx context.Context, po any, where any) (bool, error)
Count(ctx context.Context, po any, where any, options ...QueryOption) (int32, error)
Exist(ctx context.Context, po any, where any, options ...QueryOption) (bool, error)
DBWithCtx(ctx context.Context, options ...QueryOption) *gorm.DB
DB(options ...QueryOption) *gorm.DB
}
Expand Down Expand Up @@ -152,8 +153,8 @@ func (dal *dal) First(ctx context.Context, po, where any, options ...QueryOption
}

// Count get the count by Where struct
func (dal *dal) Count(ctx context.Context, po any, where any) (int32, error) {
db := dal.DBWithCtx(ctx)
func (dal *dal) Count(ctx context.Context, po any, where any, options ...QueryOption) (int32, error) {
db := dal.DBWithCtx(ctx, options...)
if db.Error != nil {
return 0, db.Error
}
Expand All @@ -171,8 +172,8 @@ func (dal *dal) Count(ctx context.Context, po any, where any) (int32, error) {
}

// Exist judge if record found by where struct
func (dal *dal) Exist(ctx context.Context, po any, where any) (bool, error) {
db, err := dal.whereDB(ctx, where)
func (dal *dal) Exist(ctx context.Context, po any, where any, options ...QueryOption) (bool, error) {
db, err := dal.whereDB(ctx, where, options...)
if err != nil {
return false, err
}
Expand Down

0 comments on commit f7d3885

Please sign in to comment.