DrizzlePlatform.php 13.1 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 22 23 24 25 26 27 28
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Platforms;

use Doctrine\DBAL\DBALException,
    Doctrine\DBAL\Schema\TableDiff,
    Doctrine\DBAL\Schema\Index,
    Doctrine\DBAL\Schema\Table;

/**
 * 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 52 53

    /**
     * {@inheritDoc}
     */    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
    /**
     * {@inheritDoc}
     */
71 72 73 74 75
    public function getDateAddDaysExpression($date, $days)
    {
        return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
    }

76 77 78
    /**
     * {@inheritDoc}
     */
79 80 81 82 83
    public function getDateSubDaysExpression($date, $days)
    {
        return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
    }

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

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

100
    /**
101
     * {@inheritDoc}
102 103 104 105 106 107
     */
    public function getBooleanTypeDeclarationSQL(array $field)
    {
        return 'BOOLEAN';
    }

108 109 110
    /**
     * {@inheritDoc}
     */
111 112 113 114 115
    public function getIntegerTypeDeclarationSQL(array $field)
    {
        return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
    }

116 117 118
    /**
     * {@inheritDoc}
     */
119 120 121 122 123 124 125 126 127
    protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
    {
        $autoinc = '';
        if ( ! empty($columnDef['autoincrement'])) {
            $autoinc = ' AUTO_INCREMENT';
        }
        return $autoinc;
    }

128 129 130
    /**
     * {@inheritDoc}
     */
131 132 133 134 135
    public function getBigIntTypeDeclarationSQL(array $field)
    {
        return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
    }

136 137 138
    /**
     * {@inheritDoc}
     */
139 140 141 142 143
    public function getSmallIntTypeDeclarationSQL(array $field)
    {
        return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
    }

144 145 146
    /**
     * {@inheritDoc}
     */
147 148 149 150 151
    protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
    {
        return $length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)';
    }

152 153 154
    /**
     * {@inheritDoc}
     */
155 156 157 158 159 160
    protected function initializeDoctrineTypeMappings()
    {
        $this->doctrineTypeMapping = array(
            'boolean'       => 'boolean',
            'varchar'       => 'string',
            'integer'       => 'integer',
161 162 163 164 165
            'blob'          => 'text',
            'decimal'       => 'decimal',
            'datetime'      => 'datetime',
            'date'          => 'date',
            'time'          => 'time',
166 167
            'text'          => 'text',
            'timestamp'     => 'datetime',
168 169
            'double'        => 'float',
            'bigint'        => 'bigint',
170 171 172
        );
    }

173 174 175
    /**
     * {@inheritDoc}
     */
176 177 178 179 180 181
    public function getClobTypeDeclarationSQL(array $field)
    {
        return 'TEXT';
    }

    /**
182
     * {@inheritDoc}
183 184 185 186 187 188
     */
    public function getBlobTypeDeclarationSQL(array $field)
    {
        return 'BLOB';
    }

189 190 191
    /**
     * {@inheritDoc}
     */
192 193 194 195 196
    public function getCreateDatabaseSQL($name)
    {
        return 'CREATE DATABASE ' . $name;
    }

197 198 199
    /**
     * {@inheritDoc}
     */
200 201 202 203 204 205 206 207 208 209
    public function getDropDatabaseSQL($name)
    {
        return 'DROP DATABASE ' . $name;
    }

    public function getListDatabasesSQL()
    {
        return "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='LOCAL'";
    }

210 211 212
    /**
     * {@inheritDoc}
     */
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    protected function getReservedKeywordsClass()
    {
        return 'Doctrine\DBAL\Platforms\Keywords\DrizzleKeywords';
    }

    public function getListTablesSQL()
    {
        return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE' AND TABLE_SCHEMA=DATABASE()";
    }

    public function getListTableColumnsSQL($table, $database = null)
    {
        if ($database) {
            $database = "'" . $database . "'";
        } else {
            $database = 'DATABASE()';
        }

231
        return "SELECT COLUMN_NAME, DATA_TYPE, COLUMN_COMMENT, IS_NULLABLE, IS_AUTO_INCREMENT, CHARACTER_MAXIMUM_LENGTH, COLUMN_DEFAULT," .
232
               " NUMERIC_PRECISION, NUMERIC_SCALE" .
233 234 235 236 237 238
               " FROM DATA_DICTIONARY.COLUMNS" .
               " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME = '" . $table . "'";
    }

    public function getListTableForeignKeysSQL($table, $database = null)
    {
239 240 241 242 243 244 245 246 247
        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 . "'";
248 249
    }

250 251 252
    /**
     * {@inheritDoc}
     */
253 254
    public function getListTableIndexesSQL($table, $database = null)
    {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
255 256 257 258 259 260 261 262 263
        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 . "'";
264 265
    }

266 267 268
    /**
     * {@inheritDoc}
     */
269 270 271 272 273
    public function prefersIdentityColumns()
    {
        return true;
    }

274 275 276
    /**
     * {@inheritDoc}
     */
277 278 279 280
    public function supportsIdentityColumns()
    {
        return true;
    }
281

282 283 284
    /**
     * {@inheritDoc}
     */
285 286 287 288
    public function supportsInlineColumnComments()
    {
        return true;
    }
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
289

290 291 292
    /**
     * {@inheritDoc}
     */
293 294 295 296 297
    public function supportsViews()
    {
        return false;
    }

298 299 300
    /**
     * {@inheritDoc}
     */
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
301 302
    public function getDropIndexSQL($index, $table=null)
    {
303
        if ($index instanceof Index) {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
304
            $indexName = $index->getQuotedName($this);
305
        } else if (is_string($index)) {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
306 307 308 309 310
            $indexName = $index;
        } else {
            throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
        }

311
        if ($table instanceof Table) {
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
            $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;
    }

    /**
     * @param Index $index
     * @param Table $table
329 330
     *
     * @return string
Kim Hemsø Rasmussen's avatar
Kim Hemsø Rasmussen committed
331 332 333 334 335 336
     */
    protected function getDropPrimaryKeySQL($table)
    {
        return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
    }

337 338 339
    /**
     * {@inheritDoc}
     */
340 341 342 343 344
    public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
    {
        if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
            return 'TIMESTAMP';
        }
345 346

        return 'DATETIME';
347 348
    }

349 350 351
    /**
     * {@inheritDoc}
     */
352 353 354 355 356
    public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
    {
        return 'TIME';
    }

357 358 359
    /**
     * {@inheritDoc}
     */
360 361 362 363
    public function getDateTypeDeclarationSQL(array $fieldDeclaration)
    {
        return 'DATE';
    }
364

365
    /**
366
     * {@inheritDoc}
367
     */
368 369 370 371 372 373 374 375 376
    public function getAlterTableSQL(TableDiff $diff)
    {
        $columnSql = array();
        $queryParts = array();

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

377
        foreach ($diff->addedColumns as $column) {
378 379 380 381 382 383 384 385 386
            if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
                continue;
            }

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

387
        foreach ($diff->removedColumns as $column) {
388 389 390 391 392 393 394
            if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
                continue;
            }

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

395
        foreach ($diff->changedColumns as $columnDiff) {
396 397 398 399
            if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
                continue;
            }

400
            /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
401 402 403 404 405 406 407
            $column = $columnDiff->column;
            $columnArray = $column->toArray();
            $columnArray['comment'] = $this->getColumnComment($column);
            $queryParts[] =  'CHANGE ' . ($columnDiff->oldColumnName) . ' '
                    . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
        }

408
        foreach ($diff->renamedColumns as $oldColumnName => $column) {
409 410 411 412 413 414 415 416 417 418 419 420 421
            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();

422
        if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
423 424 425 426 427 428 429 430 431 432 433 434
            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);
    }
435

436 437 438
    /**
     * {@inheritDoc}
     */
439 440
    public function getDropTemporaryTableSQL($table)
    {
441
        if ($table instanceof Table) {
442 443 444 445 446 447 448
            $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;
    }
449

450 451 452
    /**
     * {@inheritDoc}
     */
453 454 455 456 457 458 459 460
    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';
                }
            }
461 462
        } else if (is_bool($item) || is_numeric($item)) {
           $item = ($item) ? 'true' : 'false';
463
        }
464

465 466
        return $item;
    }
467

468 469 470
    /**
     * {@inheritDoc}
     */
471 472 473 474 475
    public function getLocateExpression($str, $substr, $startPos = false)
    {
        if ($startPos == false) {
            return 'LOCATE(' . $substr . ', ' . $str . ')';
        }
476 477

        return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
478 479
    }

480 481 482
    /**
     * {@inheritDoc}
     */
483 484 485 486 487
    public function getGuidExpression()
    {
        return 'UUID()';
    }

488 489 490
    /**
     * {@inheritDoc}
     */
491 492 493 494
    public function getRegexpExpression()
    {
        return 'RLIKE';
    }
495
}