Beispiel #1
0
	def _destroy(self, storable):
		"""
		Internal function that is responsible for doing the
		actual destruction.
		"""
		factory = storable.get_factory()
		if(factory.uses_guids()):
			primary_key = factory.get_primary_key()
			delete_query = sql.build_delete(storable.get_table(), {primary_key:storable.get_id()})
		else:
			delete_query = sql.build_delete(storable.get_table(), storable.get_data())

		self.pool.runOperation(delete_query)
		storable.reset_id()
Beispiel #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