entity.py
branchstable
changeset 7302 c281afe35b8b
parent 7288 b156200ebc9d
child 7304 66fa1b7ac784
child 7308 ff8d6ae076c4
equal deleted inserted replaced
7298:a448e470c150 7302:c281afe35b8b
   202             var = varmaker.next()
   202             var = varmaker.next()
   203             selection.append(var)
   203             selection.append(var)
   204             restriction = '%s %s %s' % (mainvar, attr, var)
   204             restriction = '%s %s %s' % (mainvar, attr, var)
   205             restrictions.append(restriction)
   205             restrictions.append(restriction)
   206             if not rschema.final:
   206             if not rschema.final:
   207                 # XXX this does not handle several destination types
       
   208                 desttype = rschema.objects(eschema.type)[0]
       
   209                 card = rdef.cardinality[0]
   207                 card = rdef.cardinality[0]
   210                 if card not in '?1':
   208                 if card not in '?1':
   211                     cls.warning('bad relation %s specified in fetch attrs for %s',
   209                     cls.warning('bad relation %s specified in fetch attrs for %s',
   212                                  attr, cls)
   210                                  attr, cls)
   213                     selection.pop()
   211                     selection.pop()
   216                 # XXX we need outer join in case the relation is not mandatory
   214                 # XXX we need outer join in case the relation is not mandatory
   217                 # (card == '?')  *or if the entity is being added*, since in
   215                 # (card == '?')  *or if the entity is being added*, since in
   218                 # that case the relation may still be missing. As we miss this
   216                 # that case the relation may still be missing. As we miss this
   219                 # later information here, systematically add it.
   217                 # later information here, systematically add it.
   220                 restrictions[-1] += '?'
   218                 restrictions[-1] += '?'
       
   219                 targettypes = rschema.objects(eschema.type)
   221                 # XXX user._cw.vreg iiiirk
   220                 # XXX user._cw.vreg iiiirk
   222                 destcls = user._cw.vreg['etypes'].etype_class(desttype)
   221                 etypecls = user._cw.vreg['etypes'].etype_class(targettypes[0])
   223                 destcls._fetch_restrictions(var, varmaker, destcls.fetch_attrs,
   222                 if len(targettypes) > 1:
   224                                             selection, orderby, restrictions,
   223                     # find fetch_attrs common to all destination types
   225                                             user, ordermethod, visited=visited)
   224                     fetchattrs = user._cw.vreg['etypes'].fetch_attrs(targettypes)
       
   225                 else:
       
   226                     fetchattrs = etypecls.fetch_attrs
       
   227                 etypecls._fetch_restrictions(var, varmaker, fetchattrs,
       
   228                                              selection, orderby, restrictions,
       
   229                                              user, ordermethod, visited=visited)
   226             if ordermethod is not None:
   230             if ordermethod is not None:
   227                 orderterm = getattr(cls, ordermethod)(attr, var)
   231                 orderterm = getattr(cls, ordermethod)(attr, var)
   228                 if orderterm:
   232                 if orderterm:
   229                     orderby.append(orderterm)
   233                     orderby.append(orderterm)
   230         return selection, orderby, restrictions
   234         return selection, orderby, restrictions
   728             if targettypes is None:
   732             if targettypes is None:
   729                 targettypes = rschema.subjects(self.e_schema)
   733                 targettypes = rschema.subjects(self.e_schema)
   730             else:
   734             else:
   731                 restriction += ', X is IN (%s)' % ','.join(targettypes)
   735                 restriction += ', X is IN (%s)' % ','.join(targettypes)
   732             card = greater_card(rschema, targettypes, (self.e_schema,), 1)
   736             card = greater_card(rschema, targettypes, (self.e_schema,), 1)
       
   737         etypecls = self._cw.vreg['etypes'].etype_class(targettypes[0])
   733         if len(targettypes) > 1:
   738         if len(targettypes) > 1:
   734             fetchattrs_list = []
   739             fetchattrs = self._cw.vreg['etypes'].fetch_attrs(targettypes)
   735             for ttype in targettypes:
   740         else:
   736                 etypecls = self._cw.vreg['etypes'].etype_class(ttype)
   741             fetchattrs = etypecls.fetch_attrs
   737                 fetchattrs_list.append(set(etypecls.fetch_attrs))
   742         rql = etypecls.fetch_rql(self._cw.user, [restriction], fetchattrs,
   738             fetchattrs = reduce(set.intersection, fetchattrs_list)
   743                                  settype=False)
   739             rql = etypecls.fetch_rql(self._cw.user, [restriction], fetchattrs,
       
   740                                      settype=False)
       
   741         else:
       
   742             etypecls = self._cw.vreg['etypes'].etype_class(targettypes[0])
       
   743             rql = etypecls.fetch_rql(self._cw.user, [restriction], settype=False)
       
   744         # optimisation: remove ORDERBY if cardinality is 1 or ? (though
   744         # optimisation: remove ORDERBY if cardinality is 1 or ? (though
   745         # greater_card return 1 for those both cases)
   745         # greater_card return 1 for those both cases)
   746         if card == '1':
   746         if card == '1':
   747             if ' ORDERBY ' in rql:
   747             if ' ORDERBY ' in rql:
   748                 rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0],
   748                 rql = '%s WHERE %s' % (rql.split(' ORDERBY ', 1)[0],