Пример #1
0
 def update(self, adict):
     """ Part of dictionary interface for field access"""
     if not self.mutable:
         raise ValueError, "instance isn't mutable!"
     if not self._unique:
         raise ValueError, "cannot update without unique index!"
     # call (normally empty) hook for modifying the update
     d=self.onUpdate(adict)
     # Check that the fields in the dictionary are kosher
     self._validateFields(d)
     # do the actual update
     self._update_raw(d)
     # if successful, modify the object's field data,
     # taking any wrapped values out of their wrappers
     unwrapped=dict((k, unwrap(v)) for k,v in d.iteritems())
     super(PyDO, self).update(unwrapped)
Пример #2
0
    def new(cls, refetch=None,  **fieldData):
        """create and return a new data class instance using the values in
        fieldData.  This will also effect an INSERT into the database.  If refetch
        is true, effectively do a getUnique on cls.
        """
        if not cls.mutable:
            raise ValueError, 'cannot make a new immutable object!'
        if refetch and not cls._unique:
            raise ValueError, "cannot refetch without a unique index!"
        # sanity check the field data
        cls._validateFields(fieldData)
        
        conn = cls.getDBI()

        if not conn.auto_increment:
            for s, sn in cls._sequenced.items():
                if not fieldData.has_key(s):
                    fieldData[s] = conn.getSequence(sn)
        cols=fieldData.keys()
        vals=[fieldData[c] for c in cols]
        converter=conn.getConverter()
        converted=map(converter, vals)
        
        sql = 'INSERT INTO %s (%s) VALUES  (%s)' \
              % (cls.table,
                 ', '.join(cols),
                 ', '.join(converted))
        res = conn.execute(sql, converter.values)
        if res != 1:
            raise PyDOError, "inserted %s rows instead of 1" % res
        
        if conn.auto_increment:
            for k, v in cls._sequenced.items():
                if not fieldData.has_key(k):
                    fieldData[k] = conn.getAutoIncrement(v)
        # unwrap any wrapped values in fieldData
        fieldData=dict((k, unwrap(v)) for k,v in fieldData.iteritems())
        if not refetch:
            return cls(fieldData)
        return cls.getUnique(**fieldData)