-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtimerange.go
More file actions
65 lines (58 loc) · 1.89 KB
/
timerange.go
File metadata and controls
65 lines (58 loc) · 1.89 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
56
57
58
59
60
61
62
63
64
65
package dbtype
import (
"fmt"
"time"
)
// TimeRange holds optional floor/ceil bounds for time-based ID range queries
// against a primary key column. It implements squirrel.Sqlizer, so it can be
// passed directly to squirrel.Where().
//
// Construct via [UUIDv7Range] or [Int64IDRange].
type TimeRange struct {
column string
floor any
ceil any
}
// UUIDv7Range builds a TimeRange that brackets column with [FloorUUIDv7] / [CeilUUIDv7].
// Nil since or until leaves that side unbounded.
func UUIDv7Range(column string, since, until *time.Time) TimeRange {
r := TimeRange{column: column}
if since != nil {
r.floor = FloorUUIDv7(*since)
}
if until != nil {
r.ceil = CeilUUIDv7(*until)
}
return r
}
// Int64IDRange builds a TimeRange that brackets column with [FloorInt64ID] / [CeilInt64ID].
// Nil since or until leaves that side unbounded.
func Int64IDRange(column string, since, until *time.Time) TimeRange {
r := TimeRange{column: column}
if since != nil {
r.floor = FloorInt64ID(*since)
}
if until != nil {
r.ceil = CeilInt64ID(*until)
}
return r
}
// Floor returns the lower-bound value and true, or (nil, false) if unbounded.
func (r TimeRange) Floor() (any, bool) { return r.floor, r.floor != nil }
// Ceil returns the upper-bound value and true, or (nil, false) if unbounded.
func (r TimeRange) Ceil() (any, bool) { return r.ceil, r.ceil != nil }
// ToSql implements squirrel.Sqlizer.
// Returns "column BETWEEN ? AND ?", "column >= ?", "column <= ?",
// or "1=1" depending on which bounds are set.
func (r TimeRange) ToSql() (string, []any, error) {
switch {
case r.floor != nil && r.ceil != nil:
return fmt.Sprintf("%s BETWEEN ? AND ?", r.column), []any{r.floor, r.ceil}, nil
case r.floor != nil:
return fmt.Sprintf("%s >= ?", r.column), []any{r.floor}, nil
case r.ceil != nil:
return fmt.Sprintf("%s <= ?", r.column), []any{r.ceil}, nil
default:
return "1=1", nil, nil
}
}