Skip to main content

Schema

Struct Schema 

Source
pub struct Schema {
Show 17 fields pub tables: FxHashMap<String, Arc<Table>>, pub materialized_view_names: FxHashSet<String>, pub materialized_view_sql: FxHashMap<String, String>, pub incremental_views: FxHashMap<String, Arc<Mutex<IncrementalView>>>, pub views: ViewsMap, pub triggers: FxHashMap<String, VecDeque<Arc<Trigger>>>, pub indexes: FxHashMap<String, VecDeque<Arc<Index>>>, pub has_indexes: FxHashSet<String>, pub schema_version: u32, pub analyze_stats: AnalyzeStats, pub table_to_materialized_views: FxHashMap<String, Vec<String>>, pub incompatible_views: FxHashSet<String>, pub broken_views: FxHashSet<String>, pub dropped_root_pages: FxHashSet<i64>, pub type_registry: FxHashMap<String, Arc<TypeDef>>, pub generated_columns_enabled: bool, pub sequences: FxHashMap<String, Arc<Sequence>>, /* private fields */
}

Fields§

§tables: FxHashMap<String, Arc<Table>>§materialized_view_names: FxHashSet<String>

Track which tables are actually materialized views

§materialized_view_sql: FxHashMap<String, String>

Store original SQL for materialized views (for .schema command)

§incremental_views: FxHashMap<String, Arc<Mutex<IncrementalView>>>

The incremental view objects (DBSP circuits)

§views: ViewsMap§triggers: FxHashMap<String, VecDeque<Arc<Trigger>>>

table_name to list of triggers

§indexes: FxHashMap<String, VecDeque<Arc<Index>>>

table_name to list of indexes for the table

§has_indexes: FxHashSet<String>§schema_version: u32§analyze_stats: AnalyzeStats

Statistics collected via ANALYZE for regular B-tree tables and indexes.

§table_to_materialized_views: FxHashMap<String, Vec<String>>

Mapping from table names to the materialized views that depend on them

§incompatible_views: FxHashSet<String>

Track views that exist but have incompatible versions

§broken_views: FxHashSet<String>

View rows in sqlite_schema whose stored SQL failed to parse (e.g. older versions wrote view column lists without identifier quoting). The rows are tolerated at load time so the database stays usable; tracking the names lets DROP VIEW remove them.

§dropped_root_pages: FxHashSet<i64>

Root pages of tables/indexes that have been dropped but not yet checkpointed. In MVCC mode, when a table is dropped, the btree pages are not freed until checkpoint. integrity_check needs to know about these pages to avoid false positives about “page never used”.

§type_registry: FxHashMap<String, Arc<TypeDef>>

Custom type registry, loaded from sqlite_turso_types

§generated_columns_enabled: bool§sequences: FxHashMap<String, Arc<Sequence>>

Named sequences (CREATE SEQUENCE)

Implementations§

Source§

impl Schema

Source

pub fn new() -> Self

Create a schema with custom types enabled.

Panics if a hardcoded built-in type definition is malformed (programmer bug). Production code that opens user databases should prefer Schema::with_options which returns Result.

Source

pub fn with_options(enable_custom_types: bool) -> Result<Self>

Source

pub fn register_internal_vtab<T>(&mut self, table: T) -> Result<String>
where T: InternalVirtualTable + 'static,

Add an InternalVirtualTable to the schema’s catalog. The wrapped table appears under the name returned by its name() method and is queryable like any other table. Returns the name actually inserted.

Intended for callers that want to surface state as a queryable table without going through CREATE VIRTUAL TABLE — for example, extensions that contribute metadata tables or alternative-dialect catalogs.

Source

pub fn get_type_def( &self, type_name: &str, is_strict: bool, ) -> Option<&Arc<TypeDef>>

Look up a custom type definition by name. Custom types are only valid on STRICT tables; pass is_strict from the owning table so that non-STRICT tables never resolve a custom type.

Source

pub fn get_type_def_unchecked(&self, type_name: &str) -> Option<&Arc<TypeDef>>

Look up a custom type definition by name without a strictness check. Only use this for operations that aren’t column-scoped (e.g. DROP TYPE, CREATE TABLE validation, CAST).

Source

pub fn resolve_type( &self, type_name: &str, is_strict: bool, ) -> Result<Option<ResolvedType>>

Resolve a custom type fully: look it up (with strictness gate) and chase the base-type chain to the ultimate primitive. Returns Ok(None) if the type is not registered (or the table isn’t strict).

Source

pub fn resolve_type_unchecked( &self, type_name: &str, ) -> Result<Option<ResolvedType>>

Resolve a custom type fully without a strictness check. Returns Ok(None) if the type is not in the registry.

Source

pub fn remove_type(&mut self, type_name: &str)

Source

pub fn resolve_base_type_chain( &self, type_name: &str, ) -> Result<(String, Vec<Arc<TypeDef>>)>

Chase the base type chain: domain_a → domain_b → integer Returns (ultimate_primitive, ordered_chain_of_TypeDefs) The chain is ordered from child to ancestor. Errors on cycles or missing intermediate types.

Source

pub fn add_type_from_sql(&mut self, sql: &str) -> Result<()>

Parse a CREATE TYPE SQL string and add the type to the in-memory registry.

Source

pub fn load_type_definitions(&mut self, type_sqls: &[String]) -> Result<()>

Load type definitions from CREATE TYPE SQL strings and resolve custom type affinities on all STRICT tables. This is the shared entry point used by both initial database open and schema reparse.

Source

pub fn resolve_all_custom_type_affinities(&mut self) -> Result<()>

Resolve custom type affinities for all STRICT tables in the schema. Call this after loading user-defined types from __turso_internal_types so that columns declared with custom types use the BASE type’s affinity.

Source

pub fn is_unique_idx_name(&self, name: &str) -> bool

Source

pub fn add_materialized_view( &mut self, view: IncrementalView, table: Arc<Table>, sql: String, )

Source

pub fn get_materialized_view( &self, name: &str, ) -> Option<Arc<Mutex<IncrementalView>>>

Source

pub fn has_compatible_dbsp_state_table(&self, view_name: &str) -> bool

Check if DBSP state table exists with the current version

Source

pub fn is_materialized_view(&self, name: &str) -> bool

Source

pub fn with_incompatible_dependent_views<F, T>( &self, table_name: &str, f: F, ) -> T
where F: FnOnce(&[&String]) -> T,

Apply a function to a table’s incompatible dependent materialized views

Source

pub fn remove_view(&mut self, name: &str) -> Result<()>

Source

pub fn add_materialized_view_dependency( &mut self, table_name: &str, view_name: &str, )

Register that a materialized view depends on a table

Source

pub fn get_dependent_materialized_views(&self, table_name: &str) -> Vec<String>

Get all materialized views that depend on a given table

Source

pub fn add_view(&mut self, view: View) -> Result<()>

Add a regular (non-materialized) view

Source

pub fn get_view(&self, name: &str) -> Option<Arc<View>>

Get a regular view by name

Source

pub fn add_trigger(&mut self, trigger: Trigger, table_name: &str) -> Result<()>

Source

pub fn remove_trigger(&mut self, name: &str) -> Result<()>

Source

pub fn remove_triggers_for_table(&mut self, table_name: &str)

Source

pub fn remove_triggers_for_table_with_db( &mut self, table_name: &str, target_db: usize, )

Like [remove_triggers_for_table] but only removes triggers whose target_database_id matches target_db (or is None, meaning “targets the parent schema’s table of this name”, which also applies). Used from DROP TABLE main.t to clean up temp triggers without accidentally removing ones that target temp.t or aux.t (the plain remove_triggers_for_table keys only on table name).

Source

pub fn get_trigger_for_table( &self, table_name: &str, name: &str, ) -> Option<Arc<Trigger>>

Source

pub fn get_triggers_for_table( &self, table_name: &str, ) -> impl Iterator<Item = &Arc<Trigger>> + Clone

Source

pub fn get_trigger(&self, name: &str) -> Option<Arc<Trigger>>

Source

pub fn add_btree_table(&mut self, table: Arc<BTreeTable>) -> Result<()>

Source

pub fn add_virtual_table(&mut self, table: Arc<VirtualTable>) -> Result<()>

Source

pub fn get_table(&self, name: &str) -> Option<Arc<Table>>

Source

pub fn table_name_for_root_page(&self, root_page: i64) -> Option<&str>

Source

pub fn remove_table(&mut self, table_name: &str)

Source

pub fn register_table_root_page(&mut self, name: &str, table: &Table)

Source

pub fn unregister_table_root_page(&mut self, table: &Table)

Source

pub fn get_btree_table(&self, name: &str) -> Option<Arc<BTreeTable>>

Source

pub fn add_index(&mut self, index: Arc<Index>) -> Result<()>

Source

pub fn get_indices(&self, table_name: &str) -> impl Iterator<Item = &Arc<Index>>

Source

pub fn get_index( &self, table_name: &str, index_name: &str, ) -> Option<&Arc<Index>>

Source

pub fn remove_indices_for_table(&mut self, table_name: &str)

Source

pub fn remove_index(&mut self, idx: &Index)

Source

pub fn table_has_indexes(&self, table_name: &str) -> bool

Source

pub fn table_set_has_index(&mut self, table_name: &str)

Source

pub fn make_from_btree( &mut self, state: &mut MakeFromBtreeState, mv_cursor: Option<Arc<RwLock<MvccLazyCursor<MvccClock, DynAllocator>>>>, pager: &Arc<Pager>, syms: &SymbolTable, ) -> Result<IOResult<()>>

Update Schema by scanning the first root page (sqlite_schema) Returns Result<IOResult<()>> to allow async operation with external IO loop

Source

pub fn populate_indices( &mut self, syms: &SymbolTable, from_sql_indexes: Vec<UnparsedFromSqlIndex>, automatic_indices: HashMap<String, Vec<(String, i64)>>, mvcc_enabled: bool, ) -> Result<()>

Populate indices parsed from the schema. from_sql_indexes: indices explicitly created with CREATE INDEX automatic_indices: indices created automatically for primary key and unique constraints

Source

pub fn populate_materialized_views( &mut self, materialized_view_info: HashMap<String, (String, i64)>, dbsp_state_roots: HashMap<String, i64>, dbsp_state_index_roots: HashMap<String, i64>, ) -> Result<()>

Populate materialized views parsed from the schema.

Source

pub fn sequence_backing_table_names(&self) -> Vec<(String, String)>

Yield (backing_table_name, sequence_name) for every backing table currently in the schema. Shared shape for the SQL-based descriptor loader in Connection so the prefix-strip lives in one place.

Source

pub fn handle_schema_row( &mut self, ty: &str, name: &str, table_name: &str, root_page: i64, maybe_sql: Option<&str>, syms: &SymbolTable, from_sql_indexes: &mut Vec<UnparsedFromSqlIndex>, automatic_indices: &mut HashMap<String, Vec<(String, i64)>>, dbsp_state_roots: &mut HashMap<String, i64>, dbsp_state_index_roots: &mut HashMap<String, i64>, materialized_view_info: &mut HashMap<String, (String, i64)>, resolve_attached_db: &dyn Fn(&str) -> Option<usize>, ) -> Result<()>

Source

pub fn resolved_fks_referencing( &self, table_name: &str, ) -> Result<Vec<ResolvedFkRef>>

Compute all resolved FKs referencing table_name (arg: table_name is the parent). Each item contains the child table, normalized columns/positions, and the parent lookup strategy (rowid vs. UNIQUE index or PK).

Source

pub fn resolved_fks_for_child( &self, child_table: &str, ) -> Result<Vec<ResolvedFkRef>>

Compute all resolved FKs declared by child_table. Unlike resolved_fks_referencing, this requires every non-rowid parent key to be backed by a non-partial UNIQUE index on exactly those columns.

Source

pub fn any_resolved_fks_referencing(&self, table_name: &str) -> bool

Returns if any table declares a FOREIGN KEY whose parent is table_name.

Source

pub fn has_child_fks(&self, table_name: &str) -> bool

Returns true if table_name declares any FOREIGN KEYs

Source

pub fn get_sequence(&self, name: &str) -> Option<&Arc<Sequence>>

Source

pub fn remove_sequence(&mut self, name: &str)

Remove a sequence and its backing table from the in-memory schema.

Source

pub fn get_object_type(&self, name: &str) -> Option<SchemaObjectType>

Returns the type of schema object with the given name, if one exists. Checks tables, views, and indexes.

Trait Implementations§

Source§

impl Debug for Schema

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Schema

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl TryClone for Schema

Source§

type Error = TryReserveError

Copying a Schema requires deep cloning of all internal tables and indexes, even though they are wrapped in Arc. Simply copying the Arc pointers would result in multiple Schema instances sharing the same underlying tables and indexes, which could lead to panics or data races if any instance attempts to modify them. To ensure each Schema is independent and safe to modify, we clone the underlying data for all tables and indexes.

Source§

fn try_clone(&self) -> Result<Self, Self::Error>

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more