import re, string, os, pickle, time postgres_data = pickle.Unpickler(open('/data/zmath/mirror/postgres/data')).load() def connect(database): return Connection() def DATETIME(datetime): return datetime class Cursor: def __init__(self): self.debug = 0 self.data = postgres_data self.list = [] self.dict = [] file = open('/data/zmath/mirror/zope/debug', 'w') file.write('init %s\n' % (time.strftime('%Y%m%d %H:%M:%S'))) file.close() return def __call__(self): return self def execute(self, query): self.list = [] self.dict = [] query = re.sub('\s+', ' ', query).strip() if self.debug: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('%s\n' % (query)) file.close() if query.startswith('SELECT'): database = table = field = where = order = limit = '' m1 = re.match("SELECT (.+) FROM ([a-z]+)([\w_]*) WHERE (.+) ORDER BY (.+) LIMIT (\d+)", query) m2 = re.match("SELECT (.+) FROM ([a-z]+)([\w_]*) WHERE (.+) ORDER BY (.+)", query) m3 = re.match("SELECT (.+) FROM ([a-z]+)([\w_]*) WHERE (.+) LIMIT (\d+)", query) m4 = re.match("SELECT (.+) FROM ([a-z]+)([\w_]*) WHERE (.+)", query) if m1: database = m1.group(2) table = m1.group(3) field = m1.group(1) where = m1.group(4) order = m1.group(5) limit = m1.group(6) elif m2: database = m2.group(2) table = m2.group(3) field = m2.group(1) where = m2.group(4) order = m2.group(5) elif m3: database = m3.group(2) table = m3.group(3) field = m3.group(1) where = m3.group(4) limit = m3.group(5) elif m4: database = m4.group(2) table = m4.group(3) field = m4.group(1) where = m4.group(4) else: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('%s\n' % (query)) file.close() i = 0 if table == '' and field == 'value' and where.startswith("name='"): i = 1 m = re.match("name='([\w\.]*)' AND type='([\w\:]+)'", where) key = '%s.%s' % (m.group(1), m.group(2)) for value in self.data[database]['content']['data'].get(key, None): self.list.append([value]) self.dict.append({'value':value}) elif table == '' and field == 'DISTINCT name' and where.startswith("type~'^object::value$'"): i = 2 m = re.match(".* name\~'\^(\w*)[\\\.]*' .*", where) key = '%s' % (m.group(1)) for name in self.data[database]['content']['data'].get(key, None): self.list.append([name]) self.dict.append({'name':name}) elif table == '_text' and field == '*' and where.startswith("id="): i = 3 key = where.split('=')[-1] self.list = self.data[database]['text']['list'].get(key, []) self.dict = self.data[database]['text']['dict'].get(key, []) elif table == '_text' and field == 'id' and where.startswith("document="): i = 4 m = re.match(".*document=(\d+) AND name='([\w\_\-]+)' AND language='(\w*)' AND owner='(\w*)'", where) key = '%s.%s.%s.%s' % (m.group(1), m.group(2), m.group(3), m.group(4)) for id in self.data[database]['text']['id'].get(key, []): self.list.append([id]) self.dict.append({'id':id}) elif table == '_content' and field == '*' and where.startswith("id="): i = 5 key = where.split('=')[-1] self.list = self.data[database]['content']['list'].get(key, []) self.dict = self.data[database]['content']['dict'].get(key, []) elif table == '_content' and field == 'id' and where.startswith("document="): i = 6 key = '' if not key: m = re.match(".*document=(\d+) AND type~'([\w\_\-]+)' AND language='(\w*)' AND owner='(\w*)'", where) if m: key = '%s.%s..%s.%s' % (m.group(1), m.group(2), m.group(3), m.group(4)) if not key: m = re.match(".*document=(\d+) AND type~'([\w\_\-]+)' AND \(name='([\w\_\-]*)'\) AND language='(\w*)' AND owner='(\w*)'", where) if m: key = '%s.%s.%s.%s.%s' % (m.group(1), m.group(2), m.group(3), m.group(4), m.group(5)) if not key: m = re.match(".*document=(\d+) AND type~'([\w\_\-]+)' AND \(parent=(\d+) and name='([\w\_\-]*)'\) AND language='(\w*)' AND owner='(\w*)'", where) if m: key = '%s.%s.%s.%s.%s.%s' % (m.group(1), m.group(3), m.group(2), m.group(4), m.group(5), m.group(6)) for id in self.data[database]['content']['id'].get(key, []): self.list.append([id]) self.dict.append({'id':id}) if self.debug: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('%s\n' % (query)) file.write(' -> %s\n' % (key)) file.write(' -> %s\n' % (str(self.list))) file.close() elif table == '_document' and field == '*' and where.startswith("id="): i = 7 key = where.split('=')[-1] self.list = self.data[database]['document']['list'].get(key, []) self.dict = self.data[database]['document']['dict'].get(key, []) elif table == '_document' and field == 'id,sort' and where.startswith("parent="): i = 8 m = re.match("parent=(\d+) .*", where) key = '%s' % (m.group(1)) self.list = self.data[database]['document']['menu'].get(key, []) self.dict = [] elif table == '_document' and field == 'id' and where.startswith("parent="): i = 9 m = re.match("parent=(\d+) AND name='([\w\.]+)' .*", where) key = '%s.%s' % (m.group(1), m.group(2)) for id in self.data[database]['document']['id'].get(key, [[],[0]][limit == '1']): self.list.append([id]) self.dict.append({'id':id}) elif table == '_document' and field == '*' and where.startswith("parent="): i = 10 m = re.match("parent=(\d+)", where) key = '%s' % (m.group(1)) for id in self.data[database]['document']['site'].get(key, []): self.list += self.data[database]['document']['list'].get(str(id), None) self.dict += self.data[database]['document']['dict'].get(str(id), None) elif table == '_document' and field == 'parent' and where.startswith("id="): i = 11 m = re.match("id=(\d+)", where) key = '%s' % (m.group(1)) parent = self.data[database]['document']['dict'].get(key, [{'parent':-1}])[0]['parent'] self.list.append([parent]) self.dict.append({'parent':parent}) elif table == '' and field == 'DISTINCT name' and where.startswith("type~'^"): i = 12 if where.endswith("value~'^(0|1)$'"): self.list = [['en']] self.dict = [{'name':'en'}] else: m = re.match("type\~'\^([a-z]+).*", where) key = '%s' % (m.group(1)) for name in self.data[database]['special']['name'].get(key, None): self.list.append([name]) self.dict.append({'name':name}) else: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('\n%s\n' % (query)) file.write(' database: %s\n' % (database)) file.write(' table: %s\n' % (table)) file.write(' field: %s\n' % (field)) file.write(' where: %s\n' % (where)) file.write(' order: %s\n' % (order)) file.write(' limit: %s\n' % (limit)) file.close() elif query.startswith('INSERT'): return None elif query.startswith('UPDATE'): return None elif query.startswith('DELETE'): return None else: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('%s\n' % (query)) file.close() if self.debug: file = open('/data/zmath/mirror/zope/count', 'a') file.write('%02d\n' % (i)) file.close() while self.list.count(None): self.list.remove(None) while self.dict.count(None): self.dict.remove(None) return [] def close(self): return None def fetchone(self): if self.debug: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('LIST: %s\n' % (self.list[0])) file.close() if self.list: return self.list[0] return None def fetchall(self): if self.debug: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('LIST: %s\n' % (self.list)) file.close() return self.list def dictfetchone(self): if self.debug: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('DICT: %s\n' % (self.dict[0])) file.close() if self.dict: return self.dict[0] return None def dictfetchall(self): if self.debug: file = open('/data/zmath/mirror/zope/debug', 'a') file.write('DICT: %s\n' % (self.dict)) file.close() return self.dict class Connection: def cursor(self): return Cursor() def __init__(self): return def commit(self): return None def rollback(self): return None def close(self): return None