server/sqlite_format library

Pure-Dart reader/writer for a useful subset of the SQLite 3 file format. Production code MUST NOT depend on this — it's a compatibility shim used by tests and ad-hoc tooling so dart-db-server can ingest and emit .sqlite files for the simple shapes it cares about.

Supported (reader and writer):

  • 100-byte database header (magic, page size, text encoding UTF-8).
  • Single-file databases (no WAL, no journal).
  • Schema parsing from sqlite_schema (table/index/view/trigger rows).
  • Table B-tree traversal: interior (page type 0x05) and leaf (0x0d).
  • Table B-tree writing: arbitrary depth (interior + leaf pages).
  • Index B-tree traversal (page types 0x02 interior, 0x0a leaf) for reading: each cell yields a record containing the index key columns followed by the integer rowid that the SQLite engine stores as the last column.
  • Index B-tree writing: arbitrary depth (interior + leaf pages).
  • Overflow pages on read and write (table-leaf, index-leaf, and index-interior separator cells).
  • Record decoding for serial types 0..9 and 12+/13+ (NULL, ints, real, text, blob).
  • UTF-8 text encoding only.

NOT supported (yet):

  • Schema bigger than one leaf page (extremely rare in practice).
  • Freelist pages, autovacuum, incremental vacuum, change counters.
  • UTF-16 encoding, application-id headers, schema cookie semantics beyond just round-tripping the bytes.
  • WITHOUT ROWID tables (which use a slightly different on-disk layout for table B-trees).

The supported subset is enough to round-trip every table that the rest of the dart-db-server engine produces today, and to ingest the table data of typical fixture .sqlite files (no overflow, no indexes that the reader cares about — indexes can simply be ignored when reading rows by table B-tree).

Classes

SqliteFile
Reader for a SQLite file held entirely in memory. Use SqliteFile.fromBytes to construct.
SqliteHeader
The 100-byte SQLite file header, decoded.
SqliteRow
One row read from a table B-tree: rowid + the decoded record values.
SqliteSchemaRow
One row from sqlite_schema.
SqliteWriteIndex
A logical index to write into a SQLite file.
SqliteWriteTable
A logical table to write into a SQLite file.

Functions

buildWal({required int pageSize, required Map<int, Uint8List> pageOverrides, required int dbSizeAfterCommit, int salt1 = 0x12345678, int salt2 = 0x9abcdef0, int checkpointSeq = 0}) Uint8List
Build a SQLite WAL file containing a single committed transaction that overrides the given pages.
writeSqliteFile(List<SqliteWriteTable> tables, {int pageSize = 4096, List<SqliteWriteIndex> indexes = const []}) Uint8List
Build a complete SQLite file from a set of tables and indexes.