Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TriggerTrait.php « databasetraits « database « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2489811f7c1a99f9d9f95d36c9194e0ddf562b6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php

/**
 * PHPPgAdmin 6.1.2
 */

namespace PHPPgAdmin\Database\Traits;

/**
 * Common trait for trigger and rules manipulation.
 */
trait TriggerTrait
{
    /**
     * Grabs a single trigger.
     *
     * @param string $table   The name of a table whose triggers to retrieve
     * @param string $trigger The name of the trigger to retrieve
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function getTrigger($table, $trigger)
    {
        $c_schema = $this->_schema;
        $this->clean($c_schema);
        $this->clean($table);
        $this->clean($trigger);

        $sql = "
            SELECT * FROM pg_catalog.pg_trigger t, pg_catalog.pg_class c
            WHERE t.tgrelid=c.oid AND c.relname='{$table}' AND t.tgname='{$trigger}'
                AND c.relnamespace=(
                    SELECT oid FROM pg_catalog.pg_namespace
                    WHERE nspname='{$c_schema}')";

        return $this->selectSet($sql);
    }

    /**
     * Creates a trigger.
     *
     * @param string $tgname      The name of the trigger to create
     * @param string $table       The name of the table
     * @param string $tgproc      The function to execute
     * @param string $tgtime      BEFORE or AFTER
     * @param string $tgevent     Event
     * @param string $tgfrequency
     * @param string $tgargs      The function arguments
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function createTrigger($tgname, $table, $tgproc, $tgtime, $tgevent, $tgfrequency, $tgargs)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($tgname);
        $this->fieldClean($table);
        $this->fieldClean($tgproc);

        /* No Statement Level Triggers in PostgreSQL (by now) */
        $sql = "CREATE TRIGGER \"{$tgname}\" {$tgtime}
                {$tgevent} ON \"{$f_schema}\".\"{$table}\"
                FOR EACH {$tgfrequency} EXECUTE PROCEDURE \"{$tgproc}\"({$tgargs})";

        return $this->execute($sql);
    }

    /**
     * Alters a trigger.
     *
     * @param string $table   The name of the table containing the trigger
     * @param string $trigger The name of the trigger to alter
     * @param string $name    The new name for the trigger
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function alterTrigger($table, $trigger, $name)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($table);
        $this->fieldClean($trigger);
        $this->fieldClean($name);

        $sql = "ALTER TRIGGER \"{$trigger}\" ON \"{$f_schema}\".\"{$table}\" RENAME TO \"{$name}\"";

        return $this->execute($sql);
    }

    /**
     * Drops a trigger.
     *
     * @param string $tgname  The name of the trigger to drop
     * @param string $table   The table from which to drop the trigger
     * @param bool   $cascade True to cascade drop, false to restrict
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function dropTrigger($tgname, $table, $cascade)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($tgname);
        $this->fieldClean($table);

        $sql = "DROP TRIGGER \"{$tgname}\" ON \"{$f_schema}\".\"{$table}\"";

        if ($cascade) {
            $sql .= ' CASCADE';
        }

        return $this->execute($sql);
    }

    /**
     * Enables a trigger.
     *
     * @param string $tgname The name of the trigger to enable
     * @param string $table  The table in which to enable the trigger
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function enableTrigger($tgname, $table)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($tgname);
        $this->fieldClean($table);

        $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ENABLE TRIGGER \"{$tgname}\"";

        return $this->execute($sql);
    }

    /**
     * Disables a trigger.
     *
     * @param string $tgname The name of the trigger to disable
     * @param string $table  The table in which to disable the trigger
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function disableTrigger($tgname, $table)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($tgname);
        $this->fieldClean($table);

        $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" DISABLE TRIGGER \"{$tgname}\"";

        return $this->execute($sql);
    }

    // Rule functions

    // Operator Class functions

    /**
     * Edits a rule on a table OR view.
     *
     * @param string $name    The name of the new rule
     * @param string $event   SELECT, INSERT, UPDATE or DELETE
     * @param string $table   Table on which to create the rule
     * @param string $where   Where to execute the rule, '' indicates always
     * @param bool   $instead True if an INSTEAD rule, false otherwise
     * @param string $type    NOTHING for a do nothing rule, SOMETHING to use given action
     * @param string $action  The action to take
     *
     * @return int 0 if operation was successful
     */
    public function setRule($name, $event, $table, $where, $instead, $type, $action)
    {
        return $this->createRule($name, $event, $table, $where, $instead, $type, $action, true);
    }

    // FTS functions

    /**
     * Creates a rule.
     *
     * @param string $name    The name of the new rule
     * @param string $event   SELECT, INSERT, UPDATE or DELETE
     * @param string $table   Table on which to create the rule
     * @param string $where   When to execute the rule, '' indicates always
     * @param bool   $instead True if an INSTEAD rule, false otherwise
     * @param string $type    NOTHING for a do nothing rule, SOMETHING to use given action
     * @param string $action  The action to take
     * @param bool   $replace (optional) True to replace existing rule, false
     *                        otherwise
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function createRule($name, $event, $table, $where, $instead, $type, $action, $replace = false)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($name);
        $this->fieldClean($table);

        if (!\in_array($event, $this->rule_events, true)) {
            return -1;
        }

        $sql = 'CREATE';

        if ($replace) {
            $sql .= ' OR REPLACE';
        }

        $sql .= " RULE \"{$name}\" AS ON {$event} TO \"{$f_schema}\".\"{$table}\"";
        // Can't escape WHERE clause
        if ('' !== $where) {
            $sql .= " WHERE {$where}";
        }

        $sql .= ' DO';

        if ($instead) {
            $sql .= ' INSTEAD';
        }

        if ('NOTHING' === $type) {
            $sql .= ' NOTHING';
        } else {
            $sql .= " ({$action})";
        }

        return $this->execute($sql);
    }

    /**
     * Removes a rule from a table OR view.
     *
     * @param string $rule     The rule to drop
     * @param string $relation The relation from which to drop
     * @param string $cascade  True to cascade drop, false to restrict
     *
     * @return int|\PHPPgAdmin\ADORecordSet
     */
    public function dropRule($rule, $relation, $cascade)
    {
        $f_schema = $this->_schema;
        $this->fieldClean($f_schema);
        $this->fieldClean($rule);
        $this->fieldClean($relation);

        $sql = "DROP RULE \"{$rule}\" ON \"{$f_schema}\".\"{$relation}\"";

        if ($cascade) {
            $sql .= ' CASCADE';
        }

        return $this->execute($sql);
    }

    abstract public function fieldClean(&$str);

    abstract public function beginTransaction();

    abstract public function rollbackTransaction();

    abstract public function endTransaction();

    abstract public function execute($sql);

    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);

    abstract public function selectSet($sql);

    abstract public function clean(&$str);

    abstract public function phpBool($parameter);

    abstract public function hasCreateTableLikeWithConstraints();

    abstract public function hasCreateTableLikeWithIndexes();

    abstract public function hasTablespaces();

    abstract public function delete($table, $conditions, $schema = '');

    abstract public function fieldArrayClean(&$arr);

    abstract public function hasCreateFieldWithConstraints();

    abstract public function getAttributeNames($table, $atts);
}