示例#1
0
文件: ref_group.py 项目: drizzd/germ
	def __session_val(self, match):
		if match.group(2) is not None:
			varname = match.group(2)[1:]
			ent_str = match.group(1)

			from helper import get_entity
			entity = get_entity(ent_str, self.__session,
					self.__ent.get_globals())

			val = entity.magic_var(varname)
			if val is None:
				from germ.error.error import error
				raise error(error.error, 'Invalid magic variable',
						'entity: %s, varname: %s' % (ent_str, varname))

			return str(val)

		varname = match.group(1)

		val = self.__ent.magic_var(varname)
		if val is not None:
			return val

		if not self.__session.has_key(varname):
			# this will evaluate all comparisons to NULL, i.e. false
			return 'NULL'

		val = self.__session[varname]

		if not isinstance(val, str):
			from germ.error.error import error
			raise error(error.fail, "Session variables for use in an " \
					"SQL condition must be strings",
					"variable: %s, type: %s" % (varname, type(val)))

		from germ.lib.db_iface import db_iface

		return "'%s'" % db_iface.escape_string(val)
示例#2
0
文件: helper.py 项目: drizzd/germ
def sql_query(sql_str, session, glob):
	"""handle failures due to non-existent tables correctly"""

	from germ.lib.db_iface import db_iface
	from _mysql_exceptions import ProgrammingError

	done = False
	while not done:
		try:
			res = db_iface.query(sql_str)
		except ProgrammingError, e:
			table = db_iface.get_missing_table(e)

			if table is None:
				import sys
				exctype, exc, tb = sys.exc_info()
				raise exctype, exc, tb

			from helper import get_entity
			entity = get_entity(table, session, glob)
			entity.create()
			del entity
		else:
			done = True