示例#1
0
def order_cols(line, table_list):
    '''
    Execute 'ORDENATELOS X' command
    :param line: line without the 'ORDENATELOS X'
    :param table_list: a dict {col_name: [entries]}
    :return: a dict {col_name: [entries]}
    '''

    def read_line(l):
        col_name, dir_str = l.split(' PA ')
        if 'RIBA' in dir_str:
            # Sort ascending
            asc = True
        elif 'BAJO' in dir_str:
            # Sort descending
            asc = False
        else:
            raise ValueError('direction string: {0}'.format(dir_str))
        return col_name.strip(), asc
    col_name, asc = read_line(line)
    # Get table using col_name
    table = [tb for tb in table_list if man.has_col(tb, col_name)][0]
    # Get the list of sorted entry indexes for col_name
    index = man.get_sorted_indexes(table[col_name], asc)
    ordered_tb = {col_name: man.order_col(entries, index)
                  for col_name, entries in table.items()}
    tb_list = [tb for tb in table_list if not man.has_col(tb, col_name)]
    tb_list.append(ordered_tb)
    return tb_list
示例#2
0
def eval_cond(cond, tables):
    '''
    Evaluates a single condition then returns a dict of the filtered indexes of
    columns involved in the condition
    :param cond: a single conditional expression
    :param tables: a dict of tables {tname: tb}
    :return: a dict {col_name: [index that fulfills condition]}
    '''
    if ' EN ' in cond:
        col_name, inner_query = cond.split(' EN ', 1)
        # On filter column by inner query result
        tname, tb = [(tname, tb) for tname, tb in tables.items()
                     if man.has_col(tb, col_name)][0]
        col_entries = tb[col_name]
        return filter_by_query(tname, col_name,
                               # inner query take away the brackets
                               col_entries, inner_query[1:-1], tables)
    if 'EXISTE ' in cond:
        inner_query = cond.replace('EXISTE (', '', 1)[:-1]
        if not exist_condition(inner_query, tables):
            # if return False, terminate the current query, otherwise continue
            raise hpr.TerminateQueryError()
        else:
            return True
    clauses = split_clauses(cond)
    var1, oper, var2 = clauses
    if oper == 'ENTRE':
        # filter by range
        # Get table, assumes unique column name
        tname, tb = [(tname, tb) for tname, tb in tables.items()
                     if man.has_col(tb, var1)][0]
        col_entries = tb[var1]
        start, end = var2.strip(' (').strip(') ').split(' Y ')
        return filter_by_range(tname, col_entries, int(start), int(end))
    else:
        if man.is_col(tables, var1) and man.is_col(tables, var2):
            # filter by cols
            tname1, tb1 = [(tname, tb) for tname, tb in tables.items()
                           if man.has_col(tb, var1)][0]
            tname2, tb2 = [(tname, tb) for tname, tb in tables.items()
                           if man.has_col(tb, var2)][0]
            col_entries1 = tb1[var1]
            col_entries2 = tb2[var2]
            return filter_by_col(tname1, col_entries1, tname2, col_entries2)
        elif man.is_col(tables, var1):
            # filter by val
            # Get table, assumes unique column name
            tname, tb = [(tname, tb) for tname, tb in tables.items()
                         if man.has_col(tb, var1)][0]
            col_entries = tb[var1]
            return filter_by_val(tname, col_entries, oper, hpr.parse_val(var2))
        else:
            # Get table, assumes unique column name
            tname, tb = [(tname, tb) for tname, tb in tables.items()
                         if man.has_col(tb, var2)][0]
            col_entries = tb[var2]
            return filter_by_val(tname, col_entries, oper, hpr.parse_val(var1))