1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PHPPgAdmin v6.0.0-beta.47 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Traits; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Common trait for column manipulation. |
11
|
|
|
*/ |
12
|
|
|
trait ColumnTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Add a new column to a table. |
16
|
|
|
* |
17
|
|
|
* @param string $table The table to add to |
18
|
|
|
* @param string $column The name of the new column |
19
|
|
|
* @param string $type The type of the column |
20
|
|
|
* @param bool $array True if array type, false otherwise |
21
|
|
|
* @param int $length The optional size of the column (ie. 30 for varchar(30)) |
22
|
|
|
* @param bool $notnull True if NOT NULL, false otherwise |
23
|
|
|
* @param mixed $default The default for the column. '' for none. |
24
|
|
|
* @param string $comment comment for the column |
25
|
|
|
* |
26
|
|
|
* @return bool|int 0 success |
27
|
|
|
*/ |
28
|
|
|
public function addColumn($table, $column, $type, $array, $length, $notnull, $default, $comment) |
29
|
|
|
{ |
30
|
|
|
$f_schema = $this->_schema; |
31
|
|
|
$this->fieldClean($f_schema); |
32
|
|
|
$this->fieldClean($table); |
33
|
|
|
$this->fieldClean($column); |
34
|
|
|
$this->clean($type); |
35
|
|
|
$this->clean($length); |
36
|
|
|
|
37
|
|
|
if ($length == '') { |
38
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" {$type}"; |
39
|
|
|
} else { |
40
|
|
|
switch ($type) { |
41
|
|
|
// Have to account for weird placing of length for with/without |
42
|
|
|
// time zone types |
43
|
|
|
case 'timestamp with time zone': |
44
|
|
|
case 'timestamp without time zone': |
45
|
|
|
$qual = substr($type, 9); |
46
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" timestamp({$length}){$qual}"; |
47
|
|
|
|
48
|
|
|
break; |
49
|
|
|
case 'time with time zone': |
50
|
|
|
case 'time without time zone': |
51
|
|
|
$qual = substr($type, 4); |
52
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" time({$length}){$qual}"; |
53
|
|
|
|
54
|
|
|
break; |
55
|
|
|
default: |
56
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ADD COLUMN \"{$column}\" {$type}({$length})"; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// Add array qualifier, if requested |
61
|
|
|
if ($array) { |
62
|
|
|
$sql .= '[]'; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// If we have advanced column adding, add the extra qualifiers |
66
|
|
|
if ($this->hasCreateFieldWithConstraints()) { |
67
|
|
|
// NOT NULL clause |
68
|
|
|
if ($notnull) { |
69
|
|
|
$sql .= ' NOT NULL'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// DEFAULT clause |
73
|
|
|
if ($default != '') { |
74
|
|
|
$sql .= ' DEFAULT '.$default; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$status = $this->beginTransaction(); |
79
|
|
|
if ($status != 0) { |
80
|
|
|
return -1; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$status = $this->execute($sql); |
84
|
|
|
if ($status != 0) { |
85
|
|
|
$this->rollbackTransaction(); |
86
|
|
|
|
87
|
|
|
return -1; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$status = $this->setComment('COLUMN', $column, $table, $comment); |
91
|
|
|
if ($status != 0) { |
92
|
|
|
$this->rollbackTransaction(); |
93
|
|
|
|
94
|
|
|
return -1; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->endTransaction(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Alters a column in a table. |
102
|
|
|
* |
103
|
|
|
* @param string $table The table in which the column resides |
104
|
|
|
* @param string $column The column to alter |
105
|
|
|
* @param string $name The new name for the column |
106
|
|
|
* @param bool $notnull (boolean) True if not null, false otherwise |
107
|
|
|
* @param bool $oldnotnull (boolean) True if column is already not null, false otherwise |
108
|
|
|
* @param mixed $default The new default for the column |
109
|
|
|
* @param mixed $olddefault The old default for the column |
110
|
|
|
* @param string $type The new type for the column |
111
|
|
|
* @param int $length The optional size of the column (ie. 30 for varchar(30)) |
112
|
|
|
* @param bool $array True if array type, false otherwise |
113
|
|
|
* @param string $oldtype The old type for the column |
114
|
|
|
* @param string $comment Comment for the column |
115
|
|
|
* |
116
|
|
|
* @return array 0 success |
117
|
|
|
*/ |
118
|
|
|
public function alterColumn( |
119
|
|
|
$table, |
120
|
|
|
$column, |
121
|
|
|
$name, |
122
|
|
|
$notnull, |
123
|
|
|
$oldnotnull, |
124
|
|
|
$default, |
125
|
|
|
$olddefault, |
126
|
|
|
$type, |
127
|
|
|
$length, |
128
|
|
|
$array, |
129
|
|
|
$oldtype, |
130
|
|
|
$comment |
131
|
|
|
) { |
132
|
|
|
// Begin transaction |
133
|
|
|
$status = $this->beginTransaction(); |
134
|
|
|
$sql = ''; |
135
|
|
|
if ($status != 0) { |
136
|
|
|
$this->rollbackTransaction(); |
137
|
|
|
|
138
|
|
|
return [-6, $sql]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
// Rename the column, if it has been changed |
142
|
|
|
if ($column != $name) { |
143
|
|
|
$status = $this->renameColumn($table, $column, $name); |
144
|
|
|
if ($status != 0) { |
145
|
|
|
$this->rollbackTransaction(); |
146
|
|
|
|
147
|
|
|
return [-4, $sql]; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$f_schema = $this->_schema; |
152
|
|
|
$this->fieldClean($f_schema); |
153
|
|
|
$this->fieldClean($name); |
154
|
|
|
$this->fieldClean($table); |
155
|
|
|
$this->fieldClean($column); |
156
|
|
|
|
157
|
|
|
$toAlter = []; |
158
|
|
|
// Create the command for changing nullability |
159
|
|
|
if ($notnull != $oldnotnull) { |
160
|
|
|
$toAlter[] = "ALTER COLUMN \"{$name}\" ".($notnull ? 'SET' : 'DROP').' NOT NULL'; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
// Add default, if it has changed |
164
|
|
|
if ($default != $olddefault) { |
165
|
|
|
if ($default == '') { |
166
|
|
|
$toAlter[] = "ALTER COLUMN \"{$name}\" DROP DEFAULT"; |
167
|
|
|
} else { |
168
|
|
|
$toAlter[] = "ALTER COLUMN \"{$name}\" SET DEFAULT {$default}"; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
// Add type, if it has changed |
173
|
|
|
if ($length == '') { |
174
|
|
|
$ftype = $type; |
175
|
|
|
} else { |
176
|
|
|
switch ($type) { |
177
|
|
|
// Have to account for weird placing of length for with/without |
178
|
|
|
// time zone types |
179
|
|
|
case 'timestamp with time zone': |
180
|
|
|
case 'timestamp without time zone': |
181
|
|
|
$qual = substr($type, 9); |
182
|
|
|
$ftype = "timestamp({$length}){$qual}"; |
183
|
|
|
|
184
|
|
|
break; |
185
|
|
|
case 'time with time zone': |
186
|
|
|
case 'time without time zone': |
187
|
|
|
$qual = substr($type, 4); |
188
|
|
|
$ftype = "time({$length}){$qual}"; |
189
|
|
|
|
190
|
|
|
break; |
191
|
|
|
default: |
192
|
|
|
$ftype = "{$type}({$length})"; |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Add array qualifier, if requested |
197
|
|
|
if ($array) { |
198
|
|
|
$ftype .= '[]'; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
if ($ftype != $oldtype) { |
202
|
|
|
$toAlter[] = "ALTER COLUMN \"{$name}\" TYPE {$ftype}"; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Attempt to process the batch alteration, if anything has been changed |
206
|
|
|
if (!empty($toAlter)) { |
207
|
|
|
// Initialise an empty SQL string |
208
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" " |
209
|
|
|
.implode(',', $toAlter); |
210
|
|
|
|
211
|
|
|
$status = $this->execute($sql); |
212
|
|
|
if ($status != 0) { |
213
|
|
|
$this->rollbackTransaction(); |
214
|
|
|
|
215
|
|
|
return [-1, $sql]; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
// Update the comment on the column |
220
|
|
|
$status = $this->setComment('COLUMN', $name, $table, $comment); |
221
|
|
|
if ($status != 0) { |
222
|
|
|
$this->rollbackTransaction(); |
223
|
|
|
|
224
|
|
|
return [-5, $sql]; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return [$this->endTransaction(), $sql]; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Renames a column in a table. |
232
|
|
|
* |
233
|
|
|
* @param string $table The table containing the column to be renamed |
234
|
|
|
* @param string $column The column to be renamed |
235
|
|
|
* @param string $newName The new name for the column |
236
|
|
|
* |
237
|
|
|
* @return int 0 if operation was successful |
238
|
|
|
*/ |
239
|
|
|
public function renameColumn($table, $column, $newName) |
240
|
|
|
{ |
241
|
|
|
$f_schema = $this->_schema; |
242
|
|
|
$this->fieldClean($f_schema); |
243
|
|
|
$this->fieldClean($table); |
244
|
|
|
$this->fieldClean($column); |
245
|
|
|
$this->fieldClean($newName); |
246
|
|
|
|
247
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" RENAME COLUMN \"{$column}\" TO \"{$newName}\""; |
248
|
|
|
|
249
|
|
|
return $this->execute($sql); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Sets default value of a column. |
254
|
|
|
* |
255
|
|
|
* @param string $table The table from which to drop |
256
|
|
|
* @param string $column The column name to set |
257
|
|
|
* @param mixed $default The new default value |
258
|
|
|
* |
259
|
|
|
* @return int 0 if operation was successful |
260
|
|
|
*/ |
261
|
|
|
public function setColumnDefault($table, $column, $default) |
262
|
|
|
{ |
263
|
|
|
$f_schema = $this->_schema; |
264
|
|
|
$this->fieldClean($f_schema); |
265
|
|
|
$this->fieldClean($table); |
266
|
|
|
$this->fieldClean($column); |
267
|
|
|
|
268
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ALTER COLUMN \"{$column}\" SET DEFAULT {$default}"; |
269
|
|
|
|
270
|
|
|
return $this->execute($sql); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Sets whether or not a column can contain NULLs. |
275
|
|
|
* |
276
|
|
|
* @param string $table The table that contains the column |
277
|
|
|
* @param string $column The column to alter |
278
|
|
|
* @param bool $state True to set null, false to set not null |
279
|
|
|
* |
280
|
|
|
* @return int 0 if operation was successful |
281
|
|
|
*/ |
282
|
|
|
public function setColumnNull($table, $column, $state) |
283
|
|
|
{ |
284
|
|
|
$f_schema = $this->_schema; |
285
|
|
|
$this->fieldClean($f_schema); |
286
|
|
|
$this->fieldClean($table); |
287
|
|
|
$this->fieldClean($column); |
288
|
|
|
|
289
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ALTER COLUMN \"{$column}\" ".($state ? 'DROP' : 'SET').' NOT NULL'; |
290
|
|
|
|
291
|
|
|
return $this->execute($sql); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Drops a column from a table. |
296
|
|
|
* |
297
|
|
|
* @param string $table The table from which to drop a column |
298
|
|
|
* @param string $column The column to be dropped |
299
|
|
|
* @param bool $cascade True to cascade drop, false to restrict |
300
|
|
|
* |
301
|
|
|
* @return int 0 if operation was successful |
302
|
|
|
*/ |
303
|
|
|
public function dropColumn($table, $column, $cascade) |
304
|
|
|
{ |
305
|
|
|
$f_schema = $this->_schema; |
306
|
|
|
$this->fieldClean($f_schema); |
307
|
|
|
$this->fieldClean($table); |
308
|
|
|
$this->fieldClean($column); |
309
|
|
|
|
310
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" DROP COLUMN \"{$column}\""; |
311
|
|
|
if ($cascade) { |
312
|
|
|
$sql .= ' CASCADE'; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $this->execute($sql); |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Drops default value of a column. |
320
|
|
|
* |
321
|
|
|
* @param string $table The table from which to drop |
322
|
|
|
* @param string $column The column name to drop default |
323
|
|
|
* |
324
|
|
|
* @return int 0 if operation was successful |
325
|
|
|
*/ |
326
|
|
|
public function dropColumnDefault($table, $column) |
327
|
|
|
{ |
328
|
|
|
$f_schema = $this->_schema; |
329
|
|
|
$this->fieldClean($f_schema); |
330
|
|
|
$this->fieldClean($table); |
331
|
|
|
$this->fieldClean($column); |
332
|
|
|
|
333
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ALTER COLUMN \"{$column}\" DROP DEFAULT"; |
334
|
|
|
|
335
|
|
|
return $this->execute($sql); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
abstract public function fieldClean(&$str); |
339
|
|
|
|
340
|
|
|
abstract public function beginTransaction(); |
341
|
|
|
|
342
|
|
|
abstract public function rollbackTransaction(); |
343
|
|
|
|
344
|
|
|
abstract public function endTransaction(); |
345
|
|
|
|
346
|
|
|
abstract public function execute($sql); |
347
|
|
|
|
348
|
|
|
abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
349
|
|
|
|
350
|
|
|
abstract public function selectSet($sql); |
351
|
|
|
|
352
|
|
|
abstract public function clean(&$str); |
353
|
|
|
|
354
|
|
|
abstract public function phpBool($parameter); |
355
|
|
|
|
356
|
|
|
abstract public function hasCreateTableLikeWithConstraints(); |
357
|
|
|
|
358
|
|
|
abstract public function hasCreateTableLikeWithIndexes(); |
359
|
|
|
|
360
|
|
|
abstract public function hasTablespaces(); |
361
|
|
|
|
362
|
|
|
abstract public function delete($table, $conditions, $schema = ''); |
363
|
|
|
|
364
|
|
|
abstract public function fieldArrayClean(&$arr); |
365
|
|
|
|
366
|
|
|
abstract public function hasCreateFieldWithConstraints(); |
367
|
|
|
|
368
|
|
|
abstract public function getAttributeNames($table, $atts); |
369
|
|
|
} |
370
|
|
|
|