Skip to content

Commit

Permalink
change: BindRowXxx check sql.ErrNoRows and return a bool
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 10, 2024
1 parent 8b6d77f commit 0102732
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 19 deletions.
10 changes: 4 additions & 6 deletions dml_oper.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ func (o Oper[T]) GetContext(ctx context.Context, sort op.Sorter, conds ...op.Con
b.Sort(sort)
}

err = b.Limit(1).BindRowStructContext(ctx, &obj)
ok, err = CheckErrNoRows(err)
ok, err = b.Limit(1).BindRowStructContext(ctx, &obj)
return
}

Expand Down Expand Up @@ -220,8 +219,7 @@ func (o Oper[T]) Sum(field string, conds ...op.Condition) (int, error) {

// SumContext is used to sum the field values of the records by the condition.
func (o Oper[T]) SumContext(ctx context.Context, field string, conds ...op.Condition) (total int, err error) {
err = o.Table.Select(Sum(field)).Where(conds...).BindRowContext(ctx, &total)
_, err = CheckErrNoRows(err)
_, err = o.Table.Select(Sum(field)).Where(conds...).BindRowContext(ctx, &total)
return
}

Expand All @@ -232,7 +230,7 @@ func (o Oper[T]) Count(conds ...op.Condition) (total int, err error) {

// CountContext is used to count the number of records by the condition.
func (o Oper[T]) CountContext(ctx context.Context, conds ...op.Condition) (total int, err error) {
err = o.Table.Select(Count("*")).Where(conds...).BindRowContext(ctx, &total)
_, err = o.Table.Select(Count("*")).Where(conds...).BindRowContext(ctx, &total)
return
}

Expand All @@ -243,7 +241,7 @@ func (o Oper[T]) CountDistinct(field string, conds ...op.Condition) (total int,

// CountDistinctContext is the same as Count, but excluding the same field records.
func (o Oper[T]) CountDistinctContext(ctx context.Context, field string, conds ...op.Condition) (total int, err error) {
err = o.Table.Select(CountDistinct(field)).Where(conds...).BindRowContext(ctx, &total)
_, err = o.Table.Select(CountDistinct(field)).Where(conds...).BindRowContext(ctx, &total)
return
}

Expand Down
9 changes: 4 additions & 5 deletions dml_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,7 @@ func (o Operation[T]) Get(conds ...op.Condition) (obj T, ok bool, err error) {

// GetContext just queries a result.
func (o Operation[T]) GetContext(ctx context.Context, conds ...op.Condition) (obj T, ok bool, err error) {
err = o.SelectStruct(obj).Where(conds...).Limit(1).BindRowStructContext(ctx, &obj)
ok, err = CheckErrNoRows(err)
ok, err = o.SelectStruct(obj).Where(conds...).Limit(1).BindRowStructContext(ctx, &obj)
return
}

Expand Down Expand Up @@ -264,7 +263,7 @@ func (o Operation[T]) Query(page, pageSize int64, conds ...op.Condition) ([]T, e
//
// o.Select(Count("*")).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&total)
func (o Operation[T]) Count(conds ...op.Condition) (total int, err error) {
err = o.Select(Count("*")).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&total)
_, err = o.Select(Count("*")).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&total)
return
}

Expand All @@ -273,13 +272,13 @@ func (o Operation[T]) Count(conds ...op.Condition) (total int, err error) {
//
// o.Select(CountDistinct(field)).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&total)
func (o Operation[T]) CountDistinct(field string, conds ...op.Condition) (total int, err error) {
err = o.Select(CountDistinct(field)).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&total)
_, err = o.Select(CountDistinct(field)).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&total)
return
}

func (o Operation[T]) Exist(conds ...op.Condition) (exist bool, err error) {
var id int
err = o.Select(op.KeyId.Key).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&id)
_, err = o.Select(op.KeyId.Key).Where(conds...).Where(op.IsNotDeletedCond).BindRow(&id)
exist, err = CheckErrNoRows(err)
return
}
16 changes: 8 additions & 8 deletions dml_select_bind_row.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@ func (db *DB) QueryRowxContext(ctx context.Context, query string, args ...any) R
}

// BindRow is equal to b.BindRowContext(context.Background(), dest...).
func (b *SelectBuilder) BindRow(dest ...any) error {
func (b *SelectBuilder) BindRow(dest ...any) (bool, error) {
return b.BindRowContext(context.Background(), dest...)
}

// BindRowStruct is equal to b.BindRowStructContext(context.Background(), dest).
func (b *SelectBuilder) BindRowStruct(dest any) error {
func (b *SelectBuilder) BindRowStruct(dest any) (bool, error) {
return b.BindRowStructContext(context.Background(), dest)
}

// BindRowContext is convenient function, which is equal to
// b.QueryRowContext(c).Scan(dest...).
func (b *SelectBuilder) BindRowContext(c context.Context, dest ...any) error {
return b.QueryRowContext(c).Scan(dest...)
// b.QueryRowContext(c).Bind(dest...).
func (b *SelectBuilder) BindRowContext(c context.Context, dest ...any) (bool, error) {
return b.QueryRowContext(c).Bind(dest...)
}

// BindRowStructContext is convenient function, which is equal to
// b.QueryRowContext(c).ScanStruct(dest).
func (b *SelectBuilder) BindRowStructContext(c context.Context, dest any) error {
return b.QueryRowContext(c).ScanStruct(dest)
// b.QueryRowContext(c).BindStruct(dest).
func (b *SelectBuilder) BindRowStructContext(c context.Context, dest any) (bool, error) {
return b.QueryRowContext(c).BindStruct(dest)
}

// QueryRow builds the sql and executes it.
Expand Down

0 comments on commit 0102732

Please sign in to comment.