Passed
Push — main ( b2ddc1...a529c9 )
by Thierry
01:51
created

DatabaseTrait::getQueries()   C

Complexity

Conditions 12
Paths 56

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 21
c 1
b 0
f 0
nc 56
nop 1
dl 0
loc 31
rs 6.9666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\PgSql\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
6
use Lagdo\DbAdmin\Driver\Entity\RoutineEntity;
7
8
use Lagdo\DbAdmin\Driver\Db\Database as AbstractDatabase;
9
10
trait DatabaseTrait
11
{
12
    /**
13
     * Get queries to create or alter table.
14
     *
15
     * @param TableEntity $tableAttrs
16
     *
17
     * @return array
18
     */
19
    private function getQueries(TableEntity $tableAttrs)
20
    {
21
        $queries = [];
22
23
        foreach ($tableAttrs->edited as $field) {
24
            $column = $this->driver->escapeId($field[0]);
25
            $val = $field[1];
26
            $val5 = $val[5] ?? '';
27
            if ($val[0] !== '' && $column != $val[0]) {
28
                $queries[] = 'ALTER TABLE ' . $this->driver->table($tableAttrs->name) . " RENAME $column TO $val[0]";
29
            }
30
            if ($column !== '' || $val5 !== '') {
31
                $queries[] = 'COMMENT ON COLUMN ' . $this->driver->table($tableAttrs->name) .
32
                    ".$val[0] IS " . ($val5 != '' ? substr($val5, 9) : "''");
33
            }
34
        }
35
        foreach ($tableAttrs->fields as $field) {
36
            $column = $this->driver->escapeId($field[0]);
37
            $val = $field[1];
38
            $val5 = $val[5] ?? '';
39
            if ($column !== '' || $val5 !== '') {
40
                $queries[] = 'COMMENT ON COLUMN ' . $this->driver->table($tableAttrs->name) .
41
                    ".$val[0] IS " . ($val5 != '' ? substr($val5, 9) : "''");
42
            }
43
        }
44
        if ($tableAttrs->comment != '') {
45
            $queries[] = 'COMMENT ON TABLE ' . $this->driver->table($tableAttrs->name) .
46
                ' IS ' . $this->driver->quote($tableAttrs->comment);
47
        }
48
49
        return $queries;
50
    }
51
52
    /**
53
     * Get queries to create or alter table.
54
     *
55
     * @param TableEntity $tableAttrs
56
     *
57
     * @return array
58
     */
59
    private function getNewColumns(TableEntity $tableAttrs)
60
    {
61
        $columns = [];
62
63
        foreach ($tableAttrs->fields as $field) {
64
            $val = $field[1];
65
            if (isset($val[6])) { // auto increment
66
                $val[1] = ($val[1] == ' bigint' ? ' big' : ($val[1] == ' smallint' ? ' small' : ' ')) . 'serial';
67
            }
68
            $columns[] = implode($val);
69
            if (isset($val[6])) {
70
                $columns[] = " PRIMARY KEY ($val[0])";
71
            }
72
        }
73
74
        return $columns;
75
    }
76
77
    /**
78
     * Get queries to create or alter table.
79
     *
80
     * @param TableEntity $tableAttrs
81
     *
82
     * @return array
83
     */
84
    private function getColumnChanges(TableEntity $tableAttrs)
85
    {
86
        $columns = [];
87
88
        foreach ($tableAttrs->fields as $field) {
89
            $val = $field[1];
90
            if (isset($val[6])) { // auto increment
91
                $val[1] = ($val[1] == ' bigint' ? ' big' : ($val[1] == ' smallint' ? ' small' : ' ')) . 'serial';
92
            }
93
            $columns[] = 'ADD ' . implode($val);
94
            if (isset($val[6])) {
95
                $columns[] = "ADD PRIMARY KEY ($val[0])";
96
            }
97
        }
98
        foreach ($tableAttrs->edited as $field) {
99
            $column = $this->driver->escapeId($field[0]);
100
            $val = $field[1];
101
            $columns[] = "ALTER $column TYPE$val[1]";
102
            if (!$val[6]) {
103
                $columns[] = "ALTER $column " . ($val[3] ? "SET$val[3]" : 'DROP DEFAULT');
104
                $columns[] = "ALTER $column " . ($val[2] == ' NULL' ? 'DROP NOT' : 'SET') . $val[2];
105
            }
106
        }
107
        foreach ($tableAttrs->dropped as $column) {
108
            $columns[] = 'DROP ' . $this->driver->escapeId($column);
109
        }
110
111
        return $columns;
112
    }
113
114
    /**
115
     * @inheritDoc
116
     */
117
    public function moveTables(array $tables, array $views, string $target)
118
    {
119
        foreach (array_merge($tables, $views) as $table) {
120
            $status = $this->driver->tableStatus($table);
121
            if (!$this->driver->execute('ALTER ' . strtoupper($status->engine) . ' ' .
122
                $this->driver->table($table) . ' SET SCHEMA ' . $this->driver->escapeId($target))) {
123
                return false;
124
            }
125
        }
126
        return true;
127
    }
128
129
    /**
130
     * @inheritDoc
131
     */
132
    public function truncateTables(array $tables)
133
    {
134
        $this->driver->execute('TRUNCATE ' . implode(', ', array_map(function ($table) {
135
            return $this->driver->table($table);
136
        }, $tables)));
137
        return true;
138
    }
139
140
    /**
141
     * @inheritDoc
142
     */
143
    public function userTypes()
144
    {
145
        $query = 'SELECT typname FROM pg_type WHERE typnamespace = ' .
146
            '(SELECT oid FROM pg_namespace WHERE nspname = current_schema()) ' .
147
            "AND typtype IN ('b','d','e') AND typelem = 0";
148
        return $this->driver->values($query);
149
    }
150
}
151