Passed
Push — develop ( e333c2...b3554d )
by nguereza
02:25
created

PostgreSQL   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 226
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 82
dl 0
loc 226
rs 10
c 1
b 0
f 0
wmc 29

17 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumns() 0 19 1
A getTypeDouble() 0 3 1
A getDatabaseName() 0 5 1
A renameTable() 0 6 1
B getTypeInteger() 0 17 9
A getTypeFloat() 0 3 1
A getTypeDecimal() 0 15 3
A getTypeEnum() 0 5 1
A getDropIndex() 0 5 1
A getTypeTime() 0 3 1
A getIndexKeys() 0 17 3
A getTypeTimestamp() 0 3 1
A getTypeDatetime() 0 3 1
A getEngine() 0 3 1
A getTypeBinary() 0 3 1
A getRenameColumn() 0 8 1
A getAddIndex() 0 7 1
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file PostgreSQL.php
33
 *
34
 *  The PostgreSQL Driver class
35
 *
36
 *  @package    Platine\Database\Driver
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database\Driver;
48
49
use Platine\Database\Schema\AlterTable;
50
use Platine\Database\Schema\BaseColumn;
51
use Platine\Database\Schema\CreateTable;
52
53
/**
54
 * Class PostgreSQL
55
 * @package Platine\Database\Driver
56
 */
57
class PostgreSQL extends Driver
58
{
0 ignored issues
show
Coding Style introduced by
Opening brace must not be followed by a blank line
Loading history...
59
60
    /**
61
     * @inheritDoc
62
     */
63
    protected array $modifiers = [
64
        'nullable',
65
        'default',
66
        'description'
67
    ];
68
69
    /**
70
     * @inheritDoc
71
     */
72
    public function getColumns(string $database, string $table): array
73
    {
74
        $sql = sprintf(
75
            'SELECT %s AS %s, %s AS %s FROM %s.%s WHERE %s = ? '
76
                . 'AND %s = ? ORDER BY %s ASC',
77
            $this->quoteIdentifier('column_name'),
78
            $this->quoteIdentifier('name'),
79
            $this->quoteIdentifier('udt_name'),
80
            $this->quoteIdentifier('type'),
81
            $this->quoteIdentifier('information_schema'),
82
            $this->quoteIdentifier('columns'),
83
            $this->quoteIdentifier('table_schema'),
84
            $this->quoteIdentifier('table_name'),
85
            $this->quoteIdentifier('ordinal_position'),
86
        );
87
88
        return [
89
            'sql' => $sql,
90
            'params' => [$database, $table]
91
        ];
92
    }
93
94
    /**
95
     * @inheritDoc
96
     */
97
    public function getDatabaseName(): array
98
    {
99
        return [
100
            'sql' => 'SELECT current_schema()',
101
            'params' => []
102
        ];
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function renameTable(string $current, string $new): array
109
    {
110
        return [
111
            'sql' => 'ALTER TABLE ' . $this->quoteIdentifier($current)
112
            . ' RENAME TO ' . $this->quoteIdentifier($new),
113
            'params' => []
114
        ];
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    protected function getTypeInteger(BaseColumn $column): string
121
    {
122
        $autoincrement = $column->get('autoincrement', false);
123
        $type = $autoincrement ? 'SERIAL' : 'INTEGER';
124
        switch ($column->get('size', 'normal')) {
125
            case 'tiny':
126
            case 'small':
127
                $type = $autoincrement ? 'SMALLSERIAL' : 'SMALLINT';
128
                break;
129
            case 'medium':
130
                $type = $autoincrement ? 'SERIAL' : 'INTEGER';
131
                break;
132
            case 'big':
133
                $type = $autoincrement ? 'BIGSERIAL' : 'BIGINT';
134
                break;
135
        }
136
        return $type;
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    protected function getTypeFloat(BaseColumn $column): string
143
    {
144
        return 'REAL';
145
    }
146
147
    /**
148
     * @inheritdoc
149
     */
150
    protected function getTypeDouble(BaseColumn $column): string
151
    {
152
        return 'DOUBLE PRECISION';
153
    }
154
155
    /**
156
     * @inheritdoc
157
     */
158
    protected function getTypeDecimal(BaseColumn $column): string
159
    {
160
        $type = 'DECIMAL';
161
        $length = $column->get('length');
162
        $precision = $column->get('precision');
163
        if ($length !== null) {
164
            if ($precision === null) {
165
                $type = 'DECIMAL(' . $this->value($length) . ')';
166
            } else {
167
                $type = 'DECIMAL(' . $this->value($length) . ', '
168
                        . $this->value($precision) . ')';
169
            }
170
        }
171
172
        return $type;
173
    }
174
    
175
    /**
176
     * @inheritdoc
177
     */
178
    protected function getTypeEnum(BaseColumn $column): string
179
    {
180
        // TODO
181
182
        return '';
183
    }
184
185
    /**
186
     * @inheritdoc
187
     */
188
    protected function getTypeBinary(BaseColumn $column): string
189
    {
190
        return 'BYTEA';
191
    }
192
193
    /**
194
     * @inheritdoc
195
     */
196
    protected function getTypeTime(BaseColumn $column): string
197
    {
198
        return 'TIME(0) WITHOUT TIME ZONE';
199
    }
200
201
    /**
202
     * @inheritdoc
203
     */
204
    protected function getTypeTimestamp(BaseColumn $column): string
205
    {
206
        return 'TIMESTAMP(0) WITHOUT TIME ZONE';
207
    }
208
209
    /**
210
     * @inheritdoc
211
     */
212
    protected function getTypeDatetime(BaseColumn $column): string
213
    {
214
        return 'TIMESTAMP(0) WITHOUT TIME ZONE';
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220
    protected function getIndexKeys(CreateTable $schema): array
221
    {
222
        $indexes = $schema->getIndexes();
223
224
        if (empty($indexes)) {
225
            return [];
226
        }
227
228
        $sql = [];
229
        $table = $schema->getTableName();
230
231
        foreach ($indexes as $name => $columns) {
232
            $sql[] = 'CREATE INDEX ' . $this->quoteIdentifier($table . '_' . $name)
233
                    . ' ON ' . $this->quoteIdentifier($table) . '(' . $this->quoteIdentifiers($columns) . ')';
234
        }
235
236
        return $sql;
237
    }
238
239
    /**
240
     * @inheritdoc
241
     */
242
    protected function getRenameColumn(AlterTable $schema, $data): string
243
    {
244
        $column = $data['column'];
245
        return sprintf(
246
            'ALTER TABLE %s RENAME COLUMN %s TO %s',
247
            $this->quoteIdentifier($schema->getTableName()),
248
            $this->quoteIdentifier($data['from']),
249
            $this->quoteIdentifier($column->getName())
250
        );
251
    }
252
253
    /**
254
     * @inheritdoc
255
     */
256
    protected function getAddIndex(AlterTable $schema, $data): string
257
    {
258
        return sprintf(
259
            'CREATE INDEX %s ON %s (%s)',
260
            $this->quoteIdentifier($schema->getTableName() . '_' . $data['name']),
261
            $this->quoteIdentifier($schema->getTableName()),
262
            $this->quoteIdentifiers($data['columns'])
263
        );
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269
    protected function getDropIndex(AlterTable $schema, $data): string
270
    {
271
        return sprintf(
272
            'DROP INDEX %s',
273
            $this->quoteIdentifier($schema->getTableName() . '_' . $data)
274
        );
275
    }
276
277
    /**
278
     * @inheritdoc
279
     */
280
    protected function getEngine(CreateTable $schema): string
281
    {
282
        return '';
283
    }
284
}
285