sqlite-parser-nom

SQLite binary database format parser.

Homonym libraries:

Usage

In your Cargo.toml:

toml [dependencies] sqlite-parser-nom = "1.0.0"

Lazily parse the file

Load and parse file in memory:

```rust use sqliteparsernom::Reader; use sqliteparsernom::error;

fn main() -> Result<(), error::SQLiteError> { let reader = Reader::openmmap("sample/sakila.db")?; println!("{}", reader.header.dbsize);

Ok(())

} ```

Parse a slice

You can also use parsers directly

```rust use nom::Finish; use sqliteparsernom::parser; use sqliteparsernom::model; use sqliteparsernom::error;

fn dosomethingwithpage(i: &[u8]) -> Result { let (, page) = parser::page(i) .finish() // the cast is necessary here, so the error could outlive the input .maperr(|e| nom::error::Error { code: e.code, input: error::OwnedBytes(e.input.toowned()), })?;

Ok(page)

} ```

Check the documentation and parser to chose correct parser for your task.

SQLite format specification

References:

Physical structure

Database file

text +---+-------+-----------+-----------+-----------+ | h | | | | | | e | | | | | | a | root | page 2 | ... | page N | | d | page | | | | | e | | | | | | r | | | | | +---+-------+-----------+-----------+-----------+ ^ ^ ^ < page size | page size | page size | page size >

Page

text +---------------------+ | page header | +---------------------+ | cell pointer array | +---------------------+ | | | unallocated space | | | +-------+-------------+ |cell N |free block | +-------+------+------+ |cell 5 |cell 4|cell 3| +-------+----+-+------+ | cell 2 | cell 1 | +------------+--------+

Page types:

Page structure: - Cell pointer array grows from the top of the page to the bottom - Pointers are byte offsets within the page - Cells grow from the bottom of the page to the top

Cell

text +-----------+--------+--------+--------+-----+--------+-----------+-----+-----------+ |Payload | | Header | Serial | | Serial | Data Cell | | Data Cell | |(Cell Size)| ... | Size | Type 1 | ... | Type N | Column 1 | ... | Column N | +-----------+--------+--------+--------+-----+--------+-----------+-----+-----------+ | | < cell header ^ record header ^ table row data > < cell size >