-
Notifications
You must be signed in to change notification settings - Fork 4
/
postgres.py
75 lines (51 loc) · 2.32 KB
/
postgres.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
##########################################################################
# z3c.sqlalchemy - A SQLAlchemy wrapper for Python/Zope
#
# (C) Zope Corporation and Contributor
# Written by Andreas Jung for Haufe Mediengruppe, Freiburg, Germany
# and ZOPYX Ltd. & Co. KG, Tuebingen, Germany
##########################################################################
import sys
import threading
import sqlalchemy
from zope.interface import implements
from z3c.sqlalchemy.interfaces import ISQLAlchemyWrapper
from z3c.sqlalchemy.base import ZopeWrapper
_cache = threading.local() # module-level cache
class PostgresMixin(object):
""" Mixin class for Postgres aspects """
implements(ISQLAlchemyWrapper)
def findDependentTables(self, schema='public', ignoreErrors=False):
""" Returns a mapping tablename -> [list of referencing table(names)].
ATT: this method is specific to Postgres databases!
ATT: This method is limited to a particular schema.
"""
if not hasattr(_cache, 'ref_mapping'):
d = {}
db = self._engine
rs = db.execute("select * from pg_tables where schemaname = '%s'" % schema)
for row in rs:
tablename = row.tablename
try:
table = sqlalchemy.Table(tablename, db, autoload=True)
except KeyError:
if ignoreErrors:
print >>sys.stderr, 'Can\'t load table %s' % tablename
continue
else:
raise
for c in table.c:
fk = c.foreign_key
if fk is not None:
ref_by_table = fk.column.table
ref_by_table_name = ref_by_table.name
if not d.has_key(ref_by_table_name):
d[ref_by_table_name] = list()
if not tablename in d[ref_by_table_name]:
d[ref_by_table_name].append(tablename)
_cache.ref_mapping = d
return _cache.ref_mapping
class ZopePostgresWrapper(ZopeWrapper, PostgresMixin):
""" A wrapper to be used from within Zope. It connects
the session with the transaction management of Zope.
"""