Sketch out runtime extension loading

This commit is contained in:
PThorpe92 2025-01-08 23:16:57 -05:00
parent bfbaa80bdc
commit 0a10d893d9
No known key found for this signature in database
GPG key ID: 66DB3FBACBDD05CC
15 changed files with 291 additions and 33 deletions

View file

@ -129,6 +129,8 @@ pub enum Command {
Tables,
/// Import data from FILE into TABLE
Import,
/// Loads an extension library
LoadExtension,
}
impl Command {
@ -141,7 +143,12 @@ impl Command {
| Self::ShowInfo
| Self::Tables
| Self::SetOutput => 0,
Self::Open | Self::OutputMode | Self::Cwd | Self::Echo | Self::NullValue => 1,
Self::Open
| Self::OutputMode
| Self::Cwd
| Self::Echo
| Self::NullValue
| Self::LoadExtension => 1,
Self::Import => 2,
} + 1) // argv0
}
@ -160,6 +167,7 @@ impl Command {
Self::NullValue => ".nullvalue <string>",
Self::Echo => ".echo on|off",
Self::Tables => ".tables",
Self::LoadExtension => ".load",
Self::Import => &IMPORT_HELP,
}
}
@ -182,6 +190,7 @@ impl FromStr for Command {
".nullvalue" => Ok(Self::NullValue),
".echo" => Ok(Self::Echo),
".import" => Ok(Self::Import),
".load" => Ok(Self::LoadExtension),
_ => Err("Unknown command".to_string()),
}
}
@ -314,6 +323,16 @@ impl Limbo {
};
}
fn handle_load_extension(&mut self) -> Result<(), String> {
let mut args = self.input_buff.split_whitespace();
let _ = args.next();
let lib = args
.next()
.ok_or("No library specified")
.map_err(|e| e.to_string())?;
self.conn.load_extension(lib).map_err(|e| e.to_string())
}
fn display_in_memory(&mut self) -> std::io::Result<()> {
if self.opts.db_file == ":memory:" {
self.writeln("Connected to a transient in-memory database.")?;
@ -537,6 +556,11 @@ impl Limbo {
let _ = self.writeln(e.to_string());
};
}
Command::LoadExtension => {
if let Err(e) = self.handle_load_extension() {
let _ = self.writeln(e.to_string());
}
}
}
} else {
let _ = self.write_fmt(format_args!(