Test Failed
Push — master ( 34dd7c...325a03 )
by Felipe
11:17 queued 06:14
created

SequenceTrait   B

Complexity

Total Complexity 53

Size/Duplication

Total Lines 550
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 53
dl 0
loc 550
rs 7.4757
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A restartSequence() 0 9 1
A dropSequence() 0 12 2
A resetSequence() 0 19 2
B _alterSequence() 0 66 6
A setvalSequence() 0 13 1
A getSequences() 0 21 2
C createSequence() 0 44 7
A alterSequenceSchema() 0 12 3
D alterSequenceProps() 0 50 16
A alterSequenceName() 0 16 4
A getSequence() 0 18 1
A alterSequenceOwner() 0 15 3
B alterSequence() 0 51 4
A nextvalSequence() 0 12 1

How to fix   Complexity   

Complex Class

Complex classes like SequenceTrait often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SequenceTrait, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.43
5
 */
6
7
namespace PHPPgAdmin\Traits;
8
9
/**
10
 * Common trait for sequence manipulation.
11
 */
12
trait SequenceTrait
13
{
14
    /**
15
     * Returns all sequences in the current database.
16
     *
17
     * @param bool $all true to get all sequences of all schemas
18
     *
19
     * @return \PHPPgAdmin\ADORecordSet A recordset
20
     */
21
    public function getSequences($all = false)
22
    {
23
        if ($all) {
24
            // Exclude pg_catalog and information_schema tables
25
            $sql = "SELECT n.nspname, c.relname AS seqname, u.usename AS seqowner
26
				FROM pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
27
				WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
28
				AND c.relkind = 'S'
29
				AND n.nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
30
				ORDER BY nspname, seqname";
31
        } else {
32
            $c_schema = $this->_schema;
33
            $this->clean($c_schema);
34
            $sql = "SELECT c.relname AS seqname, u.usename AS seqowner, pg_catalog.obj_description(c.oid, 'pg_class') AS seqcomment,
35
				(SELECT spcname FROM pg_catalog.pg_tablespace pt WHERE pt.oid=c.reltablespace) AS tablespace
36
				FROM pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
37
				WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
38
				AND c.relkind = 'S' AND n.nspname='{$c_schema}' ORDER BY seqname";
39
        }
40
41
        return $this->selectSet($sql);
42
    }
43
44
    /**
45
     * Execute nextval on a given sequence.
46
     *
47
     * @param string $sequence Sequence name
48
     *
49
     * @return int 0 if operation was successful
50
     */
51
    public function nextvalSequence($sequence)
52
    {
53
        /* This double-cleaning is deliberate */
54
        $f_schema = $this->_schema;
55
        $this->fieldClean($f_schema);
56
        $this->clean($f_schema);
57
        $this->fieldClean($sequence);
58
        $this->clean($sequence);
59
60
        $sql = "SELECT pg_catalog.NEXTVAL('\"{$f_schema}\".\"{$sequence}\"')";
61
62
        return $this->execute($sql);
63
    }
64
65
    /**
66
     * Execute setval on a given sequence.
67
     *
68
     * @param string $sequence  Sequence name
69
     * @param number $nextvalue The next value
70
     *
71
     * @return int 0 if operation was successful
72
     */
73
    public function setvalSequence($sequence, $nextvalue)
74
    {
75
        /* This double-cleaning is deliberate */
76
        $f_schema = $this->_schema;
77
        $this->fieldClean($f_schema);
78
        $this->clean($f_schema);
79
        $this->fieldClean($sequence);
80
        $this->clean($sequence);
81
        $this->clean($nextvalue);
82
83
        $sql = "SELECT pg_catalog.SETVAL('\"{$f_schema}\".\"{$sequence}\"', '{$nextvalue}')";
84
85
        return $this->execute($sql);
86
    }
87
88
    /**
89
     * Restart a given sequence to its start value.
90
     *
91
     * @param string $sequence Sequence name
92
     *
93
     * @return int 0 if operation was successful
94
     */
95
    public function restartSequence($sequence)
96
    {
97
        $f_schema = $this->_schema;
98
        $this->fieldClean($f_schema);
99
        $this->fieldClean($sequence);
100
101
        $sql = "ALTER SEQUENCE \"{$f_schema}\".\"{$sequence}\" RESTART;";
102
103
        return $this->execute($sql);
104
    }
105
106
    /**
107
     * Resets a given sequence to min value of sequence.
108
     *
109
     * @param string $sequence Sequence name
110
     *
111
     * @return int 0 if operation was successful
112
     */
113
    public function resetSequence($sequence)
114
    {
115
        // Get the minimum value of the sequence
116
        $seq = $this->getSequence($sequence);
117
        if ($seq->recordCount() != 1) {
0 ignored issues
show
introduced by
The condition $seq->recordCount() != 1 is always true.
Loading history...
118
            return -1;
119
        }
120
121
        $minvalue = $seq->fields['min_value'];
122
123
        $f_schema = $this->_schema;
124
        $this->fieldClean($f_schema);
125
        /* This double-cleaning is deliberate */
126
        $this->fieldClean($sequence);
127
        $this->clean($sequence);
128
129
        $sql = "SELECT pg_catalog.SETVAL('\"{$f_schema}\".\"{$sequence}\"', {$minvalue})";
130
131
        return $this->execute($sql);
132
    }
133
134
    /**
135
     * Returns properties of a single sequence.
136
     *
137
     * @param string $sequence Sequence name
138
     *
139
     * @return \PHPPgAdmin\ADORecordSet A recordset
140
     */
141
    public function getSequence($sequence)
142
    {
143
        $c_schema = $this->_schema;
144
        $this->clean($c_schema);
145
        $c_sequence = $sequence;
146
        $this->fieldClean($sequence);
147
        $this->clean($c_sequence);
148
149
        $sql = "
150
			SELECT c.relname AS seqname, s.*,
151
				pg_catalog.obj_description(s.tableoid, 'pg_class') AS seqcomment,
152
				u.usename AS seqowner, n.nspname
153
			FROM \"{$sequence}\" AS s, pg_catalog.pg_class c, pg_catalog.pg_user u, pg_catalog.pg_namespace n
154
			WHERE c.relowner=u.usesysid AND c.relnamespace=n.oid
155
				AND c.relname = '{$c_sequence}' AND c.relkind = 'S' AND n.nspname='{$c_schema}'
156
				AND n.oid = c.relnamespace";
157
158
        return $this->selectSet($sql);
159
    }
160
161
    /**
162
     * Creates a new sequence.
163
     *
164
     * @param string $sequence    Sequence name
165
     * @param number $increment   The increment
166
     * @param number $minvalue    The min value
167
     * @param number $maxvalue    The max value
168
     * @param number $startvalue  The starting value
169
     * @param number $cachevalue  The cache value
170
     * @param bool   $cycledvalue True if cycled, false otherwise
171
     *
172
     * @return int 0 if operation was successful
173
     */
174
    public function createSequence(
175
        $sequence,
176
        $increment,
177
        $minvalue,
178
        $maxvalue,
179
        $startvalue,
180
        $cachevalue,
181
        $cycledvalue
182
    ) {
183
        $f_schema = $this->_schema;
184
        $this->fieldClean($f_schema);
185
        $this->fieldClean($sequence);
186
        $this->clean($increment);
187
        $this->clean($minvalue);
188
        $this->clean($maxvalue);
189
        $this->clean($startvalue);
190
        $this->clean($cachevalue);
191
192
        $sql = "CREATE SEQUENCE \"{$f_schema}\".\"{$sequence}\"";
193
        if ($increment != '') {
194
            $sql .= " INCREMENT {$increment}";
195
        }
196
197
        if ($minvalue != '') {
198
            $sql .= " MINVALUE {$minvalue}";
199
        }
200
201
        if ($maxvalue != '') {
202
            $sql .= " MAXVALUE {$maxvalue}";
203
        }
204
205
        if ($startvalue != '') {
206
            $sql .= " START {$startvalue}";
207
        }
208
209
        if ($cachevalue != '') {
210
            $sql .= " CACHE {$cachevalue}";
211
        }
212
213
        if ($cycledvalue) {
214
            $sql .= ' CYCLE';
215
        }
216
217
        return $this->execute($sql);
218
    }
219
220
    /**
221
     * Alters a sequence.
222
     *
223
     * @param string    $sequence     The name of the sequence
224
     * @param string    $name         The new name for the sequence
225
     * @param string    $comment      The comment on the sequence
226
     * @param string    $owner        The new owner for the sequence
227
     * @param string    $schema       The new schema for the sequence
228
     * @param string    $increment    The increment
229
     * @param number    $minvalue     The min value
230
     * @param number    $maxvalue     The max value
231
     * @param number    $restartvalue The starting value
232
     * @param number    $cachevalue   The cache value
233
     * @param null|bool $cycledvalue  True if cycled, false otherwise
234
     * @param number    $startvalue   The sequence start value when issueing a restart
235
     *
236
     * @return bool|int 0 success
237
     */
238
    public function alterSequence(
239
        $sequence,
240
        $name,
241
        $comment,
242
        $owner = null,
243
        $schema = null,
244
        $increment = null,
245
        $minvalue = null,
246
        $maxvalue = null,
247
        $restartvalue = null,
248
        $cachevalue = null,
249
        $cycledvalue = null,
250
        $startvalue = null
251
    ) {
252
        $this->fieldClean($sequence);
253
254
        $data = $this->getSequence($sequence);
255
256
        if ($data->recordCount() != 1) {
0 ignored issues
show
introduced by
The condition $data->recordCount() != 1 is always true.
Loading history...
257
            return -2;
258
        }
259
260
        $status = $this->beginTransaction();
261
        if ($status != 0) {
262
            $this->rollbackTransaction();
263
264
            return -1;
265
        }
266
267
        $status = $this->_alterSequence(
268
            $data,
269
            $name,
270
            $comment,
271
            $owner,
272
            $schema,
273
            $increment,
274
            $minvalue,
275
            $maxvalue,
276
            $restartvalue,
277
            $cachevalue,
278
            $cycledvalue,
279
            $startvalue
280
        );
281
282
        if ($status != 0) {
283
            $this->rollbackTransaction();
284
285
            return $status;
286
        }
287
288
        return $this->endTransaction();
289
    }
290
291
    /**
292
     * Protected method which alter a sequence
293
     * SHOULDN'T BE CALLED OUTSIDE OF A TRANSACTION.
294
     *
295
     * @param \PHPPgAdmin\ADORecordSet $seqrs        The sequence recordSet returned by getSequence()
296
     * @param string                   $name         The new name for the sequence
297
     * @param string                   $comment      The comment on the sequence
298
     * @param string                   $owner        The new owner for the sequence
299
     * @param string                   $schema       The new schema for the sequence
300
     * @param string                   $increment    The increment
301
     * @param string                   $minvalue     The min value
302
     * @param string                   $maxvalue     The max value
303
     * @param string                   $restartvalue The starting value
304
     * @param string                   $cachevalue   The cache value
305
     * @param null|bool                $cycledvalue  True if cycled, false otherwise
306
     * @param string                   $startvalue   The sequence start value when issueing a restart
307
     *
308
     * @return int 0 success
309
     */
310
    protected function _alterSequence(
1 ignored issue
show
Coding Style introduced by
Protected method name "SequenceTrait::_alterSequence" must not be prefixed with an underscore
Loading history...
311
        $seqrs,
312
        $name,
313
        $comment,
314
        $owner,
315
        $schema,
316
        $increment,
317
        $minvalue,
318
        $maxvalue,
319
        $restartvalue,
320
        $cachevalue,
321
        $cycledvalue,
322
        $startvalue
323
    ) {
324
        $this->fieldArrayClean($seqrs->fields);
325
326
        // Comment
327
        $status = $this->setComment('SEQUENCE', $seqrs->fields['seqname'], '', $comment);
328
        if ($status != 0) {
329
            return -4;
330
        }
331
332
        // Owner
333
        $this->fieldClean($owner);
334
        $status = $this->alterSequenceOwner($seqrs, $owner);
335
        if ($status != 0) {
336
            return -5;
337
        }
338
339
        // Props
340
        $this->clean($increment);
341
        $this->clean($minvalue);
342
        $this->clean($maxvalue);
343
        $this->clean($restartvalue);
344
        $this->clean($cachevalue);
345
        $this->clean($cycledvalue);
346
        $this->clean($startvalue);
347
        $status = $this->alterSequenceProps(
348
            $seqrs,
349
            $increment,
350
            $minvalue,
351
            $maxvalue,
352
            $restartvalue,
353
            $cachevalue,
354
            $cycledvalue,
355
            $startvalue
356
        );
357
        if ($status != 0) {
358
            return -6;
359
        }
360
361
        // Rename
362
        $this->fieldClean($name);
363
        $status = $this->alterSequenceName($seqrs, $name);
364
        if ($status != 0) {
365
            return -3;
366
        }
367
368
        // Schema
369
        $this->clean($schema);
370
        $status = $this->alterSequenceSchema($seqrs, $schema);
371
        if ($status != 0) {
372
            return -7;
373
        }
374
375
        return 0;
376
    }
377
378
    // Index functions
379
380
    /**
381
     * Alter a sequence's owner.
382
     *
383
     * @param \PHPPgAdmin\ADORecordSet $seqrs The sequence RecordSet returned by getSequence()
384
     * @param string                   $owner the new owner of the sequence
385
     *
386
     * @return int 0 if operation was successful
387
     *
388
     * @internal string $name new owner for the sequence
389
     */
390
    public function alterSequenceOwner($seqrs, $owner)
391
    {
392
        // If owner has been changed, then do the alteration.  We are
393
        // careful to avoid this generally as changing owner is a
394
        // superuser only function.
395
        /* vars are cleaned in _alterSequence */
396
        if (!empty($owner) && ($seqrs->fields['seqowner'] != $owner)) {
397
            $f_schema = $this->_schema;
398
            $this->fieldClean($f_schema);
399
            $sql = "ALTER SEQUENCE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" OWNER TO \"{$owner}\"";
400
401
            return $this->execute($sql);
402
        }
403
404
        return 0;
405
    }
406
407
    /**
408
     * Alter a sequence's properties.
409
     *
410
     * @param \PHPPgAdmin\ADORecordSet $seqrs        The sequence RecordSet returned by getSequence()
411
     * @param number                   $increment    The sequence incremental value
412
     * @param number                   $minvalue     The sequence minimum value
413
     * @param number                   $maxvalue     The sequence maximum value
414
     * @param number                   $restartvalue The sequence current value
415
     * @param number                   $cachevalue   The sequence cache value
416
     * @param null|bool                $cycledvalue  Sequence can cycle ?
417
     * @param number                   $startvalue   The sequence start value when issueing a restart
418
     *
419
     * @return int 0 if operation was successful
420
     */
421
    public function alterSequenceProps(
422
        $seqrs,
423
        $increment,
424
        $minvalue,
425
        $maxvalue,
426
        $restartvalue,
427
        $cachevalue,
428
        $cycledvalue,
429
        $startvalue
430
    ) {
431
        $sql = '';
432
        /* vars are cleaned in _alterSequence */
433
        if (!empty($increment) && ($increment != $seqrs->fields['increment_by'])) {
434
            $sql .= " INCREMENT {$increment}";
435
        }
436
437
        if (!empty($minvalue) && ($minvalue != $seqrs->fields['min_value'])) {
438
            $sql .= " MINVALUE {$minvalue}";
439
        }
440
441
        if (!empty($maxvalue) && ($maxvalue != $seqrs->fields['max_value'])) {
442
            $sql .= " MAXVALUE {$maxvalue}";
443
        }
444
445
        if (!empty($restartvalue) && ($restartvalue != $seqrs->fields['last_value'])) {
446
            $sql .= " RESTART {$restartvalue}";
447
        }
448
449
        if (!empty($cachevalue) && ($cachevalue != $seqrs->fields['cache_value'])) {
450
            $sql .= " CACHE {$cachevalue}";
451
        }
452
453
        if (!empty($startvalue) && ($startvalue != $seqrs->fields['start_value'])) {
454
            $sql .= " START {$startvalue}";
455
        }
456
457
        // toggle cycle yes/no
458
        if (!is_null($cycledvalue)) {
459
            $sql .= (!$cycledvalue ? ' NO ' : '') . ' CYCLE';
460
        }
461
462
        if ($sql != '') {
463
            $f_schema = $this->_schema;
464
            $this->fieldClean($f_schema);
465
            $sql = "ALTER SEQUENCE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" {$sql}";
466
467
            return $this->execute($sql);
468
        }
469
470
        return 0;
471
    }
472
473
    /**
474
     * Rename a sequence.
475
     *
476
     * @param \PHPPgAdmin\ADORecordSet $seqrs The sequence RecordSet returned by getSequence()
477
     * @param string                   $name  The new name for the sequence
478
     *
479
     * @return int 0 if operation was successful
480
     */
481
    public function alterSequenceName($seqrs, $name)
482
    {
483
        /* vars are cleaned in _alterSequence */
484
        if (!empty($name) && ($seqrs->fields['seqname'] != $name)) {
485
            $f_schema = $this->_schema;
486
            $this->fieldClean($f_schema);
487
            $sql    = "ALTER SEQUENCE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" RENAME TO \"{$name}\"";
488
            $status = $this->execute($sql);
489
            if ($status == 0) {
490
                $seqrs->fields['seqname'] = $name;
491
            } else {
492
                return $status;
493
            }
494
        }
495
496
        return 0;
497
    }
498
499
    /**
500
     * Alter a sequence's schema.
501
     *
502
     * @param \PHPPgAdmin\ADORecordSet $seqrs  The sequence RecordSet returned by getSequence()
503
     * @param string                   $schema
504
     *
505
     * @return int 0 if operation was successful
506
     *
507
     * @internal param The $name new schema for the sequence
508
     */
509
    public function alterSequenceSchema($seqrs, $schema)
510
    {
511
        /* vars are cleaned in _alterSequence */
512
        if (!empty($schema) && ($seqrs->fields['nspname'] != $schema)) {
513
            $f_schema = $this->_schema;
514
            $this->fieldClean($f_schema);
515
            $sql = "ALTER SEQUENCE \"{$f_schema}\".\"{$seqrs->fields['seqname']}\" SET SCHEMA {$schema}";
516
517
            return $this->execute($sql);
518
        }
519
520
        return 0;
521
    }
522
523
    /**
524
     * Drops a given sequence.
525
     *
526
     * @param $sequence Sequence name
527
     * @param $cascade  True to cascade drop, false to restrict
528
     *
529
     * @return int 0 if operation was successful
530
     */
531
    public function dropSequence($sequence, $cascade)
532
    {
533
        $f_schema = $this->_schema;
534
        $this->fieldClean($f_schema);
535
        $this->fieldClean($sequence);
536
537
        $sql = "DROP SEQUENCE \"{$f_schema}\".\"{$sequence}\"";
538
        if ($cascade) {
539
            $sql .= ' CASCADE';
540
        }
541
542
        return $this->execute($sql);
543
    }
544
545
    abstract public function fieldClean(&$str);
546
547
    abstract public function beginTransaction();
548
549
    abstract public function rollbackTransaction();
550
551
    abstract public function endTransaction();
552
553
    abstract public function execute($sql);
554
555
    abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null);
556
557
    abstract public function selectSet($sql);
558
559
    abstract public function clean(&$str);
560
561
    abstract public function fieldArrayClean(&$arr);
562
}
563