CP363: Assignment 4 - Fall 2022

Due 11:00 PM, Monday, March 27, 2023

General Assignment Notes

When writing and submitting your assignments follow these requirements:

Marks will be deducted from any questions where these requirements are not met.


WARNING

Follow the assignment instructions to the letter (e.g. name the function module functions.py), as this assignment is auto-graded. If your Eclipse project is not defined correctly, the auto-grading fails, and your assignment will be given a mark of 0.

Links

Pages of use for this assignment:

These questions use the DCRIS database.

These functions return lists of tuples containing the results of the various database calls. How you display these results is up to you. Do not call print from within the functions. Do not close the database from within the functions. It is the responsibility of whatever opens the database to close the database.

We suggest you use Pythons triple quotes to make long SQL statements more readable:

      
sql = """SELECT ...
FROM ...
JOIN ...
ON ...
WHERE ...
ORDER BY ..."""

    

These functions use the information_schema to extract metadata from the DCRIS database. All table references must use information_schema as a prefix. For example, to query the TABLES table, you must refer to it as information_schema.TABLES.

Further, we want only DCRIS information, so all queries must use only the 'dcris' TABLE_SCHEMA or CONSTRAINT_SCHEMA as parameters to the functions.


  1. Write and test the following function:

    
    def get_table_info(cursor, tableSchema, tableName=None):
        """
        -------------------------------------------------------
        Queries information_schema.TABLES for metadata.
        Use: rows = get_table_info(cursor, tableSchema)
        Use: rows = get_table_info(cursor, tableSchema, tableName=v1)
        -------------------------------------------------------
        Parameters:
            cursor - a database cursor (cursor)
            tableSchema - the database table schema (str)
            tableName - a table name (str)
        Returns:
            rows - (list of the TABLE_NAME, TABLE_TYPE, TABLE_ROWS,
                and TABLE_COMMENT fields data)
                if tableName is not None:
                    rows containing tableName
                else:
                    all TABLES rows
                Sorted by TABLE_NAME, TABLE_TYPE
        -------------------------------------------------------
        """
    

    Add this function to the PyDev module functions.py. Test it from t01.py.

    Examples:

    TABLE_NAME TABLE_TYPE TABLE_ROWS TABLE_COMMENT
    broad BASE TABLE 19 Contains broad categories of security expertise.

    (Do not return the headers.)


  2. Write and test the following function:

    
    def get_column_info(cursor, tableSchema, tableName=None):
        """
        -------------------------------------------------------
        Queries information_schema.COLUMNS for metadata.
        Use: rows = get_column_info(cursor, tableSchema)
        Use: rows = get_column_info(cursor, tableSchema, tableName=v1)
        -------------------------------------------------------
        Parameters:
            cursor - a database cursor (cursor)
            tableSchema - the database table schema (str)
            tableName - a table name (str)
        Returns:
            rows - (list of the TABLE_NAME, COLUMN_NAME, IS_NULLABLE,
                and DATA_TYPE fields data)
                if tableName is not None:
                    rows containing tableName
                else:
                    all COLUMNS rows
                Sorted by TABLE_NAME, COLUMN_NAME
        -------------------------------------------------------
        """
    

    Add this function to the PyDev module functions.py. Test it from t02.py.

    Examples:

    TABLE_NAME COLUMN_NAME IS_NULLABLE DATA_TYPE
    member memberTitle YES varchar

    (Do not return the headers.)


  3. Write and test the following function:

    
    def get_constraint_info(cursor, tableSchema, constraintType=None):
        """
        -------------------------------------------------------
        Queries information_schema.TABLE_CONSTRAINTS for metadata.
        Use: rows = get_constraint_info(cursor, tableSchema)
        Use: rows = get_constraint_info(cursor, tableSchema, constraintType=v1)
        -------------------------------------------------------
        Parameters:
            cursor - a database cursor (cursor)
            tableSchema - the database table schema (str)
            constraintType - a database constraint type (str)
        Returns:
            rows - (list of the CONSTRAINT_NAME, TABLE_NAME,
                and CONSTRAINT_TYPE fields data)
                if constraintType is not None:
                    rows containing constraintType
                else:
                    all TABLE_CONSTRAINTS rows
                Sorted by CONSTRAINT_NAME, TABLE_NAME
        -------------------------------------------------------
        """
    

    Add this function to the PyDev module functions.py. Test it from t03.py.

    Examples:

    CONSTRAINT_NAME TABLE_NAME CONSTRAINT_TYPE
    fkMemberBroadBroad memberBroad FOREIGN KEY

    (Do not return the headers.)


  4. Write and test the following function:

    
    def get_foreign_key_info(cursor, constraintSchema, tableName=None, refTableName=None):
        """
        -------------------------------------------------------
        Queries information_schema.REFERENTIAL_CONSTRAINTS for metadata.
        Use: rows = get_foreign_key_info(cursor, constraintSchema)
        Use: rows = get_foreign_key_info(cursor, constraintSchema, tableName=v1)
        Use: rows = get_foreign_key_info(cursor, constraintSchema, refTableName=v2)
        Use: rows = get_foreign_key_info(cursor, constraintSchema, tableName=v1, refTableName=v2)
        -------------------------------------------------------
        Parameters:
            cursor - a database cursor (cursor)
            constraintSchema - the database constraint schema (str)
            tableName - a table name (str)
            refTableName - a table name (str)
        Returns:
            rows - (list of the CONSTRAINT_NAME, UPDATE_RULE, DELETE_RULE,
                TABLE_NAME, and REFERENCED_TABLE_NAME fields data)
                if tableName and/or refTableName are not None:
                    rows containing tableName and/or refTableName
                else:
                    all REFERENTIAL_CONSTRAINTS rows
                Sorted by CONSTRAINT_NAME, TABLE_NAME, REFERENCED_TABLE_NAME
        -------------------------------------------------------
        """
    

    Add this function to the PyDev module functions.py. Test it from t04.py.

    Examples:

    CONSTRAINT_NAME UPDATE_RULE DELETE_RULE TABLE_NAME REFERENCED_TABLE_NAME
    fkPubMember CASCADE CASCADE pub member

    (Do not return the headers.)


  5. Write and test the following function:

    
    def get_key_info(cursor, constraintSchema, tableName=None, refTableName=None):
        """
        -------------------------------------------------------
        Queries information_schema.KEY_COLUMN_USAGE for metadata.
        Use: rows = get_key_info(cursor, constraintSchema)
        Use: rows = get_key_info(cursor, constraintSchema, tableName=v1)
        Use: rows = get_key_info(cursor, constraintSchema, refTableName=v2)
        Use: rows = get_key_info(cursor, constraintSchema, tableName=v1, refTableName=v2)
        -------------------------------------------------------
        Parameters:
            cursor - a database cursor (cursor)
            constraintSchema - the database constraint schema (str)
            tableName - a table name (str)
            refTableName - a table name (str)
        Returns:
            rows - (list of the CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME,
                REFERENCED_TABLE_NAME, and REFERENCED_COLUMN_NAME fields data)
                if tableName and/or refTableName are not None:
                    rows containing tableName and/or refTableName
                else:
                    all KEY_COLUMN_USAGE rows
                Sorted by TABLE_NAME, COLUMN_NAME
        -------------------------------------------------------
        """
    

    Add this function to the PyDev module functions.py. Test it from t05.py.

    Examples:

    CONSTRAINT_NAME TABLE_NAME COLUMN_NAME REFERENCED_TABLE_NAME REFERENCED_COLUMN_NAME
    fkPubType pub pubPubType pubType pubType

    (Do not return the headers.)