<commit_before>from ..dataStructures.query import *

class SQLGenerator():
    def get_SQL_code(self,query,old_query):
        if isinstance(query, ConjunctiveQuery):
            sql_gen = ConjunctiveQuerySQLGenerator()
        if isinstance(query, DisjunctiveQuery):
            sql_gen = DisjunctiveQuerySQLGenerator()
        return sql_gen.get_SQL_code(query,old_query)

class DisjunctiveQuerySQLGenerator():

    def get_SQL_code(self,query,old_query):
        sql_gen = SQLGenerator()
        old_queries = old_query.get_queries()
        new_queries = query.get_queries()
        old_new_pairs = [(new_queries[aux], old_queries[aux]) for aux in range(0,len(new_queries))]
        return ' UNION '.join(['(' + sql_gen.get_SQL_code(q, old_q)[:-1] + ')' for (q, old_q) in old_new_pairs]) +';'

            
class ConjunctiveQuerySQLGenerator():

    def get_SQL_code(self,query,old_query,pretty_print=False):
        aliases = self.create_table_aliases(query)
        var_dict = query.get_var_dict()

        select_clause = self.get_select_columns(query.get_head_relation(), query.get_constraints(), var_dict, old_query.get_head_relation())
        from_clause = self.get_from_relations(query,aliases)
        implicit_constraints = self.get_implicit_constraints(query.get_relations())
        join_constraints = self.get_join_constraints(var_dict)
        explicit_constraints = self.get_explicit_constraints(query.get_constraints(),var_dict)
        negated_queries = self.get_negated_queries(query.get_relations(),var_dict)
        where_separator = ' AND \n\t' if pretty_print else ' AND '
        where_clause = where_separator.join(implicit_constraints + join_constraints + explicit_constraints + negated_queries)
        if pretty_print:
            return 'SELECT \n\t' + ', \n\t'.join(select_clause) + \
               ('\nFROM \n\t' if len(from_clause) != 0 else '') + \
               ', \n\t'.join(from_clause) + \
               ('\nWHERE \n\t' if len(where_clause) != 0 else '') + where_clause + ';'
        else:
            return 'SELECT ' + ', '.join(select_clause) + \
               (' FROM ' if len(from_clause) != 0 else '') + \
               ', '.join(from_clause) + \
               (' WHERE ' if len(where_clause) != 0 else '') + where_clause + ';'



    def create_table_aliases(self,query):
        count = {}
        for relation in query.get_relations():
            if not (relation.get_name() in count):
                count[relation.get_name()] = 0
            else:
                count[relation.get_name()]+=1

        aliases = {}

        repeated_relations = [relation for relation in query.get_relations() if count[relation.get_name()]>0]
        non_repeated_relations = [relation for relation in query.get_relations() if count[relation.get_name()]==0]

        for relation in repeated_relations:
            old_name = relation.get_name()
            relation.set_name(old_name+str(count[relation.get_name()]))
            count[old_name]-=1
            aliases[relation.get_name()] = old_name + ' as ' + relation.get_name()

        for relation in non_repeated_relations:
            aliases[relation.get_name()] = relation.get_name()

        return aliases

    def mapVariableToRelationDotField(self,var,var_dict,pos = 0):
        relation = var_dict[var][pos][0]
        position = var_dict[var][pos][1]
        return relation.get_name() + '.'+ self.mapPositionToColumnName(relation,position)

    def get_from_relations(self, query,aliases):
        '''
        Returns list of the names of all positive relations in the query
        '''
        return [aliases[rel.get_name()] for rel in query.get_relations() if not rel.is_negated()]

    def mapPositionToColumnName(self,relation,position):
        return '_' + str(position)

    def get_select_columns(self,head_relation,constraints,var_dict, original_head_relation):
        column_list = []

        element_list = head_relation.get_ordered_element_list()

        if len(element_list) == 0:
            column_list = ["'TRUE'"]
        else:
            if original_head_relation is None:
                as_names = head_relation.get_ordered_element_list()
            else:
                as_names = original_head_relation.get_ordered_element_list()<commit_msg>Fixed several trues issue - needs testing.<commit_after>from ..dataStructures.query import *

class SQLGenerator():
    def get_SQL_code(self,query,old_query):
        if isinstance(query, ConjunctiveQuery):
            sql_gen = ConjunctiveQuerySQLGenerator()
        if isinstance(query, DisjunctiveQuery):
            sql_gen = DisjunctiveQuerySQLGenerator()
        return sql_gen.get_SQL_code(query,old_query)

class DisjunctiveQuerySQLGenerator():

    def get_SQL_code(self,query,old_query):
        sql_gen = SQLGenerator()
        old_queries = old_query.get_queries()
        new_queries = query.get_queries()
        old_new_pairs = [(new_queries[aux], old_queries[aux]) for aux in range(0,len(new_queries))]
        return ' UNION '.join(['(' + sql_gen.get_SQL_code(q, old_q)[:-1] + ')' for (q, old_q) in old_new_pairs]) +';'

            
class ConjunctiveQuerySQLGenerator():

    def get_SQL_code(self,query,old_query,pretty_print=False):
        aliases = self.create_table_aliases(query)
        var_dict = query.get_var_dict()

        select_clause = self.get_select_columns(query.get_head_relation(), query.get_constraints(), var_dict, old_query.get_head_relation())
        from_clause = self.get_from_relations(query,aliases)
        implicit_constraints = self.get_implicit_constraints(query.get_relations())
        join_constraints = self.get_join_constraints(var_dict)
        explicit_constraints = self.get_explicit_constraints(query.get_constraints(),var_dict)
        negated_queries = self.get_negated_queries(query.get_relations(),var_dict)
        where_separator = ' AND \n\t' if pretty_print else ' AND '
        where_clause = where_separator.join(implicit_constraints + join_constraints + explicit_constraints + negated_queries)
        if pretty_print:
            return 'SELECT \n\t' + ', \n\t'.join(select_clause) + \
               ('\nFROM \n\t' if len(from_clause) != 0 else '') + \
               ', \n\t'.join(from_clause) + \
               ('\nWHERE \n\t' if len(where_clause) != 0 else '') + where_clause + ';'
        else:
            return 'SELECT ' + ', '.join(select_clause) + \
               (' FROM ' if len(from_clause) != 0 else '') + \
               ', '.join(from_clause) + \
               (' WHERE ' if len(where_clause) != 0 else '') + where_clause + ';'



    def create_table_aliases(self,query):
        count = {}
        for relation in query.get_relations():
            if not (relation.get_name() in count):
                count[relation.get_name()] = 0
            else:
                count[relation.get_name()]+=1

        aliases = {}

        repeated_relations = [relation for relation in query.get_relations() if count[relation.get_name()]>0]
        non_repeated_relations = [relation for relation in query.get_relations() if count[relation.get_name()]==0]

        for relation in repeated_relations:
            old_name = relation.get_name()
            relation.set_name(old_name+str(count[relation.get_name()]))
            count[old_name]-=1
            aliases[relation.get_name()] = old_name + ' as ' + relation.get_name()

        for relation in non_repeated_relations:
            aliases[relation.get_name()] = relation.get_name()

        return aliases

    def mapVariableToRelationDotField(self,var,var_dict,pos = 0):
        relation = var_dict[var][pos][0]
        position = var_dict[var][pos][1]
        return relation.get_name() + '.'+ self.mapPositionToColumnName(relation,position)

    def get_from_relations(self, query,aliases):
        '''
        Returns list of the names of all positive relations in the query
        '''
        return [aliases[rel.get_name()] for rel in query.get_relations() if not rel.is_negated()]

    def mapPositionToColumnName(self,relation,position):
        return '_' + str(position)

    def get_select_columns(self,head_relation,constraints,var_dict, original_head_relation):
        column_list = []

        element_list = head_relation.get_ordered_element_list()

        if len(element_list) == 0:
            column_list = ["CASE COUNT(*) WHEN 0 THEN 'FALSE' ELSE 'TRUE' END"]
        else:
            if original_head_relation is None:
                as_names = head_relation.get_ordered_element_list()
            else:
                as_names = original_head_relation.get_ordered_element_list()
{'fraction_non_alphanumeric': 0.11411028730305839, 'fraction_numerical': 0.0035912882298424465, 'mean_word_length': 2.8834907782276202, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '225267', 'n_tokens_mistral': 2717, 'n_tokens_neox': 2621, 'n_words': 470}