| 1 |
// Copyright (c) 2023 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package dbutil |
| 8 |
|
| 9 |
import ( |
| 10 |
"context" |
| 11 |
"database/sql" |
| 12 |
"errors" |
| 13 |
"time" |
| 14 |
|
| 15 |
"golang.org/x/exp/constraints" |
| 16 |
) |
| 17 |
|
| 18 |
// DataStruct is an interface for structs that represent a single database row. |
| 19 |
type DataStruct[T any] interface { |
| 20 |
Scan(row Scannable) (T, error) |
| 21 |
} |
| 22 |
|
| 23 |
// QueryHelper is a generic helper struct for SQL query execution boilerplate. |
| 24 |
// |
| 25 |
// After implementing the Scan and Init methods in a data struct, the query |
| 26 |
// helper allows writing query functions in a single line. |
| 27 |
type QueryHelper[T DataStruct[T]] struct { |
| 28 |
db *Database |
| 29 |
newFunc func(qh *QueryHelper[T]) T |
| 30 |
} |
| 31 |
|
| 32 |
func MakeQueryHelper[T DataStruct[T]](db *Database, new func(qh *QueryHelper[T]) T) *QueryHelper[T] { |
| 33 |
return &QueryHelper[T]{db: db, newFunc: new} |
| 34 |
} |
| 35 |
|
| 36 |
// ValueOrErr is a helper function that returns the value if err is nil, or |
| 37 |
// returns nil and the error if err is not nil. It can be used to avoid |
| 38 |
// `if err != nil { return nil, err }` boilerplate in certain cases like |
| 39 |
// DataStruct.Scan implementations. |
| 40 |
func ValueOrErr[T any](val *T, err error) (*T, error) { |
| 41 |
if err != nil { |
| 42 |
return nil, err |
| 43 |
} |
| 44 |
return val, nil |
| 45 |
} |
| 46 |
|
| 47 |
// StrPtr returns a pointer to the given string, or nil if the string is empty. |
| 48 |
func StrPtr[T ~string](val T) *string { |
| 49 |
if val == "" { |
| 50 |
return nil |
| 51 |
} |
| 52 |
strVal := string(val) |
| 53 |
return &strVal |
| 54 |
} |
| 55 |
|
| 56 |
// NumPtr returns a pointer to the given number, or nil if the number is zero. |
| 57 |
func NumPtr[T constraints.Integer | constraints.Float](val T) *T { |
| 58 |
if val == 0 { |
| 59 |
return nil |
| 60 |
} |
| 61 |
return &val |
| 62 |
} |
| 63 |
|
| 64 |
// UnixPtr returns a pointer to the given time as unix seconds, or nil if the time is zero. |
| 65 |
func UnixPtr(val time.Time) *int64 { |
| 66 |
return ConvertedPtr(val, time.Time.Unix) |
| 67 |
} |
| 68 |
|
| 69 |
// UnixMilliPtr returns a pointer to the given time as unix milliseconds, or nil if the time is zero. |
| 70 |
func UnixMilliPtr(val time.Time) *int64 { |
| 71 |
return ConvertedPtr(val, time.Time.UnixMilli) |
| 72 |
} |
| 73 |
|
| 74 |
type Zeroable interface { |
| 75 |
IsZero() bool |
| 76 |
} |
| 77 |
|
| 78 |
// ConvertedPtr returns a pointer to the converted version of the given value, or nil if the input is zero. |
| 79 |
// |
| 80 |
// This is primarily meant for time.Time, but it can be used with any type that has implements `IsZero() bool`. |
| 81 |
// |
| 82 |
// yourTime := time.Now() |
| 83 |
// unixMSPtr := dbutil.TimePtr(yourTime, time.Time.UnixMilli) |
| 84 |
func ConvertedPtr[Input Zeroable, Output any](val Input, converter func(Input) Output) *Output { |
| 85 |
if val.IsZero() { |
| 86 |
return nil |
| 87 |
} |
| 88 |
converted := converter(val) |
| 89 |
return &converted |
| 90 |
} |
| 91 |
|
| 92 |
func (qh *QueryHelper[T]) GetDB() *Database { |
| 93 |
return qh.db |
| 94 |
} |
| 95 |
|
| 96 |
func (qh *QueryHelper[T]) New() T { |
| 97 |
return qh.newFunc(qh) |
| 98 |
} |
| 99 |
|
| 100 |
// Exec executes a query with ExecContext and returns the error. |
| 101 |
// |
| 102 |
// It omits the sql.Result return value, as it is rarely used. When the result |
| 103 |
// is wanted, use `qh.GetDB().Exec(...)` instead, which is |
| 104 |
// otherwise equivalent. |
| 105 |
func (qh *QueryHelper[T]) Exec(ctx context.Context, query string, args ...any) error { |
| 106 |
_, err := qh.db.Exec(ctx, query, args...) |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
func (qh *QueryHelper[T]) scanNew(row Scannable) (T, error) { |
| 111 |
return qh.New().Scan(row) |
| 112 |
} |
| 113 |
|
| 114 |
// QueryOne executes a query with QueryRowContext, uses the associated DataStruct |
| 115 |
// to scan it, and returns the value. If the query returns no rows, it returns nil |
| 116 |
// and no error. |
| 117 |
func (qh *QueryHelper[T]) QueryOne(ctx context.Context, query string, args ...any) (val T, err error) { |
| 118 |
val, err = qh.scanNew(qh.db.QueryRow(ctx, query, args...)) |
| 119 |
if errors.Is(err, sql.ErrNoRows) { |
| 120 |
return *new(T), nil |
| 121 |
} |
| 122 |
return val, err |
| 123 |
} |
| 124 |
|
| 125 |
// QueryMany executes a query with QueryContext, uses the associated DataStruct |
| 126 |
// to scan each row, and returns the values. If the query returns no rows, it |
| 127 |
// returns a non-nil zero-length slice and no error. |
| 128 |
func (qh *QueryHelper[T]) QueryMany(ctx context.Context, query string, args ...any) ([]T, error) { |
| 129 |
return qh.QueryManyIter(ctx, query, args...).AsList() |
| 130 |
} |
| 131 |
|
| 132 |
// QueryManyIter executes a query with QueryContext and returns a RowIter |
| 133 |
// that will use the associated DataStruct to scan each row. |
| 134 |
func (qh *QueryHelper[T]) QueryManyIter(ctx context.Context, query string, args ...any) RowIter[T] { |
| 135 |
rows, err := qh.db.Query(ctx, query, args...) |
| 136 |
return NewRowIterWithError(rows, qh.scanNew, err) |
| 137 |
} |