sqlmeta.objects
SQL object models for all database object types.
- class sqlmeta.objects.Table(name: str, columns: List[SqlColumn] = None, schema: str | None = None, constraints: List[SqlConstraint] = None, temporary: bool = False, tablespace: str | None = None, dialect: str | None = None, comment: str | None = None, storage_engine: str | None = None, partitions: List | None = None, filegroup: str | None = None, memory_optimized: bool = False, system_versioned: bool = False, history_table: str | None = None, history_schema: str | None = None, derived_from: str | None = None)[source]
Represents a database table.
- __init__(name: str, columns: List[SqlColumn] = None, schema: str | None = None, constraints: List[SqlConstraint] = None, temporary: bool = False, tablespace: str | None = None, dialect: str | None = None, comment: str | None = None, storage_engine: str | None = None, partitions: List | None = None, filegroup: str | None = None, memory_optimized: bool = False, system_versioned: bool = False, history_table: str | None = None, history_schema: str | None = None, derived_from: str | None = None)[source]
Initialize a table.
- Parameters:
name – Table name
columns – List of columns
schema – Schema name (optional)
constraints – List of constraints
temporary – Whether the table is temporary
tablespace – Tablespace name (optional)
dialect – SQL dialect
comment – Table comment/description (optional)
storage_engine – Storage engine (MySQL: InnoDB, MyISAM, etc.)
partitions – List of Partition objects (optional)
filegroup – Filegroup name (SQL Server T-SQL grammar-based)
memory_optimized – Whether table is memory-optimized (SQL Server T-SQL grammar-based)
system_versioned – Whether table is system-versioned temporal table (SQL Server T-SQL grammar-based)
history_table – History table name for system-versioned tables (SQL Server T-SQL grammar-based)
history_schema – History table schema for system-versioned tables (SQL Server T-SQL grammar-based)
derived_from – Source for derived tables (e.g., “CTAS”, “LIKE source_table”, “SELECT”)
- add_column(column: SqlColumn) None[source]
Add a column to the table.
- Parameters:
column – The column to add
- add_constraint(constraint: SqlConstraint) None[source]
Add a constraint to the table.
- Parameters:
constraint – The constraint to add
- compare_with_defaults(other: SqlObject, schema_defaults: Dict[str, Any] = None) Dict[str, Any][source]
Compare two tables, taking into account schema defaults.
This method extends the base class method to handle table-specific properties.
- Parameters:
other – The other table to compare with
schema_defaults – Dictionary of schema default values
- Returns:
Dictionary of differences between the tables
- property create_statement: str
Generate CREATE TABLE statement.
- Returns:
SQL CREATE TABLE statement for this table
- property drop_statement: str
Generate DROP TABLE statement.
- Returns:
SQL DROP TABLE statement for this table
- classmethod from_dict(data: Dict[str, Any]) Table[source]
Create table from dictionary representation.
- Parameters:
data – Dictionary with table attributes
- Returns:
Table object
- get_check_constraints() List[SqlConstraint][source]
Get all check constraints.
- Returns:
List of check constraints
- get_column(name: str) SqlColumn | None[source]
Get a column by name.
- Parameters:
name – Column name
- Returns:
The column or None if not found
- get_foreign_keys() List[SqlConstraint][source]
Get all foreign key constraints.
- Returns:
List of foreign key constraints
- get_primary_key() SqlConstraint | None[source]
Get the primary key constraint.
- Returns:
The primary key constraint or None if not found
- get_unique_constraints() List[SqlConstraint][source]
Get all unique constraints.
- Returns:
List of unique constraints
- class sqlmeta.objects.View(name: str, schema: str | None = None, query: str | None = None, columns: List[str] | None = None, materialized: bool = False, dialect: str | None = None, is_populated: bool | None = None, refresh_method: str | None = None, refresh_mode: str | None = None, fast_refreshable: bool | None = None, last_refresh: str | None = None, unlogged: bool | None = None, algorithm: str | None = None, sql_security: str | None = None, definer: str | None = None, force: bool | None = None)[source]
Represents a database view.
Supports both regular views and materialized views with refresh options.
- __init__(name: str, schema: str | None = None, query: str | None = None, columns: List[str] | None = None, materialized: bool = False, dialect: str | None = None, is_populated: bool | None = None, refresh_method: str | None = None, refresh_mode: str | None = None, fast_refreshable: bool | None = None, last_refresh: str | None = None, unlogged: bool | None = None, algorithm: str | None = None, sql_security: str | None = None, definer: str | None = None, force: bool | None = None)[source]
Initialize a view.
- Parameters:
name – View name
schema – Schema name (optional)
query – SELECT query that defines the view
columns – List of column names (optional)
materialized – Whether the view is materialized
dialect – SQL dialect
is_populated – Whether the materialized view is populated (PostgreSQL, Oracle)
refresh_method – Refresh method - FAST, COMPLETE, FORCE, MANUAL (Oracle, DB2)
refresh_mode – Refresh mode - ON DEMAND, ON COMMIT (Oracle)
fast_refreshable – Whether fast refresh is available (Oracle)
last_refresh – Timestamp of last refresh (Oracle, DB2)
unlogged – Whether the materialized view is UNLOGGED (PostgreSQL grammar-based)
algorithm – View algorithm - MERGE, TEMPTABLE, UNDEFINED (MySQL grammar-based)
sql_security – SQL security - DEFINER, INVOKER (MySQL grammar-based)
definer – Definer user - user@host (MySQL grammar-based)
force – Whether view is created with FORCE (True) or NOFORCE (False) (Oracle grammar-based)
- property create_statement: str
Generate CREATE VIEW statement.
The syntax varies by dialect.
- Returns:
Dialect-specific CREATE VIEW statement
- property drop_statement: str
Generate DROP VIEW statement.
- Returns:
SQL DROP VIEW statement for this view
- class sqlmeta.objects.Sequence(name: str, schema: str | None = None, start_with: int | None = None, increment_by: int | None = None, min_value: int | None = None, max_value: int | None = None, cycle: bool = False, cache: int | None = None, dialect: str | None = None, temp: bool = False)[source]
Represents a database sequence.
- __init__(name: str, schema: str | None = None, start_with: int | None = None, increment_by: int | None = None, min_value: int | None = None, max_value: int | None = None, cycle: bool = False, cache: int | None = None, dialect: str | None = None, temp: bool = False)[source]
Initialize a sequence.
- Parameters:
name – Sequence name
schema – Schema name
start_with – Starting value
increment_by – Increment value
min_value – Minimum value
max_value – Maximum value
cycle – Whether to cycle when reaching max_value
cache – Cache size
dialect – SQL dialect
temp – Whether sequence is TEMPORARY (PostgreSQL grammar-based)
- property create_statement: str
Generate CREATE SEQUENCE statement.
The syntax varies by dialect.
- Returns:
Dialect-specific CREATE SEQUENCE statement
- property drop_statement: str
Generate DROP SEQUENCE statement.
- Returns:
SQL DROP SEQUENCE statement for this sequence
- class sqlmeta.objects.Procedure(name: str, schema: str | None = None, parameters: List[Parameter] | None = None, body: str | None = None, language: str = 'SQL', dialect: str | None = None, is_function: bool = False, return_type: str | None = None, comment: str | None = None, definition: str | None = None)[source]
Represents a stored procedure or function.
- __init__(name: str, schema: str | None = None, parameters: List[Parameter] | None = None, body: str | None = None, language: str = 'SQL', dialect: str | None = None, is_function: bool = False, return_type: str | None = None, comment: str | None = None, definition: str | None = None)[source]
Initialize a stored procedure or function.
- Parameters:
name – Procedure/function name
schema – Schema name
parameters – List of procedure/function parameters
body – Procedure/function body
language – Procedure language (SQL, PLSQL, PLPGSQL, TSQL, etc.)
dialect – SQL dialect
is_function – Whether this is a function (vs procedure)
return_type – Return type for functions
comment – Procedure/function comment/description
definition – Full procedure/function definition SQL
- property create_statement: str
Generate CREATE PROCEDURE or CREATE FUNCTION statement.
The syntax varies by dialect.
- Returns:
Dialect-specific CREATE PROCEDURE/FUNCTION statement
- property drop_statement: str
Generate DROP PROCEDURE or DROP FUNCTION statement.
- Returns:
SQL DROP PROCEDURE/FUNCTION statement
- class sqlmeta.objects.Parameter(name: str, data_type: str, direction: str = 'IN', default_value: str | None = None, dialect: str | None = None)[source]
Represents a stored procedure parameter.
- __init__(name: str, data_type: str, direction: str = 'IN', default_value: str | None = None, dialect: str | None = None)[source]
Initialize a procedure parameter.
- Parameters:
name – Parameter name
data_type – Parameter data type
direction – Parameter direction (IN, OUT, INOUT)
default_value – Default value for the parameter
dialect – SQL dialect (optional)
- class sqlmeta.objects.Index(name: str, table_name: str, columns: List[str], schema: str | None = None, table_schema: str | None = None, unique: bool = False, type: str = 'BTREE', condition: str | None = None, include_columns: List[str] | None = None, sort_directions: List[str] | None = None, dialect: str | None = None, online: bool | None = None, concurrently: bool = False, tablespace: str | None = None)[source]
Represents a database index.
- __init__(name: str, table_name: str, columns: List[str], schema: str | None = None, table_schema: str | None = None, unique: bool = False, type: str = 'BTREE', condition: str | None = None, include_columns: List[str] | None = None, sort_directions: List[str] | None = None, dialect: str | None = None, online: bool | None = None, concurrently: bool = False, tablespace: str | None = None)[source]
Initialize an index.
- Parameters:
name – Index name
table_name – Name of the table being indexed
columns – List of indexed columns
schema – Schema name for the index
table_schema – Schema name for the table (if different from index schema)
unique – Whether this is a unique index
type – Index type (BTREE, HASH, FULLTEXT, SPATIAL, etc.)
condition – Optional WHERE condition
include_columns – Optional INCLUDE columns (SQL Server)
sort_directions – Optional sort directions (ASC/DESC) for each column
dialect – SQL dialect
online – Whether index was created with ONLINE (True) or OFFLINE (False) (MySQL grammar-based)
concurrently – Whether index was created CONCURRENTLY (PostgreSQL grammar-based)
tablespace – Tablespace name for the index (Oracle grammar-based)
- property create_statement: str
Generate CREATE INDEX statement.
The syntax varies by dialect.
- Returns:
Dialect-specific CREATE INDEX statement
- property drop_statement: str
Generate DROP INDEX statement.
- Returns:
SQL DROP INDEX statement for this index
- class sqlmeta.objects.Trigger(name: str, table_name: str, schema: str | None = None, timing: str | None = None, events: list[str] | None = None, orientation: str | None = None, definition: str | None = None, enabled: bool = True, dialect: str | None = None, definer: str | None = None)[source]
Represents a database trigger.
- __eq__(other: Any) bool[source]
Check if two triggers are equal.
- Parameters:
other – Other object to compare
- Returns:
True if triggers are equal
- __init__(name: str, table_name: str, schema: str | None = None, timing: str | None = None, events: list[str] | None = None, orientation: str | None = None, definition: str | None = None, enabled: bool = True, dialect: str | None = None, definer: str | None = None)[source]
Initialize a trigger.
- Parameters:
name – Trigger name
table_name – Name of the table the trigger is on
schema – Schema name (optional)
timing – When trigger fires (BEFORE, AFTER, INSTEAD OF)
events – List of events that fire the trigger (INSERT, UPDATE, DELETE, TRUNCATE)
orientation – Trigger level (ROW, STATEMENT)
definition – Trigger body/definition
enabled – Whether trigger is enabled
dialect – SQL dialect
definer – Definer user - user@host (MySQL grammar-based)
- property create_statement: str
Generate CREATE TRIGGER statement.
The syntax varies by dialect.
- Returns:
Dialect-specific CREATE TRIGGER statement
- property event_str: str
Get events as a formatted string.
- Returns:
Events joined by ‘ OR ‘ (e.g., ‘INSERT OR UPDATE’)
- classmethod from_dict(data: Dict[str, Any]) Trigger[source]
Create trigger from dictionary representation.
- Parameters:
data – Dictionary with trigger attributes
- Returns:
Trigger object
- class sqlmeta.objects.Synonym(name: str, target_object: str, schema: str | None = None, target_schema: str | None = None, target_database: str | None = None, db_link: str | None = None, dialect: str | None = None)[source]
Represents a database synonym (alias for another object).
- __init__(name: str, target_object: str, schema: str | None = None, target_schema: str | None = None, target_database: str | None = None, db_link: str | None = None, dialect: str | None = None)[source]
Initialize a synonym.
- Parameters:
name – Synonym name
target_object – Name of the target object this synonym points to
schema – Schema where the synonym is defined (optional)
target_schema – Schema of the target object (optional)
target_database – Database of the target object (optional, SQL Server)
db_link – Database link for remote objects (optional, Oracle)
dialect – SQL dialect
- property create_statement: str
Generate CREATE SYNONYM statement.
The syntax varies by dialect.
- Returns:
Dialect-specific CREATE SYNONYM statement
- class sqlmeta.objects.UserDefinedType(name: str, type_category: str, schema: str | None = None, data_type: str | None = None, definition: str | None = None, attributes: List[Dict[str, Any]] | None = None, enum_values: List[str] | None = None, base_type: str | None = None, comment: str | None = None, dialect: str | None = None)[source]
Represents a user-defined type (UDT).
- __init__(name: str, type_category: str, schema: str | None = None, data_type: str | None = None, definition: str | None = None, attributes: List[Dict[str, Any]] | None = None, enum_values: List[str] | None = None, base_type: str | None = None, comment: str | None = None, dialect: str | None = None)[source]
Initialize a user-defined type.
- Parameters:
name – Type name
type_category – Category of type (COMPOSITE, ENUM, DOMAIN, OBJECT, DISTINCT, etc.)
schema – Schema name (optional)
data_type – SQL data type classification (optional)
definition – Type definition/source code (optional)
attributes – List of attributes for composite/structured types (optional)
enum_values – List of values for enum types (optional)
base_type – Base type for DISTINCT types (optional)
comment – Type comment/description (optional)
dialect – SQL dialect
- property create_statement: str
Generate CREATE TYPE statement.
The syntax varies significantly by dialect and type category.
- Returns:
Dialect-specific CREATE TYPE statement
- class sqlmeta.objects.Extension(name: str, version: str | None = None, schema: str | None = None, description: str | None = None, relocatable: bool = False, dialect: str | None = None)[source]
Represents a database extension (PostgreSQL-specific).
Extensions are add-on modules that provide additional functionality to the database, such as PostGIS for geographic data, pgcrypto for cryptographic functions, or pg_trgm for trigram text search.
- __init__(name: str, version: str | None = None, schema: str | None = None, description: str | None = None, relocatable: bool = False, dialect: str | None = None)[source]
Initialize an extension.
- Parameters:
name – Extension name
version – Extension version (optional)
schema – Schema where the extension is installed (optional)
description – Extension description (optional)
relocatable – Whether the extension can be relocated to another schema
dialect – SQL dialect (typically ‘postgresql’)
- class sqlmeta.objects.Package(name: str, schema: str | None = None, spec: str | None = None, body: str | None = None, dialect: str | None = 'oracle')[source]
Represents an Oracle package (specification and body).
- __init__(name: str, schema: str | None = None, spec: str | None = None, body: str | None = None, dialect: str | None = 'oracle')[source]
Initialize an Oracle package.
- Parameters:
name – Package name
schema – Schema name (optional)
spec – Package specification (header/interface)
body – Package body (implementation)
dialect – SQL dialect (defaults to oracle)
- property create_statement: str
Generate CREATE PACKAGE statements.
Returns both spec and body if available.
- Returns:
CREATE PACKAGE and CREATE PACKAGE BODY statements
- class sqlmeta.objects.Module(name: str, definition: str, schema: str | None = None, dialect: str | None = None)[source]
Represents a DB2 Module - a collection of SQL procedures, functions, and types.
DB2 Modules are similar to Oracle Packages - they group related SQL routines and user-defined types together. Modules support SQL routine encapsulation and can contain both published (public) and internal (private) routines.
- __eq__(other: Any) bool[source]
Check if two modules are equal.
Note: Case-sensitive in DB2 for delimited identifiers.
- __init__(name: str, definition: str, schema: str | None = None, dialect: str | None = None)[source]
Initialize a DB2 module.
- Parameters:
name – Module name
definition – Complete module definition (CREATE MODULE … END MODULE)
schema – Schema name (typically the module owner)
dialect – SQL dialect (typically ‘db2’)
- class sqlmeta.objects.DatabaseLink(name: str, host: str | None = None, username: str | None = None, connect_string: str | None = None, public: bool = False, schema: str | None = None, dialect: str | None = None)[source]
Represents a database link (Oracle-specific).
Database links are named connections from one Oracle database to another, allowing distributed queries across multiple databases. They are essential for enterprise applications with multi-database architectures.
- __eq__(other: Any) bool[source]
Check if two database links are equal.
Note: We compare connection parameters but not passwords for security.
- __init__(name: str, host: str | None = None, username: str | None = None, connect_string: str | None = None, public: bool = False, schema: str | None = None, dialect: str | None = None)[source]
Initialize a database link.
- Parameters:
name – Database link name
host – Remote host name or TNS name (optional)
username – Username for remote connection (optional)
connect_string – Complete connection string/TNS name (optional)
public – Whether this is a public database link
schema – Schema owner (for private links)
dialect – SQL dialect (typically ‘oracle’)
- class sqlmeta.objects.LinkedServer(name: str, product: str | None = None, provider: str | None = None, data_source: str | None = None, catalog: str | None = None, username: str | None = None, schema: str | None = None, dialect: str | None = None)[source]
Represents a linked server (SQL Server-specific).
Linked servers allow SQL Server to execute commands against remote databases (SQL Server, Oracle, MySQL, etc.) using distributed queries. Similar to Oracle Database Links.
- __eq__(other: Any) bool[source]
Check if two linked servers are equal.
Note: We compare connection parameters but not passwords for security.
- __init__(name: str, product: str | None = None, provider: str | None = None, data_source: str | None = None, catalog: str | None = None, username: str | None = None, schema: str | None = None, dialect: str | None = None)[source]
Initialize a linked server.
- Parameters:
name – Linked server name
product – Product name (e.g., ‘SQL Server’, ‘Oracle’, ‘MySQL’)
provider – OLE DB provider name (e.g., ‘SQLNCLI’, ‘OraOLEDB.Oracle’)
data_source – Network name/IP address of remote server
catalog – Default database/catalog on remote server
username – Login name for remote connection
schema – Schema owner (typically dbo)
dialect – SQL dialect (typically ‘sqlserver’)
- property create_statement: str
Generate CREATE LINKED SERVER statement using sp_addlinkedserver.
Note: For security reasons, passwords are not stored or generated. Login mappings must be configured separately with sp_addlinkedsrvlogin.
- Returns:
SQL Server EXEC sp_addlinkedserver statement (without password)
- class sqlmeta.objects.ForeignDataWrapper(name: str, handler: str | None = None, validator: str | None = None, options: Dict[str, str] | None = None, schema: str | None = None, dialect: str | None = None)[source]
Represents a foreign data wrapper (PostgreSQL-specific).
Foreign Data Wrappers (FDW) enable PostgreSQL to access external data sources including other databases (postgres_fdw, oracle_fdw), files (file_fdw), and custom data sources.
- __init__(name: str, handler: str | None = None, validator: str | None = None, options: Dict[str, str] | None = None, schema: str | None = None, dialect: str | None = None)[source]
Initialize a foreign data wrapper.
- Parameters:
name – FDW name (e.g., ‘postgres_fdw’, ‘oracle_fdw’)
handler – Handler function name (optional)
validator – Validator function name (optional)
options – FDW-specific options as key-value pairs (optional)
schema – Schema (typically ‘public’ or system schemas)
dialect – SQL dialect (typically ‘postgresql’)
- class sqlmeta.objects.ForeignServer(name: str, fdw_name: str, host: str | None = None, port: int | None = None, dbname: str | None = None, options: Dict[str, str] | None = None, schema: str | None = None, dialect: str | None = None)[source]
Represents a foreign server (PostgreSQL-specific).
Foreign Servers define connection parameters for specific remote data sources accessed through Foreign Data Wrappers. Multiple foreign tables can reference the same foreign server.
- __init__(name: str, fdw_name: str, host: str | None = None, port: int | None = None, dbname: str | None = None, options: Dict[str, str] | None = None, schema: str | None = None, dialect: str | None = None)[source]
Initialize a foreign server.
- Parameters:
name – Foreign server name
fdw_name – Name of the foreign data wrapper to use
host – Remote host address (optional)
port – Remote port number (optional)
dbname – Remote database name (optional)
options – Server-specific options as key-value pairs (optional)
schema – Schema (typically ‘public’)
dialect – SQL dialect (typically ‘postgresql’)
- class sqlmeta.objects.Event(name: str, schema: str | None = None, definition: str | None = None, schedule: str | None = None, enabled: bool = True, comment: str | None = None, definer: str | None = None, event_type: str = 'ONE TIME', dialect: str | None = 'mysql')[source]
Represents a MySQL scheduled event.
- __init__(name: str, schema: str | None = None, definition: str | None = None, schedule: str | None = None, enabled: bool = True, comment: str | None = None, definer: str | None = None, event_type: str = 'ONE TIME', dialect: str | None = 'mysql')[source]
Initialize a MySQL event.
- Parameters:
name – Event name
schema – Schema/database name (optional)
definition – Event body (DO clause)
schedule – Schedule expression (AT or EVERY clause)
enabled – Whether the event is enabled
comment – Event comment/description
definer – User who defined the event
event_type – Event type (ONE TIME or RECURRING)
dialect – SQL dialect (defaults to mysql)
- class sqlmeta.objects.Partition(name: str, table: str, partition_method: str, partition_expression: str | None = None, partition_description: str | None = None, subpartitions: List[Partition] | None = None, schema: str | None = None, dialect: str | None = None, **kwargs: Any)[source]
Represents a table partition.
- __init__(name: str, table: str, partition_method: str, partition_expression: str | None = None, partition_description: str | None = None, subpartitions: List[Partition] | None = None, schema: str | None = None, dialect: str | None = None, **kwargs: Any)[source]
Initialize a partition.
- Parameters:
name – Partition name
table – Table name this partition belongs to
partition_method – Partition method (RANGE, LIST, HASH, KEY)
partition_expression – Expression used for partitioning
partition_description – Partition boundary description (VALUES LESS THAN, VALUES IN, etc.)
subpartitions – List of subpartitions (for composite partitioning)
schema – Schema name (optional)
dialect – SQL dialect
**kwargs – Additional dialect-specific metadata
- property create_statement: str
Generate partition definition (part of ALTER TABLE or CREATE TABLE).
Note: Partitions are typically not created standalone, but as part of CREATE TABLE or ALTER TABLE statements.
- Returns:
Partition definition clause
- classmethod from_dict(data: Dict[str, Any]) Partition[source]
Create partition from dictionary representation.
- Parameters:
data – Dictionary with partition attributes
- Returns:
Partition object
Modules
Database Link SQL model class (Oracle-specific). |
|
MySQL Event SQL Model. |
|
Extension SQL model class (PostgreSQL-specific). |
|
Foreign Data Wrapper SQL model class (PostgreSQL-specific). |
|
Foreign Server SQL model class (PostgreSQL-specific). |
|
Linked Server SQL model class (SQL Server-specific). |
|
Module SQL model class (DB2-specific). |
|
Oracle Package SQL Model. |
|
Table Partition SQL Model. |
|
Synonym SQL model class. |
|
Trigger SQL Model. |
|
User-Defined Type SQL model class. |
|