-
Notifications
You must be signed in to change notification settings - Fork 402
/
impl.go
128 lines (114 loc) · 3.02 KB
/
impl.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package dbutil
import (
"strconv"
"time"
)
// Implementation type of valid DBs.
type Implementation int
const (
// Unknown is an unknown db type.
Unknown Implementation = iota
// Postgres is a Postgresdb type.
Postgres
// Cockroach is a Cockroachdb type.
Cockroach
// Bolt is a Bolt kv store.
Bolt
// Redis is a Redis kv store.
Redis
// SQLite3 is a sqlite3 database.
SQLite3
// Spanner is Google Spanner instance with Google SQL dialect.
Spanner
)
// ImplementationForScheme returns the Implementation that is used for
// the url with the provided scheme.
func ImplementationForScheme(scheme string) Implementation {
switch scheme {
case "pgx", "postgres", "postgresql":
return Postgres
case "cockroach":
return Cockroach
case "bolt":
return Bolt
case "redis":
return Redis
case "sqlite", "sqlite3":
return SQLite3
case "spanner":
return Spanner
default:
return Unknown
}
}
// SchemeForImplementation returns the scheme that is used for URLs
// that use the given Implementation.
func SchemeForImplementation(implementation Implementation) string {
return implementation.String()
}
// String returns the default name for a given implementation.
func (impl Implementation) String() string {
switch impl {
case Postgres:
return "postgres"
case Cockroach:
return "cockroach"
case Bolt:
return "bolt"
case Redis:
return "redis"
case SQLite3:
return "sqlite3"
case Spanner:
return "spanner"
default:
return "<unknown>"
}
}
// AsOfSystemTime returns a SQL query for the specifying the AS OF SYSTEM TIME using
// a concrete time.
func (impl Implementation) AsOfSystemTime(t time.Time) string {
if impl != Cockroach {
return ""
}
if t.IsZero() {
return ""
}
return " AS OF SYSTEM TIME '" + strconv.FormatInt(t.UnixNano(), 10) + "' "
}
// WrapAsOfSystemTime converts a query to include AS OF SYSTEM TIME using
// a concrete time.
func (impl Implementation) WrapAsOfSystemTime(sql string, t time.Time) string {
aost := impl.AsOfSystemTime(t)
if aost == "" {
return sql
}
return "SELECT * FROM (" + sql + ")" + aost
}
// AsOfSystemInterval returns a SQL query for the specifying the AS OF SYSTEM TIME using
// a relative interval. The interval should be negative.
func (impl Implementation) AsOfSystemInterval(interval time.Duration) string {
if impl != Cockroach {
return ""
}
// a positive or zero interval disables AS OF SYSTEM TIME.
if interval >= 0 {
return ""
}
// Cockroach does not support intervals smaller than a microsecond.
if interval > -time.Microsecond {
interval = -time.Microsecond
}
return " AS OF SYSTEM TIME '" + interval.String() + "' "
}
// WrapAsOfSystemInterval converts a query to include AS OF SYSTEM TIME using
// a relative interval. The interval should be negative.
func (impl Implementation) WrapAsOfSystemInterval(sql string, interval time.Duration) string {
aost := impl.AsOfSystemInterval(interval)
if aost == "" {
return sql
}
return "SELECT * FROM (" + sql + ")" + aost
}