mirror of
https://github.com/tursodatabase/limbo.git
synced 2025-08-04 10:08:20 +00:00
Update schema dot command to show virtual tables
This commit is contained in:
parent
0547d397b1
commit
38e54ca85e
7 changed files with 35 additions and 14 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1687,6 +1687,7 @@ dependencies = [
|
|||
name = "limbo_kv"
|
||||
version = "0.0.14"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"limbo_ext",
|
||||
"mimalloc",
|
||||
]
|
||||
|
|
|
@ -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') AND tbl_name = '{}' AND name NOT LIKE 'sqlite_%'",
|
||||
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index', 'virtual') AND tbl_name = '{}' AND name NOT LIKE 'sqlite_%'",
|
||||
table_name
|
||||
),
|
||||
None => String::from(
|
||||
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index') AND name NOT LIKE 'sqlite_%'"
|
||||
"SELECT sql FROM sqlite_schema WHERE type IN ('table', 'index', 'virtual') AND name NOT LIKE 'sqlite_%'"
|
||||
),
|
||||
};
|
||||
|
||||
|
|
|
@ -556,7 +556,6 @@ impl VirtualTable {
|
|||
}
|
||||
}
|
||||
let mut parser = Parser::new(schema.as_bytes());
|
||||
parser.reset(schema.as_bytes());
|
||||
if let ast::Cmd::Stmt(ast::Stmt::CreateTable { body, .. }) = parser.next()?.ok_or(
|
||||
LimboError::ParseError("Failed to parse schema from virtual table module".to_string()),
|
||||
)? {
|
||||
|
|
|
@ -122,7 +122,7 @@ pub fn translate_insert(
|
|||
|
||||
let inserting_multiple_rows = values.len() > 1;
|
||||
|
||||
// multiple rows - use coroutine for value population
|
||||
// Multiple rows - use coroutine for value population
|
||||
if inserting_multiple_rows {
|
||||
let yield_reg = program.alloc_register();
|
||||
let jump_on_definition_label = program.allocate_label();
|
||||
|
@ -475,10 +475,9 @@ fn translate_virtual_table_insert(
|
|||
translate_expr(program, None, expr, value_registers_start + i, resolver)?;
|
||||
}
|
||||
/* *
|
||||
* Inserts for virtual tables are done in a single step. The rowid is not provided by the user, but is generated by the
|
||||
* vtable implementation.
|
||||
* argv[0] = current_rowid (NULL for insert)
|
||||
* argv[1] = insert_rowid (NULL for insert)
|
||||
* Inserts for virtual tables are done in a single step.
|
||||
* argv[0] = (NULL for insert)
|
||||
* argv[1] = (NULL for insert)
|
||||
* argv[2..] = column values
|
||||
* */
|
||||
|
||||
|
|
|
@ -625,10 +625,14 @@ fn translate_create_virtual_table(
|
|||
|
||||
let args_reg = if !args_vec.is_empty() {
|
||||
let args_start = program.alloc_register();
|
||||
|
||||
// Emit string8 instructions for each arg
|
||||
for (i, arg) in args_vec.iter().enumerate() {
|
||||
program.emit_string8(arg.clone(), args_start + i);
|
||||
}
|
||||
let args_record_reg = program.alloc_register();
|
||||
|
||||
// VCreate expects an array of args as a record
|
||||
program.emit_insn(Insn::MakeRecord {
|
||||
start_reg: args_start,
|
||||
count: args_vec.len(),
|
||||
|
|
|
@ -988,13 +988,11 @@ impl Program {
|
|||
)));
|
||||
}
|
||||
}
|
||||
// argv[0] = current_rowid (for DELETE if applicable)
|
||||
// argv[1] = insert_rowid (for INSERT if applicable)
|
||||
// argv[2..] = column values
|
||||
let result = virtual_table.update(&argv);
|
||||
match result {
|
||||
Ok(Some(new_rowid)) => {
|
||||
if *conflict_action == 5 {
|
||||
// ResolveType::Replace
|
||||
if let Some(conn) = self.connection.upgrade() {
|
||||
conn.update_last_rowid(new_rowid as u64);
|
||||
}
|
||||
|
|
|
@ -169,7 +169,11 @@ impl VTabModule for CsvVTable {
|
|||
/// Declare the name for your virtual table
|
||||
const NAME: &'static str = "csv_data";
|
||||
|
||||
fn init_sql() -> &'static str {
|
||||
/// Declare the type of vtable (TableValuedFunction or VirtualTable)
|
||||
const VTAB_KIND: VTabKind = VTabKind::VirtualTable;
|
||||
|
||||
/// Function to initialize the schema of your vtable
|
||||
fn create_schema(_args: &[Value]) -> &'static str {
|
||||
"CREATE TABLE csv_data(
|
||||
name TEXT,
|
||||
age TEXT,
|
||||
|
@ -178,7 +182,7 @@ impl VTabModule for CsvVTable {
|
|||
}
|
||||
|
||||
/// Open to return a new cursor: In this simple example, the CSV file is read completely into memory on connect.
|
||||
fn open() -> Result<Self::VCursor, Self::Error> {
|
||||
fn open(&self) -> Result<Self::VCursor, Self::Error> {
|
||||
// Read CSV file contents from "data.csv"
|
||||
let csv_content = fs::read_to_string("data.csv").unwrap_or_default();
|
||||
// For simplicity, we'll ignore the header row.
|
||||
|
@ -195,7 +199,7 @@ impl VTabModule for CsvVTable {
|
|||
}
|
||||
|
||||
/// Filter through result columns. (not used in this simple example)
|
||||
fn filter(_cursor: &mut Self::VCursor, _arg_count: i32, _args: &[Value]) -> ResultCode {
|
||||
fn filter(_cursor: &mut Self::VCursor, _args: &[Value]) -> ResultCode {
|
||||
ResultCode::OK
|
||||
}
|
||||
|
||||
|
@ -218,6 +222,22 @@ impl VTabModule for CsvVTable {
|
|||
fn eof(cursor: &Self::VCursor) -> bool {
|
||||
cursor.index >= cursor.rows.len()
|
||||
}
|
||||
|
||||
/// *Optional* methods for non-readonly tables
|
||||
|
||||
/// Update the value at rowid
|
||||
fn update(&mut self, _rowid: i64, _args: &[Value]) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Insert the value(s)
|
||||
fn insert(&mut self, _args: &[Value]) -> Result<i64, Self::Error> {
|
||||
Ok(0)
|
||||
}
|
||||
/// Delete the value at rowid
|
||||
fn delete(&mut self, _rowid: i64) -> Result<(), Self::Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// The cursor for iterating over CSV rows.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue