- enabled by default on 64-bit macOS and Linux (`amd64`/`arm64`) - depends on merged but unreleased wazero - may cause small performance regression - users may need WithMemoryLimitPages if not enough address space available - needs docs
Go bindings to SQLite using Wazero
Go module github.com/ncruces/go-sqlite3 is cgo-free SQLite wrapper.
It provides a database/sql compatible driver,
as well as direct access to most of the C SQLite API.
It wraps a Wasm build of SQLite, and uses wazero as the runtime.
Go, wazero and x/sys are the only runtime dependencies.
Packages
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/gormliteprovides a GORM driver.
Extensions
github.com/ncruces/go-sqlite3/ext/arrayprovides thearraytable-valued function.github.com/ncruces/go-sqlite3/ext/blobiosimplifies incremental BLOB I/O.github.com/ncruces/go-sqlite3/ext/csvreads comma-separated values.github.com/ncruces/go-sqlite3/ext/fileioreads, writes and lists files.github.com/ncruces/go-sqlite3/ext/hashprovides cryptographic hash functions.github.com/ncruces/go-sqlite3/ext/linesreads data line-by-line.github.com/ncruces/go-sqlite3/ext/pivotcreates pivot tables.github.com/ncruces/go-sqlite3/ext/statementcreates parameterized views.github.com/ncruces/go-sqlite3/ext/statsprovides statistics functions.github.com/ncruces/go-sqlite3/ext/unicodeprovides Unicode aware functions.github.com/ncruces/go-sqlite3/ext/zordermaps multidimensional data to one dimension.github.com/ncruces/go-sqlite3/vfs/memdbimplements an in-memory VFS.github.com/ncruces/go-sqlite3/vfs/readervfsimplements a VFS for immutable databases.
Advanced features
- incremental BLOB I/O
- nested transactions
- custom functions
- virtual tables
- custom VFSes
- online backup
- JSON support
- math functions
- full-text search
- geospatial search
- and more…
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).
File Locking
POSIX advisory locks, which SQLite uses on Unix, 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 POSIX advisory locks.
On BSD Unixes, this module uses BSD locks. On BSD Unixes, BSD locks are fully compatible with POSIX advisory locks.
On Windows, this module uses LockFileEx and UnlockFileEx,
like SQLite.
On all other platforms, file locking is not supported, and you must use
nolock=1
(or immutable=1)
to open database files.
You can use vfs.SupportsFileLocking
to check if your platform supports file locking.
To use the database/sql driver
with nolock=1 you must disable connection pooling by calling
db.SetMaxOpenConns(1).
Testing
This project aims for high test coverage. It also benefits greatly from SQLite's and wazero's thorough testing.
The pure Go VFS is tested by running SQLite's mptest on Linux, macOS, Windows and FreeBSD.
Performance
Perfomance of the database/sql driver is
competitive with alternatives.
The Wasm and VFS layers are also tested by running SQLite's speedtest1.