Commit 93aef5ed authored by Julien Muchembled's avatar Julien Muchembled

mysql: log failed query in case of database failure

parent f8ce322b
...@@ -33,6 +33,11 @@ from neo.lib.exception import DatabaseFailure ...@@ -33,6 +33,11 @@ from neo.lib.exception import DatabaseFailure
from neo.lib.protocol import CellStates, ZERO_OID, ZERO_TID, ZERO_HASH from neo.lib.protocol import CellStates, ZERO_OID, ZERO_TID, ZERO_HASH
def getPrintableQuery(query, max=70):
return ''.join(c if c in string.printable and c not in '\t\x0b\x0c\r'
else '\\x%02x' % ord(c) for c in query)
class MySQLDatabaseManager(DatabaseManager): class MySQLDatabaseManager(DatabaseManager):
"""This class manages a database on MySQL.""" """This class manages a database on MySQL."""
...@@ -92,35 +97,31 @@ class MySQLDatabaseManager(DatabaseManager): ...@@ -92,35 +97,31 @@ class MySQLDatabaseManager(DatabaseManager):
def query(self, query): def query(self, query):
"""Query data from a database.""" """Query data from a database."""
conn = self.conn if LOG_QUERIES:
try: logging.debug('querying %s...',
if LOG_QUERIES: getPrintableQuery(query.split('\n', 1)[0][:70]))
printable_char_list = [] while 1:
for c in query.split('\n', 1)[0][:70]: conn = self.conn
if c not in string.printable or c in '\t\x0b\x0c\r': try:
c = '\\x%02x' % ord(c) conn.query(query)
printable_char_list.append(c) if query.startswith("SELECT "):
query_part = ''.join(printable_char_list) r = conn.store_result()
logging.debug('querying %s...', query_part) return tuple([
tuple([d.tostring() if isinstance(d, array) else d
conn.query(query) for d in row])
if query.startswith("SELECT "): for row in r.fetch_row(r.num_rows())])
r = conn.store_result() break
return tuple([ except OperationalError, m:
tuple([d.tostring() if isinstance(d, array) else d if self._active or m[0] not in (SERVER_GONE_ERROR, SERVER_LOST):
for d in row]) raise DatabaseFailure('MySQL error %d: %s\nQuery: %s'
for row in r.fetch_row(r.num_rows())]) % (m[0], m[1], getPrintableQuery(query[:1000])))
r = query.split(None, 1)[0]
if r in ("INSERT", "REPLACE", "DELETE", "UPDATE"):
self._active = 1
else:
assert r in ("ALTER", "CREATE", "DROP", "TRUNCATE"), query
except OperationalError, m:
if m[0] in (SERVER_GONE_ERROR, SERVER_LOST) and not self._active:
logging.info('the MySQL server is gone; reconnecting') logging.info('the MySQL server is gone; reconnecting')
self._connect() self._connect()
return self.query(query) r = query.split(None, 1)[0]
raise DatabaseFailure('MySQL error %d: %s' % (m[0], m[1])) if r in ("INSERT", "REPLACE", "DELETE", "UPDATE"):
self._active = 1
else:
assert r in ("ALTER", "CREATE", "DROP", "TRUNCATE"), query
@property @property
def escape(self): def escape(self):
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment