DrizzlePlatform.php 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Platforms;

Benjamin Morel's avatar
Benjamin Morel committed
22 23 24 25
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Schema\TableDiff;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Schema\Table;
26 27 28

/**
 * Drizzle platform
29 30
 *
 * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
31 32 33 34
 */
class DrizzlePlatform extends AbstractPlatform
{
    /**
35
     * {@inheritDoc}
36 37 38 39 40 41 42
     */
    public function getName()
    {
        return 'drizzle';
    }

    /**
43
     * {@inheritDoc}
44 45 46 47 48 49
     */
    public function getIdentifierQuoteCharacter()
    {
        return '`';
    }

50 51
    /**
     * {@inheritDoc}
Benjamin Morel's avatar
Benjamin Morel committed
52 53
     */
    public function getConcatExpression()
54 55
    {
        $args = func_get_args();
56

57 58 59
        return 'CONCAT(' . join(', ', (array) $args) . ')';
    }

60 61 62
    /**
     * {@inheritDoc}
     */
63 64 65 66 67
    public function getDateDiffExpression($date1, $date2)
    {
        return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
    }

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    /**
     * {@inheritDoc}
     */
    public function getDateAddHourExpression($date, $hours)
    {
        return 'DATE_ADD(' . $date . ', INTERVAL ' . $hours . ' HOUR)';
    }

    /**
     * {@inheritDoc}
     */
    public function getDateSubHourExpression($date, $hours)
    {
        return 'DATE_SUB(' . $date . ', INTERVAL ' . $hours . ' HOUR)';    
    }

84 85 86
    /**
     * {@inheritDoc}
     */
87 88 89 90 91
    public function getDateAddDaysExpression($date, $days)
    {
        return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
    }

92 93 94
    /**
     * {@inheritDoc}
     */
95 96 97 98 99
    public function getDateSubDaysExpression($date, $days)
    {
        return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
    }

100 101 102
    /**
     * {@inheritDoc}
     */
103 104 105 106 107
    public function getDateAddMonthExpression($date, $months)
    {
        return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
    }

108 109 110
    /**
     * {@inheritDoc}
     */
111 112 113 114 115
    public function getDateSubMonthExpression($date, $months)
    {
        return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)';
    }

116
    /**
117
     * {@inheritDoc}
118 119 120 121 122 123
     */
    public function getBooleanTypeDeclarationSQL(array $field)
    {
        return 'BOOLEAN';
    }

124 125 126
    /**
     * {@inheritDoc}
     */
127 128 129 130 131
    public function getIntegerTypeDeclarationSQL(array $field)
    {
        return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
    }

132 133 134
    /**
     * {@inheritDoc}
     */
135 136 137 138 139 140 141 142 143
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
    {
        $autoinc = '';
        if ( ! empty($columnDef['autoincrement'])) {
            $autoinc = ' AUTO_INCREMENT';
        }
        return $autoinc;
    }

144 145 146
    /**
     * {@inheritDoc}
     */
147 148 149 150 151
    public function getBigIntTypeDeclarationSQL(array $field)
    {
        return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
    }

152 153 154
    /**
     * {@inheritDoc}
     */
155 156 157 158 159
    public function getSmallIntTypeDeclarationSQL(array $field)
    {
        return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
    }

160 161 162
    /**
     * {@inheritDoc}
     */
163 164 165 166 167
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
    {
        return $length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)';
    }

168 169 170
    /**
     * {@inheritDoc}
     */
171 172 173 174 175 176
    protected function initializeDoctrineTypeMappings()
    {
        $this->doctrineTypeMapping = array(
            'boolean'       => 'boolean',
            'varchar'       => 'string',
            'integer'       => 'integer',
177 178 179 180 181
            'blob'          => 'text',
            'decimal'       => 'decimal',
            'datetime'      => 'datetime',
            'date'          => 'date',
            'time'          => 'time',
182 183
            'text'          => 'text',
            'timestamp'     => 'datetime',
184 185
            'double'        => 'float',
            'bigint'        => 'bigint',
186 187 188
        );
    }

189 190 191
    /**
     * {@inheritDoc}
     */
192 193 194 195 196 197
    public function getClobTypeDeclarationSQL(array $field)
    {
        return 'TEXT';
    }

    /**
198
     * {@inheritDoc}
199 200 201 202 203 204
     */
    public function getBlobTypeDeclarationSQL(array $field)
    {
        return 'BLOB';
    }

205 206 207
    /**
     * {@inheritDoc}
     */
208 209 210 211 212
    public function getCreateDatabaseSQL($name)
    {
        return 'CREATE DATABASE ' . $name;
    }

213 214 215
    /**
     * {@inheritDoc}
     */
216 217 218 219 220
    public function getDropDatabaseSQL($name)
    {
        return 'DROP DATABASE ' . $name;
    }

Benjamin Morel's avatar
Benjamin Morel committed
221 222 223
    /**
     * {@inheritDoc}
     */
224 225 226 227 228
    public function getListDatabasesSQL()
    {
        return "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='LOCAL'";
    }

229 230 231
    /**
     * {@inheritDoc}
     */
232 233 234 235 236
    protected function getReservedKeywordsClass()
    {
        return 'Doctrine\DBAL\Platforms\Keywords\DrizzleKeywords';
    }

Benjamin Morel's avatar
Benjamin Morel committed
237 238 239
    /**
     * {@inheritDoc}
     */
240 241 242 243 244
    public function getListTablesSQL()
    {
        return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE' AND TABLE_SCHEMA=DATABASE()";
    }

Benjamin Morel's avatar
Benjamin Morel committed
245 246 247
    /**
     * {@inheritDoc}
     */
248 249 250 251 252 253 254 255
    public function getListTableColumnsSQL($table, $database = null)
    {
        if ($database) {
            $database = "'" . $database . "'";
        } else {
            $database = 'DATABASE()';
        }

256
        return "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT, CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT," .
257
               " NUMERIC_PRECISION, NUMERIC_SCALE" .
258 259 260 261
               " FROM DATA_DICTIONARY.COLUMNS" .
               " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME = '" . $table . "'";
    }

Benjamin Morel's avatar
Benjamin Morel committed
262 263 264
    /**
     * {@inheritDoc}
     */
265 266
    public function getListTableForeignKeysSQL($table, $database = null)
    {
267 268 269 270 271 272 273 274 275
        if ($database) {
            $database = "'" . $database . "'";
        } else {
            $database = 'DATABASE()';
        }

        return "SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS, UPDATE_RULE, DELETE_RULE" .
               " FROM DATA_DICTIONARY.FOREIGN_KEYS" .
               " WHERE CONSTRAINT_SCHEMA=" . $database . " AND CONSTRAINT_TABLE='" . $table . "'";
276 277
    }

278 279 280
    /**
     * {@inheritDoc}
     */
281 282
    public function getListTableIndexesSQL($table, $database = null)
    {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
283 284 285 286 287 288 289 290 291
        if ($database) {
            $database = "'" . $database . "'";
        } else {
            $database = 'DATABASE()';
        }

        return "SELECT INDEX_NAME AS 'key_name', COLUMN_NAME AS 'column_name', IS_USED_IN_PRIMARY AS 'primary', IS_UNIQUE=0 AS 'non_unique'" .
               " FROM DATA_DICTIONARY.INDEX_PARTS" .
               " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME='" . $table . "'";
292 293
    }

294 295 296
    /**
     * {@inheritDoc}
     */
297 298 299 300 301
    public function prefersIdentityColumns()
    {
        return true;
    }

302 303 304
    /**
     * {@inheritDoc}
     */
305 306 307 308
    public function supportsIdentityColumns()
    {
        return true;
    }
309

310 311 312
    /**
     * {@inheritDoc}
     */
313 314 315 316
    public function supportsInlineColumnComments()
    {
        return true;
    }
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
317

318 319 320
    /**
     * {@inheritDoc}
     */
321 322 323 324 325
    public function supportsViews()
    {
        return false;
    }

326 327 328
    /**
     * {@inheritDoc}
     */
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
329 330
    public function getDropIndexSQL($index, $table=null)
    {
331
        if ($index instanceof Index) {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
332
            $indexName = $index->getQuotedName($this);
333
        } else if (is_string($index)) {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
334 335 336 337 338
            $indexName = $index;
        } else {
            throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
        }

339
        if ($table instanceof Table) {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
            $table = $table->getQuotedName($this);
        } else if(!is_string($table)) {
            throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
        }

        if ($index instanceof Index && $index->isPrimary()) {
            // drizzle primary keys are always named "PRIMARY",
            // so we cannot use them in statements because of them being keyword.
            return $this->getDropPrimaryKeySQL($table);
        }

        return 'DROP INDEX ' . $indexName . ' ON ' . $table;
    }

    /**
Benjamin Morel's avatar
Benjamin Morel committed
355
     * {@inheritDoc}
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
356 357 358 359 360 361
     */
    protected function getDropPrimaryKeySQL($table)
    {
        return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
    }

362 363 364
    /**
     * {@inheritDoc}
     */
365 366 367 368 369
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
    {
        if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
            return 'TIMESTAMP';
        }
370 371

        return 'DATETIME';
372 373
    }

374 375 376
    /**
     * {@inheritDoc}
     */
377 378 379 380 381
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
    {
        return 'TIME';
    }

382 383 384
    /**
     * {@inheritDoc}
     */
385 386 387 388
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
    {
        return 'DATE';
    }
389

390
    /**
391
     * {@inheritDoc}
392
     */
393 394 395 396 397 398 399 400 401
    public function getAlterTableSQL(TableDiff $diff)
    {
        $columnSql = array();
        $queryParts = array();

        if ($diff->newName !== false) {
            $queryParts[] =  'RENAME TO ' . $diff->newName;
        }

402
        foreach ($diff->addedColumns as $column) {
403 404 405 406 407 408 409 410 411
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
            }

            $columnArray = $column->toArray();
            $columnArray['comment'] = $this->getColumnComment($column);
            $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
        }

412
        foreach ($diff->removedColumns as $column) {
413 414 415 416 417 418 419
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
                continue;
            }

            $queryParts[] =  'DROP ' . $column->getQuotedName($this);
        }

420
        foreach ($diff->changedColumns as $columnDiff) {
421 422 423 424
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
                continue;
            }

425
            /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
426 427 428 429 430 431 432
            $column = $columnDiff->column;
            $columnArray = $column->toArray();
            $columnArray['comment'] = $this->getColumnComment($column);
            $queryParts[] =  'CHANGE ' . ($columnDiff->oldColumnName) . ' '
                    . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
        }

433
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
434 435 436 437 438 439 440 441 442 443 444 445 446
            if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
                continue;
            }

            $columnArray = $column->toArray();
            $columnArray['comment'] = $this->getColumnComment($column);
            $queryParts[] =  'CHANGE ' . $oldColumnName . ' '
                    . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
        }

        $sql = array();
        $tableSql = array();

447
        if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
448 449 450 451 452 453 454 455 456 457 458 459
            if (count($queryParts) > 0) {
                $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
            }
            $sql = array_merge(
                $this->getPreAlterTableIndexForeignKeySQL($diff),
                $sql,
                $this->getPostAlterTableIndexForeignKeySQL($diff)
            );
        }

        return array_merge($sql, $tableSql, $columnSql);
    }
460

461 462 463
    /**
     * {@inheritDoc}
     */
464 465
    public function getDropTemporaryTableSQL($table)
    {
466
        if ($table instanceof Table) {
467 468 469 470 471 472 473
            $table = $table->getQuotedName($this);
        } else if(!is_string($table)) {
            throw new \InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
        }

        return 'DROP TEMPORARY TABLE ' . $table;
    }
474

475 476 477
    /**
     * {@inheritDoc}
     */
478 479 480 481 482 483 484 485
    public function convertBooleans($item)
    {
        if (is_array($item)) {
            foreach ($item as $key => $value) {
                if (is_bool($value) || is_numeric($item)) {
                    $item[$key] = ($value) ? 'true' : 'false';
                }
            }
486 487
        } else if (is_bool($item) || is_numeric($item)) {
           $item = ($item) ? 'true' : 'false';
488
        }
489

490 491
        return $item;
    }
492

493 494 495
    /**
     * {@inheritDoc}
     */
496 497 498 499 500
    public function getLocateExpression($str, $substr, $startPos = false)
    {
        if ($startPos == false) {
            return 'LOCATE(' . $substr . ', ' . $str . ')';
        }
501 502

        return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
503 504
    }

505 506 507
    /**
     * {@inheritDoc}
     */
508 509 510 511 512
    public function getGuidExpression()
    {
        return 'UUID()';
    }

513 514 515
    /**
     * {@inheritDoc}
     */
516 517 518 519
    public function getRegexpExpression()
    {
        return 'RLIKE';
    }
520
}