server/sources/rql2sql.py
branchstable
changeset 3762 e416186fb91c
parent 3754 a9c641f6193b
child 3787 82bb2c7f083b
equal deleted inserted replaced
3761:2c3b72faf05d 3762:e416186fb91c
   914                 # the variable is using the fti table, no join needed
   914                 # the variable is using the fti table, no join needed
   915                 jointo = None
   915                 jointo = None
   916             elif not lhsvar.name in self._varmap:
   916             elif not lhsvar.name in self._varmap:
   917                 # join on entities instead of etype's table to get result for
   917                 # join on entities instead of etype's table to get result for
   918                 # external entities on multisources configurations
   918                 # external entities on multisources configurations
   919                 ealias = lhsvar._q_sqltable = lhsvar.name
   919                 ealias = lhsvar._q_sqltable = '_' + lhsvar.name
   920                 jointo = lhsvar._q_sql = '%s.eid' % ealias
   920                 jointo = lhsvar._q_sql = '%s.eid' % ealias
   921                 self.add_table('entities AS %s' % ealias, ealias)
   921                 self.add_table('entities AS %s' % ealias, ealias)
   922                 if not lhsvar._q_invariant or len(lhsvar.stinfo['possibletypes']) == 1:
   922                 if not lhsvar._q_invariant or len(lhsvar.stinfo['possibletypes']) == 1:
   923                     restriction = " AND %s.type='%s'" % (ealias, self._state.solution[lhs.name])
   923                     restriction = " AND %s.type='%s'" % (ealias, self._state.solution[lhs.name])
   924                 else:
   924                 else:
  1036             sql = self._linked_var_sql(variable, contextrels)
  1036             sql = self._linked_var_sql(variable, contextrels)
  1037         elif variable._q_invariant:
  1037         elif variable._q_invariant:
  1038             # since variable is invariant, we know we won't found final relation
  1038             # since variable is invariant, we know we won't found final relation
  1039             principal = variable.stinfo['principal']
  1039             principal = variable.stinfo['principal']
  1040             if principal is None:
  1040             if principal is None:
  1041                 vtablename = variable.name
  1041                 vtablename = '_' + variable.name
  1042                 self.add_table('entities AS %s' % vtablename, vtablename)
  1042                 self.add_table('entities AS %s' % vtablename, vtablename)
  1043                 sql = '%s.eid' % vtablename
  1043                 sql = '%s.eid' % vtablename
  1044                 if variable.stinfo['typerels']:
  1044                 if variable.stinfo['typerels']:
  1045                     # add additional restriction on entities.type column
  1045                     # add additional restriction on entities.type column
  1046                     pts = variable.stinfo['possibletypes']
  1046                     pts = variable.stinfo['possibletypes']
  1096             scope = -1 - self._not_scope_offset
  1096             scope = -1 - self._not_scope_offset
  1097         else:
  1097         else:
  1098             scope = -1
  1098             scope = -1
  1099         try:
  1099         try:
  1100             sql = self._varmap[var.name]
  1100             sql = self._varmap[var.name]
  1101             table = sql.split('.', 1)[0]
  1101             tablealias = sql.split('.', 1)[0]
  1102             if scope < 0:
  1102             if scope < 0:
  1103                 scope = self._varmap_table_scope(var.stmt, table)
  1103                 scope = self._varmap_table_scope(var.stmt, tablealias)
  1104             self.add_table(table, scope=scope)
  1104             self.add_table(tablealias, scope=scope)
  1105         except KeyError:
  1105         except KeyError:
  1106             etype = self._state.solution[var.name]
  1106             etype = self._state.solution[var.name]
  1107             # XXX this check should be moved in rql.stcheck
  1107             # XXX this check should be moved in rql.stcheck
  1108             if self.schema.eschema(etype).final:
  1108             if self.schema.eschema(etype).final:
  1109                 raise BadRQLQuery(var.stmt.root)
  1109                 raise BadRQLQuery(var.stmt.root)
  1110             table = var.name
  1110             tablealias = '_' + var.name
  1111             sql = '%s.%seid' % (table, SQL_PREFIX)
  1111             sql = '%s.%seid' % (tablealias, SQL_PREFIX)
  1112             self.add_table('%s%s AS %s' % (SQL_PREFIX, etype, table), table,
  1112             self.add_table('%s%s AS %s' % (SQL_PREFIX, etype, tablealias),
  1113                            scope=scope)
  1113                            tablealias, scope=scope)
  1114         return sql, table
  1114         return sql, tablealias
  1115 
  1115 
  1116     def _inlined_var_sql(self, var, rtype):
  1116     def _inlined_var_sql(self, var, rtype):
  1117         try:
  1117         try:
  1118             sql = self._varmap['%s.%s' % (var.name, rtype)]
  1118             sql = self._varmap['%s.%s' % (var.name, rtype)]
  1119             scope = var.sqlscope is var.stmt and 0 or -1
  1119             scope = var.sqlscope is var.stmt and 0 or -1