author | Radek Brich <radek.brich@devl.cz> |
Wed, 23 May 2012 11:11:16 +0200 | |
changeset 36 | e67101c22e83 |
parent 35 | e7f79c4a27ce |
child 40 | 922d7fb63384 |
permissions | -rw-r--r-- |
0 | 1 |
# -*- coding: utf-8 -*- |
2 |
# |
|
3 |
# PgBrowser - browse database schema and metadata |
|
4 |
# |
|
5 |
# Some of the queries came from psql. |
|
6 |
# |
|
7 |
# Copyright (c) 2011 Radek Brich <radek.brich@devl.cz> |
|
8 |
# |
|
9 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
|
10 |
# of this software and associated documentation files (the "Software"), to deal |
|
11 |
# in the Software without restriction, including without limitation the rights |
|
12 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
13 |
# copies of the Software, and to permit persons to whom the Software is |
|
14 |
# furnished to do so, subject to the following conditions: |
|
15 |
# |
|
16 |
# The above copyright notice and this permission notice shall be included in |
|
17 |
# all copies or substantial portions of the Software. |
|
18 |
# |
|
19 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
20 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
21 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
22 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
23 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
24 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
25 |
# THE SOFTWARE. |
|
26 |
||
27 |
||
28 |
from collections import OrderedDict |
|
29 |
||
30 |
||
31 |
class Column: |
|
32 |
def __init__(self, browser, table, |
|
33 |
name, type, notnull, hasdefault, default, description): |
|
34 |
self.browser = browser # Browser instance |
|
35 |
self.table = table # Table instance |
|
36 |
self.name = name |
|
37 |
self.type = type |
|
38 |
self.notnull = notnull |
|
39 |
self.hasdefault = hasdefault |
|
40 |
self.default = default |
|
41 |
self.description = description |
|
42 |
||
43 |
||
44 |
class Constraint: |
|
45 |
def __init__(self, browser, table, name, type, definition): |
|
46 |
self.browser = browser |
|
47 |
self.table = table |
|
48 |
self.name = name |
|
49 |
self.type = type |
|
50 |
self.definition = definition |
|
51 |
||
52 |
||
53 |
class Index: |
|
54 |
def __init__(self, browser, table, |
|
55 |
name, primary, unique, clustered, valid, definition): |
|
56 |
self.browser = browser |
|
57 |
self.table = table |
|
58 |
self.name = name |
|
59 |
self.primary = primary |
|
60 |
self.unique = unique |
|
61 |
self.clustered = clustered |
|
62 |
self.valid = valid |
|
63 |
self.definition = definition |
|
64 |
||
65 |
||
66 |
class Table: |
|
67 |
def __init__(self, browser, schema, name, owner, size, description): |
|
68 |
self._columns = None |
|
69 |
self._constraints = None |
|
70 |
self._indexes = None |
|
71 |
self.browser = browser # Browser instance |
|
72 |
self.schema = schema # Schema instance |
|
73 |
self.name = name # table name, str |
|
74 |
self.owner = owner |
|
75 |
self.size = size |
|
76 |
self.description = description |
|
77 |
||
78 |
def refresh(self): |
|
79 |
self.refresh_columns() |
|
80 |
self.refresh_constraints() |
|
81 |
self.refresh_indexes() |
|
82 |
||
83 |
def refresh_columns(self): |
|
84 |
rows = self.browser.list_columns(self.name, self.schema.name) |
|
85 |
self._columns = OrderedDict([(x['name'], Column(self.browser, self, **x)) for x in rows]) |
|
86 |
||
87 |
def refresh_constraints(self): |
|
88 |
rows = self.browser.list_constraints(self.name, self.schema.name) |
|
89 |
self._constraints = OrderedDict([(x['name'], Constraint(self.browser, self, **x)) for x in rows]) |
|
90 |
||
91 |
def refresh_indexes(self): |
|
92 |
rows = self.browser.list_indexes(self.name, self.schema.name) |
|
93 |
self._indexes = OrderedDict([(x['name'], Index(self.browser, self, **x)) for x in rows]) |
|
94 |
||
95 |
def getcolumns(self): |
|
96 |
if self._columns is None: |
|
97 |
self.refresh_columns() |
|
98 |
return self._columns |
|
99 |
columns = property(getcolumns) |
|
100 |
||
101 |
def getconstraints(self): |
|
102 |
if self._constraints is None: |
|
103 |
self.refresh_constraints() |
|
104 |
return self._constraints |
|
105 |
constraints = property(getconstraints) |
|
106 |
||
107 |
def getindexes(self): |
|
108 |
if self._indexes is None: |
|
109 |
self.refresh_indexes() |
|
110 |
return self._indexes |
|
111 |
indexes = property(getindexes) |
|
112 |
||
113 |
class Schema: |
|
114 |
def __init__(self, browser, name, owner, acl, description, system): |
|
115 |
self._tables = None |
|
116 |
self.browser = browser |
|
117 |
self.name = name |
|
118 |
self.owner = owner |
|
119 |
self.acl = acl |
|
120 |
self.description = description |
|
121 |
self.system = system |
|
122 |
||
123 |
def refresh(self): |
|
124 |
rows = self.browser.list_tables(self.name) |
|
125 |
self._tables = OrderedDict([(x['name'], Table(self.browser, self, **x)) for x in rows]) |
|
126 |
||
127 |
def gettables(self): |
|
128 |
if self._tables is None: |
|
129 |
self.refresh() |
|
130 |
return self._tables |
|
131 |
tables = property(gettables) |
|
132 |
||
133 |
||
134 |
class PgBrowser: |
|
135 |
def __init__(self, conn=None): |
|
136 |
self._schemas = None |
|
137 |
self.conn = conn |
|
138 |
||
139 |
def setconn(self, conn=None): |
|
140 |
self.conn = conn |
|
141 |
||
142 |
def refresh(self): |
|
143 |
rows = self.list_schemas() |
|
144 |
self._schemas = OrderedDict([(x['name'], Schema(self, **x)) for x in rows]) |
|
145 |
||
146 |
def getschemas(self): |
|
147 |
if self._schemas is None: |
|
148 |
self.refresh() |
|
149 |
return self._schemas |
|
150 |
schemas = property(getschemas) |
|
151 |
||
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
152 |
def _query(self, query, args): |
0 | 153 |
try: |
154 |
curs = self.conn.cursor() |
|
155 |
curs.execute(query, args) |
|
156 |
curs.connection.commit() |
|
157 |
rows = curs.fetchall() |
|
158 |
return [dict(zip([desc[0] for desc in curs.description], row)) for row in rows] |
|
159 |
finally: |
|
160 |
curs.close() |
|
161 |
||
162 |
def list_databases(self): |
|
163 |
return self._query(''' |
|
164 |
SELECT |
|
165 |
d.datname as "name", |
|
166 |
pg_catalog.pg_get_userbyid(d.datdba) as "owner", |
|
167 |
pg_catalog.pg_encoding_to_char(d.encoding) as "encoding", |
|
168 |
d.datcollate as "collation", |
|
169 |
d.datctype as "ctype", |
|
170 |
d.datacl AS "acl", |
|
171 |
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') |
|
172 |
THEN pg_catalog.pg_database_size(d.datname) |
|
173 |
ELSE -1 -- No access |
|
174 |
END as "size", |
|
175 |
t.spcname as "tablespace", |
|
176 |
pg_catalog.shobj_description(d.oid, 'pg_database') as "description" |
|
177 |
FROM pg_catalog.pg_database d |
|
178 |
JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid |
|
179 |
ORDER BY 1; |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
180 |
''', []) |
0 | 181 |
|
182 |
def list_schemas(self): |
|
183 |
return self._query(''' |
|
184 |
SELECT |
|
185 |
n.nspname AS "name", |
|
186 |
pg_catalog.pg_get_userbyid(n.nspowner) AS "owner", |
|
187 |
n.nspacl AS "acl", |
|
188 |
pg_catalog.obj_description(n.oid, 'pg_namespace') AS "description", |
|
189 |
CASE WHEN n.nspname IN ('information_schema', 'pg_catalog', 'pg_toast') |
|
190 |
OR n.nspname ~ '^pg_temp_' OR n.nspname ~ '^pg_toast_temp_' |
|
191 |
THEN TRUE |
|
192 |
ELSE FALSE |
|
193 |
END AS "system" |
|
194 |
FROM pg_catalog.pg_namespace n |
|
195 |
ORDER BY 1; |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
196 |
''', []) |
0 | 197 |
|
198 |
def list_tables(self, schema='public'): |
|
199 |
return self._query(''' |
|
200 |
SELECT |
|
201 |
c.relname as "name", |
|
202 |
pg_catalog.pg_get_userbyid(c.relowner) as "owner", |
|
203 |
pg_catalog.pg_relation_size(c.oid) as "size", |
|
204 |
pg_catalog.obj_description(c.oid, 'pg_class') as "description" |
|
205 |
FROM pg_catalog.pg_class c |
|
206 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
207 |
WHERE n.nspname = %s AND c.relkind IN ('r','s','') |
|
208 |
ORDER BY 1; |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
209 |
''', [schema]) |
0 | 210 |
|
12 | 211 |
def list_columns(self, table, schema='public', order=2): |
0 | 212 |
return self._query(''' |
213 |
SELECT |
|
12 | 214 |
a.attrelid, |
0 | 215 |
a.attname as "name", |
216 |
format_type(a.atttypid, a.atttypmod) AS "type", |
|
217 |
a.attnotnull as "notnull", |
|
218 |
a.atthasdef as "hasdefault", |
|
219 |
d.adsrc as "default", |
|
220 |
pg_catalog.col_description(a.attrelid, a.attnum) AS "description" |
|
221 |
FROM pg_catalog.pg_attribute a |
|
222 |
LEFT JOIN pg_catalog.pg_class c ON a.attrelid = c.oid |
|
223 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
224 |
LEFT JOIN pg_catalog.pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum |
|
225 |
WHERE n.nspname = %s AND c.relname = %s AND a.attnum > 0 AND NOT a.attisdropped |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
226 |
ORDER BY ''' + str(order), [schema, table]) |
0 | 227 |
|
228 |
def list_constraints(self, table, schema='public'): |
|
229 |
return self._query(''' |
|
230 |
SELECT |
|
35
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
231 |
r.conname AS "name", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
232 |
r.contype AS "type", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
233 |
cf.relname AS "fname", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
234 |
nf.nspname AS "fschema", |
0 | 235 |
pg_catalog.pg_get_constraintdef(r.oid, true) as "definition" |
236 |
FROM pg_catalog.pg_constraint r |
|
237 |
JOIN pg_catalog.pg_class c ON r.conrelid = c.oid |
|
238 |
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
35
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
239 |
LEFT JOIN pg_catalog.pg_class cf ON r.confrelid = cf.oid |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
240 |
LEFT JOIN pg_catalog.pg_namespace nf ON nf.oid = cf.relnamespace |
0 | 241 |
WHERE n.nspname = %s AND c.relname = %s |
242 |
ORDER BY 1 |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
243 |
''', [schema, table]) |
0 | 244 |
|
245 |
def list_indexes(self, table, schema='public'): |
|
246 |
return self._query(''' |
|
247 |
SELECT |
|
248 |
c2.relname as "name", |
|
249 |
i.indisprimary as "primary", |
|
250 |
i.indisunique as "unique", |
|
251 |
i.indisclustered as "clustered", |
|
252 |
i.indisvalid as "valid", |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
253 |
pg_catalog.pg_get_indexdef(i.indexrelid, 0, true) as "definition", |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
254 |
ARRAY(SELECT a.attname FROM pg_catalog.pg_attribute a WHERE a.attrelid = c2.oid ORDER BY attnum) AS "columns" |
0 | 255 |
--c2.reltablespace as "tablespace_oid" |
256 |
FROM pg_catalog.pg_class c |
|
257 |
JOIN pg_catalog.pg_index i ON c.oid = i.indrelid |
|
258 |
JOIN pg_catalog.pg_class c2 ON i.indexrelid = c2.oid |
|
259 |
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
260 |
WHERE n.nspname = %(schema)s AND c.relname = %(table)s |
0 | 261 |
ORDER BY i.indisprimary DESC, i.indisunique DESC, c2.relname |
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
262 |
''', {'schema': schema, 'table': table}) |
0 | 263 |
|
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
264 |
def list_column_usage(self, table, column, schema='public'): |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
265 |
'''List objects using the column. |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
266 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
267 |
Currently shows views and constraints which use the column. |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
268 |
|
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
269 |
This is useful to find which views block alteration of column type etc. |
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
270 |
|
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
271 |
''' |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
272 |
return self._query(''' |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
273 |
SELECT |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
274 |
'view' AS type, view_schema AS schema, view_name AS name |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
275 |
FROM information_schema.view_column_usage |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
276 |
WHERE table_schema=%(schema)s AND table_name=%(table)s AND column_name=%(column)s |
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
277 |
|
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
278 |
UNION |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
279 |
|
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
280 |
SELECT |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
281 |
'constraint' AS type, constraint_schema AS schema, constraint_name AS name |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
282 |
FROM information_schema.constraint_column_usage |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
283 |
WHERE table_schema=%(schema)s AND table_name=%(table)s AND column_name=%(column)s |
31
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
284 |
''', {'schema':schema, 'table':table, 'column':column}) |
c2e6e24b83d9
Add browser - database schema browser using tuikit (curses UI). Add listdepends - tool which shows depending views for column. Update pgdatadiff - allow composite primary key. Update pgmanager - RowDict is now OrderedDict. Drop support for Python2.x.
Radek Brich <radek.brich@devl.cz>
parents:
12
diff
changeset
|
285 |