Beispiel #1
0
    def __create_outer_quer_model(query_model1, query_model2):
        """
        Joins the prefixes, offset, limit, order and select clauses and variables of the two query models.
        Doesn't add the from clause from both variables
        :param query_model1:
        :param query_model2:
        :return: an outer query model
        """
        query_model = QueryModel()
        #query_model.from_clause.clear()
        query_model.add_prefixes(query_model1.prefixes)
        query_model.add_prefixes(query_model2.prefixes)

        query_model.set_offset(min(query_model1.offset, query_model2.offset))
        query_model.set_limit(max(query_model1.limit, query_model2.limit))
        query_model.add_order_columns(query_model1.order_clause)
        query_model.add_order_columns(query_model2.order_clause)

        # TODO: WHY do this here?
        # union the select columns
        if len(query_model1.select_columns) > 0 and len(query_model1.select_columns) > 0:
            query_model.select_columns = query_model1.select_columns.union(query_model2.select_columns)
        elif len(query_model1.select_columns) > 0:
            query_model.select_columns = query_model1.select_columns.union(query_model2.variables)
        elif len(query_model2.select_columns) > 0:
            query_model.select_columns = query_model1.variables.union(query_model2.select_columns)
        # union the variables
        query_model.variables = query_model1.variables.union(query_model2.variables)

        return query_model
Beispiel #2
0
 def __wrap_in_subquery(query_model):
     """
     create an outer query containing the variables, select columns, offset, limit, order,
     :param query_model:
     :return:
     """
     new_query_model = QueryModel()
     new_query_model.variables = copy.copy(query_model.variables)  # all prefixes are already in query_model1
     new_query_model.from_clause = copy.copy(query_model.from_clause)
     new_query_model.select_columns = copy.copy(query_model.select_columns)
     new_query_model.offset = query_model.offset
     new_query_model.limit = query_model.limit
     new_query_model.order_clause = copy.copy(query_model.order_clause)
     return new_query_model
Beispiel #3
0
 def _outer_join(query_model1, query_model2):
     joined_query_model = QueryModel()
     joined_query_model.prefixes = copy.copy(
         query_model1.prefixes)  # all prefixes are already in query_model1
     joined_query_model.variables = copy.copy(
         query_model1.variables)  # all prefixes are already in query_model1
     joined_query_model.from_clause = copy.copy(query_model1.from_clause)
     joined_query_model.select_columns = copy.copy(
         query_model1.select_columns)
     joined_query_model.offset = query_model1.offset
     joined_query_model.limit = query_model1.limit
     joined_query_model.order_clause = copy.copy(query_model1.order_clause)
     QueryModel.clean_inner_qm(query_model1)
     QueryModel.clean_inner_qm(query_model2)
     joined_query_model.add_unions(query_model1)
     joined_query_model.add_unions(query_model2)
     return joined_query_model
Beispiel #4
0
 def __join_expandable_grouped(self,
                               query_model1,
                               query_model2,
                               expandable_order=1):
     if self.join_type == JoinType.OuterJoin:  # outer join
         return JoinOperator._outer_join(query_model1, query_model2)
     elif self.join_type == JoinType.InnerJoin:
         # add query model 2 as a subquery
         QueryModel.clean_inner_qm(query_model2)
         query_model1.add_subquery(query_model2)
         return query_model1
     elif ((expandable_order == 1 and self.join_type == JoinType.LeftOuterJoin) or\
         (expandable_order == 2 and self.join_type == JoinType.RightOuterJoin)):
         # make the subquery optional
         QueryModel.clean_inner_qm(query_model2)
         query_model1.add_optional_subquery(query_model2)
         return query_model1
     elif ((expandable_order == 2 and self.join_type == JoinType.LeftOuterJoin) or\
         (expandable_order == 1 and self.join_type == JoinType.RightOuterJoin)):
         # create an outer query and add the main dataset as a subquery and the optional dataset as optional subquery
         joined_query_model = QueryModel()
         joined_query_model.prefixes = copy.copy(
             query_model1.prefixes
         )  # all prefixes are already in query_model1
         joined_query_model.variables = copy.copy(
             query_model1.variables
         )  # all prefixes are already in query_model1
         joined_query_model.from_clause = copy.copy(
             query_model1.from_clause)
         joined_query_model.select_columns = copy.copy(
             query_model1.select_columns)
         joined_query_model.offset = query_model1.offset
         joined_query_model.limit = query_model1.limit
         joined_query_model.order_clause = copy.copy(
             query_model1.order_clause)
         QueryModel.clean_inner_qm(query_model1)
         QueryModel.clean_inner_qm(query_model2)
         joined_query_model.add_optional_subquery(query_model1)
         joined_query_model.add_subquery(query_model2)
         return joined_query_model
     else:
         raise Exception("Undefined case")
Beispiel #5
0
    def __join_grouped_grouped(self, query_model1, query_model2):
        joined_query_model = QueryModel()

        joined_query_model.prefixes = copy.copy(
            query_model1.prefixes)  # all prefixes are already in query_model1
        joined_query_model.add_prefixes(query_model2.prefixes)

        joined_query_model.variables = copy.copy(
            query_model1.variables.union(query_model2.variables)
        )  # all prefixes are already in query_model1
        joined_query_model.from_clause = copy.copy(query_model1.from_clause)
        joined_query_model.select_columns = copy.copy(
            query_model1.select_columns.union(query_model2.select_columns))
        joined_query_model.offset = min(query_model1.offset,
                                        query_model2.offset)
        joined_query_model.limit = max(query_model1.limit, query_model2.limit)
        query_model1.order_clause.update(query_model2.order_clause)
        joined_query_model.order_clause = copy.copy(query_model1.order_clause)

        QueryModel.clean_inner_qm(query_model1)
        QueryModel.clean_inner_qm(query_model2)

        # add subqueries
        if self.join_type == JoinType.InnerJoin:
            joined_query_model.add_subquery(query_model1)
            joined_query_model.add_subquery(query_model2)
        elif self.join_type == JoinType.LeftOuterJoin:
            joined_query_model.add_subquery(query_model1)
            joined_query_model.add_optional_subquery(query_model2)
        elif self.join_type == JoinType.RightOuterJoin:
            joined_query_model.add_subquery(query_model2)
            joined_query_model.add_optional_subquery(query_model1)
        else:  # outer join
            joined_query_model.add_unions(query_model1)
            joined_query_model.add_unions(query_model2)
        return joined_query_model