예제 #1
0
def create_columns(username, selected_database, table):
    screen = curses.initscr()
    screen.clear()
    screen.keypad(1)

    curses.echo()
    screen.addstr(5, 5, 'Enter Field Name:')
    screen.addstr(8, 5, 'Enter Type (provide the necessary lengths and decimals):')
    field_name = screen.getstr(6, 5, 15)
    field_type = screen.getstr(9, 5, 15)

    screen.addstr(11, 5, 'Is this a primary key? Enter Y or N: ')
    field_primary_key = screen.getch()

    screen.addstr(14, 5, 'Can this value be NULL? Enter Y or N: ')
    field_null = screen.getch()

    if field_primary_key == ord('y') and field_null == ord('y'):
        field_option = 'PRIMARY KEY'

    if field_primary_key == ord('y') and field_null == ord('n'):
        field_option = 'PRIMARY KEY, NOT NULL'

    if field_primary_key == ord('n') and field_null == ord('n'):
        field_option = 'NOT NULL'

    if field_primary_key == ord('n') and field_null == ord('y'):
        field_option = ''


    # for testing
    # screen.addstr(15, 5, field_option)
    # screen.refresh()

    """
    HERE RUN THE ALTER TABLE COMMAND FOR THE DATABASE, WITH THE FOLLOWING FIELDS:
    field_name, field_type, field_option
    """
    query = "ALTER TABLE %s ADD %s %s %s %s" % (field_name, field_type, field_primary_key, field_null, field_option)
    conn = psql_db.connect()
    cursor = conn.cursor()
    cursor.execute(query)

    screen.addstr(17, 5, 'Do you have more columns to add? Enter Y or N: ')
    response = screen.getch()

    if response == ord('y') or response == ord('Y'):
        create_columns(username, selected_database, table)
    if response == ord('n') or response == ord('N'):
        table_menu(username, selected_database, table)
예제 #2
0
def edit_table_view_selected_database(username, database):
    """
    This is a poorly named function
    User is brought here if they selected that they wanted to edit an existing database
    It pulls all the tables that are in that database and that are associated the
    username
    User then selected a table from that list
    This table, the username, and database is sent to tableOptions.py tableOptions()
    :param username:
    :param selected_database:
    :return:
    """
    screen = curses.initscr()
    screen.clear()
    screen.keypad(1)

    """
    Here we query all the tables associated with this user and this database
    And put all the names as elements of 'tables' variable (instead of the hardcoded testing elements)
    Also, query the count of tables and save that as 'tables_count'
    """
    tables = ['name1', 'name2', 'name3', 'name4']
    tables_count = 4
    selection = -1
    option = 0


    screen.addstr(3, 5, 'Choose a table to edit/view', curses.A_BOLD | curses.A_UNDERLINE)

    selected_table = None

    while selection < 0:
        y = 5
        i = 0
        choices = [0] * (tables_count + 1)
        choices[option] = curses.A_REVERSE

        screen.addstr(1, 1, 'BACK TO MAIN MENU', curses.A_UNDERLINE | choices[0])

        for name in tables:
            screen.addstr(y, 5, name, choices[i])
            i += 1
            y += 3

        screen.refresh()

        action = screen.getch()

        if action == curses.KEY_UP:
            option = (option - 1) % tables_count
        elif action == curses.KEY_DOWN:
            option = (option + 1) % tables_count
        elif action == ord('\n'):
            selection = option

        if selection == 0:
            dashboard_database.database_menu(username)

        selected_table = tables[selection]

    table_menu(username, database, selected_table)