|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* PHPPgAdmin 6.1.3 |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace PHPPgAdmin\Database\Traits; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Common trait for trigger and rules manipulation. |
|
11
|
|
|
*/ |
|
12
|
|
|
trait TriggerTrait |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Grabs a single trigger. |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $table The name of a table whose triggers to retrieve |
|
18
|
|
|
* @param string $trigger The name of the trigger to retrieve |
|
19
|
|
|
* |
|
20
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getTrigger($table, $trigger) |
|
23
|
|
|
{ |
|
24
|
|
|
$c_schema = $this->_schema; |
|
25
|
|
|
$this->clean($c_schema); |
|
26
|
|
|
$this->clean($table); |
|
27
|
|
|
$this->clean($trigger); |
|
28
|
|
|
|
|
29
|
|
|
$sql = " |
|
30
|
|
|
SELECT * FROM pg_catalog.pg_trigger t, pg_catalog.pg_class c |
|
31
|
|
|
WHERE t.tgrelid=c.oid AND c.relname='{$table}' AND t.tgname='{$trigger}' |
|
32
|
|
|
AND c.relnamespace=( |
|
33
|
|
|
SELECT oid FROM pg_catalog.pg_namespace |
|
34
|
|
|
WHERE nspname='{$c_schema}')"; |
|
35
|
|
|
|
|
36
|
|
|
return $this->selectSet($sql); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Creates a trigger. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $tgname The name of the trigger to create |
|
43
|
|
|
* @param string $table The name of the table |
|
44
|
|
|
* @param string $tgproc The function to execute |
|
45
|
|
|
* @param string $tgtime BEFORE or AFTER |
|
46
|
|
|
* @param string $tgevent Event |
|
47
|
|
|
* @param string $tgfrequency |
|
48
|
|
|
* @param string $tgargs The function arguments |
|
49
|
|
|
* |
|
50
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
51
|
|
|
*/ |
|
52
|
|
|
public function createTrigger($tgname, $table, $tgproc, $tgtime, $tgevent, $tgfrequency, $tgargs) |
|
53
|
|
|
{ |
|
54
|
|
|
$f_schema = $this->_schema; |
|
55
|
|
|
$this->fieldClean($f_schema); |
|
56
|
|
|
$this->fieldClean($tgname); |
|
57
|
|
|
$this->fieldClean($table); |
|
58
|
|
|
$this->fieldClean($tgproc); |
|
59
|
|
|
|
|
60
|
|
|
/* No Statement Level Triggers in PostgreSQL (by now) */ |
|
61
|
|
|
$sql = "CREATE TRIGGER \"{$tgname}\" {$tgtime} |
|
62
|
|
|
{$tgevent} ON \"{$f_schema}\".\"{$table}\" |
|
63
|
|
|
FOR EACH {$tgfrequency} EXECUTE PROCEDURE \"{$tgproc}\"({$tgargs})"; |
|
64
|
|
|
|
|
65
|
|
|
return $this->execute($sql); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Alters a trigger. |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $table The name of the table containing the trigger |
|
72
|
|
|
* @param string $trigger The name of the trigger to alter |
|
73
|
|
|
* @param string $name The new name for the trigger |
|
74
|
|
|
* |
|
75
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
76
|
|
|
*/ |
|
77
|
|
|
public function alterTrigger($table, $trigger, $name) |
|
78
|
|
|
{ |
|
79
|
|
|
$f_schema = $this->_schema; |
|
80
|
|
|
$this->fieldClean($f_schema); |
|
81
|
|
|
$this->fieldClean($table); |
|
82
|
|
|
$this->fieldClean($trigger); |
|
83
|
|
|
$this->fieldClean($name); |
|
84
|
|
|
|
|
85
|
|
|
$sql = "ALTER TRIGGER \"{$trigger}\" ON \"{$f_schema}\".\"{$table}\" RENAME TO \"{$name}\""; |
|
86
|
|
|
|
|
87
|
|
|
return $this->execute($sql); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Drops a trigger. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $tgname The name of the trigger to drop |
|
94
|
|
|
* @param string $table The table from which to drop the trigger |
|
95
|
|
|
* @param bool $cascade True to cascade drop, false to restrict |
|
96
|
|
|
* |
|
97
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
98
|
|
|
*/ |
|
99
|
|
|
public function dropTrigger($tgname, $table, $cascade) |
|
100
|
|
|
{ |
|
101
|
|
|
$f_schema = $this->_schema; |
|
102
|
|
|
$this->fieldClean($f_schema); |
|
103
|
|
|
$this->fieldClean($tgname); |
|
104
|
|
|
$this->fieldClean($table); |
|
105
|
|
|
|
|
106
|
|
|
$sql = "DROP TRIGGER \"{$tgname}\" ON \"{$f_schema}\".\"{$table}\""; |
|
107
|
|
|
|
|
108
|
|
|
if ($cascade) { |
|
109
|
|
|
$sql .= ' CASCADE'; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $this->execute($sql); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Enables a trigger. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $tgname The name of the trigger to enable |
|
119
|
|
|
* @param string $table The table in which to enable the trigger |
|
120
|
|
|
* |
|
121
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
122
|
|
|
*/ |
|
123
|
|
|
public function enableTrigger($tgname, $table) |
|
124
|
|
|
{ |
|
125
|
|
|
$f_schema = $this->_schema; |
|
126
|
|
|
$this->fieldClean($f_schema); |
|
127
|
|
|
$this->fieldClean($tgname); |
|
128
|
|
|
$this->fieldClean($table); |
|
129
|
|
|
|
|
130
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ENABLE TRIGGER \"{$tgname}\""; |
|
131
|
|
|
|
|
132
|
|
|
return $this->execute($sql); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Disables a trigger. |
|
137
|
|
|
* |
|
138
|
|
|
* @param string $tgname The name of the trigger to disable |
|
139
|
|
|
* @param string $table The table in which to disable the trigger |
|
140
|
|
|
* |
|
141
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
142
|
|
|
*/ |
|
143
|
|
|
public function disableTrigger($tgname, $table) |
|
144
|
|
|
{ |
|
145
|
|
|
$f_schema = $this->_schema; |
|
146
|
|
|
$this->fieldClean($f_schema); |
|
147
|
|
|
$this->fieldClean($tgname); |
|
148
|
|
|
$this->fieldClean($table); |
|
149
|
|
|
|
|
150
|
|
|
$sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" DISABLE TRIGGER \"{$tgname}\""; |
|
151
|
|
|
|
|
152
|
|
|
return $this->execute($sql); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// Rule functions |
|
156
|
|
|
|
|
157
|
|
|
// Operator Class functions |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Edits a rule on a table OR view. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $name The name of the new rule |
|
163
|
|
|
* @param string $event SELECT, INSERT, UPDATE or DELETE |
|
164
|
|
|
* @param string $table Table on which to create the rule |
|
165
|
|
|
* @param string $where Where to execute the rule, '' indicates always |
|
166
|
|
|
* @param bool $instead True if an INSTEAD rule, false otherwise |
|
167
|
|
|
* @param string $type NOTHING for a do nothing rule, SOMETHING to use given action |
|
168
|
|
|
* @param string $action The action to take |
|
169
|
|
|
* |
|
170
|
|
|
* @return int 0 if operation was successful |
|
171
|
|
|
*/ |
|
172
|
|
|
public function setRule($name, $event, $table, $where, $instead, $type, $action) |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->createRule($name, $event, $table, $where, $instead, $type, $action, true); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
// FTS functions |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Creates a rule. |
|
181
|
|
|
* |
|
182
|
|
|
* @param string $name The name of the new rule |
|
183
|
|
|
* @param string $event SELECT, INSERT, UPDATE or DELETE |
|
184
|
|
|
* @param string $table Table on which to create the rule |
|
185
|
|
|
* @param string $where When to execute the rule, '' indicates always |
|
186
|
|
|
* @param bool $instead True if an INSTEAD rule, false otherwise |
|
187
|
|
|
* @param string $type NOTHING for a do nothing rule, SOMETHING to use given action |
|
188
|
|
|
* @param string $action The action to take |
|
189
|
|
|
* @param bool $replace (optional) True to replace existing rule, false |
|
190
|
|
|
* otherwise |
|
191
|
|
|
* |
|
192
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
193
|
|
|
*/ |
|
194
|
|
|
public function createRule($name, $event, $table, $where, $instead, $type, $action, $replace = false) |
|
195
|
|
|
{ |
|
196
|
|
|
$f_schema = $this->_schema; |
|
197
|
|
|
$this->fieldClean($f_schema); |
|
198
|
|
|
$this->fieldClean($name); |
|
199
|
|
|
$this->fieldClean($table); |
|
200
|
|
|
|
|
201
|
|
|
if (!\in_array($event, $this->rule_events, true)) { |
|
202
|
|
|
return -1; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
$sql = 'CREATE'; |
|
206
|
|
|
|
|
207
|
|
|
if ($replace) { |
|
208
|
|
|
$sql .= ' OR REPLACE'; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
$sql .= " RULE \"{$name}\" AS ON {$event} TO \"{$f_schema}\".\"{$table}\""; |
|
212
|
|
|
// Can't escape WHERE clause |
|
213
|
|
|
if ('' !== $where) { |
|
214
|
|
|
$sql .= " WHERE {$where}"; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$sql .= ' DO'; |
|
218
|
|
|
|
|
219
|
|
|
if ($instead) { |
|
220
|
|
|
$sql .= ' INSTEAD'; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
if ('NOTHING' === $type) { |
|
224
|
|
|
$sql .= ' NOTHING'; |
|
225
|
|
|
} else { |
|
226
|
|
|
$sql .= " ({$action})"; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this->execute($sql); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Removes a rule from a table OR view. |
|
234
|
|
|
* |
|
235
|
|
|
* @param string $rule The rule to drop |
|
236
|
|
|
* @param string $relation The relation from which to drop |
|
237
|
|
|
* @param string $cascade True to cascade drop, false to restrict |
|
238
|
|
|
* |
|
239
|
|
|
* @return int|\PHPPgAdmin\ADORecordSet |
|
240
|
|
|
*/ |
|
241
|
|
|
public function dropRule($rule, $relation, $cascade) |
|
242
|
|
|
{ |
|
243
|
|
|
$f_schema = $this->_schema; |
|
244
|
|
|
$this->fieldClean($f_schema); |
|
245
|
|
|
$this->fieldClean($rule); |
|
246
|
|
|
$this->fieldClean($relation); |
|
247
|
|
|
|
|
248
|
|
|
$sql = "DROP RULE \"{$rule}\" ON \"{$f_schema}\".\"{$relation}\""; |
|
249
|
|
|
|
|
250
|
|
|
if ($cascade) { |
|
251
|
|
|
$sql .= ' CASCADE'; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
return $this->execute($sql); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
abstract public function fieldClean(&$str); |
|
258
|
|
|
|
|
259
|
|
|
abstract public function beginTransaction(); |
|
260
|
|
|
|
|
261
|
|
|
abstract public function rollbackTransaction(); |
|
262
|
|
|
|
|
263
|
|
|
abstract public function endTransaction(); |
|
264
|
|
|
|
|
265
|
|
|
abstract public function execute($sql); |
|
266
|
|
|
|
|
267
|
|
|
abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
|
268
|
|
|
|
|
269
|
|
|
abstract public function selectSet($sql); |
|
270
|
|
|
|
|
271
|
|
|
abstract public function clean(&$str); |
|
272
|
|
|
|
|
273
|
|
|
abstract public function phpBool($parameter); |
|
274
|
|
|
|
|
275
|
|
|
abstract public function hasCreateTableLikeWithConstraints(); |
|
276
|
|
|
|
|
277
|
|
|
abstract public function hasCreateTableLikeWithIndexes(); |
|
278
|
|
|
|
|
279
|
|
|
abstract public function hasTablespaces(); |
|
280
|
|
|
|
|
281
|
|
|
abstract public function delete($table, $conditions, $schema = ''); |
|
282
|
|
|
|
|
283
|
|
|
abstract public function fieldArrayClean(&$arr); |
|
284
|
|
|
|
|
285
|
|
|
abstract public function hasCreateFieldWithConstraints(); |
|
286
|
|
|
|
|
287
|
|
|
abstract public function getAttributeNames($table, $atts); |
|
288
|
|
|
} |
|
289
|
|
|
|