2016-11-04 19:14:48 +08:00
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
class MockCursor:
|
|
|
|
def __init__(self, data):
|
|
|
|
self.cursor_pos = 0
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def fetch(self, batch_size):
|
|
|
|
batch = self.data[self.cursor_pos: self.cursor_pos + batch_size]
|
|
|
|
self.cursor_pos += batch_size
|
|
|
|
return batch
|
|
|
|
|
|
|
|
|
|
|
|
class MockPlPy:
|
|
|
|
def __init__(self):
|
|
|
|
self._reset()
|
|
|
|
|
2017-03-21 22:43:33 +08:00
|
|
|
def _reset(self, log_executed_queries=False):
|
2016-11-04 19:14:48 +08:00
|
|
|
self.infos = []
|
|
|
|
self.notices = []
|
|
|
|
self.debugs = []
|
|
|
|
self.logs = []
|
|
|
|
self.warnings = []
|
|
|
|
self.errors = []
|
|
|
|
self.fatals = []
|
|
|
|
self.executes = []
|
|
|
|
self.results = []
|
|
|
|
self.prepares = []
|
|
|
|
self.results = {}
|
2017-03-21 22:43:33 +08:00
|
|
|
self._log_executed_queries = log_executed_queries
|
|
|
|
self._logged_queries = []
|
2016-11-04 19:14:48 +08:00
|
|
|
|
|
|
|
def _define_result(self, query, result):
|
|
|
|
pattern = re.compile(query, re.IGNORECASE | re.MULTILINE)
|
|
|
|
self.results[pattern] = result
|
|
|
|
|
2017-03-21 22:43:33 +08:00
|
|
|
def _executed_queries(self):
|
|
|
|
if self._log_executed_queries:
|
|
|
|
return self._logged_queries
|
|
|
|
else:
|
|
|
|
raise Exception('Executed queries logging is not active')
|
|
|
|
|
|
|
|
def _has_executed_query(self, query):
|
|
|
|
pattern = re.compile(re.escape(query))
|
|
|
|
for executed_query in self._executed_queries():
|
|
|
|
if pattern.search(executed_query):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _start_logging_executed_queries(self):
|
|
|
|
self._logged_queries = []
|
|
|
|
self._log_executed_queries = True
|
|
|
|
|
2018-07-23 18:16:48 +08:00
|
|
|
def warning(self, msg):
|
|
|
|
self.warnings.append(msg)
|
|
|
|
|
2016-11-04 19:14:48 +08:00
|
|
|
def notice(self, msg):
|
|
|
|
self.notices.append(msg)
|
|
|
|
|
|
|
|
def debug(self, msg):
|
2018-07-23 18:16:48 +08:00
|
|
|
self.debugs.append(msg)
|
2016-11-04 19:14:48 +08:00
|
|
|
|
|
|
|
def info(self, msg):
|
|
|
|
self.infos.append(msg)
|
|
|
|
|
|
|
|
def cursor(self, query):
|
|
|
|
data = self.execute(query)
|
|
|
|
return MockCursor(data)
|
|
|
|
|
|
|
|
def execute(self, query, rows=1):
|
2017-03-21 22:43:33 +08:00
|
|
|
if self._log_executed_queries:
|
|
|
|
self._logged_queries.append(query)
|
2016-11-04 19:14:48 +08:00
|
|
|
for pattern, result in self.results.iteritems():
|
|
|
|
if pattern.search(query):
|
|
|
|
return result
|
|
|
|
return []
|
2017-03-21 22:43:33 +08:00
|
|
|
|
|
|
|
def quote_nullable(self, value):
|
|
|
|
if value is None:
|
|
|
|
return 'NULL'
|
|
|
|
else:
|
|
|
|
return "'{0}'".format(value)
|