Exemple #1
0
	def destroy(self, storable, destroy_related_storables=False):
		"""
		Destroy the provided Storable.
		
		@see: L{IFactory.destroy()}
		
		@param storable: the object you wish to destroy
		@type storable: L{storable.Storable}
		
		@param destroy_related_storables: should the items returned by
			L{Storable.get_related_storables()} be automatically destroyed?
		@type destroy_related_storables: bool
		"""
		if not(storable.is_new()):
			child_list = storable.get_related_storables()
			id_list = []
			while(child_list and destroy_related_storables):
				child = child_list.pop()
				child_id = self.fetch_id(child)
				child_table = child.get_table()
				if(child_table not in self._factories):
					raise LookupError('There is no factory registered for the table `%s`' % child_table)
				factory = self._factories[child_table]
				if(child_id in id_list and factory.uses_guids()):
					#raise AssertionError('Found circular storable reference during destroy')
					continue
				child_list.extend(child.get_related_storables())
				self._destroy(child)
				id_list.append(child_id)
			self._destroy(storable)
Exemple #2
0
	def _save(self, storable, factory):
		"""
		Internal function that is responsible for doing the
		actual saving.
		"""
		if not(storable.is_dirty()):
			return False
		
		table = storable.get_table()
		data = storable.get_data()
		
		locks_allowed = (not bool(self.req)) or self.req.app.config.get('use_db_locks', True)
		
		use_locks = False
		primary_key = factory.get_primary_key()
		
		if(storable.is_new()):
			if(factory.uses_guids()):
				data[primary_key] = self.fetch_id(storable)
			elif(locks_allowed):
				use_locks = True
				self.pool.runOperation('LOCK TABLES `%s` WRITE' % table)
			else:
				raise RuntimeError("Broken program logic is trying to lock an unlockable database.")
			
			query = sql.build_insert(table, data)
		else:
			query = sql.build_update(table, data, {primary_key:storable.get_id()})
		
		self.log(query)
		
		try:
			self.pool.runOperation(query)
			
			if not(factory.uses_guids()):
				if not(storable.get_id()):
					rows = self.pool.runQuery('SELECT MAX(%s) AS `id` FROM `%s`' % (primary_key, table))
					new_id = rows[0]['id']
					storable.set_id(new_id)
		finally:
			if(use_locks):
				self.pool.runOperation('UNLOCK TABLES')
		
		storable.clean()
		storable.set_new(False)
		storable.set_factory(factory)
		
		return True