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 register_new_person(driver, person): """ Register a new person in QLDB if not already registered. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type person: dict :param person: The person to register. :rtype: str :return: The person ID. """ gov_id = person['GovId'] if driver.execute_lambda( lambda executor: person_already_exists(executor, gov_id)): logger.info('Person with this GovId already exists.') result = driver.execute_lambda(lambda executor: get_document_ids( executor, Constants.PERSON_TABLE_NAME, 'GovId', gov_id)) result = result[0] else: result = insert_documents(driver, Constants.PERSON_TABLE_NAME, [person]) result = result[0] return result
def get_document_id_by_gov_id(transaction_executor, government_id): """ Find a driver's person ID using the given government ID. :type transaction_executor: :py:class:`pyqldb.execution.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type government_id: str :param government_id: A driver's government ID. :rtype: list :return: A list of document IDs. """ logger.info( "Finding secondary owner's person ID using given government ID: {}.". format(government_id)) return get_document_ids(transaction_executor, Constants.PERSON_TABLE_NAME, 'GovId', government_id)
def register_new_driver(transaction_executor, driver): """ Register a new driver in QLDB if not already registered. :type transaction_executor: :py:class:`pyqldb.execution.executor.Executor` :param transaction_executor: An Executor object allowing for execution of statements within a transaction. :type driver: dict :param driver: The driver's license to register. """ gov_id = driver['GovId'] if person_already_exists(transaction_executor, gov_id): logger.info('Person with this GovId already exists.') result = next(get_document_ids(transaction_executor, Constants.PERSON_TABLE_NAME, 'GovId', gov_id)) else: result = insert_documents(transaction_executor, Constants.PERSON_TABLE_NAME, [driver]) result = result[0] return result
def get_document_id_by_gov_id(driver, government_id): """ Find a driver's person ID using the given government ID. :type driver: :py:class:`pyqldb.driver.qldb_driver.QldbDriver` :param driver: An instance of the QldbDriver class. :type government_id: str :param government_id: A driver's government ID. :rtype: list :return: A list of document IDs. """ logger.info( "Finding secondary owner's person ID using given government ID: {}.". format(government_id)) return driver.execute_lambda(lambda executor: get_document_ids( executor, Constants.PERSON_TABLE_NAME, 'GovId', government_id))
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 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)