From 1bb35948a7260f884580ad78c50693afc3c05b6b Mon Sep 17 00:00:00 2001 From: Ishan Jain Date: Tue, 5 Jul 2022 01:52:40 +0530 Subject: [PATCH] Added support for .tables --- src/main.rs | 32 ++++++++++++++++++++++++++++++++ src/schema.rs | 6 +++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index f2feafd..379502e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,6 +53,38 @@ fn main() -> Result<()> { // Uncomment this block to pass the first stage // println!("number of tables: {}", schemas.len()); } + + ".tables" => { + // Parse page header from database + let page_header = PageHeader::parse(&database[100..108])?; + + // Obtain all cell pointers + let cell_pointers = database[108..] + .chunks_exact(2) + .take(page_header.number_of_cells.into()) + .map(|bytes| u16::from_be_bytes(bytes.try_into().unwrap())); + + // Obtain all records from column 5 + #[allow(unused_variables)] + let schemas = cell_pointers + .into_iter() + .map(|cell_pointer| { + let stream = &database[cell_pointer as usize..]; + let (_, offset) = parse_varint(stream); + let (_rowid, read_bytes) = parse_varint(&stream[offset..]); + parse_record(&stream[offset + read_bytes..], 5) + .map(|record| Schema::parse(record).expect("Invalid record")) + }) + .collect::>>()?; + + for schema in schemas + .into_iter() + .filter(|schema| !schema.table_name.starts_with("sqlite")) + .filter(|schema| schema.kind == "table") + { + print!("{} ", schema.name); + } + } _ => bail!("Missing or invalid command passed: {}", command), } diff --git a/src/schema.rs b/src/schema.rs index 400ec80..120c0b0 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,8 +1,8 @@ #[derive(Debug)] pub struct Schema { - kind: String, - name: String, - table_name: String, + pub kind: String, + pub name: String, + pub table_name: String, root_page: u8, sql: String, }