Passed
Push — develop ( 2d5edd...a33225 )
by Felipe
05:40
created

TriggerTrait::createRule()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 33
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 33
rs 8.439
c 0
b 0
f 0
cc 6
eloc 20
nc 17
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.47
5
 */
6
7
namespace PHPPgAdmin\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 \PHPPgAdmin\ADORecordSet A recordset
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 0 if operation was successful
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 0 if operation was successful
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 0 if operation was successful
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
        if ($cascade) {
108
            $sql .= ' CASCADE';
109
        }
110
111
        return $this->execute($sql);
112
    }
113
114
    /**
115
     * Enables a trigger.
116
     *
117
     * @param string $tgname The name of the trigger to enable
118
     * @param string $table  The table in which to enable the trigger
119
     *
120
     * @return int 0 if operation was successful
121
     */
122
    public function enableTrigger($tgname, $table)
123
    {
124
        $f_schema = $this->_schema;
125
        $this->fieldClean($f_schema);
126
        $this->fieldClean($tgname);
127
        $this->fieldClean($table);
128
129
        $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" ENABLE TRIGGER \"{$tgname}\"";
130
131
        return $this->execute($sql);
132
    }
133
134
    /**
135
     * Disables a trigger.
136
     *
137
     * @param string $tgname The name of the trigger to disable
138
     * @param string $table  The table in which to disable the trigger
139
     *
140
     * @return int 0 if operation was successful
141
     */
142
    public function disableTrigger($tgname, $table)
143
    {
144
        $f_schema = $this->_schema;
145
        $this->fieldClean($f_schema);
146
        $this->fieldClean($tgname);
147
        $this->fieldClean($table);
148
149
        $sql = "ALTER TABLE \"{$f_schema}\".\"{$table}\" DISABLE TRIGGER \"{$tgname}\"";
150
151
        return $this->execute($sql);
152
    }
153
154
    // Rule functions
155
156
    // Operator Class functions
157
158
    /**
159
     * Edits a rule on a table OR view.
160
     *
161
     * @param string $name    The name of the new rule
162
     * @param string $event   SELECT, INSERT, UPDATE or DELETE
163
     * @param string $table   Table on which to create the rule
164
     * @param string $where   Where to execute the rule, '' indicates always
165
     * @param bool   $instead True if an INSTEAD rule, false otherwise
166
     * @param string $type    NOTHING for a do nothing rule, SOMETHING to use given action
167
     * @param string $action  The action to take
168
     *
169
     * @return int 0 if operation was successful
170
     */
171
    public function setRule($name, $event, $table, $where, $instead, $type, $action)
172
    {
173
        return $this->createRule($name, $event, $table, $where, $instead, $type, $action, true);
174
    }
175
176
    // FTS functions
177
178
    /**
179
     * Creates a rule.
180
     *
181
     * @param string $name    The name of the new rule
182
     * @param string $event   SELECT, INSERT, UPDATE or DELETE
183
     * @param string $table   Table on which to create the rule
184
     * @param string $where   When to execute the rule, '' indicates always
185
     * @param bool   $instead True if an INSTEAD rule, false otherwise
186
     * @param string $type    NOTHING for a do nothing rule, SOMETHING to use given action
187
     * @param string $action  The action to take
188
     * @param bool   $replace (optional) True to replace existing rule, false
189
     *                        otherwise
190
     *
191
     * @return int 0 if operation was successful
192
     */
193
    public function createRule($name, $event, $table, $where, $instead, $type, $action, $replace = false)
194
    {
195
        $f_schema = $this->_schema;
196
        $this->fieldClean($f_schema);
197
        $this->fieldClean($name);
198
        $this->fieldClean($table);
199
        if (!in_array($event, $this->rule_events, true)) {
200
            return -1;
201
        }
202
203
        $sql = 'CREATE';
204
        if ($replace) {
205
            $sql .= ' OR REPLACE';
206
        }
207
208
        $sql .= " RULE \"{$name}\" AS ON {$event} TO \"{$f_schema}\".\"{$table}\"";
209
        // Can't escape WHERE clause
210
        if ($where != '') {
211
            $sql .= " WHERE {$where}";
212
        }
213
214
        $sql .= ' DO';
215
        if ($instead) {
216
            $sql .= ' INSTEAD';
217
        }
218
219
        if ($type == 'NOTHING') {
220
            $sql .= ' NOTHING';
221
        } else {
222
            $sql .= " ({$action})";
223
        }
224
225
        return $this->execute($sql);
226
    }
227
228
    /**
229
     * Removes a rule from a table OR view.
230
     *
231
     * @param string $rule     The rule to drop
232
     * @param string $relation The relation from which to drop
233
     * @param string $cascade  True to cascade drop, false to restrict
234
     *
235
     * @return int 0 if operation was successful
236
     */
237
    public function dropRule($rule, $relation, $cascade)
238
    {
239
        $f_schema = $this->_schema;
240
        $this->fieldClean($f_schema);
241
        $this->fieldClean($rule);
242
        $this->fieldClean($relation);
243
244
        $sql = "DROP RULE \"{$rule}\" ON \"{$f_schema}\".\"{$relation}\"";
245
        if ($cascade) {
246
            $sql .= ' CASCADE';
247
        }
248
249
        return $this->execute($sql);
250
    }
251
252
    abstract public function fieldClean(&$str);
253
254
    abstract public function beginTransaction();
255
256
    abstract public function rollbackTransaction();
257
258
    abstract public function endTransaction();
259
260
    abstract public function execute($sql);
261
262
    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
263
264
    abstract public function selectSet($sql);
265
266
    abstract public function clean(&$str);
267
268
    abstract public function phpBool($parameter);
269
270
    abstract public function hasCreateTableLikeWithConstraints();
271
272
    abstract public function hasCreateTableLikeWithIndexes();
273
274
    abstract public function hasTablespaces();
275
276
    abstract public function delete($table, $conditions, $schema = '');
277
278
    abstract public function fieldArrayClean(&$arr);
279
280
    abstract public function hasCreateFieldWithConstraints();
281
282
    abstract public function getAttributeNames($table, $atts);
283
}
284