1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Licensed to CRATE Technology GmbH("Crate") under one or more contributor |
4
|
|
|
* license agreements. See the NOTICE file distributed with this work for |
5
|
|
|
* additional information regarding copyright ownership. Crate licenses |
6
|
|
|
* this file to you under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. You may |
8
|
|
|
* obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations |
16
|
|
|
* under the License. |
17
|
|
|
* |
18
|
|
|
* However, if you have executed another commercial license agreement |
19
|
|
|
* with Crate these terms will supersede the license and you may use the |
20
|
|
|
* software solely pursuant to the terms of the relevant commercial agreement. |
21
|
|
|
*/ |
22
|
|
|
namespace Crate\DBAL\Platforms; |
23
|
|
|
|
24
|
|
|
use Crate\DBAL\Types\MapType; |
25
|
|
|
use Crate\DBAL\Types\TimestampType; |
26
|
|
|
use Doctrine\DBAL\DBALException; |
27
|
|
|
use Doctrine\DBAL\Event\SchemaCreateTableColumnEventArgs; |
28
|
|
|
use Doctrine\DBAL\Event\SchemaCreateTableEventArgs; |
29
|
|
|
use Doctrine\DBAL\Events; |
30
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
31
|
|
|
use Doctrine\DBAL\Schema\Identifier; |
32
|
|
|
use Doctrine\DBAL\Schema\Index; |
33
|
|
|
use Doctrine\DBAL\Schema\Table; |
34
|
|
|
use Doctrine\DBAL\Schema\TableDiff; |
35
|
|
|
use Doctrine\DBAL\Types\Type; |
36
|
|
|
use Doctrine\DBAL\Schema\ForeignKeyConstraint; |
37
|
|
|
|
38
|
|
|
class CratePlatform extends AbstractPlatform |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
const TIMESTAMP_FORMAT = 'Y-m-d\TH:i:s'; |
42
|
|
|
const TIMESTAMP_FORMAT_TZ = 'Y-m-d\TH:i:sO'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritDoc} |
46
|
|
|
*/ |
47
|
50 |
|
public function __construct() |
48
|
|
|
{ |
49
|
50 |
|
parent::__construct(); |
50
|
50 |
|
$this->initializeDoctrineTypeMappings(); |
51
|
50 |
|
if (!Type::hasType(MapType::NAME)) { |
52
|
1 |
|
Type::addType(MapType::NAME, 'Crate\DBAL\Types\MapType'); |
53
|
1 |
|
} |
54
|
50 |
|
if (!Type::hasType(TimestampType::NAME)) { |
55
|
1 |
|
Type::addType(TimestampType::NAME, 'Crate\DBAL\Types\TimestampType'); |
56
|
1 |
|
} |
57
|
50 |
|
Type::overrideType('array', 'Crate\DBAL\Types\ArrayType'); |
58
|
50 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritDoc} |
62
|
|
|
*/ |
63
|
1 |
|
public function getSubstringExpression($value, $from = 0, $length = null) |
64
|
|
|
{ |
65
|
1 |
|
if ($length === null) { |
66
|
1 |
|
return 'SUBSTR(' . $value . ', ' . $from . ')'; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return 'SUBSTR(' . $value . ', ' . $from . ', ' . $length . ')'; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritDoc} |
74
|
|
|
*/ |
75
|
1 |
|
public function getNowExpression() |
76
|
|
|
{ |
77
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function getRegexpExpression() |
84
|
|
|
{ |
85
|
1 |
|
return 'LIKE'; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function getDateDiffExpression($date1, $date2) |
92
|
|
|
{ |
93
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritDoc} |
98
|
|
|
*/ |
99
|
2 |
|
public function supportsSequences() |
100
|
|
|
{ |
101
|
2 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* If we want to support Schemas, we need to implement |
106
|
|
|
* getListNamespacesSQL and getCreateSchemaSQL methods |
107
|
|
|
* |
108
|
|
|
* {@inheritDoc} |
109
|
|
|
*/ |
110
|
1 |
|
public function supportsSchemas() |
111
|
|
|
{ |
112
|
1 |
|
return false; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* {@inheritDoc} |
117
|
|
|
*/ |
118
|
1 |
|
public function supportsIdentityColumns() |
119
|
|
|
{ |
120
|
1 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritDoc} |
125
|
|
|
*/ |
126
|
1 |
|
public function supportsIndexes() |
127
|
|
|
{ |
128
|
1 |
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritDoc} |
133
|
|
|
*/ |
134
|
52 |
|
public function supportsCommentOnStatement() |
135
|
|
|
{ |
136
|
52 |
|
return false; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritDoc} |
141
|
|
|
*/ |
142
|
3 |
|
public function supportsForeignKeyConstraints() |
143
|
|
|
{ |
144
|
3 |
|
return false; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* {@inheritDoc} |
149
|
|
|
*/ |
150
|
1 |
|
public function supportsForeignKeyOnUpdate() |
151
|
|
|
{ |
152
|
1 |
|
return false; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritDoc} |
157
|
|
|
*/ |
158
|
1 |
|
public function supportsViews() |
159
|
|
|
{ |
160
|
1 |
|
return false; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritDoc} |
165
|
|
|
*/ |
166
|
1 |
|
public function prefersSequences() |
167
|
|
|
{ |
168
|
1 |
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* {@inheritDoc} |
173
|
|
|
*/ |
174
|
1 |
|
public function getListDatabasesSQL() |
175
|
|
|
{ |
176
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritDoc} |
181
|
|
|
*/ |
182
|
12 |
|
public function getListTablesSQL() |
183
|
|
|
{ |
184
|
|
|
return "SELECT table_name, schema_name FROM information_schema.tables " . |
185
|
12 |
|
"WHERE schema_name = 'doc' OR schema_name = 'blob'"; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* {@inheritDoc} |
190
|
|
|
*/ |
191
|
3 |
|
public function getListTableColumnsSQL($table, $database = null) |
192
|
|
|
{ |
193
|
3 |
|
$t = explode('.', $table); |
194
|
3 |
|
if (count($t) == 1) { |
195
|
3 |
|
array_unshift($t, 'doc'); |
196
|
3 |
|
} |
197
|
|
|
// todo: make safe |
198
|
|
|
return "SELECT * from information_schema.columns " . |
199
|
3 |
|
"WHERE table_name = '$t[1]' AND schema_name = '$t[0]'"; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* {@inheritDoc} |
204
|
|
|
*/ |
205
|
3 |
|
public function getListTableConstraintsSQL($table, $database = null) |
|
|
|
|
206
|
|
|
{ |
207
|
3 |
|
$t = explode('.', $table); |
208
|
3 |
|
if (count($t) == 1) { |
209
|
3 |
|
array_unshift($t, 'doc'); |
210
|
3 |
|
} |
211
|
|
|
// todo: make safe |
212
|
|
|
return "SELECT constraint_name, constraint_type from information_schema.table_constraints " . |
213
|
3 |
|
"WHERE table_name = '$t[1]' AND schema_name = '$t[0]' AND constraint_type = 'PRIMARY_KEY'"; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* {@inheritDoc} |
218
|
|
|
*/ |
219
|
2 |
|
public function getAlterTableSQL(TableDiff $diff) |
220
|
|
|
{ |
221
|
2 |
|
$sql = array(); |
222
|
2 |
|
$commentsSQL = array(); |
223
|
2 |
|
$columnSql = array(); |
224
|
|
|
|
225
|
2 |
|
foreach ($diff->addedColumns as $column) { |
226
|
2 |
|
if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) { |
227
|
|
|
continue; |
228
|
|
|
} |
229
|
|
|
|
230
|
2 |
|
$query = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $column->toArray()); |
231
|
2 |
|
$sql[] = 'ALTER TABLE ' . $diff->name . ' ' . $query; |
232
|
2 |
|
if ($comment = $this->getColumnComment($column)) { |
233
|
|
|
$commentsSQL[] = $this->getCommentOnColumnSQL($diff->name, $column->getName(), $comment); |
234
|
|
|
} |
235
|
2 |
|
} |
236
|
|
|
|
237
|
2 |
|
if (count($diff->removedColumns) > 0) { |
238
|
|
|
throw DBALException::notSupported("Alter Table: drop columns"); |
239
|
|
|
} |
240
|
2 |
|
if (count($diff->changedColumns) > 0) { |
241
|
|
|
throw DBALException::notSupported("Alter Table: change column options"); |
242
|
|
|
} |
243
|
2 |
|
if (count($diff->renamedColumns) > 0) { |
244
|
|
|
throw DBALException::notSupported("Alter Table: rename columns"); |
245
|
|
|
} |
246
|
|
|
|
247
|
2 |
|
$tableSql = array(); |
248
|
|
|
|
249
|
2 |
|
if (!$this->onSchemaAlterTable($diff, $tableSql)) { |
250
|
2 |
|
if ($diff->newName !== false) { |
251
|
|
|
throw DBALException::notSupported("Alter Table: rename table"); |
252
|
|
|
} |
253
|
|
|
|
254
|
2 |
|
$sql = array_merge($sql, $this->_getAlterTableIndexForeignKeySQL($diff), $commentsSQL); |
255
|
2 |
|
} |
256
|
|
|
|
257
|
2 |
|
return array_merge($sql, $tableSql, $columnSql); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* {@inheritDoc} |
262
|
|
|
*/ |
263
|
56 |
|
public function getColumnDeclarationSQL($name, array $field) |
264
|
|
|
{ |
265
|
56 |
|
if (isset($field['columnDefinition'])) { |
266
|
34 |
|
$columnDef = $this->getCustomTypeDeclarationSQL($field); |
267
|
34 |
|
} else { |
268
|
55 |
|
$typeDecl = $field['type']->getSqlDeclaration($field, $this); |
269
|
55 |
|
$columnDef = $typeDecl; |
270
|
|
|
} |
271
|
|
|
|
272
|
56 |
|
return $name . ' ' . $columnDef; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Generate table index column declaration |
277
|
|
|
* @codeCoverageIgnore |
278
|
|
|
*/ |
279
|
|
|
public function getIndexDeclarationSQL($name, Index $index) |
280
|
|
|
{ |
281
|
|
|
$columns = $index->getQuotedColumns($this); |
|
|
|
|
282
|
|
|
$name = new Identifier($name); |
283
|
|
|
|
284
|
|
|
if (count($columns) == 0) { |
285
|
|
|
throw new \InvalidArgumentException("Incomplete definition. 'columns' required."); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return 'INDEX ' . $name->getQuotedName($this) . ' USING FULLTEXT ('. $this->getIndexFieldDeclarationListSQL($columns) . ')'; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* {@inheritDoc} |
293
|
|
|
* |
294
|
|
|
* Crate wants boolean values converted to the strings 'true'/'false'. |
295
|
|
|
*/ |
296
|
2 |
|
public function convertBooleans($item) |
297
|
|
|
{ |
298
|
2 |
|
if (is_array($item)) { |
299
|
1 |
|
foreach ($item as $key => $value) { |
300
|
1 |
|
if (is_bool($value)) { |
301
|
1 |
|
$item[$key] = ($value) ? 'true' : 'false'; |
302
|
1 |
|
} elseif (is_numeric($value)) { |
303
|
1 |
|
$item[$key] = ($value > 0) ? 'true' : 'false'; |
304
|
1 |
|
} |
305
|
1 |
|
} |
306
|
1 |
|
} else { |
307
|
2 |
|
if (is_bool($item)) { |
308
|
2 |
|
$item = ($item) ? 'true' : 'false'; |
309
|
2 |
|
} elseif (is_numeric($item)) { |
310
|
1 |
|
$item = ($item > 0) ? 'true' : 'false'; |
311
|
1 |
|
} |
312
|
|
|
} |
313
|
|
|
|
314
|
2 |
|
return $item; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* {@inheritDoc} |
319
|
|
|
*/ |
320
|
1 |
|
public function getBooleanTypeDeclarationSQL(array $field) |
321
|
|
|
{ |
322
|
1 |
|
return 'BOOLEAN'; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* {@inheritDoc} |
327
|
|
|
*/ |
328
|
53 |
|
public function getIntegerTypeDeclarationSQL(array $field) |
329
|
|
|
{ |
330
|
53 |
|
return 'INTEGER'; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* {@inheritDoc} |
335
|
|
|
*/ |
336
|
1 |
|
public function getBigIntTypeDeclarationSQL(array $field) |
337
|
|
|
{ |
338
|
1 |
|
return 'LONG'; |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* {@inheritDoc} |
343
|
|
|
*/ |
344
|
1 |
|
public function getSmallIntTypeDeclarationSQL(array $field) |
345
|
|
|
{ |
346
|
1 |
|
return 'SHORT'; |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* {@inheritDoc} |
351
|
|
|
*/ |
352
|
34 |
|
public function getFloatDeclarationSQL(array $field) |
353
|
|
|
{ |
354
|
34 |
|
return 'DOUBLE'; |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* {@inheritDoc} |
359
|
|
|
*/ |
360
|
|
|
public function getDecimalTypeDeclarationSQL(array $columnDef) |
361
|
|
|
{ |
362
|
|
|
return 'DOUBLE'; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* {@inheritDoc} |
367
|
|
|
*/ |
368
|
24 |
|
public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration) |
369
|
|
|
{ |
370
|
24 |
|
return 'TIMESTAMP'; |
371
|
|
|
} |
372
|
|
|
|
373
|
|
|
/** |
374
|
|
|
* {@inheritDoc} |
375
|
|
|
*/ |
376
|
1 |
|
public function getDateTimeTzTypeDeclarationSQL(array $fieldDeclaration) |
377
|
|
|
{ |
378
|
1 |
|
return 'TIMESTAMP'; |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
/** |
382
|
|
|
* {@inheritDoc} |
383
|
|
|
*/ |
384
|
1 |
|
public function getDateTypeDeclarationSQL(array $fieldDeclaration) |
385
|
|
|
{ |
386
|
1 |
|
return 'TIMESTAMP'; |
387
|
|
|
} |
388
|
|
|
|
389
|
|
|
/** |
390
|
|
|
* {@inheritDoc} |
391
|
|
|
*/ |
392
|
1 |
|
public function getTimeTypeDeclarationSQL(array $fieldDeclaration) |
393
|
|
|
{ |
394
|
1 |
|
return 'TIMESTAMP'; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* {@inheritDoc} |
399
|
|
|
*/ |
400
|
|
|
protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef) |
401
|
|
|
{ |
402
|
|
|
return ''; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
/** |
406
|
|
|
* {@inheritDoc} |
407
|
|
|
*/ |
408
|
50 |
|
protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed) |
409
|
|
|
{ |
410
|
50 |
|
return 'STRING'; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* {@inheritDoc} |
415
|
|
|
*/ |
416
|
|
|
public function getClobTypeDeclarationSQL(array $field) |
417
|
|
|
{ |
418
|
|
|
return 'STRING'; |
419
|
|
|
} |
420
|
|
|
|
421
|
|
|
/** |
422
|
|
|
* Gets the SQL snippet used to declare an OBJECT column type. |
423
|
|
|
* |
424
|
|
|
* @param array $field |
425
|
|
|
* |
426
|
|
|
* @return string |
427
|
|
|
*/ |
428
|
36 |
|
public function getMapTypeDeclarationSQL(array $field, array $options) |
|
|
|
|
429
|
|
|
{ |
430
|
36 |
|
$type = array_key_exists('type', $options) ? $options['type'] : MapType::DYNAMIC; |
431
|
|
|
|
432
|
36 |
|
$fields = array_key_exists('fields', $options) ? $options['fields'] : array(); |
433
|
36 |
|
$columns = array(); |
434
|
36 |
|
foreach ($fields as $field) { |
435
|
35 |
|
$columns[$field->getQuotedName($this)] = $this->prepareColumnData($field); |
436
|
36 |
|
} |
437
|
36 |
|
$objectFields = $this->getColumnDeclarationListSQL($columns); |
438
|
|
|
|
439
|
36 |
|
$declaration = count($columns) > 0 ? ' AS ( ' . $objectFields . ' )' : ''; |
440
|
36 |
|
return 'OBJECT ( ' . $type . ' )' . $declaration ; |
441
|
|
|
} |
442
|
|
|
|
443
|
|
|
/** |
444
|
|
|
* Gets the SQL snippet used to declare an ARRAY column type. |
445
|
|
|
* |
446
|
|
|
* @param array $field |
447
|
|
|
* |
448
|
|
|
* @return string |
449
|
|
|
*/ |
450
|
4 |
|
public function getArrayTypeDeclarationSQL(array $field, array $options) |
451
|
|
|
{ |
452
|
4 |
|
$type = array_key_exists('type', $options) ? $options['type'] : Type::STRING; |
453
|
4 |
|
return 'ARRAY ( ' . Type::getType($type)->getSQLDeclaration($field, $this) . ' )'; |
454
|
|
|
} |
455
|
|
|
|
456
|
|
|
/** |
457
|
|
|
* {@inheritDoc} |
458
|
|
|
*/ |
459
|
2 |
|
public function getName() |
460
|
|
|
{ |
461
|
2 |
|
return 'crate'; |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* {@inheritDoc} |
466
|
|
|
* |
467
|
|
|
* PostgreSQL returns all column names in SQL result sets in lowercase. |
468
|
|
|
*/ |
469
|
1 |
|
public function getSQLResultCasing($column) |
470
|
|
|
{ |
471
|
1 |
|
return strtolower($column); |
472
|
|
|
} |
473
|
|
|
|
474
|
|
|
/** |
475
|
|
|
* {@inheritDoc} |
476
|
|
|
*/ |
477
|
2 |
|
public function getDateTimeTzFormatString() |
478
|
|
|
{ |
479
|
2 |
|
return self::TIMESTAMP_FORMAT_TZ; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* {@inheritDoc} |
484
|
|
|
*/ |
485
|
5 |
|
public function getDateTimeFormatString() |
486
|
|
|
{ |
487
|
5 |
|
return self::TIMESTAMP_FORMAT; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* {@inheritDoc} |
492
|
|
|
*/ |
493
|
1 |
|
public function getDateFormatString() |
494
|
|
|
{ |
495
|
1 |
|
return self::TIMESTAMP_FORMAT; |
496
|
|
|
} |
497
|
|
|
|
498
|
|
|
/** |
499
|
|
|
* {@inheritDoc} |
500
|
|
|
*/ |
501
|
1 |
|
public function getTimeFormatString() |
502
|
|
|
{ |
503
|
1 |
|
return self::TIMESTAMP_FORMAT; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* {@inheritDoc} |
508
|
|
|
*/ |
509
|
1 |
|
public function getTruncateTableSQL($tableName, $cascade = false) |
510
|
|
|
{ |
511
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
512
|
|
|
} |
513
|
|
|
|
514
|
|
|
/** |
515
|
|
|
* {@inheritDoc} |
516
|
|
|
*/ |
517
|
1 |
|
public function getReadLockSQL() |
518
|
|
|
{ |
519
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* {@inheritDoc} |
524
|
|
|
*/ |
525
|
50 |
|
protected function initializeDoctrineTypeMappings() |
526
|
|
|
{ |
527
|
50 |
|
$this->doctrineTypeMapping = array( |
528
|
50 |
|
'short' => 'smallint', |
529
|
50 |
|
'integer' => 'integer', |
530
|
50 |
|
'long' => 'bigint', |
531
|
50 |
|
'int' => 'integer', |
532
|
50 |
|
'bool' => 'boolean', |
533
|
50 |
|
'boolean' => 'boolean', |
534
|
50 |
|
'string' => 'string', |
535
|
50 |
|
'float' => 'float', |
536
|
50 |
|
'double' => 'float', |
537
|
50 |
|
'timestamp' => 'timestamp', |
538
|
50 |
|
'object' => 'map', |
539
|
50 |
|
'string_array' => 'array', |
540
|
50 |
|
'float_array' => 'array', |
541
|
50 |
|
'double_array' => 'array', |
542
|
50 |
|
'integer_array' => 'array', |
543
|
50 |
|
'object_array' => 'array', |
544
|
50 |
|
'array' => 'array', |
545
|
|
|
); |
546
|
50 |
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* {@inheritDoc} |
550
|
|
|
*/ |
551
|
50 |
|
public function getVarcharMaxLength() |
552
|
|
|
{ |
553
|
50 |
|
return PHP_INT_MAX; |
554
|
|
|
} |
555
|
|
|
|
556
|
|
|
/** |
557
|
|
|
* {@inheritDoc} |
558
|
|
|
*/ |
559
|
11 |
|
protected function getReservedKeywordsClass() |
560
|
|
|
{ |
561
|
11 |
|
return 'Crate\DBAL\Platforms\Keywords\CrateKeywords'; |
562
|
|
|
} |
563
|
|
|
|
564
|
|
|
/** |
565
|
|
|
* {@inheritDoc} |
566
|
|
|
*/ |
567
|
1 |
|
public function getBlobTypeDeclarationSQL(array $field) |
568
|
|
|
{ |
569
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
/** |
573
|
|
|
* {@inheritDoc} |
574
|
|
|
* Gets the SQL statement(s) to create a table with the specified name, columns and constraints |
575
|
|
|
* on this platform. |
576
|
|
|
* |
577
|
|
|
* @param Table $table The name of the table. |
578
|
|
|
* @param integer $createFlags |
579
|
|
|
* |
580
|
|
|
* @return array The sequence of SQL statements. |
581
|
|
|
*/ |
582
|
53 |
|
public function getCreateTableSQL(Table $table, $createFlags = self::CREATE_INDEXES) |
583
|
|
|
{ |
584
|
53 |
|
if (!is_int($createFlags)) { |
585
|
|
|
$msg = "Second argument of CratePlatform::getCreateTableSQL() has to be integer."; |
586
|
|
|
throw new \InvalidArgumentException($msg); |
587
|
|
|
} |
588
|
|
|
|
589
|
53 |
|
if (count($table->getColumns()) === 0) { |
590
|
2 |
|
throw DBALException::noColumnsSpecifiedForTable($table->getName()); |
591
|
|
|
} |
592
|
|
|
|
593
|
51 |
|
$tableName = $table->getQuotedName($this); |
594
|
51 |
|
$options = $table->getOptions(); |
595
|
51 |
|
$options['uniqueConstraints'] = array(); |
596
|
51 |
|
$options['indexes'] = array(); |
597
|
51 |
|
$options['primary'] = array(); |
598
|
|
|
|
599
|
51 |
|
if (($createFlags&self::CREATE_INDEXES) > 0) { |
600
|
51 |
|
foreach ($table->getIndexes() as $index) { |
601
|
|
|
/* @var $index Index */ |
602
|
37 |
|
if ($index->isPrimary()) { |
603
|
37 |
|
$platform = $this; |
604
|
37 |
|
$options['primary'] = array_map(function ($columnName) use ($table, $platform) { |
605
|
37 |
|
return $table->getColumn($columnName)->getQuotedName($platform); |
606
|
37 |
|
}, $index->getColumns()); |
607
|
37 |
|
$options['primary_index'] = $index; |
608
|
37 |
|
} else { |
609
|
|
|
$options['indexes'][$index->getName()] = $index; |
610
|
|
|
} |
611
|
51 |
|
} |
612
|
51 |
|
} |
613
|
|
|
|
614
|
51 |
|
$columnSql = array(); |
615
|
51 |
|
$columns = array(); |
616
|
|
|
|
617
|
51 |
|
foreach ($table->getColumns() as $column) { |
618
|
51 |
|
if (null !== $this->_eventManager && |
619
|
51 |
|
$this->_eventManager->hasListeners(Events::onSchemaCreateTableColumn)) { |
620
|
|
|
|
621
|
1 |
|
$eventArgs = new SchemaCreateTableColumnEventArgs($column, $table, $this); |
622
|
1 |
|
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTableColumn, $eventArgs); |
623
|
|
|
|
624
|
1 |
|
$columnSql = array_merge($columnSql, $eventArgs->getSql()); |
625
|
|
|
|
626
|
1 |
|
if ($eventArgs->isDefaultPrevented()) { |
627
|
|
|
continue; |
628
|
|
|
} |
629
|
1 |
|
} |
630
|
51 |
|
$columns[$column->getQuotedName($this)] = $this->prepareColumnData($column, $options['primary']); |
|
|
|
|
631
|
51 |
|
} |
632
|
|
|
|
633
|
51 |
|
if (null !== $this->_eventManager && $this->_eventManager->hasListeners(Events::onSchemaCreateTable)) { |
634
|
1 |
|
$eventArgs = new SchemaCreateTableEventArgs($table, $columns, $options, $this); |
635
|
1 |
|
$this->_eventManager->dispatchEvent(Events::onSchemaCreateTable, $eventArgs); |
636
|
|
|
|
637
|
1 |
|
if ($eventArgs->isDefaultPrevented()) { |
638
|
|
|
return array_merge($eventArgs->getSql(), $columnSql); |
639
|
|
|
} |
640
|
1 |
|
} |
641
|
|
|
|
642
|
51 |
|
$sql = $this->_getCreateTableSQL($tableName, $columns, $options); |
643
|
51 |
|
if ($this->supportsCommentOnStatement()) { |
644
|
|
|
foreach ($table->getColumns() as $column) { |
645
|
|
|
if ($this->getColumnComment($column)) { |
646
|
|
|
$sql[] = $this->getCommentOnColumnSQL( |
647
|
|
|
$tableName, |
648
|
|
|
$column->getName(), |
649
|
|
|
$this->getColumnComment($column) |
650
|
|
|
); |
651
|
|
|
} |
652
|
|
|
} |
653
|
|
|
} |
654
|
|
|
|
655
|
51 |
|
return array_merge($sql, $columnSql); |
656
|
|
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* {@inheritDoc} |
660
|
|
|
*/ |
661
|
51 |
|
protected function _getCreateTableSQL($tableName, array $columns, array $options = array()) |
662
|
|
|
{ |
663
|
51 |
|
$columnListSql = $this->getColumnDeclarationListSQL($columns); |
664
|
|
|
|
665
|
51 |
|
if (isset($options['primary']) && ! empty($options['primary'])) { |
666
|
37 |
|
$keyColumns = array_unique(array_values($options['primary'])); |
667
|
37 |
|
$columnListSql .= ', PRIMARY KEY(' . implode(', ', $keyColumns) . ')'; |
668
|
37 |
|
} |
669
|
|
|
|
670
|
51 |
|
if (isset($options['indexes']) && ! empty($options['indexes'])) { |
671
|
|
|
foreach ($options['indexes'] as $index => $definition) { |
672
|
|
|
$columnListSql .= ', ' . $this->getIndexDeclarationSQL($index, $definition); |
673
|
|
|
} |
674
|
|
|
} |
675
|
|
|
|
676
|
51 |
|
if (isset($options['foreignKeys'])) { |
677
|
|
|
throw DBALException::notSupported("Create Table: foreign keys"); |
678
|
|
|
} |
679
|
|
|
|
680
|
51 |
|
$query = 'CREATE TABLE ' . $tableName . ' (' . $columnListSql . ')'; |
681
|
51 |
|
return array($query); |
682
|
|
|
} |
683
|
|
|
|
684
|
|
|
/** |
685
|
|
|
* @param \Doctrine\DBAL\Schema\Column $column The name of the table. |
686
|
|
|
* @param array $primaries |
687
|
|
|
* |
688
|
|
|
* @return array The column data as associative array. |
689
|
|
|
*/ |
690
|
53 |
|
public function prepareColumnData($column, $primaries = array()) |
691
|
|
|
{ |
692
|
|
|
|
693
|
53 |
|
$columnData = array(); |
694
|
53 |
|
$columnData['name'] = $column->getQuotedName($this); |
695
|
53 |
|
$columnData['type'] = $column->getType(); |
696
|
53 |
|
$columnData['length'] = $column->getLength(); |
697
|
53 |
|
$columnData['notnull'] = $column->getNotNull(); |
698
|
53 |
|
$columnData['fixed'] = $column->getFixed(); |
699
|
53 |
|
$columnData['unique'] = false; // TODO: what do we do about this? |
700
|
53 |
|
$columnData['version'] = $column->hasPlatformOption("version") ? $column->getPlatformOption('version') : false; |
701
|
|
|
|
702
|
53 |
|
if (strtolower($columnData['type']) == "string" && $columnData['length'] === null) { |
703
|
44 |
|
$columnData['length'] = 255; |
704
|
44 |
|
} |
705
|
|
|
|
706
|
53 |
|
$columnData['unsigned'] = $column->getUnsigned(); |
707
|
53 |
|
$columnData['precision'] = $column->getPrecision(); |
708
|
53 |
|
$columnData['scale'] = $column->getScale(); |
709
|
53 |
|
$columnData['default'] = $column->getDefault(); |
710
|
53 |
|
$columnData['columnDefinition'] = $column->getColumnDefinition(); |
711
|
53 |
|
$columnData['autoincrement'] = $column->getAutoincrement(); |
712
|
53 |
|
$columnData['comment'] = $this->getColumnComment($column); |
713
|
|
|
|
714
|
53 |
|
$columnData['platformOptions'] = $column->getPlatformOptions(); |
715
|
|
|
|
716
|
53 |
|
if (in_array($column->getName(), $primaries)) { |
717
|
36 |
|
$columnData['primary'] = true; |
718
|
36 |
|
} |
719
|
53 |
|
return $columnData; |
720
|
|
|
} |
721
|
|
|
|
722
|
|
|
/** |
723
|
|
|
* {@inheritDoc} |
724
|
|
|
*/ |
725
|
1 |
|
public function getCreateDatabaseSQL($database) |
726
|
|
|
{ |
727
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
728
|
|
|
} |
729
|
|
|
|
730
|
|
|
/** |
731
|
|
|
* {@inheritDoc} |
732
|
|
|
*/ |
733
|
1 |
|
public function getDropDatabaseSQL($database) |
734
|
|
|
{ |
735
|
1 |
|
throw DBALException::notSupported(__METHOD__); |
736
|
|
|
} |
737
|
|
|
|
738
|
|
|
/** |
739
|
|
|
* {@inheritDoc} |
740
|
|
|
*/ |
741
|
|
|
public function getCreateForeignKeySQL(ForeignKeyConstraint $foreignKey, $table) |
742
|
|
|
{ |
743
|
|
|
throw DBALException::notSupported(__METHOD__); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
/** |
747
|
|
|
* {@inheritDoc} |
748
|
|
|
*/ |
749
|
|
|
public function getGuidTypeDeclarationSQL(array $field) |
750
|
|
|
{ |
751
|
|
|
throw DBALException::notSupported(__METHOD__); |
752
|
|
|
} |
753
|
|
|
} |
754
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.