-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneric.go
More file actions
55 lines (52 loc) · 1.68 KB
/
Copy pathgeneric.go
File metadata and controls
55 lines (52 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package relica
// One executes a SELECT query and scans a single row into a new value of type T.
// Returns the populated value and any error.
// If no rows match, returns relica.ErrNotFound.
//
// This is the generic equivalent of SelectQuery.One(&dest) — providing
// compile-time type safety for the scan destination.
//
// Example:
//
// user, err := relica.One[User](db.Select("id", "name").From("users").Where(relica.Eq("id", 1)))
// if errors.Is(err, relica.ErrNotFound) {
// // not found
// }
func One[T any](sq *SelectQuery) (T, error) {
var dest T
err := sq.One(&dest)
return dest, err
}
// All executes a SELECT query and scans all rows into a slice of type T.
// Returns an empty slice (not nil) if no rows match.
//
// This is the generic equivalent of SelectQuery.All(&dest) — providing
// compile-time type safety for the scan destination.
//
// Example:
//
// users, err := relica.All[User](
// db.Select("id", "name", "email").
// From("users").
// Where(relica.Eq("status", "active")).
// OrderBy("name").
// Limit(100),
// )
func All[T any](sq *SelectQuery) ([]T, error) {
var dest []T
err := sq.All(&dest)
return dest, err
}
// Scalar executes a SELECT query and scans a single scalar value of type T.
// Useful for COUNT, SUM, MAX, MIN, and other aggregate queries.
//
// Example:
//
// count, err := relica.Scalar[int64](db.Select("COUNT(*)").From("users"))
// maxPrice, err := relica.Scalar[float64](db.Select("MAX(price)").From("products"))
// name, err := relica.Scalar[string](db.Select("name").From("users").Where(relica.Eq("id", 1)))
func Scalar[T any](sq *SelectQuery) (T, error) {
var dest T
err := sq.Row(&dest)
return dest, err
}