-
Notifications
You must be signed in to change notification settings - Fork 111
/
registry.go
93 lines (78 loc) · 2.48 KB
/
registry.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
91
92
93
package sqlite
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/rilldata/rill/runtime/drivers"
)
// FindInstances implements drivers.RegistryStore
func (c *connection) FindInstances(ctx context.Context) ([]*drivers.Instance, error) {
return c.findInstances(ctx, "")
}
// FindInstance implements drivers.RegistryStore
func (c *connection) FindInstance(ctx context.Context, id string) (*drivers.Instance, error) {
is, err := c.findInstances(ctx, "WHERE id = $1", id)
if err != nil {
return nil, err
}
if len(is) == 0 {
return nil, drivers.ErrNotFound
}
return is[0], nil
}
func (c *connection) findInstances(_ context.Context, whereClause string, args ...any) ([]*drivers.Instance, error) {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
sql := fmt.Sprintf("SELECT id, olap_driver, olap_dsn, repo_driver, repo_dsn, embed_catalog, created_on, updated_on FROM instances %s ORDER BY id", whereClause)
rows, err := c.db.QueryxContext(ctx, sql, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var res []*drivers.Instance
for rows.Next() {
i := &drivers.Instance{}
err := rows.Scan(&i.ID, &i.OLAPDriver, &i.OLAPDSN, &i.RepoDriver, &i.RepoDSN, &i.EmbedCatalog, &i.CreatedOn, &i.UpdatedOn)
if err != nil {
return nil, err
}
res = append(res, i)
}
return res, nil
}
// CreateInstance implements drivers.RegistryStore
func (c *connection) CreateInstance(_ context.Context, inst *drivers.Instance) error {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
if inst.ID == "" {
inst.ID = uuid.NewString()
}
now := time.Now()
_, err := c.db.ExecContext(
ctx,
"INSERT INTO instances(id, olap_driver, olap_dsn, repo_driver, repo_dsn, embed_catalog, created_on, updated_on) "+
"VALUES ($1, $2, $3, $4, $5, $6, $7, $7)",
inst.ID,
inst.OLAPDriver,
inst.OLAPDSN,
inst.RepoDriver,
inst.RepoDSN,
inst.EmbedCatalog,
now,
)
if err != nil {
return err
}
// We assign manually instead of using RETURNING because it doesn't work for timestamps in SQLite
inst.CreatedOn = now
inst.UpdatedOn = now
return nil
}
// DeleteInstance implements drivers.RegistryStore
func (c *connection) DeleteInstance(_ context.Context, id string) error {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
_, err := c.db.ExecContext(ctx, "DELETE FROM instances WHERE id=$1", id)
return err
}