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 |
'DocumentRoot'
|
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
property
¶
Yields tables in create/insert order (tables referenced in foreign keys first)
fk_ordered_tables_reversed
property
¶
Yields tables in drop/delete order (tables referencing foreign keys first)
_build_model()
¶
Build model from the provided XSD schema and config.
It will parse the XML schema, then simplify it, then create all sqlalchemy objects.
Source code in xml2db/model.py
_create_table_model(table_name, type_name, is_root_table=False, is_virtual_node=False)
¶
Helper to create a data table model
Parameters:
Name | Type | Description | Default |
---|---|---|---|
table_name
|
str
|
name of the table |
required |
type_name
|
str
|
type of the table |
required |
is_root_table
|
bool
|
is this table the root table? |
False
|
is_virtual_node
|
bool
|
was this table created to store multiple root elements? |
False
|
Returns:
Type | Description |
---|---|
Union[DataModelTableReused, DataModelTableDuplicated]
|
A data model instance. |
Source code in xml2db/model.py
_parse_tree(parent_node, nodes_path=None)
¶
Parse a node of an XML schema recursively and create a target data model without any simplification
We parse the XSD tree recursively to create for each node (basically a complex type in the XSD) an equivalent DataModelTable (which represents a table in the target data model). By default, tables are named after the first field name of this type. This is because we hope that fields names will be 'better' than actual type names. To be on the safe side, we need to make our new table names unique in the event where different XSD types are used with the same field names somewhere in the data model. Actual XSD types names and our table names are bijective. This step is fairly straightforward, as we create DataModelTable objects recursively along the XSD tree, and populate them with appropriate columns and relations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent_node
|
XsdElement
|
the current XSD node being parsed |
required |
nodes_path
|
list
|
a list of nodes types from the root node |
None
|
Source code in xml2db/model.py
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 |
|
_repr_tree(parent_table)
¶
Build a text representation of the data model tree
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parent_table
|
Union[DataModelTableReused, DataModelTableDuplicated]
|
the current data model table object |
required |
Source code in xml2db/model.py
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:
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: