Skip to content

Commit 9231dfc

Browse files
authored
Merge pull request #5 from CovenantSQL/feature/nocurrenttime-vfs
Add the nocurrenttime VFS and the sqlite_omit_localtime tag for deterministic date and time
2 parents 11c149c + 4156137 commit 9231dfc

8 files changed

Lines changed: 242 additions & 4 deletions

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@ Pass the key in the DSN, `sql.Open("sqlite3", "file:test.db?_crypto_key=secret")
1616
- With a shared cache (`cache=shared`), a connection with a wrong key or no key can read a database that another connection of the process has opened with the right key.
1717
- Backups between an encrypted and a plain database fail; use `sqlcipher_export` to copy between them.
1818

19+
## Deterministic date and time
20+
21+
For results that do not depend on when or where a query runs, as when several replicas execute the same statements:
22+
23+
- `vfs=nocurrenttime` in the DSN uses a copy of the default VFS without the current time, so `'now'`, date and time functions called without a time value, and the `CURRENT_*` keywords give NULL.
24+
- The `sqlite_omit_localtime` build tag removes the `localtime` and `utc` modifiers, which depend on the time zone of the host; they give NULL as well. The tag only changes the bundled SQLite, so with the `libsqlite3` tag, opening a database fails unless the system SQLite is built with `SQLITE_OMIT_LOCALTIME`.
25+
26+
Without a current time, a column declared `NOT NULL DEFAULT CURRENT_TIMESTAMP` cannot get its default, and inserts that rely on it fail. SQLite also skips the profile callbacks of statements, so `sqlite_trace` profile events and SQLCipher's `PRAGMA cipher_profile` report nothing.
27+
1928
## Changes from go-sqlite3
2029

2130
- `sqlite3-binding.c`, `sqlite3-binding.h` and `sqlite3ext.h` are generated from the SQLCipher sources. The `upgrade` tool, which replaces them with the official SQLite amalgamation, is removed.
2231
- The LibTomCrypt sources SQLCipher needs: the `tomcrypt*.h` headers and the `.c` files whose names do not start with `sqlite3`.
2332
- `sqlite3.go` defines the SQLCipher compile options, turns off the default logging of SQLCipher to stderr, and adds the `_crypto_key` DSN parameter.
2433
- `Exec` without arguments reports missing arguments again, which go-sqlite3 v1.14.52 does not.
34+
- `sqlite3_nocurrenttime.go` adds the `nocurrenttime` VFS, and `sqlite3_opt_omit_localtime.go` the `sqlite_omit_localtime` tag.
2535
- `sqlite3_opt_icu.go` adds the libraries that a static link of ICU needs.
2636
- The CIFuzz workflow, which builds the OSS-Fuzz project of go-sqlite3, is removed.
2737

‎sqlite3.go‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,14 @@ func Version() (libVersion string, libVersionNumber int, sourceID string) {
381381
return libVersion, libVersionNumber, sourceID
382382
}
383383

384+
// compileOptionUsed reports whether SQLite was built with the option, named
385+
// without the SQLITE_ prefix.
386+
func compileOptionUsed(name string) bool {
387+
cname := C.CString(name)
388+
defer C.free(unsafe.Pointer(cname))
389+
return C.sqlite3_compileoption_used(cname) != 0
390+
}
391+
384392
const (
385393
// used by authorizer and pre_update_hook
386394
SQLITE_DELETE = C.SQLITE_DELETE
@@ -1182,6 +1190,11 @@ func (c *SQLiteConn) begin(ctx context.Context) (driver.Tx, error) {
11821190
// _crypto_key=XXX
11831191
// Specify the SQLCipher encryption key, as text without quotes.
11841192
//
1193+
// vfs=nocurrenttime
1194+
// Use a copy of the default VFS without the current time: 'now', date and
1195+
// time functions without a time value and the CURRENT_* keywords give NULL,
1196+
// so results do not depend on when a query runs.
1197+
//
11851198
// _auto_vacuum=X | _vacuum=X
11861199
// 0 | none - Auto Vacuum disabled
11871200
// 1 | full - Auto Vacuum FULL
@@ -1236,6 +1249,11 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
12361249
if C.sqlite3_threadsafe() == 0 {
12371250
return nil, errors.New("sqlite library was not compiled for thread-safe operation")
12381251
}
1252+
// The tag only changes the bundled SQLite: a system SQLite linked with the
1253+
// libsqlite3 tag may still have the localtime and utc modifiers.
1254+
if omitLocaltime && !compileOptionUsed("OMIT_LOCALTIME") {
1255+
return nil, errors.New("sqlite3: the sqlite_omit_localtime tag needs SQLite built with SQLITE_OMIT_LOCALTIME")
1256+
}
12391257

12401258
var pkey string
12411259

@@ -1322,10 +1340,7 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
13221340
if cryptoKey != "" {
13231341
// Without SQLCipher, as with the libsqlite3 tag and a plain system SQLite,
13241342
// PRAGMA key does nothing and the database would be written in plain text.
1325-
hasCodec := C.CString("HAS_CODEC")
1326-
used := C.sqlite3_compileoption_used(hasCodec)
1327-
C.free(unsafe.Pointer(hasCodec))
1328-
if used == 0 {
1343+
if !compileOptionUsed("HAS_CODEC") {
13291344
return nil, errors.New("sqlite3: _crypto_key needs SQLite built with SQLCipher")
13301345
}
13311346
// A NUL would cut the PRAGMA key statement short, and the syntax error
@@ -1634,9 +1649,19 @@ func (d *SQLiteDriver) Open(dsn string) (driver.Conn, error) {
16341649
stmtCacheSize = iv
16351650
}
16361651

1652+
// SQLite uses the last vfs parameter of a file: URI, while vfsName
1653+
// would be the first one.
1654+
if len(params["vfs"]) > 1 {
1655+
return nil, errors.New("sqlite3: more than one vfs parameter")
1656+
}
16371657
if val := params.Get("vfs"); val != "" {
16381658
vfsName = val
16391659
}
1660+
if vfsName == noCurrentTimeVFS {
1661+
if err := registerNoCurrentTimeVFS(); err != nil {
1662+
return nil, err
1663+
}
1664+
}
16401665

16411666
if !strings.HasPrefix(dsn, "file:") {
16421667
dsn = dsn[:pos]

‎sqlite3_nocurrenttime.go‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//go:build cgo
2+
3+
package sqlite3
4+
5+
/*
6+
#ifndef USE_LIBSQLITE3
7+
#include "sqlite3-binding.h"
8+
#else
9+
#include <sqlite3.h>
10+
#endif
11+
12+
static sqlite3_vfs noCurrentTimeVfs;
13+
14+
static int noCurrentTime(sqlite3_vfs *vfs, double *now) {
15+
*now = 0;
16+
return SQLITE_ERROR;
17+
}
18+
19+
static int noCurrentTimeInt64(sqlite3_vfs *vfs, sqlite3_int64 *now) {
20+
*now = 0;
21+
return SQLITE_ERROR;
22+
}
23+
24+
// Register a copy of the default VFS named "nocurrenttime" whose current time
25+
// is unavailable. The copy shares the methods and pAppData of the default VFS,
26+
// which suits the unix and win32 VFSes, but not a default VFS that checks its
27+
// own name or extends sqlite3_vfs.
28+
static int _sqlite3_register_no_current_time_vfs(void) {
29+
sqlite3_vfs *base = sqlite3_vfs_find(0);
30+
if (base == 0) {
31+
return SQLITE_ERROR;
32+
}
33+
noCurrentTimeVfs = *base;
34+
noCurrentTimeVfs.zName = "nocurrenttime";
35+
noCurrentTimeVfs.pNext = 0;
36+
noCurrentTimeVfs.xCurrentTime = noCurrentTime;
37+
if (noCurrentTimeVfs.iVersion >= 2) {
38+
noCurrentTimeVfs.xCurrentTimeInt64 = noCurrentTimeInt64;
39+
}
40+
return sqlite3_vfs_register(&noCurrentTimeVfs, 0);
41+
}
42+
*/
43+
import "C"
44+
45+
import (
46+
"fmt"
47+
"sync"
48+
)
49+
50+
// noCurrentTimeVFS is the VFS that the DSN parameter vfs=nocurrenttime selects.
51+
// Date and time functions get no current time from it, so 'now', date and time
52+
// functions without a time value and the CURRENT_* keywords give NULL, and
53+
// their results do not depend on when a query runs. Without a current time,
54+
// SQLite also skips the profile callbacks of statements.
55+
const noCurrentTimeVFS = "nocurrenttime"
56+
57+
var (
58+
noCurrentTimeOnce sync.Once
59+
noCurrentTimeErr error
60+
)
61+
62+
func registerNoCurrentTimeVFS() error {
63+
noCurrentTimeOnce.Do(func() {
64+
if rv := C._sqlite3_register_no_current_time_vfs(); rv != C.SQLITE_OK {
65+
noCurrentTimeErr = fmt.Errorf("sqlite3: registering the %s VFS failed: %d", noCurrentTimeVFS, rv)
66+
}
67+
})
68+
return noCurrentTimeErr
69+
}

‎sqlite3_nocurrenttime_test.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//go:build cgo
2+
3+
package sqlite3
4+
5+
import (
6+
"database/sql"
7+
"path/filepath"
8+
"testing"
9+
)
10+
11+
func TestNoCurrentTimeVFS(t *testing.T) {
12+
path := filepath.Join(t.TempDir(), "nocurrenttime.db")
13+
db, err := sql.Open("sqlite3", "file:"+path+"?vfs=nocurrenttime&_journal_mode=WAL")
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
defer db.Close()
18+
19+
// Files are read and written as with the default VFS.
20+
if _, err := db.Exec("CREATE TABLE t (v TEXT); INSERT INTO t VALUES (date('2000-01-02'))"); err != nil {
21+
t.Fatal(err)
22+
}
23+
var v string
24+
if err := db.QueryRow("SELECT v FROM t").Scan(&v); err != nil || v != "2000-01-02" {
25+
t.Fatalf("read back %q, err %v", v, err)
26+
}
27+
28+
for _, q := range []struct {
29+
query string
30+
args []any
31+
}{
32+
{"SELECT date('now')", nil},
33+
{"SELECT date(?)", []any{"now"}},
34+
{"SELECT date('n' || 'ow')", nil},
35+
{"SELECT datetime()", nil},
36+
{"SELECT julianday('now')", nil},
37+
{"SELECT unixepoch()", nil},
38+
{"SELECT strftime('%Y')", nil},
39+
{"SELECT CURRENT_TIMESTAMP", nil},
40+
} {
41+
var got sql.NullString
42+
if err := db.QueryRow(q.query, q.args...).Scan(&got); err != nil {
43+
t.Errorf("%s: %v", q.query, err)
44+
} else if got.Valid {
45+
t.Errorf("%s: got the current time %q, want NULL", q.query, got.String)
46+
}
47+
}
48+
49+
plain, err := sql.Open("sqlite3", ":memory:")
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
defer plain.Close()
54+
var now sql.NullString
55+
if err := plain.QueryRow("SELECT date('now')").Scan(&now); err != nil || !now.Valid {
56+
t.Fatalf("the default VFS has no current time: %q, err %v", now.String, err)
57+
}
58+
}
59+
60+
func TestMoreThanOneVFS(t *testing.T) {
61+
// SQLite would open this with the last one, the default VFS.
62+
db, err := sql.Open("sqlite3", "file:"+filepath.Join(t.TempDir(), "two.db")+"?vfs=nocurrenttime&vfs=unix")
63+
if err != nil {
64+
t.Fatal(err)
65+
}
66+
defer db.Close()
67+
if err := db.Ping(); err == nil {
68+
t.Fatal("opened a database with two vfs parameters")
69+
}
70+
}

‎sqlite3_opt_omit_localtime.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//go:build sqlite_omit_localtime
2+
3+
package sqlite3
4+
5+
// Without the localtime and utc modifiers, date and time functions do not
6+
// depend on the time zone of the host.
7+
8+
/*
9+
#cgo CFLAGS: -DSQLITE_OMIT_LOCALTIME
10+
*/
11+
import "C"
12+
13+
const omitLocaltime = true
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//go:build cgo && sqlite_omit_localtime && libsqlite3
2+
3+
package sqlite3
4+
5+
import (
6+
"database/sql"
7+
"testing"
8+
)
9+
10+
func TestOmitLocaltimeWithoutTheOption(t *testing.T) {
11+
if compileOptionUsed("OMIT_LOCALTIME") {
12+
t.Skip("the system SQLite is built with SQLITE_OMIT_LOCALTIME")
13+
}
14+
db, err := sql.Open("sqlite3", ":memory:")
15+
if err != nil {
16+
t.Fatal(err)
17+
}
18+
defer db.Close()
19+
if err := db.Ping(); err == nil {
20+
t.Fatal("opened a database with the sqlite_omit_localtime tag and a SQLite that has localtime")
21+
}
22+
}

‎sqlite3_opt_omit_localtime_omit.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//go:build !sqlite_omit_localtime
2+
3+
package sqlite3
4+
5+
const omitLocaltime = false

‎sqlite3_opt_omit_localtime_test.go‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build cgo && sqlite_omit_localtime && !libsqlite3
2+
3+
package sqlite3
4+
5+
import (
6+
"database/sql"
7+
"testing"
8+
)
9+
10+
func TestOmitLocaltime(t *testing.T) {
11+
db, err := sql.Open("sqlite3", ":memory:")
12+
if err != nil {
13+
t.Fatal(err)
14+
}
15+
defer db.Close()
16+
for _, modifier := range []string{"localtime", "utc"} {
17+
var got sql.NullString
18+
if err := db.QueryRow("SELECT datetime('2000-01-01 12:00:00', ?)", modifier).Scan(&got); err != nil {
19+
t.Errorf("%s: %v", modifier, err)
20+
} else if got.Valid {
21+
t.Errorf("%s: got %q, want NULL", modifier, got.String)
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)