server/sources/rql2sql.py
branchstable
changeset 2199 bd0a0f219751
parent 2073 173c646981a7
child 2354 9b4bac626977
equal deleted inserted replaced
2198:ac45d4dbaf76 2199:bd0a0f219751
   488                 sql.insert(1, 'FROM %s' % ', '.join(sorted(tables)))
   488                 sql.insert(1, 'FROM %s' % ', '.join(sorted(tables)))
   489             elif self._state.restrictions and self.dbms_helper.needs_from_clause:
   489             elif self._state.restrictions and self.dbms_helper.needs_from_clause:
   490                 sql.insert(1, 'FROM (SELECT 1) AS _T')
   490                 sql.insert(1, 'FROM (SELECT 1) AS _T')
   491             sqls.append('\n'.join(sql))
   491             sqls.append('\n'.join(sql))
   492         if select.need_intersect:
   492         if select.need_intersect:
   493             if distinct or not self.dbms_helper.intersect_all_support:
   493             #if distinct or not self.dbms_helper.intersect_all_support:
   494                 return '\nINTERSECT\n'.join(sqls)
   494             return '\nINTERSECT\n'.join(sqls)
   495             else:
   495             #else:
   496                 return '\nINTERSECT ALL\n'.join(sqls)
   496             #    return '\nINTERSECT ALL\n'.join(sqls)
   497         elif distinct:
   497         elif distinct:
   498             return '\nUNION\n'.join(sqls)
   498             return '\nUNION\n'.join(sqls)
   499         else:
   499         else:
   500             return '\nUNION ALL\n'.join(sqls)
   500             return '\nUNION ALL\n'.join(sqls)
   501 
   501 
   659 
   659 
   660     def _visit_inlined_relation(self, relation):
   660     def _visit_inlined_relation(self, relation):
   661         lhsvar, _, rhsvar, rhsconst = relation_info(relation)
   661         lhsvar, _, rhsvar, rhsconst = relation_info(relation)
   662         # we are sure here to have a lhsvar
   662         # we are sure here to have a lhsvar
   663         assert lhsvar is not None
   663         assert lhsvar is not None
   664         lhssql = self._inlined_var_sql(lhsvar, relation.r_type)
       
   665         if isinstance(relation.parent, Not):
   664         if isinstance(relation.parent, Not):
   666             self._state.done.add(relation.parent)
   665             self._state.done.add(relation.parent)
   667             sql = "%s IS NULL" % lhssql
       
   668             if rhsvar is not None and not rhsvar._q_invariant:
   666             if rhsvar is not None and not rhsvar._q_invariant:
   669                 sql = '(%s OR %s!=%s)' % (sql, lhssql, rhsvar.accept(self))
   667                 # if the lhs variable is only linked to this relation, this mean we
       
   668                 # only want the relation to NOT exists
       
   669                 self._state.push_scope()
       
   670                 lhssql = self._inlined_var_sql(lhsvar, relation.r_type)
       
   671                 rhssql = rhsvar.accept(self)
       
   672                 restrictions, tables = self._state.pop_scope()
       
   673                 restrictions.append('%s=%s' % (lhssql, rhssql))
       
   674                 if not tables:
       
   675                     sql = 'NOT EXISTS(SELECT 1 WHERE %s)' % (
       
   676                         ' AND '.join(restrictions))
       
   677                 else:
       
   678                     sql = 'NOT EXISTS(SELECT 1 FROM %s WHERE %s)' % (
       
   679                         ', '.join(tables), ' AND '.join(restrictions))
       
   680             else:
       
   681                 lhssql = self._inlined_var_sql(lhsvar, relation.r_type)
       
   682                 sql = '%s IS NULL' % self._inlined_var_sql(lhsvar, relation.r_type)
   670             return sql
   683             return sql
       
   684         lhssql = self._inlined_var_sql(lhsvar, relation.r_type)
   671         if rhsconst is not None:
   685         if rhsconst is not None:
   672             return '%s=%s' % (lhssql, rhsconst.accept(self))
   686             return '%s=%s' % (lhssql, rhsconst.accept(self))
   673         if isinstance(rhsvar, Variable) and not rhsvar.name in self._varmap:
   687         if isinstance(rhsvar, Variable) and not rhsvar.name in self._varmap:
   674             # if the rhs variable is only linked to this relation, this mean we
   688             # if the rhs variable is only linked to this relation, this mean we
   675             # only want the relation to exists, eg NOT NULL in case of inlined
   689             # only want the relation to exists, eg NOT NULL in case of inlined