ncruces/go-sqlite3

repository·main·Indexed 21 days ago

https://github.com/ncruces/go-sqlite3

A cgo-free Go wrapper for SQLite that utilizes a Wasm build of SQLite translated via wasm2go. It provides a standard database/sql driver, a GORM driver via the gormlite package, and direct access to the C SQLite API. The library includes a pure Go implementation of the Virtual File System (VFS) with specialized options such as memdb, mvcc, readervfs, and adiantum for encryption at rest, as well as a variety of optional SQLite extensions for search, indexing, and data manipulation.

Tokens
23.6K
Snippets
87
Records
124
Agent score
77%

What's inside ncruces/go-sqlite3

  1. Overview of go-sqlite3 packages

    main

    The repository is organized into several packages depending on your needs:

    • github.com/ncruces/go-sqlite3: Provides direct access to the C SQLite API.
    • github.com/ncruces/go-sqlite3/driver: Provides a database/sql compatible driver.
    • github.com/ncruces/go-sqlite3/vfs: Wraps the C SQLite VFS API and provides a pure Go implementation of the Virtual File System.
    • github.com/ncruces/go-sqlite3/gormlite: Provides a driver specifically for GORM.
  2. Use SQLite utility functions for extension development

    main
    The sql3util package provides a collection of assorted SQLite utility functions designed specifically for extension writers. Additionally, it includes a wrapper for a SQLite parser that handles CREATE and ALTER TABLE commands, which can be used to programmatically inspect or manipulate table definitions.
  3. Understand the Go SQLite VFS implementation

    main

    This package provides a pure Go implementation of the SQLite OS Interface (VFS), replacing the default SQLite VFS. It exposes interfaces that allow developers to implement custom VFSes.

    Key Differences from standard SQLite:

    • File Locking: Uses OFD locks on Linux/macOS (requires Linux 3.15+) and LockFileEx on Windows. It can also use BSD locks or dot-file locks via build tags.
    • WAL Mode: Uses mmap on Unix and MapViewOfFile on Windows for shared-memory WAL-index. If these are not supported, WAL support is limited and requires EXCLUSIVE locking mode.
    • Compatibility: The default configuration is compatible with standard Unix and Windows SQLite VFSes. However, using incompatible VFSes concurrently on the same database will cause data corruption.
  4. Available Go SQLite Extensions

    main

    The ncruces/go-sqlite3 repository provides a collection of optional SQLite extensions that can be loaded into your database connections to extend SQLite's core functionality. These extensions cover various domains such as full-text search, geospatial indexing, cryptographic functions, and data manipulation.

    Extension Categories

    • Data Formats & I/O: csv (read CSV), fileio (file system access), lines (line-by-line reading), blobio (incremental BLOB I/O), and serdes (database serialization).
    • Search & Indexing: fts5 (full-text search), rtree (multi-dimensional/geospatial indexes), bloom (Bloom filter virtual table), vec1 (vector search), and spellfix1 (close match searching).
    • Mathematical & Specialized Data: array (array table-valued functions), ipaddr (IP/CIDR manipulation), uuid (UUID generation), unicode (Unicode-aware functions), decimal (decimal arithmetic), and julianday (Julian day math).
    • Logic & Transformation: regexp (regular expressions), hash (cryptographic hashes), pivot (pivot tables), closure (transitive closure), statement (parameterized views), stats (statistics), and zorder (multidimensional mapping).
  5. What is the Go `xts` SQLite VFS?

    main

    The xts VFS is a package that wraps a standard SQLite VFS to provide encryption at rest using the AES-XTS tweakable and length-preserving encryption mode. It is designed to encrypt file contents in 512-byte sectors, which matches the minimum SQLite page size.

    Key technical details:

    • Cryptographic Primitives: Uses NIST and FIPS 140-3 approved primitives (AES-128, AES-192, or AES-256). It uses PBKDF2-HMAC-SHA512 to derive AES-128 keys from plain text.
    • File Coverage: Encrypts all files except super journals (which only contain filenames).
    • Temporary Files: These are encrypted using random AES-128 keys to ensure security, as they may contain database data.
    • Security Model: XTS is a deterministic cipher mode. While it protects data content, an adversary with access to multiple snapshots (e.g., backups) can perform differential analysis to see which sectors changed, stayed the same, or were reverted.
  6. Concurrency and Goroutine safety

    main

    This module behaves similarly to SQLite in multi-thread mode:

    1. database/sql API: Safe to use concurrently according to standard Go documentation.
    2. Direct Connections: The module is goroutine-safe, provided that no single database connection (or any object derived from it) is used concurrently by multiple goroutines.
  7. Use the Go `reader` SQLite VFS to access `io.ReaderAt` as a database

    main

    The reader VFS allows you to treat any object implementing the io.ReaderAt interface as an immutable SQLite database. This is useful for accessing database files that are provided as streams or in-memory buffers without needing to write them to a physical file on disk first.

    // Example concept: any io.ReaderAt can be used as an immutable database
    // via the reader VFS.
    var myReader io.ReaderAt = ... 
    // Use myReader with the reader VFS to open the database.
  8. Understand the Adiantum encryption construction

    main

    The adiantum VFS uses a specific cipher composition for disk encryption:

    • Stream Cipher: XChaCha12
    • Block Cipher: AES
    • Hashing: NH and Poly1305
    • Key Derivation: Argon2id (derives 256-bit keys from plain text)
    • Block Size: 4 KiB

    Note: Adiantum is designed for disk encryption where the threat model assumes an adversary can read multiple snapshots of a disk. The primary security property is that an adversary can only determine if data in a sector has changed or not.

  9. Caveats and Performance considerations

    main

    When using this module, be aware of the following:

    • VFS Implementation: This module replaces the standard SQLite OS Interface (VFS) with a pure Go implementation. This has specific design trade-offs.
    • Memory Usage: Because each database connection executes within a Wasm sandboxed environment, memory usage will be higher than other SQLite drivers.
    • Performance: The database/sql driver performance is competitive with other alternatives, and the Wasm/VFS layers are benchmarked against SQLite's own speedtest1.
  10. Use the `mvcc` SQLite VFS for in-memory databases

    main

    The mvcc (Multi-Version Concurrency Control) VFS is an EXPERIMENTAL in-memory SQLite implementation. It is designed to provide several advantages over the standard memdb VFS:

    • Isolation from Panics: Panics in one connection do not corrupt the shared database.
    • Non-blocking Concurrency: A single writer does not block readers, and readers never block each other or the writer.
    • Instant Snapshots: Allows for immediate creation of database snapshots.

    Use this VFS when you need high concurrency or the ability to create multiple independent copies of a database state quickly.

  11. Security considerations and backup best practices for `xts` VFS

    main

    Users of the xts VFS should be aware of the following security constraints:

    • No Tamper Protection: This package does not protect against database tampering or forgery. It does not use page-level MACs (Message Authentication Codes).
    • Backup Forgery: Because the encryption is deterministic, an adversary can identify changes between snapshots. If you are keeping xts encrypted backups and require protection against forgery, you must sign your backups and verify those signatures before restoring them.
    • Alternative: For significantly better performance and potentially improved security via a "wide-block" cipher, consider using the adiantum VFS instead.
  12. Configure Write-Ahead Logging (WAL) and shared memory

    main

    The package supports WAL mode using shared memory.

    Using sqlite3_dotlk for WAL: If you use the sqlite3_dotlk build tag, you get a cross-platform, in-process memory sharing implementation. To avoid excessive concurrency with this implementation, set your connection pool limit using: db.SetMaxOpenConns(max(2, runtime.GOMAXPROCS(0)))

    Limited WAL support: If your build does not support shared memory (check via vfs.SupportsSharedMemory), you must use EXCLUSIVE locking mode to create, read, and write WAL databases. When using database/sql in this mode, you must disable connection pooling with db.SetMaxOpenConns(1).

    // Recommended connection limit for sqlite3_dotlk
    db.SetMaxOpenConns(max(2, runtime.GOMAXPROCS(0)))