Beispiel #1
0
    def add_join(self, connection, implicit=True, outer=False, extra=None, extra_params=[]):
        """ Join a destination table to the current table.

            :param implicit: False if the join is an explicit join. This allows
                to fall back on the previous implementation of ``join`` before
                OpenERP 7.0. It therefore adds the JOIN specified in ``connection``
                If True, the join is done implicitely, by adding the table alias
                in the from clause and the join condition in the where clause
                of the query. Implicit joins do not handle outer, extra, extra_params parameters.
            :param connection: a tuple ``(lhs, table, lhs_col, col, link)``.
                The join corresponds to the SQL equivalent of::

                (lhs.lhs_col = table.col)

                Note that all connection elements are strings. Please refer to expression.py for more details about joins.

            :param outer: True if a LEFT OUTER JOIN should be used, if possible
                      (no promotion to OUTER JOIN is supported in case the JOIN
                      was already present in the query, as for the moment
                      implicit INNER JOINs are only connected from NON-NULL
                      columns so it would not be correct (e.g. for
                      ``_inherits`` or when a domain criterion explicitly
                      adds filtering)

            :param extra: A string with the extra join condition (SQL), or None.
                This is used to provide an additional condition to the join
                clause that cannot be added in the where clause (e.g., for LEFT
                JOIN concerns). The condition string should refer to the table
                aliases as "{lhs}" and "{rhs}".

            :param extra_params: a list of parameters for the `extra` condition.
        """
        from openerp.osv.expression import generate_table_alias
        (lhs, table, lhs_col, col, link) = connection
        alias, alias_statement = generate_table_alias(lhs, [(table, link)])

        if implicit:
            if alias_statement not in self.tables:
                self.tables.append(alias_statement)
                condition = '("%s"."%s" = "%s"."%s")' % (lhs, lhs_col, alias, col)
                self.where_clause.append(condition)
            else:
                # already joined
                pass
            return alias, alias_statement
        else:
            aliases = self._get_table_aliases()
            assert lhs in aliases, "Left-hand-side table %s must already be part of the query tables %s!" % (lhs, str(self.tables))
            if alias_statement in self.tables:
                # already joined, must ignore (promotion to outer and multiple joins not supported yet)
                pass
            else:
                # add JOIN
                self.tables.append(alias_statement)
                join_tuple = (alias, lhs_col, col, outer and 'LEFT JOIN' or 'JOIN')
                self.joins.setdefault(lhs, []).append(join_tuple)
                if extra:
                    extra = extra.format(lhs=lhs, rhs=alias)
                    self.extras[(lhs, join_tuple)] = (extra, extra_params)
            return alias, alias_statement
Beispiel #2
0
    def add_join(self, connection, implicit=True, outer=False, extra=None, extra_params=[]):
        """ Join a destination table to the current table.

            :param implicit: False if the join is an explicit join. This allows
                to fall back on the previous implementation of ``join`` before
                OpenERP 7.0. It therefore adds the JOIN specified in ``connection``
                If True, the join is done implicitely, by adding the table alias
                in the from clause and the join condition in the where clause
                of the query. Implicit joins do not handle outer, extra, extra_params parameters.
            :param connection: a tuple ``(lhs, table, lhs_col, col, link)``.
                The join corresponds to the SQL equivalent of::

                (lhs.lhs_col = table.col)

                Note that all connection elements are strings. Please refer to expression.py for more details about joins.

            :param outer: True if a LEFT OUTER JOIN should be used, if possible
                      (no promotion to OUTER JOIN is supported in case the JOIN
                      was already present in the query, as for the moment
                      implicit INNER JOINs are only connected from NON-NULL
                      columns so it would not be correct (e.g. for
                      ``_inherits`` or when a domain criterion explicitly
                      adds filtering)

            :param extra: A string with the extra join condition (SQL), or None.
                This is used to provide an additional condition to the join
                clause that cannot be added in the where clause (e.g., for LEFT
                JOIN concerns). The condition string should refer to the table
                aliases as "{lhs}" and "{rhs}".

            :param extra_params: a list of parameters for the `extra` condition.
        """
        from openerp.osv.expression import generate_table_alias
        (lhs, table, lhs_col, col, link) = connection
        alias, alias_statement = generate_table_alias(lhs, [(table, link)])

        if implicit:
            if alias_statement not in self.tables:
                self.tables.append(alias_statement)
                condition = '("%s"."%s" = "%s"."%s")' % (lhs, lhs_col, alias, col)
                self.where_clause.append(condition)
            else:
                # already joined
                pass
            return alias, alias_statement
        else:
            aliases = self._get_table_aliases()
            assert lhs in aliases, "Left-hand-side table %s must already be part of the query tables %s!" % (lhs, str(self.tables))
            if alias_statement in self.tables:
                # already joined, must ignore (promotion to outer and multiple joins not supported yet)
                pass
            else:
                # add JOIN
                self.tables.append(alias_statement)
                join_tuple = (alias, lhs_col, col, outer and 'LEFT JOIN' or 'JOIN')
                self.joins.setdefault(lhs, []).append(join_tuple)
                if extra or extra_params:
                    extra = (extra or '').format(lhs=lhs, rhs=alias)
                    self.extras[(lhs, join_tuple)] = (extra, extra_params)
            return alias, alias_statement