示例#1
0
def test_get_all_data():
    global test_db_file
    filename = os.path.join(os.getcwd(), test_db_file)
    query1 = 'Select TotalFrameCount, TotalOctetCount, \
              SigFrameCount from AnalyzerPortResults'
    data = sql_utils.get_all_data([filename], query1)
    assert data is not None
    column_names = data[ProviderConst.COLUMN_DISPLAY_NAMES]
    rows = data[ProviderConst.ROW]
    assert len(column_names) == 3
    assert len(rows) == 2
    assert len(rows[0]) == 3
    assert len(rows[1]) == 3
    assert column_names[0] == 'TotalFrameCount'
    assert column_names[1] == 'TotalOctetCount'
    assert column_names[2] == 'SigFrameCount'
    assert rows[0][0] == 1151261
    assert rows[1][2] == 1239945

    query2 = 'Select TotalFrameCount as "Tx Frame Count", ' +\
        'TotalOctetCount as "Total Octet Count", ' +\
        'SigFrameCount as "Sig Frame Count" from AnalyzerPortResults'
    data = sql_utils.get_all_data([filename], query2)
    assert data is not None
    column_names = data[ProviderConst.COLUMN_DISPLAY_NAMES]
    rows = data[ProviderConst.ROW]
    assert len(column_names) == 3
    assert len(rows) == 2
    assert len(rows[0]) == 3
    assert len(rows[1]) == 3
    assert column_names[0] == 'Tx Frame Count'
    assert column_names[1] == 'Total Octet Count'
    assert column_names[2] == 'Sig Frame Count'
    assert rows[0][0] == 1151261
    assert rows[1][2] == 1239945
def get_data_from_query(db_file_list, query):
    result_data = sql_utils.get_all_data(db_file_list, query)
    rows = result_data[ProviderConst.ROW]
    row_data = []
    for row in rows:
        row_data.append(row[0] if len(row) == 1 else (row[0], row[1], row[2]))
    return row_data
def expand_embedded_sql(value, result_file_list):
    global SQL_RE
    if not has_embedded_sql(value):
        return value
    result = ""
    for expr in SQL_RE.split(value):
        if SQL_RE.match(expr):
            # Remove leading braces
            sql = expr.strip('{}')
            result_data = sql_utils.get_all_data(result_file_list, sql)
            row_data = []
            for row in result_data[ProviderConst.ROW]:
                if len(row) == 1:
                    row_data.extend(row)
                else:
                    row_data.append(row)
            if len(row_data) == 1:
                if type(row_data[0]) != type(list()):
                    result += json.dumps(row_data[0])
                elif len(row_data[0]) == 1:
                    result += json.dumps(row_data[0][0])
                else:
                    result += json.dumps(row_data[0])
            else:
                result += json.dumps(row_data)
        else:
            result += expr
    return result
def get_data_from_query(db_file_list, query):
    result_data = sql_utils.get_all_data(db_file_list, query)
    rows = result_data[ProviderConst.ROW]
    row_data = []
    for row in rows:
        row_data.append(row[0])
    logger.debug("    row_data in get_data_from_query: " + str(row_data))
    return row_data
示例#5
0
def test_get_all_data_from_multi_cmd_sql():
    global test_db_file
    filename = os.path.join(os.getcwd(), test_db_file)
    query1 = 'Create Temp Table A (id int);' + \
        'Insert Into A Values (12);' + \
        'Insert Into A Values (51);' + \
        'Insert Into A Values (46);' + \
        'Insert Into A Values (31);' + \
        'Insert Into A Values (78);' + \
        'Select id From A;'
    data = sql_utils.get_all_data([filename], query1)
    assert data is not None
    rows = data[ProviderConst.ROW]
    assert len(rows) == 5
def get_sql_data(provider_data, result_file_list):
    this_cmd = get_this_cmd()
    data = provider_data[ProviderConst.DATA]
    sql_param_dict = {ChartConst.TITLE: this_cmd.Get('Title'),
                      ChartConst.X_CAT: this_cmd.GetCollection('XAxisCategories'),
                      ChartConst.X_LAB: this_cmd.Get('XAxisTitle'),
                      ChartConst.Y_CAT: this_cmd.GetCollection('YAxisCategories'),
                      ChartConst.Y_LAB: this_cmd.Get('YAxisTitle'),
                      ChartConst.MOD_LIST: this_cmd.Get('TemplateModifier')}
    for key, value in sql_param_dict.iteritems():
        if type(value) is str:
            if has_embedded_sql(value):
                data['{0}'.format(key)] = expand_embedded_sql(value,
                                                              result_file_list)
            elif is_sql_query(value):
                result_data = sql_utils.get_all_data(result_file_list, value)
                row_data = result_data[ProviderConst.ROW]
                data['{0}'.format(key)] = {ChartConst.TEXT: str(row_data[0][0])}
        else:
            result_list = []
            for v in value:
                if has_embedded_sql(v):
                    v = expand_embedded_sql(v, result_file_list)
                    result_list.append(v)
                elif is_sql_query(v):
                    row_data = []
                    result_data = sql_utils.get_all_data(result_file_list, v)
                    for row in result_data[ProviderConst.ROW]:
                        row_data.append(str(row[0]))
                    result_list.extend(row_data)
                else:
                    result_list.append(v)
            if (key == ChartConst.X_CAT or key == ChartConst.Y_CAT) and \
               len(result_list) == 0:
                result_list = None
            data['{0}'.format(key)] = result_list
def get_data_from_query(db_file_list, query):
    if not db_file_list or not query:
        return []
    result_data = sql_utils.get_all_data(db_file_list, query)
    rows = result_data[ProviderConst.ROW]
    return rows