2023-01-12 16:29:57 +00:00
|
|
|
# Go bindings to SQLite using Wazero
|
|
|
|
|
|
2023-01-26 01:11:09 +00:00
|
|
|
[](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
|
|
|
|
|
[](https://goreportcard.com/report/github.com/ncruces/go-sqlite3)
|
2023-03-01 13:27:50 +00:00
|
|
|
[](https://github.com/ncruces/go-sqlite3/wiki/Test-coverage-report)
|
2023-01-12 16:29:57 +00:00
|
|
|
|
2023-02-24 14:31:41 +00:00
|
|
|
Go module `github.com/ncruces/go-sqlite3` wraps a [WASM](https://webassembly.org/) build of [SQLite](https://sqlite.org/),
|
|
|
|
|
and uses [wazero](https://wazero.io/) to provide `cgo`-free SQLite bindings.
|
|
|
|
|
|
|
|
|
|
- Package [`github.com/ncruces/go-sqlite3`](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
|
|
|
|
|
wraps the [C SQLite API](https://www.sqlite.org/cintro.html)
|
|
|
|
|
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-package)).
|
|
|
|
|
- Package [`github.com/ncruces/go-sqlite3/driver`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver)
|
|
|
|
|
provides a [`database/sql`](https://pkg.go.dev/database/sql) driver
|
|
|
|
|
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver#example-package)).
|
|
|
|
|
- Package [`github.com/ncruces/go-sqlite3/embed`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/embed)
|
|
|
|
|
embeds a build of SQLite into your application.
|
|
|
|
|
|
|
|
|
|
### Caveats
|
|
|
|
|
|
2023-03-10 16:25:07 +00:00
|
|
|
This module replaces the SQLite [OS Interface](https://www.sqlite.org/vfs.html) (aka VFS)
|
|
|
|
|
with a pure Go implementation.
|
2023-03-18 03:03:11 +00:00
|
|
|
This has numerous benefits, but also comes with some drawbacks.
|
2023-03-10 16:25:07 +00:00
|
|
|
|
2023-03-01 12:16:36 +00:00
|
|
|
#### Write-Ahead Logging
|
|
|
|
|
|
2023-02-24 14:31:41 +00:00
|
|
|
Because WASM does not support shared memory,
|
|
|
|
|
[WAL](https://www.sqlite.org/wal.html) support is [limited](https://www.sqlite.org/wal.html#noshm).
|
|
|
|
|
|
|
|
|
|
To work around this limitation, SQLite is compiled with
|
|
|
|
|
[`SQLITE_DEFAULT_LOCKING_MODE=1`](https://www.sqlite.org/compile.html#default_locking_mode),
|
|
|
|
|
making `EXCLUSIVE` the default locking mode.
|
|
|
|
|
For non-WAL databases, `NORMAL` locking mode can be activated with
|
|
|
|
|
[`PRAGMA locking_mode=NORMAL`](https://www.sqlite.org/pragma.html#pragma_locking_mode).
|
|
|
|
|
|
|
|
|
|
Because connection pooling is incompatible with `EXCLUSIVE` locking mode,
|
2023-03-10 15:50:11 +00:00
|
|
|
the `database/sql` driver defaults to `NORMAL` locking mode.
|
|
|
|
|
To open WAL databases, or use `EXCLUSIVE` locking mode,
|
|
|
|
|
disable connection pooling by calling
|
|
|
|
|
[`db.SetMaxOpenConns(1)`](https://pkg.go.dev/database/sql#DB.SetMaxOpenConns).
|
2023-02-24 14:31:41 +00:00
|
|
|
|
2023-03-23 13:28:25 +00:00
|
|
|
#### POSIX Advisory Locks
|
2023-03-01 12:16:36 +00:00
|
|
|
|
2023-03-23 13:28:25 +00:00
|
|
|
POSIX advisory locks, which SQLite uses, are
|
|
|
|
|
[broken by design](https://www.sqlite.org/src/artifact/90c4fa?ln=1073-1161).
|
2023-03-22 03:15:54 +00:00
|
|
|
|
2023-03-23 13:28:25 +00:00
|
|
|
On Linux, macOS and illumos, this module uses
|
|
|
|
|
[OFD locks](https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html)
|
2023-03-01 12:16:36 +00:00
|
|
|
to synchronize access to database files.
|
2023-03-22 03:15:54 +00:00
|
|
|
OFD locks are fully compatible with process-associated POSIX advisory locks.
|
2023-03-01 12:16:36 +00:00
|
|
|
|
2023-03-23 13:28:25 +00:00
|
|
|
On BSD Unixes, this module uses
|
|
|
|
|
[BSD locks](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2).
|
2023-03-22 03:15:54 +00:00
|
|
|
BSD locks may _not_ be compatible with process-associated POSIX advisory locks.
|
2023-03-01 12:16:36 +00:00
|
|
|
|
2023-03-10 16:25:07 +00:00
|
|
|
#### Testing
|
|
|
|
|
|
|
|
|
|
The pure Go VFS is stress tested by running an unmodified build of SQLite's
|
|
|
|
|
[mptest](https://github.com/sqlite/sqlite/blob/master/mptest/mptest.c)
|
|
|
|
|
on Linux, macOS and Windows.
|
2023-03-18 03:03:11 +00:00
|
|
|
Performance is tested by running
|
|
|
|
|
[speedtest1](https://github.com/sqlite/sqlite/blob/master/test/speedtest1.c).
|
|
|
|
|
|
2023-02-24 14:31:41 +00:00
|
|
|
### Roadmap
|
2023-01-12 16:29:57 +00:00
|
|
|
|
2023-02-22 17:51:30 +00:00
|
|
|
- [ ] advanced SQLite features
|
2023-02-24 14:31:41 +00:00
|
|
|
- [x] nested transactions
|
2023-02-27 04:08:38 +00:00
|
|
|
- [x] incremental BLOB I/O
|
2023-03-07 12:12:48 +00:00
|
|
|
- [x] online backup
|
2023-02-26 04:49:10 +00:00
|
|
|
- [ ] session extension
|
2023-02-28 14:50:15 +00:00
|
|
|
- [ ] custom SQL functions
|
2023-02-24 14:31:41 +00:00
|
|
|
- [ ] custom VFSes
|
2023-03-24 21:17:30 +00:00
|
|
|
- [ ] in-memory VFS
|
2023-02-24 14:31:41 +00:00
|
|
|
- [ ] read-only VFS, wrapping an [`io.ReaderAt`](https://pkg.go.dev/io#ReaderAt)
|
2023-02-28 14:50:15 +00:00
|
|
|
- [ ] cloud-based VFS, based on [Cloud Backed SQLite](https://sqlite.org/cloudsqlite/doc/trunk/www/index.wiki)
|
|
|
|
|
- [ ] custom VFS API
|
2023-03-24 21:17:30 +00:00
|
|
|
|
2023-02-24 14:31:41 +00:00
|
|
|
### Alternatives
|
|
|
|
|
|
|
|
|
|
- [`modernc.org/sqlite`](https://pkg.go.dev/modernc.org/sqlite)
|
|
|
|
|
- [`crawshaw.io/sqlite`](https://pkg.go.dev/crawshaw.io/sqlite)
|
2023-02-26 03:22:08 +00:00
|
|
|
- [`github.com/mattn/go-sqlite3`](https://pkg.go.dev/github.com/mattn/go-sqlite3)
|
|
|
|
|
- [`github.com/zombiezen/go-sqlite`](https://pkg.go.dev/github.com/zombiezen/go-sqlite)
|