Skip to content

Commit

Permalink
Fix sqlite_schema and remove explicit vtables
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Feb 17, 2025
1 parent 6dbe7e2 commit d1c0e10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions cli/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,11 +765,11 @@ impl<'a> Limbo<'a> {
fn display_schema(&mut self, table: Option<&str>) -> anyhow::Result<()> {
let sql = match table {
Some(table_name) => format!(
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index', 'virtual') AND tbl_name = '{}' AND name NOT LIKE 'sqlite_%'",
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index') AND tbl_name = '{}' AND name NOT LIKE 'sqlite_%'",
table_name
),
None => String::from(
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index', 'virtual') AND name NOT LIKE 'sqlite_%'"
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index') AND name NOT LIKE 'sqlite_%'"
),
};

Expand Down
6 changes: 2 additions & 4 deletions core/translate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,13 @@ addr opcode p1 p2 p3 p4 p5 comment
enum SchemaEntryType {
Table,
Index,
Virtual,
}

impl SchemaEntryType {
fn as_str(&self) -> &'static str {
match self {
SchemaEntryType::Table => "table",
SchemaEntryType::Index => "index",
SchemaEntryType::Virtual => "virtual",
}
}
}
Expand All @@ -211,7 +209,7 @@ fn emit_schema_entry(
program.emit_string8_new_reg(tbl_name.to_string());

let rootpage_reg = program.alloc_register();
if matches!(entry_type, SchemaEntryType::Virtual) {
if root_page_reg == 0 {
program.emit_insn(Insn::Integer {
dest: rootpage_reg,
value: 0, // virtual tables in sqlite always have rootpage=0
Expand Down Expand Up @@ -664,7 +662,7 @@ fn translate_create_virtual_table(
emit_schema_entry(
&mut program,
sqlite_schema_cursor_id,
SchemaEntryType::Virtual,
SchemaEntryType::Table,
&tbl_name.name.0,
&tbl_name.name.0,
0, // virtual tables dont have a root page
Expand Down
17 changes: 9 additions & 8 deletions core/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,21 @@ pub fn parse_schema_rows(
StepResult::Row => {
let row = rows.row().unwrap();
let ty = row.get::<&str>(0)?;
if !["table", "index", "virtual"].contains(&ty) {
if !["table", "index"].contains(&ty) {
continue;
}
match ty {
"table" => {
let root_page: i64 = row.get::<i64>(3)?;
let sql: &str = row.get::<&str>(4)?;
let table = schema::BTreeTable::from_sql(sql, root_page as usize)?;
schema.add_btree_table(Rc::new(table));
}
"virtual" => {
let name: &str = row.get::<&str>(1)?;
let vtab = syms.vtabs.get(name).unwrap().clone();
schema.add_virtual_table(vtab);
if root_page == 0 && sql.to_lowercase().contains("virtual") {
let name: &str = row.get::<&str>(1)?;
let vtab = syms.vtabs.get(name).unwrap().clone();
schema.add_virtual_table(vtab);
} else {
let table = schema::BTreeTable::from_sql(sql, root_page as usize)?;
schema.add_btree_table(Rc::new(table));
}
}
"index" => {
let root_page: i64 = row.get::<i64>(3)?;
Expand Down

0 comments on commit d1c0e10

Please sign in to comment.