def build_categorias(self, categorias, options): ''' Constrói a parte dos atributos selecionados na query ''' if not QueryBuilder.check_params(options, ['categorias']): raise ValueError('Invalid Categories - required') categorias = QueryBuilder.transform_categorias(categorias) prepended_aggr = QueryBuilder.prepend_aggregations(options.get('agregacao')) str_calcs = '' if QueryBuilder.check_params(options, ['calcs']): calcs_options = options.copy() calcs_options['categorias'] = categorias str_calcs += self.build_std_calcs(calcs_options) if QueryBuilder.check_params(options, ['agregacao', 'valor']): tmp_cats = self.combine_val_aggr(options.get('valor'), options.get('agregacao')) if not isinstance(tmp_cats, list): categorias += tmp_cats.split(", ") else: categorias += tmp_cats elif (not QueryBuilder.check_params(options, ['agregacao']) and QueryBuilder.check_params(options, ['valor'])): categorias += options['valor'] elif (QueryBuilder.check_params(options, ['agregacao']) and not QueryBuilder.check_params(options, ['valor'])): categorias += self.build_generic_agr_array(options['agregacao']) if (QueryBuilder.validate_field_array(categorias) and QueryBuilder.validate_field_array(prepended_aggr)): if 'calcs' not in options or options['calcs'] is None or str_calcs == '': return ' '.join(prepended_aggr) + ' ' + ', '.join(categorias) return ' '.join(prepended_aggr) + ' ' + ', '.join(categorias) + ', ' + str_calcs raise ValueError('Invalid attributes')
def build_joined_categorias(self, categorias, valor=None, agregacao=None, joined=None): ''' Constrói a parte dos atributos selecionados na query ''' if categorias is None or not categorias: raise ValueError('Invalid Categories - required') str_cat = [] suffix = self.get_join_suffix(joined) str_cat += QueryBuilder.transform_joined_categorias(categorias, suffix) if agregacao is not None and valor is not None: str_cat += self.combine_val_aggr(valor, agregacao, suffix) elif agregacao is not None and valor is None: str_cat += self.build_generic_agr_array(agregacao) if QueryBuilder.validate_field_array(str_cat): return ', '.join(str_cat) raise ValueError('Invalid attributes')
def build_order_string(ordenacao=None): ''' Prepara ordenação ''' if ordenacao is None or not ordenacao: return '' if not QueryBuilder.validate_field_array(ordenacao): raise ValueError('Invalid aggregation') order_str = '' for field in ordenacao: if order_str == '': order_str += 'ORDER BY ' else: order_str += ', ' if "-" in field: order_str += field[1:] + ' DESC' else: order_str += field return order_str
def test_validate_positive(self): ''' Verifica positivo para separador de SQL ''' fields = ['vl_indicador', 'cd_ibge;select'] validation = QueryBuilder.validate_field_array(fields) self.assertEqual(validation, False)
def test_validate_negative(self): ''' Verifica negativo para separador de SQL ''' fields = ['vl_indicador', 'cd_ibge'] validation = QueryBuilder.validate_field_array(fields) self.assertEqual(validation, True)