architxt.database.loader.sql#

Functions

build_group(table, row)

Create a tree representation for a table with its columns and data.

build_relation(left_table, right_table, ...)

Handle the current data for a table and its referred table.

get_cycle_tables(tables)

Retrieve tables that are part of a cycle in the database relations.

get_root_tables(tables)

Retrieve the root tables in the database by identifying tables that are not referenced as foreign keys.

is_association_table(table)

Check if a table is a many-to-many association table.

parse_association_table(table, row, *, conn)

Parse a row of an association table into trees.

parse_table(table, row, *, conn[, ...])

Parse a row of a table into trees.

read_database(conn, *[, ...])

Read the database instance as a tree.

read_table(table, *, conn[, ...])

Process the relations of a given table, retrieve data, and construct tree representations.

read_unreferenced_table(foreign_key, *, conn)

Process the relations of a table that is not referenced by any other tables.

architxt.database.loader.sql.build_group(table, row)[source]#

Create a tree representation for a table with its columns and data.

Parameters:
  • table (Table) – The table to process.

  • row (Row) – A row of the table.

Return type:

Tree

Returns:

A tree representing the table’s structure and data.

architxt.database.loader.sql.build_relation(left_table, right_table, left_row, right_row, node_data=None, name='')[source]#

Handle the current data for a table and its referred table.

Parameters:
  • left_table (Table) – The left table of the relation.

  • right_table (Table) – The right table of the relation.

  • left_row (Row) – The left table row of the relation.

  • right_row (Row) – The right table row of the relation.

  • node_data (Optional[dict[str, Any]]) – Dictionary containing relation data.

  • name (str) – Name of the relation, if not set, it will be automatically generated.

Return type:

Tree

Returns:

The tree of the relation.

architxt.database.loader.sql.get_cycle_tables(tables)[source]#

Retrieve tables that are part of a cycle in the database relations.

If multiple tables are in a cycle, only the one with the maximum foreign keys is returned.

Parameters:

tables (set[Table]) – A collection of tables to analyze.

Return type:

set[Table]

Returns:

A set of tables that are part of a cycle but should be considered as root.

architxt.database.loader.sql.get_root_tables(tables)[source]#

Retrieve the root tables in the database by identifying tables that are not referenced as foreign keys.

Parameters:

tables (set[Table]) – A collection of tables to analyze.

Return type:

set[Table]

Returns:

A set of root table.

architxt.database.loader.sql.is_association_table(table)[source]#

Check if a table is a many-to-many association table.

Parameters:

table (Table) – The table to check.

Return type:

bool

Returns:

True if the tale is a relation else False.

architxt.database.loader.sql.parse_association_table(table, row, *, conn)[source]#

Parse a row of an association table into trees.

The table is discarded and represented only as a relation between the two linked tables.

Parameters:
  • table (Table) – The table to process.

  • row (Row) – A row of the table.

  • conn (Connection) – SQLAlchemy connection.

Yield:

Trees representing the relations and data for the table.

Return type:

Generator[Tree, None, None]

architxt.database.loader.sql.parse_table(table, row, *, conn, _visited_links=None)[source]#

Parse a row of a table into trees.

Parameters:
  • table (Table) – The table to process.

  • row (Row) – A row of the table.

  • conn (Connection) – SQLAlchemy connection.

  • _visited_links (Optional[set[ForeignKey]]) – Set of visited relations to avoid cycles.

Yield:

Trees representing the relations and data for the table.

Return type:

Generator[Tree, None, None]

architxt.database.loader.sql.read_database(conn, *, simplify_association=True, search_all_instances=False, sample=0)[source]#

Read the database instance as a tree.

Parameters:
  • conn (Connection) – SQLAlchemy connection to the database.

  • simplify_association (bool) – Flag to simplify non attributed association tables.

  • search_all_instances (bool) – Flag to search for all instances of database.

  • sample (int) – Number of samples for each table to get.

Return type:

Generator[Tree, None, None]

Returns:

A list of trees representing the database.

architxt.database.loader.sql.read_table(table, *, conn, simplify_association=False, sample=0)[source]#

Process the relations of a given table, retrieve data, and construct tree representations.

Parameters:
  • table (Table) – The table to process.

  • conn (Connection) – SQLAlchemy connection.

  • simplify_association (bool) – Flag to simplify non attributed association tables.

  • sample (int) – Number of samples for each table to get.

Return type:

Generator[Tree, None, None]

Returns:

A list of trees representing the relations and data for the table.

architxt.database.loader.sql.read_unreferenced_table(foreign_key, *, conn, sample=0, _visited_links=None)[source]#

Process the relations of a table that is not referenced by any other tables.

Parameters:
  • foreign_key (ForeignKey) – The foreign key to process.

  • conn (Connection) – SQLAlchemy connection.

  • sample (int) – Number of samples for each table to get.

  • _visited_links (Optional[set[ForeignKey]]) – Set of visited relations to avoid cycles.

Return type:

Generator[Tree, None, None]

Returns:

A list of trees representing the relations and data for the table.