server/sources/rql2sql.py
changeset 6333 e3994fcc21c3
parent 6297 23c1e50ff97b
child 6491 ee9a10b6620e
equal deleted inserted replaced
6293:df44d7163582 6333:e3994fcc21c3
   759         restrictions, tables = self._state.pop_scope()
   759         restrictions, tables = self._state.pop_scope()
   760         if restriction:
   760         if restriction:
   761             restrictions.append(restriction)
   761             restrictions.append(restriction)
   762         restriction = ' AND '.join(restrictions)
   762         restriction = ' AND '.join(restrictions)
   763         if not restriction:
   763         if not restriction:
       
   764             if tables:
       
   765                 return 'SELECT 1 FROM %s' % ', '.join(tables)
   764             return ''
   766             return ''
   765         if not tables:
   767         if not tables:
   766             # XXX could leave surrounding EXISTS() in this case no?
   768             # XXX could leave surrounding EXISTS() in this case no?
   767             sql = 'SELECT 1 WHERE %s' % restriction
   769             sql = 'SELECT 1 WHERE %s' % restriction
   768         else:
   770         else:
  1139         # func_as_sql will check function is supported by the backend
  1141         # func_as_sql will check function is supported by the backend
  1140         return self.dbhelper.func_as_sql(func.name, args)
  1142         return self.dbhelper.func_as_sql(func.name, args)
  1141 
  1143 
  1142     def visit_constant(self, constant):
  1144     def visit_constant(self, constant):
  1143         """generate SQL name for a constant"""
  1145         """generate SQL name for a constant"""
  1144         value = constant.value
       
  1145         if constant.type is None:
  1146         if constant.type is None:
  1146             return 'NULL'
  1147             return 'NULL'
       
  1148         value = constant.value
  1147         if constant.type == 'Int' and  isinstance(constant.parent, SortTerm):
  1149         if constant.type == 'Int' and  isinstance(constant.parent, SortTerm):
  1148             return constant.value
  1150             return value
  1149         if constant.type in ('Date', 'Datetime'):
  1151         if constant.type in ('Date', 'Datetime'):
  1150             rel = constant.relation()
  1152             rel = constant.relation()
  1151             if rel is not None:
  1153             if rel is not None:
  1152                 rel._q_needcast = value
  1154                 rel._q_needcast = value
  1153             return self.keyword_map[value]()
  1155             return self.keyword_map[value]()
  1156         if constant.type == 'Substitute':
  1158         if constant.type == 'Substitute':
  1157             try:
  1159             try:
  1158                 # we may found constant from simplified var in varmap
  1160                 # we may found constant from simplified var in varmap
  1159                 return self._mapped_term(constant, '%%(%s)s' % value)[0]
  1161                 return self._mapped_term(constant, '%%(%s)s' % value)[0]
  1160             except KeyError:
  1162             except KeyError:
  1161                 _id = constant.value
  1163                 _id = value
  1162                 if isinstance(_id, unicode):
  1164                 if isinstance(_id, unicode):
  1163                     _id = _id.encode()
  1165                     _id = _id.encode()
  1164         else:
  1166         else:
  1165             _id = str(id(constant)).replace('-', '', 1)
  1167             _id = str(id(constant)).replace('-', '', 1)
  1166             self._query_attrs[_id] = value
  1168             self._query_attrs[_id] = value