-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
90 lines (74 loc) · 1.75 KB
/
query.go
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cdb
import (
"fmt"
"strings"
sq "github.com/Masterminds/squirrel"
)
// Query represents a database query.
type Query struct {
query sq.SelectBuilder
sort string
limit int
}
func newQuery(cols []string) *Query {
q := &Query{
sort: "timestamp",
}
q.query = sq.Select(strings.Join(cols, ",\n")).
From("books")
return q
}
// NewQuery initializes a query builder.
func NewQuery(cols []string) *Query {
q := &Query{
sort: "timestamp",
}
q.query = sq.Select("JSON_OBJECT( \n" + strings.Join(cols, ",\n") + ") meta").
From("books")
return q
}
// GetByID retrieves records by id.
func (q *Query) GetByID(ids ...any) *Query {
q.query = q.query.Where(sq.Eq{"id": ids})
return q
}
// Sort sets Sort By. Takes a string argument in the form of 'sort:order'.
func (q *Query) Sort(v string) *Query {
db := strings.Split(v, ":")
sort := db[0]
if order := db[1]; order == "desc" {
sort += " DESC\n"
}
q.query = q.query.OrderBy(sort)
return q
}
// Limit sets the number of records returned.
func (q *Query) Limit(v int) *Query {
q.limit = v
q.query = q.query.Limit(uint64(v))
return q
}
// Page is used with the limit to determine OFFSET.
func (q *Query) Page(v int) *Query {
if q.limit == 0 {
q.Limit(20)
}
q.query = q.query.Offset(calculateOffset(v, q.limit))
return q
}
// Filter takes an arbitrary WHERE expression.
func (q *Query) Filter(w string) *Query {
q.query = q.query.Where(w)
return q
}
// ToSql satisfies squirrel.Sqlizer to build the sql expression with paramaters.
func (q *Query) ToSql() (string, []any, error) {
return q.query.ToSql()
}
// AddedInThePastDays retrieves the past n days of records.
func AddedInThePastDays(d int) string {
if d == 0 {
d = 7
}
return fmt.Sprintf("last_modified > DATE('now', '-%d day')", d)
}