author | Radek Brich <radek.brich@devl.cz> |
Fri, 04 Oct 2013 16:38:24 +0200 | |
changeset 90 | 43e5d2cf4ddb |
parent 87 | 3ef717ee9253 |
child 94 | a10f553e6f6a |
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, |
|
87
3ef717ee9253
PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
33 |
name, type, notnull, hasdefault, default, description): |
0 | 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 |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
42 |
|
0 | 43 |
|
44 |
class Constraint: |
|
40
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
45 |
def __init__(self, browser, table, name, type, fname, fschema, definition): |
0 | 46 |
self.browser = browser |
47 |
self.table = table |
|
48 |
self.name = name |
|
49 |
self.type = type |
|
40
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
50 |
self.fname = fname # foreign table name |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
51 |
self.fschema = fschema # foreign table schema |
0 | 52 |
self.definition = definition |
53 |
||
54 |
||
55 |
class Index: |
|
56 |
def __init__(self, browser, table, |
|
87
3ef717ee9253
PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
57 |
name, primary, unique, clustered, valid, definition, |
3ef717ee9253
PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
58 |
columns): |
0 | 59 |
self.browser = browser |
60 |
self.table = table |
|
61 |
self.name = name |
|
62 |
self.primary = primary |
|
63 |
self.unique = unique |
|
64 |
self.clustered = clustered |
|
65 |
self.valid = valid |
|
66 |
self.definition = definition |
|
87
3ef717ee9253
PgDiff: Add support for indexes.
Radek Brich <radek.brich@devl.cz>
parents:
86
diff
changeset
|
67 |
self.columns = columns |
0 | 68 |
|
69 |
||
70 |
class Table: |
|
68 | 71 |
def __init__(self, browser, schema, name, owner, size, description, options): |
0 | 72 |
self._columns = None |
73 |
self._constraints = None |
|
74 |
self._indexes = None |
|
75 |
self.browser = browser # Browser instance |
|
76 |
self.schema = schema # Schema instance |
|
77 |
self.name = name # table name, str |
|
78 |
self.owner = owner |
|
79 |
self.size = size |
|
80 |
self.description = description |
|
68 | 81 |
self.options = options or [] |
0 | 82 |
|
83 |
def refresh(self): |
|
84 |
self.refresh_columns() |
|
85 |
self.refresh_constraints() |
|
86 |
self.refresh_indexes() |
|
87 |
||
88 |
def refresh_columns(self): |
|
89 |
rows = self.browser.list_columns(self.name, self.schema.name) |
|
90 |
self._columns = OrderedDict([(x['name'], Column(self.browser, self, **x)) for x in rows]) |
|
91 |
||
92 |
def refresh_constraints(self): |
|
93 |
rows = self.browser.list_constraints(self.name, self.schema.name) |
|
94 |
self._constraints = OrderedDict([(x['name'], Constraint(self.browser, self, **x)) for x in rows]) |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
95 |
|
0 | 96 |
def refresh_indexes(self): |
97 |
rows = self.browser.list_indexes(self.name, self.schema.name) |
|
98 |
self._indexes = OrderedDict([(x['name'], Index(self.browser, self, **x)) for x in rows]) |
|
99 |
||
100 |
def getcolumns(self): |
|
101 |
if self._columns is None: |
|
102 |
self.refresh_columns() |
|
103 |
return self._columns |
|
104 |
columns = property(getcolumns) |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
105 |
|
0 | 106 |
def getconstraints(self): |
107 |
if self._constraints is None: |
|
108 |
self.refresh_constraints() |
|
109 |
return self._constraints |
|
110 |
constraints = property(getconstraints) |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
111 |
|
0 | 112 |
def getindexes(self): |
113 |
if self._indexes is None: |
|
114 |
self.refresh_indexes() |
|
115 |
return self._indexes |
|
116 |
indexes = property(getindexes) |
|
117 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
118 |
|
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
119 |
class Argument: |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
120 |
def __init__(self, browser, function, name, type, mode, default): |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
121 |
# PgBrowser instance |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
122 |
self.browser = browser |
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
123 |
# Function instance |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
124 |
self.function = function |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
125 |
self.name = name |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
126 |
self.type = type |
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
127 |
self.mode = mode |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
128 |
self.default = default |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
129 |
|
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
130 |
class Function: |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
131 |
def __init__(self, browser, schema, oid, name, function_name, type, result, source): |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
132 |
self.browser = browser |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
133 |
self.schema = schema |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
134 |
self.oid = oid |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
135 |
#: unique name - function name + arg types |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
136 |
self.name = name |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
137 |
#: pure function name without args |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
138 |
self.function_name = function_name |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
139 |
self.type = type |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
140 |
self.result = result |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
141 |
self.source = source |
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
142 |
self._arguments = None |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
143 |
self._definition = None |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
144 |
|
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
145 |
def refresh(self): |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
146 |
self.refresh_args() |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
147 |
|
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
148 |
def refresh_args(self): |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
149 |
rows = self.browser.list_function_args(self.oid) |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
150 |
self._arguments = OrderedDict([(x['name'], Argument(self.browser, self, **x)) for x in rows]) |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
151 |
|
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
152 |
@property |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
153 |
def arguments(self): |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
154 |
if self._arguments is None: |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
155 |
self.refresh_args() |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
156 |
return self._arguments |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
157 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
158 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
159 |
def definition(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
160 |
"""Get full function definition including CREATE command.""" |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
161 |
if not self._definition: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
162 |
self._definition = self.browser.get_function_definition(self.oid) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
163 |
return self._definition |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
164 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
165 |
|
85
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
166 |
class Type: |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
167 |
def __init__(self, browser, schema, name, type, elements, description): |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
168 |
self.browser = browser |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
169 |
self.schema = schema |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
170 |
self.name = name |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
171 |
self.type = type |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
172 |
self.elements = elements |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
173 |
self.description = description |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
174 |
|
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
175 |
|
0 | 176 |
class Schema: |
177 |
def __init__(self, browser, name, owner, acl, description, system): |
|
178 |
self._tables = None |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
179 |
self._functions = None |
85
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
180 |
self._types = None |
0 | 181 |
self.browser = browser |
182 |
self.name = name |
|
183 |
self.owner = owner |
|
184 |
self.acl = acl |
|
185 |
self.description = description |
|
186 |
self.system = system |
|
187 |
||
188 |
def refresh(self): |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
189 |
self.refresh_tables() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
190 |
self.refresh_functions() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
191 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
192 |
def refresh_tables(self): |
0 | 193 |
rows = self.browser.list_tables(self.name) |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
194 |
self._tables = OrderedDict([(x['name'], Table(self.browser, self, **x)) for x in rows]) |
0 | 195 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
196 |
def refresh_functions(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
197 |
rows = self.browser.list_functions(self.name) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
198 |
self._functions = OrderedDict([(x['name'], Function(self.browser, self, **x)) for x in rows]) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
199 |
|
85
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
200 |
def refresh_types(self): |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
201 |
rows = self.browser.list_types(self.name) |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
202 |
self._types = OrderedDict([(x['name'], Type(self.browser, self, **x)) for x in rows]) |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
203 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
204 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
205 |
def tables(self): |
0 | 206 |
if self._tables is None: |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
207 |
self.refresh_tables() |
0 | 208 |
return self._tables |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
209 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
210 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
211 |
def functions(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
212 |
if self._functions is None: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
213 |
self.refresh_functions() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
214 |
return self._functions |
0 | 215 |
|
85
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
216 |
@property |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
217 |
def types(self): |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
218 |
if self._types is None: |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
219 |
self.refresh_types() |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
220 |
return self._types |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
221 |
|
0 | 222 |
|
223 |
class PgBrowser: |
|
224 |
def __init__(self, conn=None): |
|
225 |
self._schemas = None |
|
226 |
self.conn = conn |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
227 |
|
0 | 228 |
def setconn(self, conn=None): |
229 |
self.conn = conn |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
230 |
|
0 | 231 |
def refresh(self): |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
232 |
self.refresh_schemas() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
233 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
234 |
def refresh_schemas(self): |
0 | 235 |
rows = self.list_schemas() |
236 |
self._schemas = OrderedDict([(x['name'], Schema(self, **x)) for x in rows]) |
|
237 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
238 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
239 |
def schemas(self): |
0 | 240 |
if self._schemas is None: |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
241 |
self.refresh_schemas() |
0 | 242 |
return self._schemas |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
243 |
|
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
|
244 |
def _query(self, query, args): |
0 | 245 |
try: |
246 |
curs = self.conn.cursor() |
|
247 |
curs.execute(query, args) |
|
248 |
curs.connection.commit() |
|
249 |
rows = curs.fetchall() |
|
250 |
return [dict(zip([desc[0] for desc in curs.description], row)) for row in rows] |
|
251 |
finally: |
|
252 |
curs.close() |
|
253 |
||
254 |
def list_databases(self): |
|
255 |
return self._query(''' |
|
256 |
SELECT |
|
257 |
d.datname as "name", |
|
258 |
pg_catalog.pg_get_userbyid(d.datdba) as "owner", |
|
259 |
pg_catalog.pg_encoding_to_char(d.encoding) as "encoding", |
|
260 |
d.datcollate as "collation", |
|
261 |
d.datctype as "ctype", |
|
262 |
d.datacl AS "acl", |
|
263 |
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') |
|
264 |
THEN pg_catalog.pg_database_size(d.datname) |
|
265 |
ELSE -1 -- No access |
|
266 |
END as "size", |
|
267 |
t.spcname as "tablespace", |
|
268 |
pg_catalog.shobj_description(d.oid, 'pg_database') as "description" |
|
269 |
FROM pg_catalog.pg_database d |
|
270 |
JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid |
|
271 |
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
|
272 |
''', []) |
0 | 273 |
|
274 |
def list_schemas(self): |
|
275 |
return self._query(''' |
|
276 |
SELECT |
|
277 |
n.nspname AS "name", |
|
278 |
pg_catalog.pg_get_userbyid(n.nspowner) AS "owner", |
|
279 |
n.nspacl AS "acl", |
|
280 |
pg_catalog.obj_description(n.oid, 'pg_namespace') AS "description", |
|
281 |
CASE WHEN n.nspname IN ('information_schema', 'pg_catalog', 'pg_toast') |
|
282 |
OR n.nspname ~ '^pg_temp_' OR n.nspname ~ '^pg_toast_temp_' |
|
283 |
THEN TRUE |
|
284 |
ELSE FALSE |
|
285 |
END AS "system" |
|
286 |
FROM pg_catalog.pg_namespace n |
|
287 |
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
|
288 |
''', []) |
0 | 289 |
|
290 |
def list_tables(self, schema='public'): |
|
291 |
return self._query(''' |
|
292 |
SELECT |
|
293 |
c.relname as "name", |
|
294 |
pg_catalog.pg_get_userbyid(c.relowner) as "owner", |
|
295 |
pg_catalog.pg_relation_size(c.oid) as "size", |
|
68 | 296 |
pg_catalog.obj_description(c.oid, 'pg_class') as "description", |
297 |
c.reloptions as "options" |
|
0 | 298 |
FROM pg_catalog.pg_class c |
299 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
300 |
WHERE n.nspname = %s AND c.relkind IN ('r','s','') |
|
301 |
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
|
302 |
''', [schema]) |
0 | 303 |
|
12 | 304 |
def list_columns(self, table, schema='public', order=2): |
0 | 305 |
return self._query(''' |
306 |
SELECT |
|
40
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
307 |
--a.attrelid, |
0 | 308 |
a.attname as "name", |
309 |
format_type(a.atttypid, a.atttypmod) AS "type", |
|
310 |
a.attnotnull as "notnull", |
|
311 |
a.atthasdef as "hasdefault", |
|
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
312 |
pg_catalog.pg_get_expr(d.adbin, d.adrelid) as "default", |
0 | 313 |
pg_catalog.col_description(a.attrelid, a.attnum) AS "description" |
314 |
FROM pg_catalog.pg_attribute a |
|
315 |
LEFT JOIN pg_catalog.pg_class c ON a.attrelid = c.oid |
|
316 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
317 |
LEFT JOIN pg_catalog.pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum |
|
318 |
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
|
319 |
ORDER BY ''' + str(order), [schema, table]) |
0 | 320 |
|
321 |
def list_constraints(self, table, schema='public'): |
|
322 |
return self._query(''' |
|
323 |
SELECT |
|
35
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
324 |
r.conname AS "name", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
325 |
r.contype AS "type", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
326 |
cf.relname AS "fname", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
327 |
nf.nspname AS "fschema", |
0 | 328 |
pg_catalog.pg_get_constraintdef(r.oid, true) as "definition" |
329 |
FROM pg_catalog.pg_constraint r |
|
330 |
JOIN pg_catalog.pg_class c ON r.conrelid = c.oid |
|
331 |
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
|
332 |
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
|
333 |
LEFT JOIN pg_catalog.pg_namespace nf ON nf.oid = cf.relnamespace |
0 | 334 |
WHERE n.nspname = %s AND c.relname = %s |
335 |
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
|
336 |
''', [schema, table]) |
0 | 337 |
|
338 |
def list_indexes(self, table, schema='public'): |
|
339 |
return self._query(''' |
|
340 |
SELECT |
|
341 |
c2.relname as "name", |
|
342 |
i.indisprimary as "primary", |
|
343 |
i.indisunique as "unique", |
|
344 |
i.indisclustered as "clustered", |
|
345 |
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
|
346 |
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
|
347 |
ARRAY(SELECT a.attname FROM pg_catalog.pg_attribute a WHERE a.attrelid = c2.oid ORDER BY attnum) AS "columns" |
0 | 348 |
--c2.reltablespace as "tablespace_oid" |
349 |
FROM pg_catalog.pg_class c |
|
350 |
JOIN pg_catalog.pg_index i ON c.oid = i.indrelid |
|
351 |
JOIN pg_catalog.pg_class c2 ON i.indexrelid = c2.oid |
|
352 |
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
|
353 |
WHERE n.nspname = %(schema)s AND c.relname = %(table)s |
0 | 354 |
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
|
355 |
''', {'schema': schema, 'table': table}) |
0 | 356 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
357 |
def list_functions(self, schema='public'): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
358 |
'''List functions in schema.''' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
359 |
return self._query(''' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
360 |
SELECT |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
361 |
p.oid as "oid", |
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
362 |
p.proname || '(' || array_to_string( |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
363 |
array(SELECT pg_catalog.format_type(unnest(p.proargtypes), NULL)), |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
364 |
', ' |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
365 |
) || ')' as "name", |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
366 |
p.proname as "function_name", |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
367 |
pg_catalog.pg_get_function_result(p.oid) as "result", |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
368 |
p.prosrc as "source", |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
369 |
CASE |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
370 |
WHEN p.proisagg THEN 'agg' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
371 |
WHEN p.proiswindow THEN 'window' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
372 |
WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
373 |
ELSE 'normal' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
374 |
END as "type" |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
375 |
FROM pg_catalog.pg_proc p |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
376 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace |
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
377 |
WHERE n.nspname = %s |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
378 |
ORDER BY 1, 2, 4; |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
379 |
''', [schema]) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
380 |
|
59
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
381 |
def list_function_args(self, oid): |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
382 |
"""List function arguments. |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
383 |
|
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
384 |
Notes about query: |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
385 |
type: Use allargtypes if present, argtypes otherwise. |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
386 |
The trick with [0:999] moves lower bound from 0 to default 1 |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
387 |
by slicing all elements (slices has always lower bound 1). |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
388 |
mode: This trick makes array of NULLs of same length as argnames, |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
389 |
in case argmodes is NULL. |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
390 |
default: Use pg_get_expr, split output by ', ' |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
391 |
FIXME: will fail if ', ' is present in default value string. |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
392 |
""" |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
393 |
return self._query(''' |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
394 |
SELECT |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
395 |
unnest(p.proargnames) AS "name", |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
396 |
pg_catalog.format_type(unnest( |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
397 |
COALESCE(p.proallargtypes, (p.proargtypes::oid[])[0:999]) |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
398 |
), NULL) AS "type", |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
399 |
unnest( |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
400 |
COALESCE( |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
401 |
p.proargmodes::text[], |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
402 |
array(SELECT NULL::text FROM generate_series(1, array_upper(p.proargnames, 1))) |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
403 |
) |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
404 |
) AS "mode", |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
405 |
unnest(array_cat( |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
406 |
array_fill(NULL::text, array[COALESCE(array_upper(p.proargnames,1),0) - p.pronargdefaults]), |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
407 |
string_to_array(pg_get_expr(p.proargdefaults, 'pg_proc'::regclass, true), ', ') |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
408 |
)) AS "default" |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
409 |
FROM pg_proc p |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
410 |
WHERE p.oid = %s''', [oid]) |
65efd0c6919f
PgBrowser: add function arguments as another level in hierarchy. PgDiff: compare function arguments one by one.
Radek Brich <radek.brich@devl.cz>
parents:
58
diff
changeset
|
411 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
412 |
def get_function_definition(self, oid): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
413 |
"""Get full function definition, including CREATE command etc. |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
414 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
415 |
Args: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
416 |
oid: function oid from pg_catalog.pg_proc (returned by list_functions) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
417 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
418 |
""" |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
419 |
return self._query('''select pg_get_functiondef(%s);''', [oid]) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
420 |
|
85
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
421 |
def list_types(self, schema='public'): |
86
b61b54aa9f96
Fix: ConfigParser add_argument -> add_option.
Radek Brich <brich.radek@ifortuna.cz>
parents:
85
diff
changeset
|
422 |
"""List types in schema. |
b61b54aa9f96
Fix: ConfigParser add_argument -> add_option.
Radek Brich <brich.radek@ifortuna.cz>
parents:
85
diff
changeset
|
423 |
|
b61b54aa9f96
Fix: ConfigParser add_argument -> add_option.
Radek Brich <brich.radek@ifortuna.cz>
parents:
85
diff
changeset
|
424 |
http://www.postgresql.org/docs/8.4/static/catalog-pg-type.html |
b61b54aa9f96
Fix: ConfigParser add_argument -> add_option.
Radek Brich <brich.radek@ifortuna.cz>
parents:
85
diff
changeset
|
425 |
|
b61b54aa9f96
Fix: ConfigParser add_argument -> add_option.
Radek Brich <brich.radek@ifortuna.cz>
parents:
85
diff
changeset
|
426 |
""" |
85
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
427 |
return self._query(''' |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
428 |
SELECT |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
429 |
t.typname AS "name", |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
430 |
CASE |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
431 |
WHEN t.typtype = 'b' THEN 'base'::text |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
432 |
WHEN t.typtype = 'c' THEN 'composite'::text |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
433 |
WHEN t.typtype = 'd' THEN 'domain'::text |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
434 |
WHEN t.typtype = 'e' THEN 'enum'::text |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
435 |
WHEN t.typtype = 'p' THEN 'pseudo'::text |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
436 |
END AS "type", |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
437 |
ARRAY( |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
438 |
SELECT e.enumlabel |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
439 |
FROM pg_catalog.pg_enum e |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
440 |
WHERE e.enumtypid = t.oid |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
441 |
ORDER BY e.oid |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
442 |
) AS "elements", |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
443 |
pg_catalog.obj_description(t.oid, 'pg_type') AS "description" |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
444 |
FROM pg_catalog.pg_type t |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
445 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
446 |
WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
447 |
AND NOT EXISTS(SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid) |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
448 |
AND n.nspname <> 'pg_catalog' |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
449 |
AND n.nspname <> 'information_schema' |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
450 |
AND n.nspname = %(schema)s |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
451 |
ORDER BY 1, 2; |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
452 |
''', {'schema': schema}) |
11a282e23e0d
Add basic support for types in browser and schema diff.
Radek Brich <brich.radek@ifortuna.cz>
parents:
68
diff
changeset
|
453 |
|
40
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
454 |
def list_sequences(self, schema=None): |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
455 |
'''List sequences in schema.''' |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
456 |
return self._query(''' |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
457 |
SELECT |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
458 |
nc.nspname AS "sequence_schema", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
459 |
c.relname AS "sequence_name", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
460 |
t.relname AS "related_table", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
461 |
a.attname AS "related_column", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
462 |
format_type(a.atttypid, a.atttypmod) AS "related_column_type" |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
463 |
FROM pg_class c |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
464 |
JOIN pg_namespace nc ON nc.oid = c.relnamespace |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
465 |
JOIN pg_depend d ON d.objid = c.oid |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
466 |
JOIN pg_class t ON d.refobjid = t.oid |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
467 |
JOIN pg_attribute a ON (d.refobjid, d.refobjsubid) = (a.attrelid, a.attnum) |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
468 |
WHERE c.relkind = 'S' AND NOT pg_is_other_temp_schema(nc.oid) |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
469 |
''' + (schema and ' AND nc.nspname = %(schema)s' or '') + ''' |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
470 |
''', {'schema': schema}) |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
471 |
|
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
|
472 |
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
|
473 |
'''List objects using the column. |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
474 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
475 |
Currently shows views and constraints which use the column. |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
476 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
477 |
This is useful to find which views block alteration of column type etc. |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
478 |
|
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
|
479 |
''' |
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
|
480 |
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
|
481 |
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
|
482 |
'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
|
483 |
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
|
484 |
WHERE table_schema=%(schema)s AND table_name=%(table)s AND column_name=%(column)s |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
485 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
486 |
UNION |
52
26121a8fe78b
Update analyzeall tool: add REINDEX option. Add ibrowser tool (useful for PgBrowser testing). Fix PgBrowser.list_columns default value.
Radek Brich <radek.brich@devl.cz>
parents:
40
diff
changeset
|
487 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
488 |
SELECT |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
489 |
'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
|
490 |
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
|
491 |
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
|
492 |
''', {'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
|
493 |