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.
|
|
|
|
|
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3`](https://pkg.go.dev/github.com/ncruces/go-sqlite3)
|
2023-09-01 15:54:40 +01:00
|
|
|
wraps the [C SQLite API](https://www.sqlite.org/cintro.html)
|
|
|
|
|
([example usage](https://pkg.go.dev/github.com/ncruces/go-sqlite3#example-package)).
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/driver`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/driver)
|
2023-09-01 15:54:40 +01:00
|
|
|
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)).
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/embed`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/embed)
|
2023-09-01 15:54:40 +01:00
|
|
|
embeds a build of SQLite into your application.
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/vfs`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs)
|
2023-09-01 15:54:40 +01:00
|
|
|
wraps the [C SQLite VFS API](https://www.sqlite.org/vfs.html) and provides a pure Go implementation.
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/vfs/memdb`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/memdb)
|
2023-09-01 15:54:40 +01:00
|
|
|
implements an in-memory VFS.
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/vfs/readervfs`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/vfs/readervfs)
|
2023-09-01 15:54:40 +01:00
|
|
|
implements a VFS for immutable databases.
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/ext/unicode`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/unicode)
|
2023-09-01 15:54:40 +01:00
|
|
|
registers Unicode aware functions.
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/ext/stats`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/ext/stats)
|
2023-09-01 15:54:40 +01:00
|
|
|
registers [statistics functions](https://www.oreilly.com/library/view/sql-in-a/9780596155322/ch04s02.html).
|
2023-09-15 15:37:57 +01:00
|
|
|
- [`github.com/ncruces/go-sqlite3/gormlite`](https://pkg.go.dev/github.com/ncruces/go-sqlite3/gormlite)
|
2023-09-01 15:54:40 +01:00
|
|
|
provides a [GORM](https://gorm.io) driver.
|
2023-02-24 14:31:41 +00:00
|
|
|
|
|
|
|
|
### Caveats
|
|
|
|
|
|
2023-05-31 18:45:45 +01:00
|
|
|
This module replaces the SQLite [OS Interface](https://www.sqlite.org/vfs.html)
|
2023-06-01 18:11:37 +01:00
|
|
|
(aka VFS) with a [pure Go](vfs/) implementation.
|
2023-05-31 18:45:45 +01:00
|
|
|
This has 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).
|
|
|
|
|
|
2023-06-24 02:18:56 +01:00
|
|
|
To work around this limitation, SQLite is [patched](sqlite3/locking_mode.patch)
|
|
|
|
|
to always use `EXCLUSIVE` locking mode for WAL databases.
|
2023-02-24 14:31:41 +00:00
|
|
|
|
|
|
|
|
Because connection pooling is incompatible with `EXCLUSIVE` locking mode,
|
2023-09-01 15:54:40 +01:00
|
|
|
to use the [`database/sql`](https://pkg.go.dev/database/sql)
|
2023-09-15 15:37:57 +01:00
|
|
|
driver with WAL mode databases you should disable connection pooling by calling
|
2023-03-10 15:50:11 +00:00
|
|
|
[`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
|
2023-09-15 15:37:57 +01:00
|
|
|
[broken by design](https://www.sqlite.org/src/artifact/2e8b12?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-09-18 14:47:55 +01:00
|
|
|
On BSD Unixes, this module may use
|
2023-03-23 13:28:25 +00:00
|
|
|
[BSD locks](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2).
|
2023-09-18 14:47:55 +01:00
|
|
|
BSD locks may _not_ be compatible with process-associated POSIX advisory locks.
|
2023-03-01 12:16:36 +00:00
|
|
|
|
2023-09-01 15:54:40 +01:00
|
|
|
##### TL;DR
|
|
|
|
|
|
|
|
|
|
In all platforms for which this package builds,
|
2023-09-18 14:47:55 +01:00
|
|
|
it should be safe to use it to access databases concurrently,
|
|
|
|
|
from multiple goroutines, processes, and
|
2023-09-01 15:54:40 +01:00
|
|
|
with _other_ implementations of SQLite.
|
|
|
|
|
|
2023-09-18 14:47:55 +01:00
|
|
|
If the package does not build for your platform,
|
|
|
|
|
see [this](vfs/README.md#portability).
|
2023-09-01 15:54:40 +01:00
|
|
|
|
2023-03-10 16:25:07 +00:00
|
|
|
#### Testing
|
|
|
|
|
|
2023-08-09 15:22:24 +01:00
|
|
|
The pure Go VFS is tested by running SQLite's
|
2023-03-10 16:25:07 +00:00
|
|
|
[mptest](https://github.com/sqlite/sqlite/blob/master/mptest/mptest.c)
|
2023-09-18 14:47:55 +01:00
|
|
|
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-07-01 00:15:28 +01:00
|
|
|
- [x] custom functions
|
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-10-13 17:06:05 +01:00
|
|
|
- [ ] JSON support
|
2023-02-26 04:49:10 +00:00
|
|
|
- [ ] session extension
|
2023-02-24 14:31:41 +00:00
|
|
|
- [ ] custom VFSes
|
2023-05-26 11:04:48 +01:00
|
|
|
- [x] custom VFS API
|
|
|
|
|
- [x] in-memory VFS
|
2023-05-23 16:34:09 +01:00
|
|
|
- [x] 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)
|
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)
|
2023-09-01 15:54:40 +01:00
|
|
|
- [`github.com/zombiezen/go-sqlite`](https://pkg.go.dev/github.com/zombiezen/go-sqlite)
|