From 6f1d03d64dfae8cf3d302ba4f353046d6ba9694c Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Sun, 27 Aug 2023 21:18:17 +0300 Subject: [PATCH] Look up columns from table schema --- core/schema.rs | 4 ++-- core/vdbe.rs | 27 +++++++++++++++++++++------ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/core/schema.rs b/core/schema.rs index 037c5e202..916dab934 100644 --- a/core/schema.rs +++ b/core/schema.rs @@ -45,8 +45,8 @@ impl Table { } pub struct Column { - name: String, - ty: Type, + pub name: String, + pub ty: Type, } pub enum Type { diff --git a/core/vdbe.rs b/core/vdbe.rs index 2d7fb1db5..7b01f2b38 100644 --- a/core/vdbe.rs +++ b/core/vdbe.rs @@ -11,7 +11,7 @@ pub enum Insn { OpenReadAwait, RewindAsync, RewindAwait(RewindAwaitInsn), - Column, + Column(ColumnInsn), ResultRow, NextAsync, NextAwait, @@ -31,6 +31,10 @@ pub struct RewindAwaitInsn { pub pc_if_empty: usize, } +pub struct ColumnInsn { + pub column: usize, +} + pub struct GotoInsn { pub target_pc: usize, } @@ -104,7 +108,7 @@ impl Program { // TODO: Check if empty self.pc = rewind_await.pc_if_empty; } - Insn::Column => { + Insn::Column(_) => { self.pc += 1; } Insn::ResultRow => { @@ -140,7 +144,9 @@ pub fn translate(schema: &Schema, stmt: Stmt) -> Result { fn translate_select(schema: &Schema, select: Select) -> Result { match select.body.select { OneSelect::Select { - from: Some(from), .. + columns, + from: Some(from), + .. } => { let table_name = match from.select { Some(select_table) => match *select_table { @@ -162,8 +168,17 @@ fn translate_select(schema: &Schema, select: Select) -> Result { program.emit_insn(Insn::OpenReadAwait); program.emit_insn(Insn::RewindAsync); let rewind_await_offset = program.emit_placeholder(); - program.emit_insn(Insn::Column); - program.emit_insn(Insn::Column); + for col in columns { + match col { + sqlite3_parser::ast::ResultColumn::Expr(_, _) => todo!(), + sqlite3_parser::ast::ResultColumn::Star => { + for i in 0..table.columns.len() { + program.emit_insn(Insn::Column(ColumnInsn { column: i })); + } + } + sqlite3_parser::ast::ResultColumn::TableStar(_) => todo!(), + } + } program.emit_insn(Insn::ResultRow); program.emit_insn(Insn::NextAsync); program.emit_insn(Insn::NextAwait); @@ -221,7 +236,7 @@ fn print_insn(addr: usize, insn: &Insn) { 0, "".to_string(), ), - Insn::Column => ("Column", 0, 0, 0, "", 0, "".to_string()), + Insn::Column(column) => ("Column", 0, column.column, 0, "", 0, "".to_string()), Insn::ResultRow => ("ResultRow", 0, 0, 0, "", 0, "".to_string()), Insn::NextAsync => ("NextAsync", 0, 0, 0, "", 0, "".to_string()), Insn::NextAwait => ("NextAwait", 0, 0, 0, "", 0, "".to_string()),