feat: listing all modules

This commit is contained in:
Lucas Forato
2025-08-04 08:30:06 -03:00
parent ff157e10e5
commit 5aa99f6743
4 changed files with 39 additions and 0 deletions

View File

@@ -62,6 +62,7 @@ pub use io::{
};
use parking_lot::RwLock;
use schema::Schema;
use std::ffi::CStr;
use std::{
borrow::Cow,
cell::{Cell, RefCell, UnsafeCell},
@@ -1838,6 +1839,19 @@ impl Connection {
pub fn get_pager(&self) -> Rc<Pager> {
self.pager.borrow().clone()
pub fn get_used_vtab_mods(&self) -> std::collections::HashSet<String> {
let mut mods = std::collections::HashSet::new();
let schema = self._db.schema.lock().unwrap();
for table in schema.tables.values() {
if let Table::Virtual(vtab) = table.as_ref() {
if let Some(module_name) = vtab.module_name() {
mods.insert(module_name.to_string());
}
}
}
mods
}
pub fn get_query_only(&self) -> bool {

View File

@@ -58,6 +58,11 @@ pub fn pragma_for(pragma: &PragmaName) -> Pragma {
LegacyFileFormat => {
unreachable!("pragma_for() called with LegacyFileFormat, which is unsupported")
}
ModuleList => Pragma::new(
// TODO:: change the flags below
PragmaFlags::NeedSchema | PragmaFlags::Result0 | PragmaFlags::SchemaReq,
&["module_list"],
),
PageCount => Pragma::new(
PragmaFlags::NeedSchema | PragmaFlags::Result0 | PragmaFlags::SchemaReq,
&["page_count"],

View File

@@ -146,6 +146,7 @@ fn update_pragma(
connection,
program,
),
PragmaName::ModuleList => Ok((program, TransactionMode::None)),
PragmaName::PageCount => query_pragma(
PragmaName::PageCount,
schema,
@@ -399,6 +400,17 @@ fn query_pragma(
program.emit_result_row(register, 3);
Ok((program, TransactionMode::None))
}
PragmaName::ModuleList => {
let modules = connection.get_used_vtab_mods();
for module in modules {
program.emit_string8(module.to_string(), register);
program.emit_result_row(register, 1);
}
program.add_pragma_result_column(pragma.to_string());
Ok((program, TransactionMode::None))
}
PragmaName::PageCount => {
program.emit_insn(Insn::PageCount {
db: 0,

View File

@@ -22,10 +22,15 @@ pub struct VirtualTable {
pub(crate) name: String,
pub(crate) columns: Vec<Column>,
pub(crate) kind: VTabKind,
pub(crate) module_name: Option<String>,
vtab_type: VirtualTableType,
}
impl VirtualTable {
pub fn module_name(&self) -> Option<&str> {
self.module_name.as_deref()
}
pub(crate) fn readonly(self: &Arc<VirtualTable>) -> bool {
match &self.vtab_type {
VirtualTableType::Pragma(_) => true,
@@ -43,6 +48,7 @@ impl VirtualTable {
columns: Self::resolve_columns(schema)
.expect("built-in function schema resolution should not fail"),
kind: VTabKind::TableValuedFunction,
module_name: Some("pragma".to_string()),
vtab_type: VirtualTableType::Pragma(tab),
};
Arc::new(vtab)
@@ -65,6 +71,7 @@ impl VirtualTable {
name: name.to_owned(),
columns: Self::resolve_columns(schema)?,
kind: VTabKind::TableValuedFunction,
module_name: Some(name.to_string()),
vtab_type,
};
Ok(Arc::new(vtab))
@@ -83,6 +90,7 @@ impl VirtualTable {
name: tbl_name.unwrap_or(module_name).to_owned(),
columns: Self::resolve_columns(schema)?,
kind: VTabKind::VirtualTable,
module_name: Some(module_name.to_string()),
vtab_type: VirtualTableType::External(table),
};
Ok(Arc::new(vtab))