DataModel¶
A class to manage a data model based on an XML schema and its database equivalent.
It is the main entry point for xml2db
.
This class allows parsing an XSD file to build a representation of the XML schema, simplify it and convert it into a set of database tables. It also allows parsing XML documents that fit this XML schema and importing their content into a database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xsd_file |
str
|
A path to a XSD file |
required |
short_name |
str
|
A short name for the schema |
None
|
long_name |
str
|
A longer name for the schema |
None
|
base_url |
str
|
The root folder to find other dependant XSD files (by default, the location of the provided XSD file) |
None
|
model_config |
dict
|
A config dict to provide options for building the model (full options available here: Configuring your data model) |
None
|
connection_string |
str
|
A database connection string (optional if you will not be loading data) |
None
|
db_engine |
Engine
|
A |
None
|
db_type |
str
|
The targeted database backend ( |
None
|
db_schema |
str
|
A schema name to use in the database |
None
|
temp_prefix |
str
|
A prefix to use for temporary tables (if |
None
|
Attributes:
Name | Type | Description |
---|---|---|
xml_schema |
The |
|
lxml_schema |
The |
|
data_flow_name |
A short identifier used for the data model ( |
|
data_flow_long_name |
A longer for the data model ( |
|
db_schema |
A database schema name to store the database tables |
|
source_tree |
A text representation of the source data model tree |
|
target_tree |
A text representation of the simplified data model tree which will be used to create target tables |
Examples:
Create a DataModel
like this:
>>> data_model = DataModel(
>>> xsd_file="path/to/file.xsd",
>>> connection_string="postgresql+psycopg2://testuser:testuser@localhost:5432/testdb",
>>> )
Source code in xml2db/model.py
fk_ordered_tables: Iterable[Union[DataModelTableDuplicated, DataModelTableReused]]
property
¶
Yields tables in create/insert order (tables referenced in foreign keys first)
fk_ordered_tables_reversed: Iterable[Union[DataModelTableDuplicated, DataModelTableReused]]
property
¶
Yields tables in drop/delete order (tables referencing foreign keys first)
create_all_tables(temp=False)
¶
Create tables for the data model, either target tables or temp tables used to import data.
You do not have to call this method explicitly when using
Document.insert_into_target_tables()
,
which will create tables if they do not exist.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
temp |
bool
|
If |
False
|
Source code in xml2db/model.py
create_db_schema()
¶
Create database schema if it does not already exist.
You do not have to call this method explicitly when using
Document.insert_into_target_tables()
.
Source code in xml2db/model.py
drop_all_tables()
¶
Drop the data model target (unprefixed) tables.
Danger
BE CAUTIOUS, THIS METHOD DROPS TABLES WITHOUT FURTHER NOTICE!
drop_all_temp_tables()
¶
Drop the data model temporary (prefixed) tables.
Danger
BE CAUTIOUS, THIS METHOD DROPS TABLES WITHOUT FURTHER NOTICE!
extract_from_database(root_select_where, force_tz=None)
¶
Extract a document from the database, based on a where clause applied to the root table. For instance, you
can use the column xml2db_input_file_path
to filter the data loaded from a specific file.
It will query all the data in the database corresponding to the rows that you select from the root table of your data model. Typically, a single XML file will correspond to a single row in the root table. This function will query the data tree below this record.
This method was not optimized for performance and can be quite slow. It is used in integration tests to check the output against the data inserted into the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
root_select_where |
str
|
A where clause to filter the root table of the model, as a string |
required |
force_tz |
Union[str, None]
|
Apply this timezone if database returns timezone-naïve datetime |
None
|
Returns:
Type | Description |
---|---|
Document
|
A |
Examples:
Source code in xml2db/model.py
get_all_create_index_statements()
¶
get_all_create_table_statements(temp=False)
¶
Yield sqlalchemy create table
statements for all tables
Parameters:
Name | Type | Description | Default |
---|---|---|---|
temp |
bool
|
If |
False
|
Source code in xml2db/model.py
get_entity_rel_diagram(text_context=True)
¶
Build an entity relationship diagram for the data model
The ERD syntax is used by mermaid.js to create a visual representation of the diagram, which is supported by Pycharm IDE or GitHub in markdown files, among others
Parameters:
Name | Type | Description | Default |
---|---|---|---|
text_context |
bool
|
Should we add a title, a text explanation, etc. or just the ERD? |
True
|
Returns:
Type | Description |
---|---|
str
|
A string representation of the ERD |
Source code in xml2db/model.py
parse_xml(xml_file, metadata=None, skip_validation=True, iterparse=True, recover=False, flat_data=None)
¶
Parse an XML document based on this data model
This method is just a wrapper around the parse_xml method of the Document class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
xml_file |
Union[str, BytesIO]
|
The path or the file object of an XML file to parse |
required |
metadata |
dict
|
A dict of metadata values to add to the root table (a value for each key defined in
|
None
|
skip_validation |
bool
|
Should we validate the documents against the schema first? |
True
|
iterparse |
bool
|
Parse XML using iterative parsing, which is a bit slower but uses less memory |
True
|
recover |
bool
|
Should we try to parse incorrect XML? (argument passed to lxml parser) |
False
|
flat_data |
dict
|
A dict containing flat data if we want to add data to another dataset instead of creating a new one |
None
|
Returns:
Type | Description |
---|---|
Document
|
A parsed |