Esempio n. 1
0
    def test_table_to_alias(self):

        t1alias = t1.alias('t1alias')

        vis = sql_util.ClauseAdapter(t1alias)
        ff = vis.traverse(func.count(t1.c.col1).label('foo'))
        assert list(_from_objects(ff)) == [t1alias]

        self.assert_compile(vis.traverse(select(['*'], from_obj=[t1])), "SELECT * FROM table1 AS t1alias")
        self.assert_compile(select(['*'], t1.c.col1==t2.c.col2), "SELECT * FROM table1, table2 WHERE table1.col1 = table2.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2)), "SELECT * FROM table1 AS t1alias, table2 WHERE t1alias.col1 = table2.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2, from_obj=[t1, t2])), "SELECT * FROM table1 AS t1alias, table2 WHERE t1alias.col1 = table2.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2, from_obj=[t1, t2]).correlate(t1)), "SELECT * FROM table2 WHERE t1alias.col1 = table2.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2, from_obj=[t1, t2]).correlate(t2)), "SELECT * FROM table1 AS t1alias WHERE t1alias.col1 = table2.col2")


        s = select(['*'], from_obj=[t1]).alias('foo')
        self.assert_compile(s.select(), "SELECT foo.* FROM (SELECT * FROM table1) AS foo")
        self.assert_compile(vis.traverse(s.select()), "SELECT foo.* FROM (SELECT * FROM table1 AS t1alias) AS foo")
        self.assert_compile(s.select(), "SELECT foo.* FROM (SELECT * FROM table1) AS foo")

        ff = vis.traverse(func.count(t1.c.col1).label('foo'))
        self.assert_compile(select([ff]), "SELECT count(t1alias.col1) AS foo FROM table1 AS t1alias")
        assert list(_from_objects(ff)) == [t1alias]

# TODO:
    #    self.assert_compile(vis.traverse(select([func.count(t1.c.col1).label('foo')]), clone=True), "SELECT count(t1alias.col1) AS foo FROM table1 AS t1alias")

        t2alias = t2.alias('t2alias')
        vis.chain(sql_util.ClauseAdapter(t2alias))
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2)), "SELECT * FROM table1 AS t1alias, table2 AS t2alias WHERE t1alias.col1 = t2alias.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2, from_obj=[t1, t2])), "SELECT * FROM table1 AS t1alias, table2 AS t2alias WHERE t1alias.col1 = t2alias.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2, from_obj=[t1, t2]).correlate(t1)), "SELECT * FROM table2 AS t2alias WHERE t1alias.col1 = t2alias.col2")
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1==t2.c.col2, from_obj=[t1, t2]).correlate(t2)), "SELECT * FROM table1 AS t1alias WHERE t1alias.col1 = t2alias.col2")
Esempio n. 2
0
def find_join_source(clauses, join_to):
    """Given a list of FROM clauses and a selectable, 
    return the first index and element from the list of 
    clauses which can be joined against the selectable.  returns 
    None, None if no match is found.
    
    e.g.::
    
        clause1 = table1.join(table2)
        clause2 = table4.join(table5)
        
        join_to = table2.join(table3)
        
        find_join_source([clause1, clause2], join_to) == clause1
    
    """

    selectables = list(expression._from_objects(join_to))
    for i, f in enumerate(clauses):
        for s in selectables:
            if f.is_derived_from(s):
                return i, f
    else:
        return None, None
Esempio n. 3
0
File: util.py Progetto: Kellel/items
def find_join_source(clauses, join_to):
    """Given a list of FROM clauses and a selectable, 
    return the first index and element from the list of 
    clauses which can be joined against the selectable.  returns 
    None, None if no match is found.

    e.g.::

        clause1 = table1.join(table2)
        clause2 = table4.join(table5)

        join_to = table2.join(table3)

        find_join_source([clause1, clause2], join_to) == clause1

    """

    selectables = list(expression._from_objects(join_to))
    for i, f in enumerate(clauses):
        for s in selectables:
            if f.is_derived_from(s):
                return i, f
    else:
        return None, None
Esempio n. 4
0
    def visit_select(self,
                     select,
                     asfrom=False,
                     parens=True,
                     iswrapper=False,
                     compound_index=1,
                     **kwargs):

        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for select(select(select))
        # where innermost select should correlate to outermost
        # if existingfroms:
        #     correlate_froms = correlate_froms.union(existingfroms)

        self.stack.append({'from': correlate_froms, 'iswrapper': iswrapper})

        if compound_index == 1 and not entry or entry.get('iswrapper', False):
            column_clause_args = {'result_map': self.result_map}
        else:
            column_clause_args = {}

        # the actual list of columns to print in the SELECT column list.
        inner_columns = util.unique_list(c for c in [
            self.process(self.label_select_column(select, co, asfrom=asfrom),
                         within_columns_clause=True,
                         **column_clause_args) for co in select.inner_columns
        ] if c is not None)

        text = "SELECT "  # we're off to a good start !
        if select._prefixes:
            text += " ".join(self.process(x) for x in select._prefixes) + " "
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        if froms:
            text += " \nFROM "
            text += ', '.join(self.process(f, asfrom=True) for f in froms)
        else:
            text += self.default_from()

        if select._whereclause is not None:
            t = self.process(select._whereclause)
            if t:
                text += " \nWHERE " + t

        if select._group_by_clause.clauses:
            group_by = self.process(select._group_by_clause)
            if group_by:
                text += " GROUP BY " + group_by

        if select._having is not None:
            t = self.process(select._having)
            if t:
                text += " \nHAVING " + t

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select)
        if select._limit is not None or select._offset is not None:
            text += self.limit_clause(select)
        if select.for_update:
            text += self.for_update_clause(select)

        self.stack.pop(-1)

        if asfrom and parens:
            return "(" + text + ")"
        else:
            return text
Esempio n. 5
0
    def visit_select(self, select, asfrom=False, parens=True,
                            iswrapper=False, fromhints=None,
                            compound_index=0,
                            force_result_map=False,
                            positional_names=None, **kwargs):

        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms, asfrom=asfrom)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for
        # select(select(select)) where innermost select should correlate
        # to outermost if existingfroms: correlate_froms =
        # correlate_froms.union(existingfroms)

        populate_result_map = force_result_map or (
                                compound_index == 0 and (
                                    not entry or \
                                    entry.get('iswrapper', False)
                                )
                            )

        self.stack.append({'from': correlate_froms,
                            'iswrapper': iswrapper})

        sel = resolver.SelectResolver()

        column_clause_args = kwargs.copy()
        column_clause_args.update({
                'positional_names': positional_names,
                'within_label_clause': False,
                'within_columns_clause': False
            })

        # the actual list of columns to print in the SELECT column list.
        inner_columns = [
            c for c in [
                self._label_select_column(select, column,
                                    populate_result_map, asfrom,
                                    column_clause_args)
                for column in util.unique_list(select.inner_columns)
                ]
            if c is not None
        ]
        sel.columns.extend(inner_columns)

        if froms:
            for f in froms:
                sel.dataframes.append(
                    f._compiler_dispatch(self, asfrom=True, **kwargs)
                )

        if select._whereclause is not None:
            t = select._whereclause._compiler_dispatch(self, **kwargs)
            sel.whereclause = t

        if select._group_by_clause.clauses:
            group_by = select._group_by_clause._compiler_dispatch(
                                        self, **kwargs)
            sel.group_by = group_by

        if select._order_by_clause.clauses:
            order_by = select._order_by_clause._compiler_dispatch(
                                        self, **kwargs)
            sel.order_by = order_by

        if select._having is not None:
            sel.having = select._having._compiler_dispatch(self, **kwargs)

        sel.limit = select._limit
        sel.offset = select._offset

        self.stack.pop(-1)

        return sel
Esempio n. 6
0
    def visit_select(self,
                     select,
                     asfrom=False,
                     parens=True,
                     iswrapper=False,
                     fromhints=None,
                     compound_index=1,
                     force_result_map=False,
                     nested_join_translation=False,
                     **kwargs):
        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for
        # select(select(select)) where innermost select should correlate
        # to outermost if existingfroms: correlate_froms =
        # correlate_froms.union(existingfroms)

        self.stack.append({'from': correlate_froms, 'iswrapper': iswrapper})

        # the actual list of columns to print in the SELECT column list.

        unique_co = []
        distinct_alias = None
        for co in util.unique_list(select.inner_columns):
            sql_util = self._label_select_column(select, co, True, asfrom, {})
            if "DISTINCT" in sql_util:
                distinct_alias = sql_util.split(" AS ")[-1]
            unique_co.append(sql_util)
        result_columns = []
        if distinct_alias:
            for idx, rc_tuple in enumerate(self._result_columns):
                if rc_tuple[0] == distinct_alias:
                    if rc_tuple[-2][-1] == distinct_alias:
                        target_name = "@distinct"
                        temp_rc = list(rc_tuple)
                        temp_rc[0] = target_name
                        inner_tuple = list(temp_rc[-2])
                        inner_tuple[-1] = target_name
                        if not select._group_by_clause.clauses:
                            raise AssertionError(
                                "Can't query distinct if no group by  is selected"
                            )
                        temp_rc[-2] = tuple(inner_tuple)
                        result_columns.append(tuple(temp_rc))
                else:
                    result_columns.append(rc_tuple)
        if result_columns:
            self._result_columns = result_columns

        inner_columns = [c for c in unique_co if c is not None]

        text = "SELECT "  # we're off to a good start !
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        text += " \nFROM "

        text += ', '.join(
            [f._compiler_dispatch(self, asfrom=True, **kwargs) for f in froms])

        def check_match_clause(clause):
            left_tuple = []
            right_tuple = []
            match_operators = []
            if isinstance(clause.type, MatchType):
                left_tuple.append(clause.left)
                right_tuple.append(clause.right)
                match_operators.append(clause.operator)
            elif isinstance(clause, Function):
                if clause.name.lower() == "match":
                    func_left, func_right = clause.clauses
                    left_tuple.append(func_left)
                    right_tuple.append(func_right)
            elif isinstance(clause, ClauseList):
                for xclause in clause.clauses:
                    l, r, m = check_match_clause(xclause)
                    left_tuple.extend(l)
                    right_tuple.extend(r)
                    match_operators.extend(m)
            return left_tuple, right_tuple, match_operators

        if select._whereclause is not None:
            # Match Clauses must be done in the same compiler
            left_tuple = []
            right_tuple = []
            match_operators = []
            l, r, m = check_match_clause(select._whereclause)
            left_tuple.extend(l)
            right_tuple.extend(r)
            match_operators.extend(m)

            if left_tuple and right_tuple:
                self.left_match = tuple(left_tuple)
                self.right_match = tuple(right_tuple)
                self.match_operators = tuple(match_operators)

            t = select._whereclause._compiler_dispatch(self, **kwargs)
            if t:
                text += " \nWHERE " + t

            if hasattr(self, "options_list"):
                if self.options_list:
                    option_text = " OPTION {}".format(", ".join(
                        self.options_list))
                    text += option_text

        if select._group_by_clause.clauses:
            group_by = select._group_by_clause._compiler_dispatch(
                self, **kwargs)
            text += " GROUP BY " + group_by

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select, **kwargs)
        if select._limit is not None:
            text += self.limit_clause(select)

        self.stack.pop(-1)
        return text
Esempio n. 7
0
    def test_table_to_alias(self):

        t1alias = t1.alias('t1alias')

        vis = sql_util.ClauseAdapter(t1alias)
        ff = vis.traverse(func.count(t1.c.col1).label('foo'))
        assert list(_from_objects(ff)) == [t1alias]

        self.assert_compile(vis.traverse(select(['*'], from_obj=[t1])),
                            "SELECT * FROM table1 AS t1alias")
        self.assert_compile(
            select(['*'], t1.c.col1 == t2.c.col2),
            "SELECT * FROM table1, table2 WHERE table1.col1 = table2.col2")
        self.assert_compile(
            vis.traverse(select(['*'], t1.c.col1 == t2.c.col2)),
            "SELECT * FROM table1 AS t1alias, table2 WHERE t1alias.col1 = table2.col2"
        )
        self.assert_compile(
            vis.traverse(
                select(['*'], t1.c.col1 == t2.c.col2, from_obj=[t1, t2])),
            "SELECT * FROM table1 AS t1alias, table2 WHERE t1alias.col1 = table2.col2"
        )
        self.assert_compile(
            vis.traverse(
                select(['*'], t1.c.col1 == t2.c.col2,
                       from_obj=[t1, t2]).correlate(t1)),
            "SELECT * FROM table2 WHERE t1alias.col1 = table2.col2")
        self.assert_compile(
            vis.traverse(
                select(['*'], t1.c.col1 == t2.c.col2,
                       from_obj=[t1, t2]).correlate(t2)),
            "SELECT * FROM table1 AS t1alias WHERE t1alias.col1 = table2.col2")

        self.assert_compile(
            vis.traverse(case([(t1.c.col1 == 5, t1.c.col2)], else_=t1.c.col1)),
            "CASE WHEN (t1alias.col1 = :col1_1) THEN t1alias.col2 ELSE t1alias.col1 END"
        )
        self.assert_compile(
            vis.traverse(
                case([(5, t1.c.col2)], value=t1.c.col1, else_=t1.c.col1)),
            "CASE t1alias.col1 WHEN :param_1 THEN t1alias.col2 ELSE t1alias.col1 END"
        )

        s = select(['*'], from_obj=[t1]).alias('foo')
        self.assert_compile(s.select(),
                            "SELECT foo.* FROM (SELECT * FROM table1) AS foo")
        self.assert_compile(
            vis.traverse(s.select()),
            "SELECT foo.* FROM (SELECT * FROM table1 AS t1alias) AS foo")
        self.assert_compile(s.select(),
                            "SELECT foo.* FROM (SELECT * FROM table1) AS foo")

        ff = vis.traverse(func.count(t1.c.col1).label('foo'))
        self.assert_compile(
            select([ff]),
            "SELECT count(t1alias.col1) AS foo FROM table1 AS t1alias")
        assert list(_from_objects(ff)) == [t1alias]

        # TODO:
        #    self.assert_compile(vis.traverse(select([func.count(t1.c.col1).label('foo')]), clone=True), "SELECT count(t1alias.col1) AS foo FROM table1 AS t1alias")

        t2alias = t2.alias('t2alias')
        vis.chain(sql_util.ClauseAdapter(t2alias))
        self.assert_compile(
            vis.traverse(select(['*'], t1.c.col1 == t2.c.col2)),
            "SELECT * FROM table1 AS t1alias, table2 AS t2alias WHERE t1alias.col1 = t2alias.col2"
        )
        self.assert_compile(
            vis.traverse(
                select(['*'], t1.c.col1 == t2.c.col2, from_obj=[t1, t2])),
            "SELECT * FROM table1 AS t1alias, table2 AS t2alias WHERE t1alias.col1 = t2alias.col2"
        )
        self.assert_compile(
            vis.traverse(
                select(['*'], t1.c.col1 == t2.c.col2,
                       from_obj=[t1, t2]).correlate(t1)),
            "SELECT * FROM table2 AS t2alias WHERE t1alias.col1 = t2alias.col2"
        )
        self.assert_compile(
            vis.traverse(
                select(['*'], t1.c.col1 == t2.c.col2,
                       from_obj=[t1, t2]).correlate(t2)),
            "SELECT * FROM table1 AS t1alias WHERE t1alias.col1 = t2alias.col2"
        )
Esempio n. 8
0
    def visit_select(self, select, asfrom=False, parens=True, iswrapper=False, compound_index=1, **kwargs):

        entry = self.stack and self.stack[-1] or {}
        
        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for select(select(select))
        # where innermost select should correlate to outermost
        # if existingfroms:
        #     correlate_froms = correlate_froms.union(existingfroms)

        self.stack.append({'from':correlate_froms, 'iswrapper':iswrapper})

        if compound_index==1 and not entry or entry.get('iswrapper', False):
            column_clause_args = {'result_map':self.result_map}
        else:
            column_clause_args = {}

        # the actual list of columns to print in the SELECT column list.
        inner_columns = util.unique_list(
            c for c in [
                self.process(
                    self.label_select_column(select, co, asfrom=asfrom), 
                    within_columns_clause=True,
                    **column_clause_args) 
                for co in select.inner_columns
            ]
            if c is not None
        )
        
        text = "SELECT "  # we're off to a good start !
        if select._prefixes:
            text += " ".join(self.process(x) for x in select._prefixes) + " "
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        if froms:
            text += " \nFROM "
            text += ', '.join(self.process(f, asfrom=True) for f in froms)
        else:
            text += self.default_from()

        if select._whereclause is not None:
            t = self.process(select._whereclause)
            if t:
                text += " \nWHERE " + t

        if select._group_by_clause.clauses:
            group_by = self.process(select._group_by_clause)
            if group_by:
                text += " GROUP BY " + group_by

        if select._having is not None:
            t = self.process(select._having)
            if t:
                text += " \nHAVING " + t

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select)
        if select._limit is not None or select._offset is not None:
            text += self.limit_clause(select)
        if select.for_update:
            text += self.for_update_clause(select)

        self.stack.pop(-1)

        if asfrom and parens:
            return "(" + text + ")"
        else:
            return text
    def test_table_to_alias(self):

        t1alias = t1.alias('t1alias')

        vis = sql_util.ClauseAdapter(t1alias)
        ff = vis.traverse(func.count(t1.c.col1).label('foo'))
        assert list(_from_objects(ff)) == [t1alias]

        self.assert_compile(vis.traverse(select(['*'], from_obj=[t1])),
                            'SELECT * FROM table1 AS t1alias')
        self.assert_compile(select(['*'], t1.c.col1 == t2.c.col2),
                            'SELECT * FROM table1, table2 WHERE '
                            'table1.col1 = table2.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2)),
                            'SELECT * FROM table1 AS t1alias, table2 '
                            'WHERE t1alias.col1 = table2.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2, from_obj=[t1, t2])),
                            'SELECT * FROM table1 AS t1alias, table2 '
                            'WHERE t1alias.col1 = table2.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2, from_obj=[t1,
                            t2]).correlate(t1)),
                            'SELECT * FROM table2 WHERE t1alias.col1 = '
                            'table2.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2, from_obj=[t1,
                            t2]).correlate(t2)),
                            'SELECT * FROM table1 AS t1alias WHERE '
                            't1alias.col1 = table2.col2')
        self.assert_compile(vis.traverse(case([(t1.c.col1 == 5,
                            t1.c.col2)], else_=t1.c.col1)),
                            'CASE WHEN (t1alias.col1 = :col1_1) THEN '
                            't1alias.col2 ELSE t1alias.col1 END')
        self.assert_compile(vis.traverse(case([(5, t1.c.col2)],
                            value=t1.c.col1, else_=t1.c.col1)),
                            'CASE t1alias.col1 WHEN :param_1 THEN '
                            't1alias.col2 ELSE t1alias.col1 END')


        s = select(['*'], from_obj=[t1]).alias('foo')
        self.assert_compile(s.select(),
                            'SELECT foo.* FROM (SELECT * FROM table1) '
                            'AS foo')
        self.assert_compile(vis.traverse(s.select()),
                            'SELECT foo.* FROM (SELECT * FROM table1 '
                            'AS t1alias) AS foo')
        self.assert_compile(s.select(),
                            'SELECT foo.* FROM (SELECT * FROM table1) '
                            'AS foo')
        ff = vis.traverse(func.count(t1.c.col1).label('foo'))
        self.assert_compile(select([ff]),
                            'SELECT count(t1alias.col1) AS foo FROM '
                            'table1 AS t1alias')
        assert list(_from_objects(ff)) == [t1alias]

        # TODO: self.assert_compile(vis.traverse(select([func.count(t1.c
        # .col1).l abel('foo')]), clone=True), "SELECT
        # count(t1alias.col1) AS foo FROM table1 AS t1alias")

        t2alias = t2.alias('t2alias')
        vis.chain(sql_util.ClauseAdapter(t2alias))
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2)),
                            'SELECT * FROM table1 AS t1alias, table2 '
                            'AS t2alias WHERE t1alias.col1 = '
                            't2alias.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2, from_obj=[t1, t2])),
                            'SELECT * FROM table1 AS t1alias, table2 '
                            'AS t2alias WHERE t1alias.col1 = '
                            't2alias.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2, from_obj=[t1,
                            t2]).correlate(t1)),
                            'SELECT * FROM table2 AS t2alias WHERE '
                            't1alias.col1 = t2alias.col2')
        self.assert_compile(vis.traverse(select(['*'], t1.c.col1
                            == t2.c.col2, from_obj=[t1,
                            t2]).correlate(t2)),
                            'SELECT * FROM table1 AS t1alias WHERE '
                            't1alias.col1 = t2alias.col2')
Esempio n. 10
0
    def visit_select(self,
                     select,
                     asfrom=False,
                     parens=True,
                     iswrapper=False,
                     fromhints=None,
                     compound_index=1,
                     force_result_map=False,
                     nested_join_translation=False,
                     **kwargs):
        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for
        # select(select(select)) where innermost select should correlate
        # to outermost if existingfroms: correlate_froms =
        # correlate_froms.union(existingfroms)

        self.stack.append({'from': correlate_froms, 'iswrapper': iswrapper})

        # the actual list of columns to print in the SELECT column list.
        inner_columns = [
            c for c in [
                self._label_select_column(select, co, True, asfrom, {})
                for co in util.unique_list(select.inner_columns)
            ] if c is not None
        ]

        text = "SELECT "  # we're off to a good start !

        if select._hints:
            byfrom = dict([(from_, hinttext % {
                'name': from_._compiler_dispatch(self, ashint=True)
            }) for (from_, dialect), hinttext in select._hints.iteritems()
                           if dialect in ('*', self.dialect.name)])
            hint_text = self.get_select_hint_text(byfrom)
            if hint_text:
                text += hint_text + " "

        if select._prefixes:
            text += " ".join(
                x._compiler_dispatch(self, **kwargs)
                for x in select._prefixes) + " "
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        if froms:
            text += " \nFROM "

            if select._hints:
                text += ', '.join([
                    f._compiler_dispatch(self,
                                         asfrom=True,
                                         fromhints=byfrom,
                                         **kwargs) for f in froms
                ])
            else:
                text += ', '.join([
                    f._compiler_dispatch(self, asfrom=True, **kwargs)
                    for f in froms
                ])
        else:
            text += self.default_from()

        if select._whereclause is not None:
            t = select._whereclause._compiler_dispatch(self, **kwargs)
            if t:
                text += " \nWHERE " + t

        if select._group_by_clause.clauses:
            group_by = select._group_by_clause._compiler_dispatch(
                self, **kwargs)
            if group_by:
                text += " GROUP BY " + group_by

        if select._having is not None:
            t = select._having._compiler_dispatch(self, **kwargs)
            if t:
                text += " \nHAVING " + t

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select, **kwargs)
        if getattr(select, "_within_group_order_by_clause", None) is not None:
            if select._within_group_order_by_clause.clauses:
                text += self.within_group_order_by_clause(select, **kwargs)
        if select._limit is not None:
            text += self.limit_clause(select)
        if getattr(select, "_options", None) is not None:
            if select._options.options:
                text += self.options_clause(select, **kwargs)
        if select.for_update:
            text += self.for_update_clause(select)

        self.stack.pop(-1)

        if asfrom and parens:
            return "(" + text + ")"
        else:
            return text
Esempio n. 11
0
    def visit_select(self, select, asfrom=False, parens=True,
                            iswrapper=False, fromhints=None,
                            compound_index=1, **kwargs):

        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for
        # select(select(select)) where innermost select should correlate
        # to outermost if existingfroms: correlate_froms =
        # correlate_froms.union(existingfroms)

        self.stack.append({'from': correlate_froms,
                           'iswrapper': iswrapper})

        if compound_index == 1 and not entry or entry.get('iswrapper', False):
            column_clause_args = {'result_map': self.result_map}
        else:
            column_clause_args = {}

        # the actual list of columns to print in the SELECT column list.
        inner_columns = [
            c for c in [
                self.label_select_column(select, co, asfrom=asfrom).\
                    _compiler_dispatch(self,
                        within_columns_clause=True,
                        **column_clause_args)
                for co in util.unique_list(select.inner_columns)
            ]
            if c is not None
        ]

        text = "SELECT "  # we're off to a good start !

        if select._hints:
            byfrom = dict([
                            (from_, hinttext % {
                                'name':from_._compiler_dispatch(
                                    self, ashint=True)
                            })
                            for (from_, dialect), hinttext in
                            select._hints.iteritems()
                            if dialect in ('*', self.dialect.name)
                        ])
            hint_text = self.get_select_hint_text(byfrom)
            if hint_text:
                text += hint_text + " "

        if select._prefixes:
            text += " ".join(
                            x._compiler_dispatch(self, **kwargs)
                            for x in select._prefixes) + " "
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        if froms:
            text += " \nFROM "

            if select._hints:
                text += ', '.join([f._compiler_dispatch(self,
                                    asfrom=True, fromhints=byfrom,
                                    **kwargs)
                                for f in froms])
            else:
                text += ', '.join([f._compiler_dispatch(self,
                                    asfrom=True, **kwargs)
                                for f in froms])
        else:
            text += self.default_from()

        if select._whereclause is not None:
            t = select._whereclause._compiler_dispatch(self, **kwargs)
            if t:
                text += " \nWHERE " + t

        if select._group_by_clause.clauses:
            group_by = select._group_by_clause._compiler_dispatch(
                                        self, **kwargs)
            if group_by:
                text += " GROUP BY " + group_by

        if select._having is not None:
            t = select._having._compiler_dispatch(self, **kwargs)
            if t:
                text += " \nHAVING " + t

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select, **kwargs)
        if getattr(select, "_within_group_order_by_clause", None) is not None:
            if select._within_group_order_by_clause.clauses:
                text += self.within_group_order_by_clause(select, **kwargs)
        if select._limit is not None:
            text += self.limit_clause(select)
        if getattr(select, "_options", None) is not None:
            if select._options.options:
                text += self.options_clause(select, **kwargs)
        if select.for_update:
            text += self.for_update_clause(select)

        self.stack.pop(-1)

        if asfrom and parens:
            return "(" + text + ")"
        else:
            return text
Esempio n. 12
0
    def visit_select(self, select,
                     asfrom=False, parens=True, iswrapper=False,
                     fromhints=None, compound_index=1, force_result_map=False,
                     nested_join_translation=False, **kwargs):
        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(sql._from_objects(*froms))

        # TODO: might want to propagate existing froms for
        # select(select(select)) where innermost select should correlate
        # to outermost if existingfroms: correlate_froms =
        # correlate_froms.union(existingfroms)

        self.stack.append({'from': correlate_froms,
                           'iswrapper': iswrapper})

        # the actual list of columns to print in the SELECT column list.

        unique_co = []
        distinct_alias = None
        for co in util.unique_list(select.inner_columns):
            sql_util = self._label_select_column(select, co, True, asfrom, {})
            if "DISTINCT" in sql_util:
                distinct_alias = sql_util.split(" AS ")[-1]
            unique_co.append(sql_util)
        result_columns = []
        if distinct_alias:
            for idx, rc_tuple in enumerate(self._result_columns):
                if rc_tuple[0] == distinct_alias:
                    if rc_tuple[-2][-1] == distinct_alias:
                        target_name = "@distinct"
                        temp_rc = list(rc_tuple)
                        temp_rc[0] = target_name
                        inner_tuple = list(temp_rc[-2])
                        inner_tuple[-1] = target_name
                        if not select._group_by_clause.clauses:
                            raise AssertionError("Can't query distinct if no group by  is selected")
                        temp_rc[-2] = tuple(inner_tuple)
                        result_columns.append(tuple(temp_rc))
                else:
                    result_columns.append(rc_tuple)
        if result_columns:
            self._result_columns = result_columns

        inner_columns = [
            c for c in unique_co
            if c is not None
        ]

        text = "SELECT "  # we're off to a good start !
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        text += " \nFROM "

        text += ', '.join([f._compiler_dispatch(self,
                                                asfrom=True, **kwargs)
                           for f in froms])

        def check_match_clause(clause):
            left_tuple = []
            right_tuple = []
            match_operators = []
            if isinstance(clause.type, MatchType):
                left_tuple.append(clause.left)
                right_tuple.append(clause.right)
                match_operators.append(clause.operator)
            elif isinstance(clause, Function):
                if clause.name.lower() == "match":
                    func_left, func_right = clause.clauses
                    left_tuple.append(func_left)
                    right_tuple.append(func_right)
            elif isinstance(clause, ClauseList):
                for xclause in clause.clauses:
                    l, r, m = check_match_clause(xclause)
                    left_tuple.extend(l)
                    right_tuple.extend(r)
                    match_operators.extend(m)
            return left_tuple, right_tuple, match_operators

        if select._whereclause is not None:
            # Match Clauses must be done in the same compiler
            left_tuple = []
            right_tuple = []
            match_operators = []
            l, r, m = check_match_clause(select._whereclause)
            left_tuple.extend(l)
            right_tuple.extend(r)
            match_operators.extend(m)

            if left_tuple and right_tuple:
                self.left_match = tuple(left_tuple)
                self.right_match = tuple(right_tuple)
                self.match_operators = tuple(match_operators)

            t = select._whereclause._compiler_dispatch(self, **kwargs)
            if t:
                text += " \nWHERE " + t

            if hasattr(self, "options_list"):
                if self.options_list:
                    option_text = " OPTION {}".format(", ".join(self.options_list))
                    text += option_text

        if select._group_by_clause.clauses:
            group_by = select._group_by_clause._compiler_dispatch(
                self, **kwargs)
            text += " GROUP BY " + group_by

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select, **kwargs)
        if select._limit is not None:
            text += self.limit_clause(select)

        self.stack.pop(-1)
        return text
Esempio n. 13
0
    def visit_select(self, select,
                     asfrom=False,
                     parens=True,
                     iswrapper=False,
                     fromhints=None,
                     compound_index=1,
                     force_result_map=False,
                     positional_name=None, **kwargs):

        entry = self.stack and self.stack[-1] or {}

        existingfroms = entry.get('from', None)

        froms = select._get_display_froms(existingfroms)

        correlate_froms = set(expression._from_objects(*froms))

        # TODO: might want to propagate existing froms for
        # select(select(select)) where innermost select should correlate
        # to outermost if existingfroms: correlate_froms =
        # correlate_froms.union(existingfroms)

        self.stack.append({'from': correlate_froms,
                           'iswrapper': iswrapper})

        if compound_index == 1 and not entry or entry.get('iswrapper', False):
            column_clause_args = {'result_map': self.result_map}
        else:
            column_clause_args = {}

        populate_result_map = force_result_map or (
            compound_index == 0 and (not entry or entry.get('iswrapper', False)))

        # the actual list of columns to print in the SELECT column list.
        inner_columns = [
            c for c in [
                self._label_select_column(select,
                                          column,
                                          populate_result_map,
                                          asfrom,
                                          column_clause_args,
                                          name=name)
                for name, column in select._columns_plus_names]
            if c is not None
        ]

        text = 'SELECT '

        if select._hints:
            byfrom = dict([
                            (from_, hinttext % {
                                'name':from_._compiler_dispatch(
                                    self, ashint=True)
                            })
                            for (from_, dialect), hinttext in
                            select._hints.iteritems()
                            if dialect in ('*', self.dialect.name)
                        ])
            hint_text = self.get_select_hint_text(byfrom)
            if hint_text:
                text += hint_text + ' '

        if select._prefixes:
            text += ' '.join(
                            x._compiler_dispatch(self, **kwargs)
                            for x in select._prefixes) + " "
        text += self.get_select_precolumns(select)
        text += ', '.join(inner_columns)

        if froms:
            text += ' \nFROM '

            if select._hints:
                text += ', '.join([f._compiler_dispatch(self,
                                    asfrom=True, fromhints=byfrom,
                                    **kwargs)
                                for f in froms])
            else:
                text += ', '.join([f._compiler_dispatch(self,
                                    asfrom=True, **kwargs)
                                for f in froms])
        else:
            text += self.default_from()

        if select._whereclause is not None:
            t = select._whereclause._compiler_dispatch(self, **kwargs)
            if t:
                text += ' \nWHERE ' + t

        if select._group_by_clause.clauses:
            group_by = select._group_by_clause._compiler_dispatch(
                                        self, **kwargs)
            if group_by:
                text += ' GROUP BY ' + group_by

        if select._having is not None:
            t = select._having._compiler_dispatch(self, **kwargs)
            if t:
                text += ' \nHAVING ' + t

        if select._order_by_clause.clauses:
            text += self.order_by_clause(select, **kwargs)
        if getattr(select, '_within_group_order_by_clause', None) is not None:
            if select._within_group_order_by_clause.clauses:
                text += self.within_group_order_by_clause(select, **kwargs)
        if select._limit is not None:
            text += self.limit_clause(select)
        if getattr(select, '_options', None) is not None:
            if select._options.options:
                text += self.options_clause(select, **kwargs)
        if select.for_update:
            text += self.for_update_clause(select)

        self.stack.pop(-1)

        if asfrom and parens:
            return "(" + text + ")"
        else:
            return text