Crates.io | sqlite-parser-nom |
lib.rs | sqlite-parser-nom |
version | 1.0.0 |
source | src |
created_at | 2023-02-25 22:07:13.385844 |
updated_at | 2023-03-03 19:54:24.552225 |
description | SQLite database file parser |
homepage | |
repository | https://github.com/mycelial/sqlite-parser-nom |
max_upload_size | |
id | 794653 |
size | 5,872,742 |
SQLite binary database format parser.
Homonym libraries:
In your Cargo.toml:
[dependencies]
sqlite-parser-nom = "1.0.0"
Load and parse file in memory:
use sqlite_parser_nom::Reader;
use sqlite_parser_nom::error;
fn main() -> Result<(), error::SQLiteError> {
let reader = Reader::open_mmap("sample/sakila.db")?;
println!("{}", reader.header.db_size);
Ok(())
}
You can also use parsers directly
use nom::Finish;
use sqlite_parser_nom::parser;
use sqlite_parser_nom::model;
use sqlite_parser_nom::error;
fn do_something_with_page(i: &[u8]) -> Result<model::Page, error::SQLiteError> {
let (_, page) = parser::page(i)
.finish()
// the cast is necessary here, so the error could outlive the input
.map_err(|e| nom::error::Error {
code: e.code,
input: error::OwnedBytes(e.input.to_owned()),
})?;
Ok(page)
}
Check the documentation and parser to chose correct parser for your task.
References:
+---+-------+-----------+-----------+-----------+
| h | | | | |
| e | | | | |
| a | root | page 2 | ... | page N |
| d | page | | | |
| e | | | | |
| r | | | | |
+---+-------+-----------+-----------+-----------+
^ ^ ^
< page size | page size | page size | page size >
+---------------------+
| page header |
+---------------------+
| cell pointer array |
+---------------------+
| |
| unallocated space |
| |
+-------+-------------+
|cell N |free block |
+-------+------+------+
|cell 5 |cell 4|cell 3|
+-------+----+-+------+
| cell 2 | cell 1 |
+------------+--------+
Page types:
0x00
in header and the rest is payloadPage structure:
+-----------+--------+--------+--------+-----+--------+-----------+-----+-----------+
|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 >