816 sql = self._visit_relation(relation, rschema) |
816 sql = self._visit_relation(relation, rschema) |
817 return sql |
817 return sql |
818 |
818 |
819 def _visit_inlined_relation(self, relation): |
819 def _visit_inlined_relation(self, relation): |
820 lhsvar, _, rhsvar, rhsconst = relation_info(relation) |
820 lhsvar, _, rhsvar, rhsconst = relation_info(relation) |
821 # we are sure here to have a lhsvar |
821 # we are sure lhsvar is not None |
822 assert lhsvar is not None |
822 lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
823 if isinstance(relation.parent, Not) \ |
823 if rhsvar is None: |
824 and len(lhsvar.stinfo['relations']) > 1 \ |
824 moresql = None |
825 and (rhsvar is not None and rhsvar._q_invariant): |
825 else: |
|
826 moresql = self._extra_join_sql(relation, lhssql, rhsvar) |
|
827 if isinstance(relation.parent, Not): |
826 self._state.done.add(relation.parent) |
828 self._state.done.add(relation.parent) |
827 return '%s IS NULL' % self._inlined_var_sql(lhsvar, relation.r_type) |
829 if rhsvar is not None and rhsvar._q_invariant: |
828 lhssql = self._inlined_var_sql(lhsvar, relation.r_type) |
830 sql = '%s IS NULL' % lhssql |
829 if rhsconst is not None: |
831 else: |
830 return '%s=%s' % (lhssql, rhsconst.accept(self)) |
832 # column != 1234 may not get back rows where column is NULL... |
831 if isinstance(rhsvar, Variable) and not rhsvar.name in self._varmap: |
833 sql = '(%s IS NULL OR %s!=%s)' % ( |
|
834 lhssql, lhssql, (rhsvar or rhsconst).accept(self)) |
|
835 elif rhsconst is not None: |
|
836 sql = '%s=%s' % (lhssql, rhsconst.accept(self)) |
|
837 elif isinstance(rhsvar, Variable) and rhsvar._q_invariant and \ |
|
838 not rhsvar.name in self._varmap: |
832 # if the rhs variable is only linked to this relation, this mean we |
839 # if the rhs variable is only linked to this relation, this mean we |
833 # only want the relation to exists, eg NOT NULL in case of inlined |
840 # only want the relation to exists, eg NOT NULL in case of inlined |
834 # relation |
841 # relation |
835 if rhsvar._q_invariant: |
842 if moresql is not None: |
836 sql = self._extra_join_sql(relation, lhssql, rhsvar) |
843 return moresql |
837 if sql: |
844 return '%s IS NOT NULL' % lhssql |
838 return sql |
845 else: |
839 return '%s IS NOT NULL' % lhssql |
846 sql = '%s=%s' % (lhssql, rhsvar.accept(self)) |
840 return '%s=%s' % (lhssql, rhsvar.accept(self)) |
847 if moresql is None: |
|
848 return sql |
|
849 return '%s AND %s' % (sql, moresql) |
841 |
850 |
842 def _process_relation_term(self, relation, rid, termvar, termconst, relfield): |
851 def _process_relation_term(self, relation, rid, termvar, termconst, relfield): |
843 if termconst or not termvar._q_invariant: |
852 if termconst or not termvar._q_invariant: |
844 termsql = termconst and termconst.accept(self) or termvar.accept(self) |
853 termsql = termconst and termconst.accept(self) or termvar.accept(self) |
845 yield '%s.%s=%s' % (rid, relfield, termsql) |
854 yield '%s.%s=%s' % (rid, relfield, termsql) |
847 # if the variable is mapped, generate restriction anyway |
856 # if the variable is mapped, generate restriction anyway |
848 if termvar.name in self._varmap: |
857 if termvar.name in self._varmap: |
849 termsql = termvar.accept(self) |
858 termsql = termvar.accept(self) |
850 yield '%s.%s=%s' % (rid, relfield, termsql) |
859 yield '%s.%s=%s' % (rid, relfield, termsql) |
851 extrajoin = self._extra_join_sql(relation, '%s.%s' % (rid, relfield), termvar) |
860 extrajoin = self._extra_join_sql(relation, '%s.%s' % (rid, relfield), termvar) |
852 if extrajoin: |
861 if extrajoin is not None: |
853 yield extrajoin |
862 yield extrajoin |
854 |
863 |
855 def _visit_relation(self, relation, rschema): |
864 def _visit_relation(self, relation, rschema): |
856 """generate SQL for a relation |
865 """generate SQL for a relation |
857 |
866 |
1232 return '%s%s%s' % tuple(args) |
1241 return '%s%s%s' % tuple(args) |
1233 except KeyError: |
1242 except KeyError: |
1234 # no principal defined, relation is necessarily the principal and |
1243 # no principal defined, relation is necessarily the principal and |
1235 # so nothing to return here |
1244 # so nothing to return here |
1236 pass |
1245 pass |
1237 return '' |
1246 return None |
1238 |
1247 |
1239 def _temp_table_scope(self, select, table): |
1248 def _temp_table_scope(self, select, table): |
1240 scope = 9999 |
1249 scope = 9999 |
1241 for var, sql in self._varmap.iteritems(): |
1250 for var, sql in self._varmap.iteritems(): |
1242 # skip "attribute variable" in varmap (such 'T.login') |
1251 # skip "attribute variable" in varmap (such 'T.login') |