Example #1
0
    def find_joined_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!')
        if options['joined'] is None:
            raise ValueError('Joined table is required')
        str_where = ''
        if options['where'] is not None:
            str_where = ' WHERE ' + self.build_filter_string(options['where'], options['joined'],
                                                             False)
        str_group = ''
        if options['agregacao'] is not None:
            str_group = self.build_joined_grouping_string(
                options['categorias'],
                options['agregacao'],
                options['joined']
            )
        if 'theme' not in options:
            options['theme'] = 'MAIN'
        str_categorias = self.build_joined_categorias(options['categorias'], options['valor'],
                                                      options['agregacao'], options['joined'])
        query = self.get_named_query('QRY_FIND_JOINED_DATASET').format(
            str_categorias,
            self.get_table_name(options.get('theme')), # FROM
            self.get_table_name(options.get('joined')), # JOIN
            self.get_join_condition(options['joined'], options['where']), # ON
            str_where, # WHERE
            str_group, # GROUP BY
            self.build_order_string(options.get('ordenacao')) # ORDER BY
        )

        return self.fetch_data(query)
Example #2
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 #3
0
 def test_validate_negative_null(self):
     ''' Verifica negativo para atributo nulo '''
     categorias = ["vl_indicador", "cd_ibge"]
     options = {"categorias": categorias, "valor": None}
     validation = QueryBuilder.catch_injection(options)
     self.assertEqual(validation, False)
Example #4
0
 def test_validate_negative(self):
     ''' Verifica negativo para palavras-chave de SQL '''
     categorias = ["vl_indicador", "cd_ibge"]
     options = {"categorias": categorias}
     validation = QueryBuilder.catch_injection(options)
     self.assertEqual(validation, False)
Example #5
0
 def test_validate_positive_complex(self):
     ''' Verifica positivo para palavras-chave de SQL '''
     categorias = ["vl_indicador", "cd_ibge-truncate"]
     options = {"categorias": categorias}
     validation = QueryBuilder.catch_injection(options)
     self.assertEqual(validation, True)