def add(self, record, key = None): """ Insert a new entry in the database. New entries MUST be added with this method, not via an update with a hand-made Key. key is only useful for importing an existing database, by proposing a key choice. """ self._id, key = Tools.id_make (self._id, key) key = Store.Key (key) assert not self.has_key (key), \ _("a duplicate key has been generated: %d") % key record = copy.copy (record) record.key = key record = self.validate (record) self._dict [key] = record if self._indexed: self._idxadd(key, record) self.emit ('add-item', key) return key
def write (self, fd, rs, db): """ Write a result set to a given file descriptor """ self.db = db self.rs = rs self.doctype = {} for v in db.schema.txo['doctype'].values (): self.doctype [v.names ['C'].lower ()] = v for e in rs.itervalues (): self.record = e self.field = {} self.type = None self.key = None self.to_delete = False self.record_begin () for k, v in e.items (): self.record_parse (k, v) self.record_end () if self.to_delete: continue # Fully support the (bad) case where there is no key in # the record, in order to support bad behaved applications # that use it. if self.key is None: key = '' else: key = self.key + ',' ret = '@%s{%s\n' % (self.type, key) attrs = [] keys = self.field.keys () keys.sort () maxlen = 0 for k in keys: l = len (k) if l > maxlen: maxlen = l for k in keys: v = self.field [k] left = ' %s%s = ' % (k, ' ' * (maxlen - len (k))) attrs.append (left + Tools.format (v, 75, 0, len (left))) fd.write (ret + ',\n'.join (attrs) + '\n}\n') return
def testIdMake (self): """ Check the id generator """ # No proposal k = Tools.id_make (1, None) assert k == (2, 1) # The proposed id is smaller than the latest k = Tools.id_make (2, 1) assert k == (2, 1) # The proposed id is larget than the latest k = Tools.id_make (2, 4) assert k == (5, 4) return
def new(self, rsid=None): """ Create an empty result set """ db = self._db() assert db is not None (self._id, rsid) = Tools.id_make(self._id, rsid) # a result set keeps a strong reference on the database, as it # accesses its content pretty naturally rs = resultset.ResultSet(rsid, db) return rs