def getNoAccessIds(attr, trans):
     lstNoAccess = []
     for sID in attr.value:
         oItem = dbEnv.getItem(sID, trans)
         # do not replay in case of txn abort
         del trans.actions[-1]
         
         if not(oItem):
             lstNoAccess.append(sID)
     return lstNoAccess
 def getParent(self, trans=None):
     """
     Returns the parent container.
             
     @param trans: A valid transaction handle
         
     @return: the parent container object
     @rtype: type
     """
     return(dbEnv.getItem(self._parentid, trans))
 def getChildByName(self, name, trans=None):
     """
     This method returns the child with the specified name.
     
     @param name: The name of the child
     @type name: str
     
     @param trans: A valid transaction handle
         
     @return: The child object if a child with the given name exists
              else None.
     @rtype: type
     """
     if (self._items.has_key(name)):
         oChild = dbEnv.getItem(self._items[name], trans)
         return(oChild)
     elif (self._subfolders.has_key(name)):
         oChild = dbEnv.getItem(self._subfolders[name], trans)
         return(oChild)
     else:
         return(None)
 def getItem(self, trans=None):
     """
     This method returns the object that this data type
     instance references. If the current user has no read
     permission on the referenced item then it returns None.
     
     @param trans: A valid transaction handle
     
     @rtype: type
     @return: The referenced object, otherwise None
     """
     if self.value:
         return(dbEnv.getItem(self.value, trans))
     return(None)
 def getSubFolders(self, trans=None):
     """
     This method returns the children that are containers.
     
     @param trans: A valid transaction handle
         
     @rtype: L{ObjectSet<porcupine.core.objectSet.ObjectSet>}
     """
     lstFolders = self._subfolders.values()
     subFolders = []
     for sID in lstFolders:
         oChild = dbEnv.getItem(sID)
         if oChild:
             subFolders.append(oChild)
     return(objectSet.ObjectSet(subFolders))
 def getItems(self, trans=None):
     """
     This method returns the children that are not containers.
     
     @param trans: A valid transaction handle
     
     @rtype: L{ObjectSet<porcupine.core.objectSet.ObjectSet>}
     """
     lstItems = self._items.values()
     items = []
     for sID in lstItems:
         oChild = dbEnv.getItem(sID, trans)
         if oChild:
             items.append(oChild)
     return(objectSet.ObjectSet(items))
 def getItems(self, trans=None):
     """
     This method returns the items that this data type
     instance references.
     
     @param trans: A valid transaction handle
     
     @rtype: L{ObjectSet<porcupine.core.objectSet.ObjectSet>}
     """
     lstItems = []
     for sID in self.value:
         oItem = dbEnv.getItem(sID, trans)
         if oItem:
             lstItems.append(oItem)
     return(objectSet.ObjectSet(lstItems))
 def getItem(self, trans=None):
     """
     This method returns the object that this data type
     instance references. If the current user has no read
     permission on the referenced item or it has been deleted
     then it returns None.
     
     @param trans: A valid transaction handle
     
     @rtype: type
     @return: The referenced object, otherwise None
     """
     oItem = None
     if self.value:
         try:
             oItem = dbEnv.getItem(self.value, trans)
         except serverExceptions.DBItemNotFound:
             pass
     return(oItem)
Exemplo n.º 9
0
def select(deep, children, fields, condition, variables):
    results = []
    
    for child in [dbEnv.getItem(child_id) for child_id in children]:
        if child:
            if condition:
                res = evaluateStack(condition[:], variables, child)
            else:
                res = True
            
            if res:
                fieldlist = [evaluateStack(expr[1], variables, child) for expr in fields]
                results.append(tuple(fieldlist))
                
            if deep and child.isCollection:
                children_ids = child._subfolders.values() + child._items.values()
                results1 = select(deep, children_ids, fields, condition, variables)
                results.extend(results1)

    return (results)
Exemplo n.º 10
0
def h_200(params, variables, forObject = None):
    select_fields = params[0]
    
    # get aliases
    aliases = []
    for expr, alias, aggr in select_fields:
        if expr != alias:
            variables[alias] = (expr, None, None)
            aliases.append(alias)

    field_names = [ x[1] for x in select_fields ]
    expressions = [ tuple(x[0::2]) for x in select_fields ]
    
    select_from = params[1]
    where_condition = params[2]
    
    if params[3]:
        sort_order, order_by = params[3]
        
        for ind, order_field in enumerate(order_by):
            expr, alias, aggr = order_field
            if alias in aliases:
                order_by[ind] = select_fields[ field_names.index(alias) ]
            elif expr != alias:
                if (expr, aggr) in expressions:
                    order_by[ind] = select_fields[ expressions.index((expr, aggr)) ]
#                    print 'caching expression: ' + repr(order_by[ind])
                else:
                    variables[alias] = (expr, None, None)
                    aliases.append(alias)
    else:
        order_by = []

    group_by = params[4]
    if group_by:
        for ind, group_field in enumerate(group_by):
            expr, alias, aggr = group_field
            if alias in aliases:
                group_by[ind] = select_fields[ field_names.index(alias) ]
            elif expr != alias:
                if (expr, aggr) in expressions:
                    group_by[ind] = select_fields[ expressions.index((expr, aggr)) ]
#                    print 'caching expression: ' + repr(group_by[ind])
                else:
                    variables[alias] = (expr, None, None)
                    aliases.append(alias)

    if select_fields:
        all_fields = select_fields + order_by + group_by
    else:
        all_fields = [['id','id', '']] + order_by + group_by
    
    aggregates = [x[2] for x in all_fields]
    results = []
    
    for deep, object_id in select_from:
        if deep==2:
            # this.attr
            if not forObject:
                raise TypeError, 'Inner scopes using "this:" are valid only in subqueries'
            if hasattr(forObject, object_id):
                refObjects = getattr(forObject, object_id).value
                r = select(0, refObjects, all_fields, where_condition, variables)
                results.extend(r)
        else:
            # swallow-deep
            obj = dbEnv.getItem(object_id)    
            if obj and obj.isCollection:
                children = obj._subfolders.values() + obj._items.values()
                r = select(deep, children, all_fields, 
                                where_condition, variables)
                results.extend(r)
#    print results
    if results:
        if group_by:
            if select_fields:
                #construct groups
                group_dict = {}
                igrpstart = len(select_fields) + len(order_by)
                igrpend = igrpstart + len(group_by)
                for rec in results:
                    group_value = tuple(rec[igrpstart:igrpend])
                    group_dict[group_value] = group_dict.setdefault(group_value, [])
                    group_dict[group_value].append(rec)
                groups = [tuple(g) for g in group_dict.values()]
            else:
                raise TypeError, 'GROUP BY clause is incompatible with SELECT *'
        else:
            groups = [results]
        
#        print len(groups)
        results = []

        if aggregates != [''] * len(aggregates) or group_by:
            for ind, group in enumerate(groups):
                group_sum = []
                for aggr_index, aggr_type in enumerate(aggregates):
                    # aggregates exclude None values
                    group_sum.append( computeAggregate(aggr_type, [x[aggr_index] for x in group if x[aggr_index] != None]) )
                groups[ind] = (tuple(group_sum),)

        for group in groups:
            results.extend(group)

        if order_by:
            # extract sort values
            istart = 1
            if select_fields:
                istart = len(select_fields)
            sortlist = tuple([x[istart:istart + len(order_by)] for x in results])
            results = sortList(sortlist, results)
            # if it is descending reverse the result list
            if not sort_order: results.reverse()
    
    # remove aliases
    for alias in aliases:
        del variables[alias]
    
    if select_fields:
        schema = field_names
        # truncate to keep only select fields
        iend = len(select_fields)
        results = tuple([x[:iend] for x in results])
    else:
        schema = None
        results = tuple([x[:1][0] for x in results])
        
#    print results

    return objectSet.ObjectSet(tuple(results), schema)