Example #1
0
    def find_dataset(self, options=None):
        """ Obtém dataset de acordo com os parâmetros informados """
        if QueryBuilder.catch_injection(options):
            raise ValueError('SQL reserved words not allowed!')
        str_where = ''
        if options.get('where') is not None:
            str_where = ' WHERE ' + self.build_filter_string(
                options.get('where'))
        str_group = ''
        nu_cats = options['categorias']
        if options.get('pivot'):
            nu_cats = nu_cats + options.get('pivot')
        if options.get('agregacao', False):
            str_group = QueryBuilder.build_grouping_string(
                nu_cats, options['agregacao'])
        str_categorias = self.build_categorias(nu_cats, options)
        str_limit = ''
        if options.get('limit'):
            str_limit = f'LIMIT {options.get("limit")}'
        str_offset = ''
        if options.get('offset') is not None:
            str_offset = f'OFFSET {options.get("offset")}'
        if 'theme' not in options:
            options['theme'] = 'MAIN'

        query = self.get_named_query('QRY_FIND_DATASET').format(
            str_categorias, self.get_table_name(options.get('theme')),
            str_where, str_group,
            self.build_order_string(options.get('ordenacao')), str_limit,
            str_offset)
        return self.fetch_data(query)
Example #2
0
 def exclude_from_partition(self, categorias, agregacoes, options=None):
     """ Remove do partition as categorias não geradas pela agregação """
     partitions = self.get_default_partitioning(options).split(", ")
     groups = QueryBuilder.build_grouping_string(
         categorias, agregacoes).replace('GROUP BY ', '').split(", ")
     result = []
     for partition in partitions:
         if partition in groups:
             result.append(partition)
     return ", ".join(result)
Example #3
0
 def test_with_distinct(self):
     ''' Retorna exceção quando não há agregação para agrupar '''
     cats = ['nm_indicador-nome', 'nu_competencia']
     agrs = ['SUM', 'MAX', 'DISTINCT']
     result = QueryBuilder.build_grouping_string(cats, agrs)
     self.assertEqual(result, '')
Example #4
0
 def test_renamed_cats(self):
     ''' Retorna exceção quando não há agregação para agrupar '''
     cats = ['nm_indicador-nome', 'nu_competencia']
     agrs = ['SUM', 'MAX']
     result = QueryBuilder.build_grouping_string(cats, agrs)
     self.assertEqual(result, 'GROUP BY nm_indicador, nu_competencia')