Go bindings to SQLite using Wazero
Go module github.com/ncruces/go-sqlite3 wraps a WASM build of SQLite,
and uses wazero to provide cgo-free SQLite bindings.
github.com/ncruces/go-sqlite3wraps the C SQLite API (example usage).github.com/ncruces/go-sqlite3/driverprovides adatabase/sqldriver (example usage).github.com/ncruces/go-sqlite3/embedembeds a build of SQLite into your application.github.com/ncruces/go-sqlite3/vfswraps the C SQLite VFS API and provides a pure Go implementation.github.com/ncruces/go-sqlite3/vfs/memdbimplements an in-memory VFS.github.com/ncruces/go-sqlite3/vfs/readervfsimplements a VFS for immutable databases.github.com/ncruces/go-sqlite3/ext/unicoderegisters Unicode aware functions.github.com/ncruces/go-sqlite3/ext/statsregisters statistics functions.github.com/ncruces/go-sqlite3/gormliteprovides a GORM driver.
Caveats
This module replaces the SQLite OS Interface (aka VFS) with a pure Go implementation. This has benefits, but also comes with some drawbacks.
Write-Ahead Logging
Because WASM does not support shared memory, WAL support is limited.
To work around this limitation, SQLite is patched
to always use EXCLUSIVE locking mode for WAL databases.
Because connection pooling is incompatible with EXCLUSIVE locking mode,
to use the database/sql
driver with WAL mode databases you should disable connection pooling by calling
db.SetMaxOpenConns(1).
POSIX Advisory Locks
POSIX advisory locks, which SQLite uses, are broken by design.
On Linux, macOS and illumos, this module uses OFD locks to synchronize access to database files. OFD locks are fully compatible with process-associated POSIX advisory locks.
On BSD Unixes, this module uses BSD locks. On BSD Unixes, BSD locks should be compatible with process-associated POSIX advisory locks.
TL;DR
In all platforms for which this package builds out of the box, it should be safe to use it to access databases concurrently, from multiple goroutines, processes, and with other implementations of SQLite.
If the package does not build for your platform, see this.
Testing
The pure Go VFS is tested by running SQLite's mptest on Linux, macOS and Windows. Performance is tested by running speedtest1.
Roadmap
- advanced SQLite features
- custom functions
- nested transactions
- incremental BLOB I/O
- online backup
- JSON support
- session extension
- custom VFSes
- custom VFS API
- in-memory VFS
- read-only VFS, wrapping an
io.ReaderAt - cloud-based VFS, based on Cloud Backed SQLite