def main(ledger_name=Constants.LEDGER_NAME): """ Scan for all the documents in a table. """ try: with create_qldb_driver(ledger_name) as driver: # Scan all the tables and print their documents. tables = driver.list_tables() for table in tables: cursor = scan_table(driver, table) logger.info('Scan successful!') print_result(cursor) except Exception as e: logger.exception('Unable to scan tables.') raise e
def previous_primary_owners(driver, vin): """ Find previous primary owners for the given VIN in a single transaction. In this example, query the `VehicleRegistration` history table to find all previous primary owners for a VIN. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type vin: str :param vin: VIN to find previous primary owners for. """ person_ids = driver.execute_lambda(lambda executor: get_document_ids( executor, Constants.VEHICLE_REGISTRATION_TABLE_NAME, 'VIN', vin)) todays_date = datetime.utcnow() - timedelta(seconds=1) three_months_ago = todays_date - timedelta(days=90) query = 'SELECT data.Owners.PrimaryOwner, metadata.version FROM history({}, {}, {}) AS h WHERE h.metadata.id = ?'.\ format(Constants.VEHICLE_REGISTRATION_TABLE_NAME, format_date_time(three_months_ago), format_date_time(todays_date)) for ids in person_ids: logger.info( "Querying the 'VehicleRegistration' table's history using VIN: {}." .format(vin)) cursor = driver.execute_lambda( lambda executor: executor.execute_statement(query, ids)) if not (print_result(cursor)) > 0: logger.info( 'No modification history found within the given time frame for document ID: {}' .format(ids))
def find_vehicles_for_owner(transaction_executor, gov_id): """ Find vehicles registered under a driver using their government ID. :type transaction_executor: :py:class:`pyqldb.session.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type gov_id: str :param gov_id: The owner's government ID. """ document_ids = get_document_ids(transaction_executor, Constants.PERSON_TABLE_NAME, 'GovId', gov_id) query = "SELECT Vehicle FROM Vehicle INNER JOIN VehicleRegistration AS r " \ "ON Vehicle.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?" for ids in document_ids: cursor = transaction_executor.execute_statement(query, [ids]) logger.info('List of Vehicles for owner with GovId: {}...'.format(gov_id)) print_result(cursor)
def add_secondary_owner_for_vin(transaction_executor, vin, parameter): """ Add a secondary owner into `VehicleRegistration` table for a particular VIN. :type transaction_executor: :py:class:`pyqldb.execution.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type vin: str :param vin: VIN of the vehicle to add a secondary owner for. :type parameter: :py:class:`amazon.ion.simple_types.IonPyValue` :param parameter: The Ion value or Python native type that is convertible to Ion for filling in parameters of the statement. """ logger.info( 'Inserting secondary owner for vehicle with VIN: {}...'.format(vin)) statement = "FROM VehicleRegistration AS v WHERE v.VIN = '{}' INSERT INTO v.Owners.SecondaryOwners VALUE ?"\ .format(vin) cursor = transaction_executor.execute_statement(statement, parameter) logger.info( 'VehicleRegistration Document IDs which had secondary owners added: ') print_result(cursor)
def add_secondary_owner_for_vin(transaction_executor, vin, parameters): """ Add a secondary owner into `VehicleRegistration` table for a particular VIN. :type transaction_executor: :py:class:`pyqldb.session.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type vin: str :param vin: VIN of the vehicle to add a secondary owner for. :type parameters: list :param parameters: list of parameters in Ion format. """ logger.info( 'Inserting secondary owner for vehicle with VIN: {}...'.format(vin)) statement = "FROM VehicleRegistration AS v WHERE v.VIN = '{}' INSERT INTO v.Owners.SecondaryOwners VALUE ?".format( vin) cursor = transaction_executor.execute_statement(statement, parameters) logger.info( 'VehicleRegistration Document IDs which had secondary owners added: ') print_result(cursor)
def find_vehicles_for_owner(driver, gov_id): """ Find vehicles registered under a driver using their government ID. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type gov_id: str :param gov_id: The owner's government ID. """ document_ids = driver.execute_lambda(lambda executor: get_document_ids( executor, Constants.PERSON_TABLE_NAME, 'GovId', gov_id)) query = "SELECT Vehicle FROM Vehicle INNER JOIN VehicleRegistration AS r " \ "ON Vehicle.VIN = r.VIN WHERE r.Owners.PrimaryOwner.PersonId = ?" for ids in document_ids: cursor = driver.execute_lambda( lambda executor: executor.execute_statement(query, ids)) logger.info( 'List of Vehicles for owner with GovId: {}...'.format(gov_id)) print_result(cursor)
def add_secondary_owner_for_vin(driver, vin, parameter): """ Add a secondary owner into `VehicleRegistration` table for a particular VIN. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type vin: str :param vin: VIN of the vehicle to add a secondary owner for. :type parameter: :py:class:`amazon.ion.simple_types.IonPyValue` :param parameter: The Ion value or Python native type that is convertible to Ion for filling in parameters of the statement. """ logger.info( 'Inserting secondary owner for vehicle with VIN: {}...'.format(vin)) statement = "FROM VehicleRegistration AS v WHERE v.VIN = '{}' INSERT INTO v.Owners.SecondaryOwners VALUE ?"\ .format(vin) cursor = driver.execute_lambda( lambda executor: executor.execute_statement(statement, parameter)) logger.info( 'VehicleRegistration Document IDs which had secondary owners added: ') print_result(cursor)
logger.info('Scanning {}...'.format(table_name)) query = 'SELECT * FROM {}'.format(table_name) return transaction_executor.execute_statement(query) if __name__ == '__main__': try: with create_qldb_session() as session: cursor = session.execute_lambda(lambda executor: scan_table(executor, 'Person'), lambda retry_attempt: logger.info('Retrying due to OCC conflict...')) n = next(cursor).get('Id') m = next(cursor).get('Salvaconducto') l = next(cursor).get('Estado') print(n) print_result(cursor) except Exception: logger.exception('Unable to scan tables.') class Datos(Resource): def get(self): return {n,m,l} api.add_resource(Datos, '/datos') # Route_1 if __name__ == '__main__': app.run(port=5002)