remove LIMIT call from FetchOne helpers

This commit is contained in:
Elijah Duffy
2025-06-12 18:04:37 -07:00
parent e20984d2b5
commit 8f3b5118c2

18
dbx.go
View File

@@ -60,12 +60,6 @@ type Statement interface {
ExecContext(ctx context.Context, db qrm.Executable) (sql.Result, error) ExecContext(ctx context.Context, db qrm.Executable) (sql.Result, error)
} }
// SelectStatement is a Jet statement that can be executed to fetch rows from the database.
type SelectStatement interface {
Statement
LIMIT(limit int64) SelectStatement
}
var ( var (
// ErrNoRows is returned when a query returns no rows. // ErrNoRows is returned when a query returns no rows.
ErrNoRows = qrm.ErrNoRows ErrNoRows = qrm.ErrNoRows
@@ -114,7 +108,7 @@ func ModuleDB(dialect Dialect, cfg *DBConfig, forceDebugLog bool) *app.Module {
// Fetch queries the database and returns the result as a slice. If the query // Fetch queries the database and returns the result as a slice. If the query
// returns no rows, it returns an empty slice and no error. // returns no rows, it returns an empty slice and no error.
func Fetch[T any](sqlo Queryable, stmt SelectStatement) ([]*T, error) { func Fetch[T any](sqlo Queryable, stmt Statement) ([]*T, error) {
var result []*T var result []*T
if err := stmt.Query(sqlo, &result); err != nil && !errors.Is(err, ErrNoRows) { if err := stmt.Query(sqlo, &result); err != nil && !errors.Is(err, ErrNoRows) {
return nil, err return nil, err
@@ -124,7 +118,7 @@ func Fetch[T any](sqlo Queryable, stmt SelectStatement) ([]*T, error) {
// MustFetch queries the database and returns the result as a slice. If the query // MustFetch queries the database and returns the result as a slice. If the query
// returns no rows, it returns an empty slice and the desired error. // returns no rows, it returns an empty slice and the desired error.
func MustFetch[T any](sqlo Queryable, stmt SelectStatement, notFoundErr error) ([]*T, error) { func MustFetch[T any](sqlo Queryable, stmt Statement, notFoundErr error) ([]*T, error) {
result, err := Fetch[T](sqlo, stmt) result, err := Fetch[T](sqlo, stmt)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -137,8 +131,8 @@ func MustFetch[T any](sqlo Queryable, stmt SelectStatement, notFoundErr error) (
// FetchOne queries the database and returns a single result. If the query // FetchOne queries the database and returns a single result. If the query
// returns no rows, it returns nil and no error. // returns no rows, it returns nil and no error.
func FetchOne[T any](sqlo Queryable, stmt SelectStatement) (*T, error) { func FetchOne[T any](sqlo Queryable, stmt Statement) (*T, error) {
result, err := Fetch[T](sqlo, stmt.LIMIT(1)) result, err := Fetch[T](sqlo, stmt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -150,8 +144,8 @@ func FetchOne[T any](sqlo Queryable, stmt SelectStatement) (*T, error) {
// MustFetchOne queries the database and returns a single result. If the query // MustFetchOne queries the database and returns a single result. If the query
// returns no rows, it returns nil and the desired error. // returns no rows, it returns nil and the desired error.
func MustFetchOne[T any](sqlo Queryable, stmt SelectStatement, notFoundErr error) (*T, error) { func MustFetchOne[T any](sqlo Queryable, stmt Statement, notFoundErr error) (*T, error) {
result, err := MustFetch[T](sqlo, stmt.LIMIT(1), notFoundErr) result, err := MustFetch[T](sqlo, stmt, notFoundErr)
if err != nil { if err != nil {
return nil, err return nil, err
} }