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 https://www.platine-php.com |
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
|
|
|
{ |
59
|
|
|
/** |
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
62
|
|
|
protected array $modifiers = [ |
63
|
|
|
'nullable', |
64
|
|
|
'default', |
65
|
|
|
'description' |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
*/ |
71
|
|
|
public function getColumns(string $database, string $table): array |
72
|
|
|
{ |
73
|
|
|
$sql = sprintf( |
74
|
|
|
'SELECT %s AS %s, %s AS %s FROM %s.%s WHERE %s = ? ' |
75
|
|
|
. 'AND %s = ? ORDER BY %s ASC', |
76
|
|
|
$this->quoteIdentifier('column_name'), |
77
|
|
|
$this->quoteIdentifier('name'), |
78
|
|
|
$this->quoteIdentifier('udt_name'), |
79
|
|
|
$this->quoteIdentifier('type'), |
80
|
|
|
$this->quoteIdentifier('information_schema'), |
81
|
|
|
$this->quoteIdentifier('columns'), |
82
|
|
|
$this->quoteIdentifier('table_schema'), |
83
|
|
|
$this->quoteIdentifier('table_name'), |
84
|
|
|
$this->quoteIdentifier('ordinal_position'), |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
return [ |
88
|
|
|
'sql' => $sql, |
89
|
|
|
'params' => [$database, $table] |
90
|
|
|
]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritDoc |
95
|
|
|
*/ |
96
|
|
|
public function getDatabaseName(): array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'sql' => 'SELECT current_schema()', |
100
|
|
|
'params' => [] |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @inheritdoc |
106
|
|
|
*/ |
107
|
|
|
public function renameTable(string $current, string $new): array |
108
|
|
|
{ |
109
|
|
|
return [ |
110
|
|
|
'sql' => 'ALTER TABLE ' . $this->quoteIdentifier($current) |
111
|
|
|
. ' RENAME TO ' . $this->quoteIdentifier($new), |
112
|
|
|
'params' => [] |
113
|
|
|
]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @inheritdoc |
118
|
|
|
*/ |
119
|
|
|
protected function getTypeInteger(BaseColumn $column): string |
120
|
|
|
{ |
121
|
|
|
$autoincrement = $column->get('autoincrement', false); |
122
|
|
|
$type = $autoincrement ? 'SERIAL' : 'INTEGER'; |
123
|
|
|
switch ($column->get('size', 'normal')) { |
124
|
|
|
case 'tiny': |
125
|
|
|
case 'small': |
126
|
|
|
$type = $autoincrement ? 'SMALLSERIAL' : 'SMALLINT'; |
127
|
|
|
break; |
128
|
|
|
case 'medium': |
129
|
|
|
$type = $autoincrement ? 'SERIAL' : 'INTEGER'; |
130
|
|
|
break; |
131
|
|
|
case 'big': |
132
|
|
|
$type = $autoincrement ? 'BIGSERIAL' : 'BIGINT'; |
133
|
|
|
break; |
134
|
|
|
} |
135
|
|
|
return $type; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @inheritdoc |
140
|
|
|
*/ |
141
|
|
|
protected function getTypeFloat(BaseColumn $column): string |
142
|
|
|
{ |
143
|
|
|
return 'REAL'; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @inheritdoc |
148
|
|
|
*/ |
149
|
|
|
protected function getTypeDouble(BaseColumn $column): string |
150
|
|
|
{ |
151
|
|
|
return 'DOUBLE PRECISION'; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @inheritdoc |
156
|
|
|
*/ |
157
|
|
|
protected function getTypeDecimal(BaseColumn $column): string |
158
|
|
|
{ |
159
|
|
|
$type = 'DECIMAL'; |
160
|
|
|
$length = $column->get('length'); |
161
|
|
|
$precision = $column->get('precision'); |
162
|
|
|
if ($length !== null) { |
163
|
|
|
if ($precision === null) { |
164
|
|
|
$type = 'DECIMAL(' . $this->value($length) . ')'; |
165
|
|
|
} else { |
166
|
|
|
$type = 'DECIMAL(' . $this->value($length) . ', ' |
167
|
|
|
. $this->value($precision) . ')'; |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $type; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @inheritdoc |
176
|
|
|
*/ |
177
|
|
|
protected function getTypeEnum(BaseColumn $column): string |
178
|
|
|
{ |
179
|
|
|
// TODO |
180
|
|
|
|
181
|
|
|
return ''; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @inheritdoc |
186
|
|
|
*/ |
187
|
|
|
protected function getTypeBinary(BaseColumn $column): string |
188
|
|
|
{ |
189
|
|
|
return 'BYTEA'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
protected function getTypeTime(BaseColumn $column): string |
196
|
|
|
{ |
197
|
|
|
return 'TIME(0) WITHOUT TIME ZONE'; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @inheritdoc |
202
|
|
|
*/ |
203
|
|
|
protected function getTypeTimestamp(BaseColumn $column): string |
204
|
|
|
{ |
205
|
|
|
return 'TIMESTAMP(0) WITHOUT TIME ZONE'; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @inheritdoc |
210
|
|
|
*/ |
211
|
|
|
protected function getTypeDatetime(BaseColumn $column): string |
212
|
|
|
{ |
213
|
|
|
return 'TIMESTAMP(0) WITHOUT TIME ZONE'; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @inheritdoc |
218
|
|
|
*/ |
219
|
|
|
protected function getIndexKeys(CreateTable $schema): array |
220
|
|
|
{ |
221
|
|
|
$indexes = $schema->getIndexes(); |
222
|
|
|
|
223
|
|
|
if (empty($indexes)) { |
224
|
|
|
return []; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$sql = []; |
228
|
|
|
$table = $schema->getTableName(); |
229
|
|
|
|
230
|
|
|
foreach ($indexes as $name => $columns) { |
231
|
|
|
$sql[] = 'CREATE INDEX ' . $this->quoteIdentifier($table . '_' . $name) |
232
|
|
|
. ' ON ' . $this->quoteIdentifier($table) . '(' . $this->quoteIdentifiers($columns) . ')'; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
return $sql; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @inheritdoc |
240
|
|
|
*/ |
241
|
|
|
protected function getRenameColumn(AlterTable $schema, mixed $data): string |
242
|
|
|
{ |
243
|
|
|
$column = $data['column']; |
244
|
|
|
return sprintf( |
245
|
|
|
'ALTER TABLE %s RENAME COLUMN %s TO %s', |
246
|
|
|
$this->quoteIdentifier($schema->getTableName()), |
247
|
|
|
$this->quoteIdentifier($data['from']), |
248
|
|
|
$this->quoteIdentifier($column->getName()) |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @inheritdoc |
254
|
|
|
*/ |
255
|
|
|
protected function getAddIndex(AlterTable $schema, mixed $data): string |
256
|
|
|
{ |
257
|
|
|
return sprintf( |
258
|
|
|
'CREATE INDEX %s ON %s (%s)', |
259
|
|
|
$this->quoteIdentifier($schema->getTableName() . '_' . $data['name']), |
260
|
|
|
$this->quoteIdentifier($schema->getTableName()), |
261
|
|
|
$this->quoteIdentifiers($data['columns']) |
262
|
|
|
); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @inheritdoc |
267
|
|
|
*/ |
268
|
|
|
protected function getDropIndex(AlterTable $schema, mixed $data): string |
269
|
|
|
{ |
270
|
|
|
return sprintf( |
271
|
|
|
'DROP INDEX %s', |
272
|
|
|
$this->quoteIdentifier($schema->getTableName() . '_' . $data) |
273
|
|
|
); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @inheritdoc |
278
|
|
|
*/ |
279
|
|
|
protected function getEngine(CreateTable $schema): string |
280
|
|
|
{ |
281
|
|
|
return ''; |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|