DatabaseTrait   A
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 66
c 1
b 0
f 0
dl 0
loc 183
rs 9.36
wmc 38

10 Methods

Rating   Name   Duplication   Size   Complexity  
A truncateTables() 0 6 1
A moveTables() 0 10 3
A userTypes() 0 6 1
B _getRenameColumnQueries() 0 12 7
A getColumnChanges() 0 11 2
A _getCreateColumnQueries() 0 10 6
A _getAlterColumnQueries() 0 9 5
A getNewColumns() 0 16 6
A getQueries() 0 12 2
A _getColumnCommentQueries() 0 9 5
1
<?php
2
3
namespace Lagdo\DbAdmin\Driver\PgSql\Db;
4
5
use Lagdo\DbAdmin\Driver\Entity\TableEntity;
6
7
use function substr;
8
use function implode;
9
use function array_merge;
10
use function strtoupper;
11
use function array_map;
12
13
trait DatabaseTrait
14
{
15
    /**
16
     * @param TableEntity $tableAttrs
17
     * @param array $queries
18
     *
19
     * @return void
20
     */
21
    private function _getRenameColumnQueries(TableEntity $tableAttrs, array &$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->escapeTableName($tableAttrs->name) . " RENAME $column TO $val[0]";
29
            }
30
            if ($column !== '' || $val5 !== '') {
31
                $queries[] = 'COMMENT ON COLUMN ' . $this->driver->escapeTableName($tableAttrs->name) .
32
                    ".$val[0] IS " . ($val5 !== '' ? substr($val5, 9) : "''");
33
            }
34
        }
35
    }
36
37
    /**
38
     * @param TableEntity $tableAttrs
39
     * @param array $queries
40
     *
41
     * @return void
42
     */
43
    private function _getColumnCommentQueries(TableEntity $tableAttrs, array &$queries)
44
    {
45
        foreach ($tableAttrs->fields as $field) {
46
            $column = $this->driver->escapeId($field[0]);
47
            $val = $field[1];
48
            $val5 = $val[5] ?? '';
49
            if ($column !== '' || $val5 !== '') {
50
                $queries[] = 'COMMENT ON COLUMN ' . $this->driver->escapeTableName($tableAttrs->name) .
51
                    ".$val[0] IS " . ($val5 !== '' ? substr($val5, 9) : "''");
52
            }
53
        }
54
    }
55
56
    /**
57
     * Get queries to create or alter table.
58
     *
59
     * @param TableEntity $tableAttrs
60
     *
61
     * @return array
62
     */
63
    private function getQueries(TableEntity $tableAttrs): array
64
    {
65
        $queries = [];
66
67
        $this->_getRenameColumnQueries($tableAttrs, $queries);
68
        $this->_getColumnCommentQueries($tableAttrs, $queries);
69
        if ($tableAttrs->comment !== '') {
70
            $queries[] = 'COMMENT ON TABLE ' . $this->driver->escapeTableName($tableAttrs->name) .
71
                ' IS ' . $this->driver->quote($tableAttrs->comment);
72
        }
73
74
        return $queries;
75
    }
76
77
    /**
78
     * Get queries to create or alter table.
79
     *
80
     * @param TableEntity $tableAttrs
81
     *
82
     * @return array
83
     */
84
    private function getNewColumns(TableEntity $tableAttrs): array
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[] = implode($val);
94
            if (isset($val[6])) {
95
                $columns[] = " PRIMARY KEY ($val[0])";
96
            }
97
        }
98
99
        return $columns;
100
    }
101
102
    /**
103
     * @param TableEntity $tableAttrs
104
     * @param array $columns
105
     *
106
     * @return void
107
     */
108
    private function _getCreateColumnQueries(TableEntity $tableAttrs, array &$columns)
109
    {
110
        foreach ($tableAttrs->fields as $field) {
111
            $val = $field[1];
112
            if (isset($val[6])) { // auto increment
113
                $val[1] = ($val[1] === ' bigint' ? ' big' : ($val[1] === ' smallint' ? ' small' : ' ')) . 'serial';
114
            }
115
            $columns[] = 'ADD ' . implode($val);
116
            if (isset($val[6])) {
117
                $columns[] = "ADD PRIMARY KEY ($val[0])";
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param TableEntity $tableAttrs
124
     * @param array $columns
125
     *
126
     * @return void
127
     */
128
    private function _getAlterColumnQueries(TableEntity $tableAttrs, array &$columns)
129
    {
130
        foreach ($tableAttrs->edited as $field) {
131
            $column = $this->driver->escapeId($field[0]);
132
            $val = $field[1];
133
            $columns[] = "ALTER $column TYPE$val[1]";
134
            if (!$val[6]) {
135
                $columns[] = "ALTER $column " . ($val[3] ? "SET$val[3]" : 'DROP DEFAULT');
136
                $columns[] = "ALTER $column " . ($val[2] === ' NULL' ? 'DROP NOT' : 'SET') . $val[2];
137
            }
138
        }
139
    }
140
141
    /**
142
     * Get queries to create or alter table.
143
     *
144
     * @param TableEntity $tableAttrs
145
     *
146
     * @return array
147
     */
148
    private function getColumnChanges(TableEntity $tableAttrs): array
149
    {
150
        $columns = [];
151
152
        $this->_getCreateColumnQueries($tableAttrs, $columns);
153
        $this->_getAlterColumnQueries($tableAttrs, $columns);
154
        foreach ($tableAttrs->dropped as $column) {
155
            $columns[] = 'DROP ' . $this->driver->escapeId($column);
156
        }
157
158
        return $columns;
159
    }
160
161
    /**
162
     * @inheritDoc
163
     */
164
    public function moveTables(array $tables, array $views, string $target)
165
    {
166
        foreach (array_merge($tables, $views) as $table) {
167
            $status = $this->driver->tableStatus($table);
168
            if (!$this->driver->execute('ALTER ' . strtoupper($status->engine) . ' ' .
169
                $this->driver->escapeTableName($table) . ' SET SCHEMA ' . $this->driver->escapeId($target))) {
170
                return false;
171
            }
172
        }
173
        return true;
174
    }
175
176
    /**
177
     * @inheritDoc
178
     */
179
    public function truncateTables(array $tables)
180
    {
181
        $this->driver->execute('TRUNCATE ' . implode(', ', array_map(function ($table) {
182
            return $this->driver->escapeTableName($table);
183
        }, $tables)));
184
        return true;
185
    }
186
187
    /**
188
     * @inheritDoc
189
     */
190
    public function userTypes()
191
    {
192
        $query = 'SELECT typname FROM pg_type WHERE typnamespace = ' .
193
            '(SELECT oid FROM pg_namespace WHERE nspname = current_schema()) ' .
194
            "AND typtype IN ('b','d','e') AND typelem = 0";
195
        return $this->driver->values($query);
196
    }
197
}
198