691 |
691 |
692 def _visit_inlined_relation(self, relation): |
692 def _visit_inlined_relation(self, relation): |
693 lhsvar, _, rhsvar, rhsconst = relation_info(relation) |
693 lhsvar, _, rhsvar, rhsconst = relation_info(relation) |
694 # we are sure here to have a lhsvar |
694 # we are sure here to have a lhsvar |
695 assert lhsvar is not None |
695 assert lhsvar is not None |
696 if isinstance(relation.parent, Not): |
696 if isinstance(relation.parent, Not) \ |
|
697 and len(lhsvar.stinfo['relations']) > 1 \ |
|
698 and (rhsvar is None or rhsvar._q_invariant): |
697 self._state.done.add(relation.parent) |
699 self._state.done.add(relation.parent) |
698 if rhsvar is not None and not rhsvar._q_invariant: |
700 return '%s IS NULL' % self._inlined_var_sql(lhsvar, relation.r_type) |
699 # if the lhs variable is only linked to this relation, this mean we |
|
700 # only want the relation to NOT exists |
|
701 self._state.push_scope() |
|
702 lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
|
703 rhssql = rhsvar.accept(self) |
|
704 restrictions, tables = self._state.pop_scope() |
|
705 restrictions.append('%s=%s' % (lhssql, rhssql)) |
|
706 if not tables: |
|
707 sql = 'NOT EXISTS(SELECT 1 WHERE %s)' % ( |
|
708 ' AND '.join(restrictions)) |
|
709 else: |
|
710 sql = 'NOT EXISTS(SELECT 1 FROM %s WHERE %s)' % ( |
|
711 ', '.join(tables), ' AND '.join(restrictions)) |
|
712 else: |
|
713 lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
|
714 sql = '%s IS NULL' % self._inlined_var_sql(lhsvar, relation.r_type) |
|
715 return sql |
|
716 lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
701 lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
717 if rhsconst is not None: |
702 if rhsconst is not None: |
718 return '%s=%s' % (lhssql, rhsconst.accept(self)) |
703 return '%s=%s' % (lhssql, rhsconst.accept(self)) |
719 if isinstance(rhsvar, Variable) and not rhsvar.name in self._varmap: |
704 if isinstance(rhsvar, Variable) and not rhsvar.name in self._varmap: |
720 # if the rhs variable is only linked to this relation, this mean we |
705 # if the rhs variable is only linked to this relation, this mean we |