use crate::pragma::PragmaVirtualTable;
use crate::schema::{Schema, Table};
use crate::sync::Arc;
use crate::vtab::{VirtualTable, VirtualTableType};
use turso_ext::VTabKind;
pub fn register_builtin_catalog(
schema: &mut Schema,
enable_custom_types: bool,
) -> crate::Result<()> {
for vtab in pragma_vtabs() {
schema.tables.insert(
vtab.name.to_owned(),
Arc::new(Table::Virtual(Arc::new((*vtab).clone()))),
);
}
#[cfg(feature = "json")]
{
schema.register_internal_vtab(crate::json::vtab::JsonVirtualTable::json_each())?;
schema.register_internal_vtab(crate::json::vtab::JsonVirtualTable::json_tree())?;
}
#[cfg(feature = "cli_only")]
{
schema.register_internal_vtab(crate::dbpage::DbPageTable::new())?;
schema.register_internal_vtab(crate::btree_dump::BtreeDumpTable::new())?;
}
if enable_custom_types {
schema.register_internal_vtab(crate::turso_types_vtab::TursoTypesTable::new())?;
}
Ok(())
}
fn pragma_vtabs() -> Vec<Arc<VirtualTable>> {
PragmaVirtualTable::functions()
.into_iter()
.map(|(tab, schema_sql)| {
Arc::new(VirtualTable {
name: format!("pragma_{}", tab.pragma_name),
columns: VirtualTable::resolve_columns(schema_sql)
.expect("pragma table-valued function schema resolution should not fail"),
kind: VTabKind::TableValuedFunction,
vtab_type: VirtualTableType::Pragma(tab),
vtab_id: 0,
innocuous: true,
})
})
.collect()
}