[docs]classTransactionalTable(Select):""" The TransactionalTable class implements the Select base class for querying a chain-specific table within the Data Solutions system. It provides the structure for constructing and executing SQL queries using Common Table Expressions. """
[docs]defget_table(self,chain,table)->None:""" Return a SQLAlchemy Table object for the given chain and table name. :param chain: The chain name. :type chain: str :param table: The table name. :type table: str :return: A SQLAlchemy Table object. :rtype: Table :raises ValueException: If the chain or table does not exist in the database. """get_schemas_url=(f"{BASE_URL['base_url']}/{TRANSACTIONAL_ENDPOINTS['get_schemas']}")self.schemas:dict=issue_request(get_schemas_url,self.api_key)["schema"]chain_schema=self.schemas.get(chain)ifnotchain_schema:raiseValueException(f"Chain '{chain}' does not exist in the database. Check your input. "f"If you believe the inputted chain should exist, contact Data Solutions.")# If the chain exists, get the schema for the tableforlayer_tablesinchain_schema.values():table_data=next((_tablefor_tableinlayer_tablesiftablein_table),None)ifnottable_data:raiseValueException(f"Table '{table}' does not exist in the database for chain '{chain}'. Check your input. "f"If you believe the inputted table should exist, contact Data Solutions.")# If the table exists, return the tablecolumns=table_data[table]["schema"]table_columns=[Column(col["column"],type_mapping[col["type"]],comment=col["description"],)forcolincolumns]returnTable(f"{chain}.{table}",MetaData(),*table_columns)
[docs]defexecute(self,options:dict={},)->Transactional:""" Execute the query and return a Transactional object. :param options: The options for the query. :type options: dict :return: Transactional object. :rtype: Transactional """returnTransactional(self.api_key).__call__(self.sql().replace('"',""),options=options,)