author | Radek Brich <radek.brich@devl.cz> |
Tue, 19 Mar 2013 16:45:37 +0100 | |
changeset 78 | 64c62ac8f65d |
parent 68 | b0d972be2631 |
child 85 | 11a282e23e0d |
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 |
|
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, |
|
57 |
name, primary, unique, clustered, valid, definition): |
|
58 |
self.browser = browser |
|
59 |
self.table = table |
|
60 |
self.name = name |
|
61 |
self.primary = primary |
|
62 |
self.unique = unique |
|
63 |
self.clustered = clustered |
|
64 |
self.valid = valid |
|
65 |
self.definition = definition |
|
66 |
||
67 |
||
68 |
class Table: |
|
68 | 69 |
def __init__(self, browser, schema, name, owner, size, description, options): |
0 | 70 |
self._columns = None |
71 |
self._constraints = None |
|
72 |
self._indexes = None |
|
73 |
self.browser = browser # Browser instance |
|
74 |
self.schema = schema # Schema instance |
|
75 |
self.name = name # table name, str |
|
76 |
self.owner = owner |
|
77 |
self.size = size |
|
78 |
self.description = description |
|
68 | 79 |
self.options = options or [] |
0 | 80 |
|
81 |
def refresh(self): |
|
82 |
self.refresh_columns() |
|
83 |
self.refresh_constraints() |
|
84 |
self.refresh_indexes() |
|
85 |
||
86 |
def refresh_columns(self): |
|
87 |
rows = self.browser.list_columns(self.name, self.schema.name) |
|
88 |
self._columns = OrderedDict([(x['name'], Column(self.browser, self, **x)) for x in rows]) |
|
89 |
||
90 |
def refresh_constraints(self): |
|
91 |
rows = self.browser.list_constraints(self.name, self.schema.name) |
|
92 |
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
|
93 |
|
0 | 94 |
def refresh_indexes(self): |
95 |
rows = self.browser.list_indexes(self.name, self.schema.name) |
|
96 |
self._indexes = OrderedDict([(x['name'], Index(self.browser, self, **x)) for x in rows]) |
|
97 |
||
98 |
def getcolumns(self): |
|
99 |
if self._columns is None: |
|
100 |
self.refresh_columns() |
|
101 |
return self._columns |
|
102 |
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
|
103 |
|
0 | 104 |
def getconstraints(self): |
105 |
if self._constraints is None: |
|
106 |
self.refresh_constraints() |
|
107 |
return self._constraints |
|
108 |
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
|
109 |
|
0 | 110 |
def getindexes(self): |
111 |
if self._indexes is None: |
|
112 |
self.refresh_indexes() |
|
113 |
return self._indexes |
|
114 |
indexes = property(getindexes) |
|
115 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
116 |
|
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
|
117 |
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
|
118 |
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
|
119 |
# PgBrowser instance |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
120 |
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
|
121 |
# 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
|
122 |
self.function = function |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
123 |
self.name = name |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
124 |
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
|
125 |
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
|
126 |
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
|
127 |
|
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 |
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
|
129 |
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
|
130 |
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
|
131 |
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
|
132 |
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
|
133 |
#: 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
|
134 |
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
|
135 |
#: 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
|
136 |
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
|
137 |
self.type = type |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
138 |
self.result = result |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
139 |
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
|
140 |
self._arguments = None |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
141 |
self._definition = None |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
142 |
|
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
|
143 |
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
|
144 |
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
|
145 |
|
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 |
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
|
147 |
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
|
148 |
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
|
149 |
|
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 |
@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
|
151 |
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
|
152 |
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
|
153 |
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
|
154 |
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
|
155 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
156 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
157 |
def definition(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
158 |
"""Get full function definition including CREATE command.""" |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
159 |
if not self._definition: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
160 |
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
|
161 |
return self._definition |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
162 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
163 |
|
0 | 164 |
class Schema: |
165 |
def __init__(self, browser, name, owner, acl, description, system): |
|
166 |
self._tables = None |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
167 |
self._functions = None |
0 | 168 |
self.browser = browser |
169 |
self.name = name |
|
170 |
self.owner = owner |
|
171 |
self.acl = acl |
|
172 |
self.description = description |
|
173 |
self.system = system |
|
174 |
||
175 |
def refresh(self): |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
176 |
self.refresh_tables() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
177 |
self.refresh_functions() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
178 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
179 |
def refresh_tables(self): |
0 | 180 |
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
|
181 |
self._tables = OrderedDict([(x['name'], Table(self.browser, self, **x)) for x in rows]) |
0 | 182 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
183 |
def refresh_functions(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
184 |
rows = self.browser.list_functions(self.name) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
185 |
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
|
186 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
187 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
188 |
def tables(self): |
0 | 189 |
if self._tables is None: |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
190 |
self.refresh_tables() |
0 | 191 |
return self._tables |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
192 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
193 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
194 |
def functions(self): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
195 |
if self._functions is None: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
196 |
self.refresh_functions() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
197 |
return self._functions |
0 | 198 |
|
199 |
||
200 |
class PgBrowser: |
|
201 |
def __init__(self, conn=None): |
|
202 |
self._schemas = None |
|
203 |
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
|
204 |
|
0 | 205 |
def setconn(self, conn=None): |
206 |
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
|
207 |
|
0 | 208 |
def refresh(self): |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
209 |
self.refresh_schemas() |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
210 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
211 |
def refresh_schemas(self): |
0 | 212 |
rows = self.list_schemas() |
213 |
self._schemas = OrderedDict([(x['name'], Schema(self, **x)) for x in rows]) |
|
214 |
||
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
215 |
@property |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
216 |
def schemas(self): |
0 | 217 |
if self._schemas is None: |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
218 |
self.refresh_schemas() |
0 | 219 |
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
|
220 |
|
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
|
221 |
def _query(self, query, args): |
0 | 222 |
try: |
223 |
curs = self.conn.cursor() |
|
224 |
curs.execute(query, args) |
|
225 |
curs.connection.commit() |
|
226 |
rows = curs.fetchall() |
|
227 |
return [dict(zip([desc[0] for desc in curs.description], row)) for row in rows] |
|
228 |
finally: |
|
229 |
curs.close() |
|
230 |
||
231 |
def list_databases(self): |
|
232 |
return self._query(''' |
|
233 |
SELECT |
|
234 |
d.datname as "name", |
|
235 |
pg_catalog.pg_get_userbyid(d.datdba) as "owner", |
|
236 |
pg_catalog.pg_encoding_to_char(d.encoding) as "encoding", |
|
237 |
d.datcollate as "collation", |
|
238 |
d.datctype as "ctype", |
|
239 |
d.datacl AS "acl", |
|
240 |
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') |
|
241 |
THEN pg_catalog.pg_database_size(d.datname) |
|
242 |
ELSE -1 -- No access |
|
243 |
END as "size", |
|
244 |
t.spcname as "tablespace", |
|
245 |
pg_catalog.shobj_description(d.oid, 'pg_database') as "description" |
|
246 |
FROM pg_catalog.pg_database d |
|
247 |
JOIN pg_catalog.pg_tablespace t on d.dattablespace = t.oid |
|
248 |
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
|
249 |
''', []) |
0 | 250 |
|
251 |
def list_schemas(self): |
|
252 |
return self._query(''' |
|
253 |
SELECT |
|
254 |
n.nspname AS "name", |
|
255 |
pg_catalog.pg_get_userbyid(n.nspowner) AS "owner", |
|
256 |
n.nspacl AS "acl", |
|
257 |
pg_catalog.obj_description(n.oid, 'pg_namespace') AS "description", |
|
258 |
CASE WHEN n.nspname IN ('information_schema', 'pg_catalog', 'pg_toast') |
|
259 |
OR n.nspname ~ '^pg_temp_' OR n.nspname ~ '^pg_toast_temp_' |
|
260 |
THEN TRUE |
|
261 |
ELSE FALSE |
|
262 |
END AS "system" |
|
263 |
FROM pg_catalog.pg_namespace n |
|
264 |
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
|
265 |
''', []) |
0 | 266 |
|
267 |
def list_tables(self, schema='public'): |
|
268 |
return self._query(''' |
|
269 |
SELECT |
|
270 |
c.relname as "name", |
|
271 |
pg_catalog.pg_get_userbyid(c.relowner) as "owner", |
|
272 |
pg_catalog.pg_relation_size(c.oid) as "size", |
|
68 | 273 |
pg_catalog.obj_description(c.oid, 'pg_class') as "description", |
274 |
c.reloptions as "options" |
|
0 | 275 |
FROM pg_catalog.pg_class c |
276 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
277 |
WHERE n.nspname = %s AND c.relkind IN ('r','s','') |
|
278 |
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
|
279 |
''', [schema]) |
0 | 280 |
|
12 | 281 |
def list_columns(self, table, schema='public', order=2): |
0 | 282 |
return self._query(''' |
283 |
SELECT |
|
40
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
284 |
--a.attrelid, |
0 | 285 |
a.attname as "name", |
286 |
format_type(a.atttypid, a.atttypmod) AS "type", |
|
287 |
a.attnotnull as "notnull", |
|
288 |
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
|
289 |
pg_catalog.pg_get_expr(d.adbin, d.adrelid) as "default", |
0 | 290 |
pg_catalog.col_description(a.attrelid, a.attnum) AS "description" |
291 |
FROM pg_catalog.pg_attribute a |
|
292 |
LEFT JOIN pg_catalog.pg_class c ON a.attrelid = c.oid |
|
293 |
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
294 |
LEFT JOIN pg_catalog.pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum |
|
295 |
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
|
296 |
ORDER BY ''' + str(order), [schema, table]) |
0 | 297 |
|
298 |
def list_constraints(self, table, schema='public'): |
|
299 |
return self._query(''' |
|
300 |
SELECT |
|
35
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
301 |
r.conname AS "name", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
302 |
r.contype AS "type", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
303 |
cf.relname AS "fname", |
e7f79c4a27ce
TableCopyTool: order tables with respect to references.
Radek Brich <radek.brich@devl.cz>
parents:
32
diff
changeset
|
304 |
nf.nspname AS "fschema", |
0 | 305 |
pg_catalog.pg_get_constraintdef(r.oid, true) as "definition" |
306 |
FROM pg_catalog.pg_constraint r |
|
307 |
JOIN pg_catalog.pg_class c ON r.conrelid = c.oid |
|
308 |
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
|
309 |
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
|
310 |
LEFT JOIN pg_catalog.pg_namespace nf ON nf.oid = cf.relnamespace |
0 | 311 |
WHERE n.nspname = %s AND c.relname = %s |
312 |
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
|
313 |
''', [schema, table]) |
0 | 314 |
|
315 |
def list_indexes(self, table, schema='public'): |
|
316 |
return self._query(''' |
|
317 |
SELECT |
|
318 |
c2.relname as "name", |
|
319 |
i.indisprimary as "primary", |
|
320 |
i.indisunique as "unique", |
|
321 |
i.indisclustered as "clustered", |
|
322 |
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
|
323 |
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
|
324 |
ARRAY(SELECT a.attname FROM pg_catalog.pg_attribute a WHERE a.attrelid = c2.oid ORDER BY attnum) AS "columns" |
0 | 325 |
--c2.reltablespace as "tablespace_oid" |
326 |
FROM pg_catalog.pg_class c |
|
327 |
JOIN pg_catalog.pg_index i ON c.oid = i.indrelid |
|
328 |
JOIN pg_catalog.pg_class c2 ON i.indexrelid = c2.oid |
|
329 |
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
|
330 |
WHERE n.nspname = %(schema)s AND c.relname = %(table)s |
0 | 331 |
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
|
332 |
''', {'schema': schema, 'table': table}) |
0 | 333 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
334 |
def list_functions(self, schema='public'): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
335 |
'''List functions in schema.''' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
336 |
return self._query(''' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
337 |
SELECT |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
338 |
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
|
339 |
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
|
340 |
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
|
341 |
', ' |
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
|
342 |
) || ')' 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
|
343 |
p.proname as "function_name", |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
344 |
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
|
345 |
p.prosrc as "source", |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
346 |
CASE |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
347 |
WHEN p.proisagg THEN 'agg' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
348 |
WHEN p.proiswindow THEN 'window' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
349 |
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
|
350 |
ELSE 'normal' |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
351 |
END as "type" |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
352 |
FROM pg_catalog.pg_proc p |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
353 |
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
|
354 |
WHERE n.nspname = %s |
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
355 |
ORDER BY 1, 2, 4; |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
356 |
''', [schema]) |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
357 |
|
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
|
358 |
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
|
359 |
"""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
|
360 |
|
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
|
361 |
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
|
362 |
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
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
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
|
367 |
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
|
368 |
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
|
369 |
""" |
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
|
370 |
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
|
371 |
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
|
372 |
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
|
373 |
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
|
374 |
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
|
375 |
), 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
|
376 |
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
|
377 |
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
|
378 |
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
|
379 |
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
|
380 |
) |
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 |
) 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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
)) 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
|
386 |
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
|
387 |
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
|
388 |
|
58
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
389 |
def get_function_definition(self, oid): |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
390 |
"""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
|
391 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
392 |
Args: |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
393 |
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
|
394 |
|
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
395 |
""" |
0bcc13460dae
PgBrowser: Add functions. PgDiff: Compare functions.
Radek Brich <radek.brich@devl.cz>
parents:
52
diff
changeset
|
396 |
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
|
397 |
|
40
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
398 |
def list_sequences(self, schema=None): |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
399 |
'''List sequences in schema.''' |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
400 |
return self._query(''' |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
401 |
SELECT |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
402 |
nc.nspname AS "sequence_schema", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
403 |
c.relname AS "sequence_name", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
404 |
t.relname AS "related_table", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
405 |
a.attname AS "related_column", |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
406 |
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
|
407 |
FROM pg_class c |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
408 |
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
|
409 |
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
|
410 |
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
|
411 |
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
|
412 |
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
|
413 |
''' + (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
|
414 |
''', {'schema': schema}) |
922d7fb63384
Add listserial tool. PgBrowser: Add list_sequences.
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
415 |
|
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
|
416 |
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
|
417 |
'''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
|
418 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
419 |
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
|
420 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
421 |
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
|
422 |
|
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
|
423 |
''' |
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
|
424 |
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
|
425 |
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
|
426 |
'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
|
427 |
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
|
428 |
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
|
429 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
430 |
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
|
431 |
|
32
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
432 |
SELECT |
d59c473c9ad7
Add batchupdate tool. PgBrowser: add constraints to list_column_usage().
Radek Brich <radek.brich@devl.cz>
parents:
31
diff
changeset
|
433 |
'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
|
434 |
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
|
435 |
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
|
436 |
''', {'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
|
437 |