Text2SQL API
Models
maticlib.core.text2sql.models.ColumnSchema
Bases: BaseModel
maticlib.core.text2sql.models.TableSchema
Bases: BaseModel
to_ddl
Convert the TableSchema to a simplified DDL representation for prompts. We can use sqlglot or just basic formatting here.
Source code in maticlib/core/text2sql/models.py
maticlib.core.text2sql.models.DatabaseSchema
Bases: BaseModel
to_prompt_string
Returns the schema formatted for the LLM. subset_tables filters which tables are included (for schema pruning).
Source code in maticlib/core/text2sql/models.py
Loaders
maticlib.core.text2sql.loaders.BaseSchemaLoader
Bases: ABC
Abstract base class for database schema loaders.
load_schema
abstractmethod
Loads and parses the schema from the given connection string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_string
|
str
|
A database URI (e.g. |
required |
Returns:
| Type | Description |
|---|---|
DatabaseSchema
|
A fully populated DatabaseSchema object. |
Source code in maticlib/core/text2sql/loaders.py
maticlib.core.text2sql.loaders.SQLAlchemySchemaLoader
Bases: BaseSchemaLoader
Reflects a live database schema using SQLAlchemy's inspect API.
load_schema
Reflects all tables and columns from the connected database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_string
|
str
|
A SQLAlchemy-compatible database URI. |
required |
Returns:
| Type | Description |
|---|---|
DatabaseSchema
|
A DatabaseSchema containing all tables and columns. |
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
If sqlalchemy is not installed. |
SchemaLoadError
|
If the schema cannot be reflected. |
Source code in maticlib/core/text2sql/loaders.py
Executors
maticlib.core.text2sql.executors.BaseExecutor
Bases: ABC
Abstract base class for SQL query executors.
execute
abstractmethod
Executes a SQL query and returns columns and rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
A validated SELECT SQL query string. |
required |
Returns:
| Type | Description |
|---|---|
List[str]
|
A tuple of |
List[tuple]
|
and rows is a list of row tuples. |
Source code in maticlib/core/text2sql/executors.py
maticlib.core.text2sql.executors.SQLAlchemyExecutor
Bases: BaseExecutor
Executes validated SQL queries against any SQLAlchemy-supported database.
Initializes the SQLAlchemyExecutor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_string
|
str
|
A SQLAlchemy database URI. |
required |
read_only
|
bool
|
If True (default), opens SQLite connections in read-only mode. |
True
|
Source code in maticlib/core/text2sql/executors.py
Tabular Ingestor
maticlib.core.text2sql.tabular_ingestor.TabularIngestor
Source code in maticlib/core/text2sql/tabular_ingestor.py
ingest_file
Ingest a CSV, Excel, or Parquet file into the database. if_exists can be 'fail', 'replace', or 'append'.
Source code in maticlib/core/text2sql/tabular_ingestor.py
Guards
maticlib.core.text2sql.guards.SQLInjectionGuard
Parses, validates, and transpiles SQL queries to prevent injection attacks.
Initializes the SQLInjectionGuard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
allowed_dialect
|
str
|
Target SQL dialect for transpilation (e.g. |
'sqlite'
|
Raises:
| Type | Description |
|---|---|
MissingDependencyError
|
If sqlglot is not installed. |
Source code in maticlib/core/text2sql/guards.py
validate_and_format
Validates the SQL query against injection attacks and returns a cleanly formatted transpiled version of the query for the target dialect. Ensures the query is only performing SELECT operations.