1
0

read table row count

This commit is contained in:
Ishan Jain 2022-07-20 15:11:10 +05:30
parent 1bb35948a7
commit 668ae1bd9d
2 changed files with 43 additions and 4 deletions

View File

@ -12,7 +12,7 @@ fn main() -> Result<()> {
match args.len() {
0 | 1 => bail!("Missing <database path> and <command>"),
2 => bail!("Missing <command>"),
_ => {}
_ => (),
}
// Read database file into database
@ -85,8 +85,47 @@ fn main() -> Result<()> {
print!("{} ", schema.name);
}
}
_ => bail!("Missing or invalid command passed: {}", command),
}
v => {
// Assume it's valid SQL
let table_name = v.split_whitespace().last().unwrap();
let db_page_size = u16::from_be_bytes([database[16], database[17]]);
// 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::<Result<Vec<_>>>()?;
let schema = schemas
.into_iter()
.find(|schema| schema.table_name == table_name)
.unwrap();
let table_page_offset = db_page_size as usize * (schema.root_page as usize - 1);
let page_header =
PageHeader::parse(&database[table_page_offset..table_page_offset + 8]).unwrap();
println!("{}", page_header.number_of_cells);
}
}
Ok(())
}

View File

@ -3,7 +3,7 @@ pub struct Schema {
pub kind: String,
pub name: String,
pub table_name: String,
root_page: u8,
pub root_page: u8,
sql: String,
}