1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-beta.40 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Database; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Common trait for domains manipulation. |
11
|
|
|
*/ |
12
|
|
|
trait DomainTrait |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Gets all information for a single domain. |
17
|
|
|
* |
18
|
|
|
* @param string $domain The name of the domain to fetch |
19
|
|
|
* |
20
|
|
|
* @return \PHPPgAdmin\ADORecordSet A recordset |
21
|
|
|
*/ |
22
|
|
|
public function getDomain($domain) |
23
|
|
|
{ |
24
|
|
|
$c_schema = $this->_schema; |
25
|
|
|
$this->clean($c_schema); |
|
|
|
|
26
|
|
|
$this->clean($domain); |
27
|
|
|
|
28
|
|
|
$sql = " |
29
|
|
|
SELECT |
30
|
|
|
t.typname AS domname, |
31
|
|
|
pg_catalog.format_type(t.typbasetype, t.typtypmod) AS domtype, |
32
|
|
|
t.typnotnull AS domnotnull, |
33
|
|
|
t.typdefault AS domdef, |
34
|
|
|
pg_catalog.pg_get_userbyid(t.typowner) AS domowner, |
35
|
|
|
pg_catalog.obj_description(t.oid, 'pg_type') AS domcomment |
36
|
|
|
FROM |
37
|
|
|
pg_catalog.pg_type t |
38
|
|
|
WHERE |
39
|
|
|
t.typtype = 'd' |
40
|
|
|
AND t.typname = '{$domain}' |
41
|
|
|
AND t.typnamespace = (SELECT oid FROM pg_catalog.pg_namespace |
42
|
|
|
WHERE nspname = '{$c_schema}')"; |
43
|
|
|
|
44
|
|
|
return $this->selectSet($sql); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return all domains in current schema. Excludes domain constraints. |
49
|
|
|
* |
50
|
|
|
* @return \PHPPgAdmin\ADORecordSet All tables, sorted alphabetically |
51
|
|
|
*/ |
52
|
|
|
public function getDomains() |
53
|
|
|
{ |
54
|
|
|
$c_schema = $this->_schema; |
55
|
|
|
$this->clean($c_schema); |
56
|
|
|
|
57
|
|
|
$sql = " |
58
|
|
|
SELECT |
59
|
|
|
t.typname AS domname, |
60
|
|
|
pg_catalog.format_type(t.typbasetype, t.typtypmod) AS domtype, |
61
|
|
|
t.typnotnull AS domnotnull, |
62
|
|
|
t.typdefault AS domdef, |
63
|
|
|
pg_catalog.pg_get_userbyid(t.typowner) AS domowner, |
64
|
|
|
pg_catalog.obj_description(t.oid, 'pg_type') AS domcomment |
65
|
|
|
FROM |
66
|
|
|
pg_catalog.pg_type t |
67
|
|
|
WHERE |
68
|
|
|
t.typtype = 'd' |
69
|
|
|
AND t.typnamespace = (SELECT oid FROM pg_catalog.pg_namespace |
70
|
|
|
WHERE nspname='{$c_schema}') |
71
|
|
|
ORDER BY t.typname"; |
72
|
|
|
|
73
|
|
|
return $this->selectSet($sql); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Get domain constraints. |
78
|
|
|
* |
79
|
|
|
* @param $domain The name of the domain whose constraints to fetch |
|
|
|
|
80
|
|
|
* |
81
|
|
|
* @return \PHPPgAdmin\ADORecordSet A recordset |
82
|
|
|
*/ |
83
|
|
|
public function getDomainConstraints($domain) |
84
|
|
|
{ |
85
|
|
|
$c_schema = $this->_schema; |
86
|
|
|
$this->clean($c_schema); |
87
|
|
|
$this->clean($domain); |
88
|
|
|
|
89
|
|
|
$sql = " |
90
|
|
|
SELECT |
91
|
|
|
conname, |
92
|
|
|
contype, |
93
|
|
|
pg_catalog.pg_get_constraintdef(oid, true) AS consrc |
94
|
|
|
FROM |
95
|
|
|
pg_catalog.pg_constraint |
96
|
|
|
WHERE |
97
|
|
|
contypid = ( |
98
|
|
|
SELECT oid FROM pg_catalog.pg_type |
99
|
|
|
WHERE typname='{$domain}' |
100
|
|
|
AND typnamespace = ( |
101
|
|
|
SELECT oid FROM pg_catalog.pg_namespace |
102
|
|
|
WHERE nspname = '{$c_schema}') |
103
|
|
|
) |
104
|
|
|
ORDER BY conname"; |
105
|
|
|
|
106
|
|
|
return $this->selectSet($sql); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Creates a domain. |
111
|
|
|
* |
112
|
|
|
* @param string $domain The name of the domain to create |
113
|
|
|
* @param string $type The base type for the domain |
114
|
|
|
* @param string $length Optional type length |
115
|
|
|
* @param bool $array True for array type, false otherwise |
116
|
|
|
* @param bool $notnull True for NOT NULL, false otherwise |
117
|
|
|
* @param string $default Default value for domain |
118
|
|
|
* @param string $check A CHECK constraint if there is one |
119
|
|
|
* |
120
|
|
|
* @return int 0 if operation was successful |
121
|
|
|
*/ |
122
|
|
|
public function createDomain($domain, $type, $length, $array, $notnull, $default, $check) |
123
|
|
|
{ |
124
|
|
|
$f_schema = $this->_schema; |
125
|
|
|
$this->fieldClean($f_schema); |
|
|
|
|
126
|
|
|
$this->fieldClean($domain); |
127
|
|
|
|
128
|
|
|
$sql = "CREATE DOMAIN \"{$f_schema}\".\"{$domain}\" AS "; |
129
|
|
|
|
130
|
|
|
if ($length == '') { |
131
|
|
|
$sql .= $type; |
132
|
|
|
} else { |
133
|
|
|
switch ($type) { |
134
|
|
|
// Have to account for weird placing of length for with/without |
135
|
|
|
// time zone types |
136
|
|
|
case 'timestamp with time zone': |
137
|
|
|
case 'timestamp without time zone': |
138
|
|
|
$qual = substr($type, 9); |
139
|
|
|
$sql .= "timestamp({$length}){$qual}"; |
140
|
|
|
|
141
|
|
|
break; |
142
|
|
|
case 'time with time zone': |
143
|
|
|
case 'time without time zone': |
144
|
|
|
$qual = substr($type, 4); |
145
|
|
|
$sql .= "time({$length}){$qual}"; |
146
|
|
|
|
147
|
|
|
break; |
148
|
|
|
default: |
149
|
|
|
$sql .= "{$type}({$length})"; |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
// Add array qualifier, if requested |
154
|
|
|
if ($array) { |
155
|
|
|
$sql .= '[]'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if ($notnull) { |
159
|
|
|
$sql .= ' NOT NULL'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
if ($default != '') { |
163
|
|
|
$sql .= " DEFAULT {$default}"; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($this->hasDomainConstraints() && $check != '') { |
|
|
|
|
167
|
|
|
$sql .= " CHECK ({$check})"; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return $this->execute($sql); |
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Alters a domain. |
175
|
|
|
* |
176
|
|
|
* @param string $domain The domain to alter |
177
|
|
|
* @param string $domdefault The domain default |
178
|
|
|
* @param bool $domnotnull True for NOT NULL, false otherwise |
179
|
|
|
* @param string $domowner The domain owner |
180
|
|
|
* |
181
|
|
|
* @return bool|int 0 success |
182
|
|
|
*/ |
183
|
|
|
public function alterDomain($domain, $domdefault, $domnotnull, $domowner) |
184
|
|
|
{ |
185
|
|
|
$f_schema = $this->_schema; |
186
|
|
|
$this->fieldClean($f_schema); |
187
|
|
|
$this->fieldClean($domain); |
188
|
|
|
$this->fieldClean($domowner); |
189
|
|
|
|
190
|
|
|
$status = $this->beginTransaction(); |
|
|
|
|
191
|
|
|
if ($status != 0) { |
192
|
|
|
$this->rollbackTransaction(); |
|
|
|
|
193
|
|
|
|
194
|
|
|
return -1; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
// Default |
198
|
|
|
if ($domdefault == '') { |
199
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" DROP DEFAULT"; |
200
|
|
|
} else { |
201
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" SET DEFAULT {$domdefault}"; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$status = $this->execute($sql); |
205
|
|
|
if ($status != 0) { |
206
|
|
|
$this->rollbackTransaction(); |
207
|
|
|
|
208
|
|
|
return -2; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
// NOT NULL |
212
|
|
|
if ($domnotnull) { |
213
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" SET NOT NULL"; |
214
|
|
|
} else { |
215
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" DROP NOT NULL"; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$status = $this->execute($sql); |
219
|
|
|
if ($status != 0) { |
220
|
|
|
$this->rollbackTransaction(); |
221
|
|
|
|
222
|
|
|
return -3; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
// Owner |
226
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" OWNER TO \"{$domowner}\""; |
227
|
|
|
|
228
|
|
|
$status = $this->execute($sql); |
229
|
|
|
if ($status != 0) { |
230
|
|
|
$this->rollbackTransaction(); |
231
|
|
|
|
232
|
|
|
return -4; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $this->endTransaction(); |
|
|
|
|
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Drops a domain. |
240
|
|
|
* |
241
|
|
|
* @param string $domain The name of the domain to drop |
242
|
|
|
* @param string $cascade True to cascade drop, false to restrict |
243
|
|
|
* |
244
|
|
|
* @return int 0 if operation was successful |
245
|
|
|
*/ |
246
|
|
|
public function dropDomain($domain, $cascade) |
247
|
|
|
{ |
248
|
|
|
$f_schema = $this->_schema; |
249
|
|
|
$this->fieldClean($f_schema); |
250
|
|
|
$this->fieldClean($domain); |
251
|
|
|
|
252
|
|
|
$sql = "DROP DOMAIN \"{$f_schema}\".\"{$domain}\""; |
253
|
|
|
if ($cascade) { |
254
|
|
|
$sql .= ' CASCADE'; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this->execute($sql); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Adds a check constraint to a domain. |
262
|
|
|
* |
263
|
|
|
* @param string $domain The domain to which to add the check |
264
|
|
|
* @param string $definition The definition of the check |
265
|
|
|
* @param string $name (optional) The name to give the check, otherwise default name is assigned |
266
|
|
|
* |
267
|
|
|
* @return int 0 if operation was successful |
268
|
|
|
*/ |
269
|
|
|
public function addDomainCheckConstraint($domain, $definition, $name = '') |
270
|
|
|
{ |
271
|
|
|
$f_schema = $this->_schema; |
272
|
|
|
$this->fieldClean($f_schema); |
273
|
|
|
$this->fieldClean($domain); |
274
|
|
|
$this->fieldClean($name); |
275
|
|
|
|
276
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" ADD "; |
277
|
|
|
if ($name != '') { |
278
|
|
|
$sql .= "CONSTRAINT \"{$name}\" "; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
$sql .= "CHECK ({$definition})"; |
282
|
|
|
|
283
|
|
|
return $this->execute($sql); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Drops a domain constraint. |
288
|
|
|
* |
289
|
|
|
* @param string $domain The domain from which to remove the constraint |
290
|
|
|
* @param string $constraint The constraint to remove |
291
|
|
|
* @param bool $cascade True to cascade, false otherwise |
292
|
|
|
* |
293
|
|
|
* @return int 0 if operation was successful |
294
|
|
|
*/ |
295
|
|
|
public function dropDomainConstraint($domain, $constraint, $cascade) |
296
|
|
|
{ |
297
|
|
|
$f_schema = $this->_schema; |
298
|
|
|
$this->fieldClean($f_schema); |
299
|
|
|
$this->fieldClean($domain); |
300
|
|
|
$this->fieldClean($constraint); |
301
|
|
|
|
302
|
|
|
$sql = "ALTER DOMAIN \"{$f_schema}\".\"{$domain}\" DROP CONSTRAINT \"{$constraint}\""; |
303
|
|
|
if ($cascade) { |
304
|
|
|
$sql .= ' CASCADE'; |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
return $this->execute($sql); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|