-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathex_test.go
More file actions
90 lines (77 loc) · 2.24 KB
/
ex_test.go
File metadata and controls
90 lines (77 loc) · 2.24 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
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 sqlite_test
import (
"context"
"fmt"
"log"
"time"
"github.com/TroutSoftware/sqlite"
)
// whatTimeIsIt defines the structure of the virtual table.
// Location is set as:
// - filtered, so it will be made available to the Filter method
// - hidden, so it can be called as a function in SQL
//
// Typical code will want to annotate those structures with a call to generate accessors to all interesting fields.
//
// //go:generate constrainer whatTimeIsIt
type whatTimeIsIt struct {
Location string `vtab:"location,filtered,hidden"`
Time string `vtab:"time"`
}
var epoch = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
// WhatTimeIsItAPI provides the (very important) service to know a point in time at a given location.
func WhatTimeIsItAPI(where *time.Location) string {
return epoch.In(where).Format(time.Kitchen)
}
// This function is typically generated in its own file whatTimeIsIt_access.go.
func (r whatTimeIsIt) GetLocation(cs sqlite.Constraints) (v string, ok bool) {
match := 0
for _, c := range cs {
if c.Column == "location" {
if c.Operation == sqlite.ConstraintEQ {
v = c.ValueString()
match++
} else {
panic("Value getter with non-constrained values")
}
}
}
if match == 0 {
return v, false
}
if match == 1 {
return v, true
}
panic("more than one match")
}
func (wtc whatTimeIsIt) Filter(_ int, cs sqlite.Constraints) sqlite.Iter[whatTimeIsIt] {
loc := time.UTC
if lspec, ok := wtc.GetLocation(cs); ok {
var err error
if loc, err = time.LoadLocation(lspec); err != nil {
return sqlite.FromError[whatTimeIsIt](err)
}
}
now := WhatTimeIsItAPI(loc)
return sqlite.FromOne(whatTimeIsIt{
Location: loc.String(),
Time: now,
})
}
/*
Virtual tables can represent much more than data on disk
In this example a virtual table is used to get the time in a local-dependent fashion.
*/
func ExampleRegisterTable() {
db, err := sqlite.Open(sqlite.MemoryPath, sqlite.RegisterTable("whattimeisit", whatTimeIsIt{}))
if err != nil {
log.Fatal(err)
}
var kitchenclock string
err = db.Exec(context.Background(), "select time from whattimeisit('Europe/Dublin')").ScanOne(&kitchenclock)
if err != nil {
log.Fatal(err)
}
fmt.Println("epoch time", kitchenclock)
// Output: epoch time 12:00AM
}