pub struct Catalog { /* private fields */ }Expand description
A concrete, eager schema registry. Build it with Catalog::new
and Catalog::table (or collect an iterator of CatalogTable),
then hand Some(&catalog) to an extractor.
Internally a flat list of CatalogTables — the resolver scans it
with right-anchored, cased matching, so there is no name-keyed
index (a bare users may match several *.users entries, which is
not a hashable equivalence). The optional default catalog / schema
fill a bare or partially-qualified query reference before matching
(like a single-entry search path); when unset, matching stays
best-effort right-anchored.
Implementations§
Source§impl Catalog
impl Catalog
Sourcepub fn table(self, table: CatalogTable) -> Self
pub fn table(self, table: CatalogTable) -> Self
Add one registered table. Returns self for chaining.
Sourcepub fn default_catalog(self, catalog: impl Into<String>) -> Self
pub fn default_catalog(self, catalog: impl Into<String>) -> Self
Set the default catalog used to fill a query reference that
omits its catalog segment before matching. Returns self.
Sourcepub fn default_schema(self, schema: impl Into<String>) -> Self
pub fn default_schema(self, schema: impl Into<String>) -> Self
Set the default schema used to fill a bare query reference
before matching. Returns self.
Sourcepub fn from_ddl(dialect: &dyn Dialect, ddl: &str) -> Result<Self, Error>
pub fn from_ddl(dialect: &dyn Dialect, ddl: &str) -> Result<Self, Error>
Build a catalog from SQL DDL: every CREATE TABLE with an explicit
column list becomes a registered table — its [catalog.]schema.name
path and column names. Column types and constraints are ignored
(only names are needed), so dialect-specific type syntax doesn’t
matter as long as the statement parses. ddl is parsed with
dialect, so pass the dialect matching your schema dump.
An unqualified CREATE TABLE t registers schema-less (no schema
is fabricated); right-anchored matching still lets a bare query t
— or even a qualified s.t — resolve to it. Skipped (not
registered): statements that aren’t CREATE TABLE, CREATE TABLEs
with no column definitions (... AS SELECT, ... LIKE ...), and
names with more than three catalog.schema.name segments. A parse
failure (the DDL is invalid for dialect) returns Err.
Only CREATE TABLE is interpreted. Session-default statements
(USE ..., SET search_path ...) are not read — query-side
defaults are the caller’s responsibility via
Catalog::default_schema / Catalog::default_catalog.
use sql_insight::catalog::Catalog;
use sql_insight::sqlparser::dialect::GenericDialect;
let ddl = "CREATE TABLE users (id INT, name TEXT); \
CREATE TABLE app.orders (id INT, total NUMERIC);";
let catalog = Catalog::from_ddl(&GenericDialect {}, ddl).unwrap();
// `users` registered schema-less; `app.orders` keeps its schema.Sourcepub fn from_ddl_with_casing(
dialect: &dyn Dialect,
ddl: &str,
casing: IdentifierCasing,
) -> Result<Self, Error>
pub fn from_ddl_with_casing( dialect: &dyn Dialect, ddl: &str, casing: IdentifierCasing, ) -> Result<Self, Error>
Like from_ddl but normalizes the stored identifiers
with an explicit casing instead of the dialect default. Pass the same
IdentifierCasing you give the extractor via
ExtractorOptions::with_casing,
so the catalog’s canonical form matches what a query reference folds to:
from_ddl (dialect default) only matches when the extraction also uses
the default, so a case-sensitive override needs this to resolve.