Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Platforms / DrizzlePlatform.php
1 <?php
2 /*
3  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14  *
15  * This software consists of voluntary contributions made by many individuals
16  * and is licensed under the MIT license. For more information, see
17  * <http://www.doctrine-project.org>.
18  */
19
20 namespace Doctrine\DBAL\Platforms;
21
22 use Doctrine\DBAL\DBALException,
23     Doctrine\DBAL\Schema\TableDiff,
24     Doctrine\DBAL\Schema\Index,
25     Doctrine\DBAL\Schema\Table;
26
27 /**
28  * Drizzle platform
29  *
30  * @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
31  */
32 class DrizzlePlatform extends AbstractPlatform
33 {
34     /**
35      * {@inheritDoc}
36      */
37     public function getName()
38     {
39         return 'drizzle';
40     }
41
42     /**
43      * {@inheritDoc}
44      */
45     public function getIdentifierQuoteCharacter()
46     {
47         return '`';
48     }
49
50
51     /**
52      * {@inheritDoc}
53      */    public function getConcatExpression()
54     {
55         $args = func_get_args();
56
57         return 'CONCAT(' . join(', ', (array) $args) . ')';
58     }
59
60     /**
61      * {@inheritDoc}
62      */
63     public function getDateDiffExpression($date1, $date2)
64     {
65         return 'DATEDIFF(' . $date1 . ', ' . $date2 . ')';
66     }
67
68     /**
69      * {@inheritDoc}
70      */
71     public function getDateAddDaysExpression($date, $days)
72     {
73         return 'DATE_ADD(' . $date . ', INTERVAL ' . $days . ' DAY)';
74     }
75
76     /**
77      * {@inheritDoc}
78      */
79     public function getDateSubDaysExpression($date, $days)
80     {
81         return 'DATE_SUB(' . $date . ', INTERVAL ' . $days . ' DAY)';
82     }
83
84     /**
85      * {@inheritDoc}
86      */
87     public function getDateAddMonthExpression($date, $months)
88     {
89         return 'DATE_ADD(' . $date . ', INTERVAL ' . $months . ' MONTH)';
90     }
91
92     /**
93      * {@inheritDoc}
94      */
95     public function getDateSubMonthExpression($date, $months)
96     {
97         return 'DATE_SUB(' . $date . ', INTERVAL ' . $months . ' MONTH)';
98     }
99
100     /**
101      * {@inheritDoc}
102      */
103     public function getBooleanTypeDeclarationSQL(array $field)
104     {
105         return 'BOOLEAN';
106     }
107
108     /**
109      * {@inheritDoc}
110      */
111     public function getIntegerTypeDeclarationSQL(array $field)
112     {
113         return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
114     }
115
116     /**
117      * {@inheritDoc}
118      */
119     protected function _getCommonIntegerTypeDeclarationSQL(array $columnDef)
120     {
121         $autoinc = '';
122         if ( ! empty($columnDef['autoincrement'])) {
123             $autoinc = ' AUTO_INCREMENT';
124         }
125         return $autoinc;
126     }
127
128     /**
129      * {@inheritDoc}
130      */
131     public function getBigIntTypeDeclarationSQL(array $field)
132     {
133         return 'BIGINT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
134     }
135
136     /**
137      * {@inheritDoc}
138      */
139     public function getSmallIntTypeDeclarationSQL(array $field)
140     {
141         return 'INT' . $this->_getCommonIntegerTypeDeclarationSQL($field);
142     }
143
144     /**
145      * {@inheritDoc}
146      */
147     protected function getVarcharTypeDeclarationSQLSnippet($length, $fixed)
148     {
149         return $length ? 'VARCHAR(' . $length . ')' : 'VARCHAR(255)';
150     }
151
152     /**
153      * {@inheritDoc}
154      */
155     protected function initializeDoctrineTypeMappings()
156     {
157         $this->doctrineTypeMapping = array(
158             'boolean'       => 'boolean',
159             'varchar'       => 'string',
160             'integer'       => 'integer',
161             'blob'          => 'text',
162             'decimal'       => 'decimal',
163             'datetime'      => 'datetime',
164             'date'          => 'date',
165             'time'          => 'time',
166             'text'          => 'text',
167             'timestamp'     => 'datetime',
168             'double'        => 'float',
169             'bigint'        => 'bigint',
170         );
171     }
172
173     /**
174      * {@inheritDoc}
175      */
176     public function getClobTypeDeclarationSQL(array $field)
177     {
178         return 'TEXT';
179     }
180
181     /**
182      * {@inheritDoc}
183      */
184     public function getBlobTypeDeclarationSQL(array $field)
185     {
186         return 'BLOB';
187     }
188
189     /**
190      * {@inheritDoc}
191      */
192     public function getCreateDatabaseSQL($name)
193     {
194         return 'CREATE DATABASE ' . $name;
195     }
196
197     /**
198      * {@inheritDoc}
199      */
200     public function getDropDatabaseSQL($name)
201     {
202         return 'DROP DATABASE ' . $name;
203     }
204
205     public function getListDatabasesSQL()
206     {
207         return "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE CATALOG_NAME='LOCAL'";
208     }
209
210     /**
211      * {@inheritDoc}
212      */
213     protected function getReservedKeywordsClass()
214     {
215         return 'Doctrine\DBAL\Platforms\Keywords\DrizzleKeywords';
216     }
217
218     public function getListTablesSQL()
219     {
220         return "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE' AND TABLE_SCHEMA=DATABASE()";
221     }
222
223     public function getListTableColumnsSQL($table, $database = null)
224     {
225         if ($database) {
226             $database = "'" . $database . "'";
227         } else {
228             $database = 'DATABASE()';
229         }
230
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                " FROM DATA_DICTIONARY.COLUMNS" .
234                " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME = '" . $table . "'";
235     }
236
237     public function getListTableForeignKeysSQL($table, $database = null)
238     {
239         if ($database) {
240             $database = "'" . $database . "'";
241         } else {
242             $database = 'DATABASE()';
243         }
244
245         return "SELECT CONSTRAINT_NAME, CONSTRAINT_COLUMNS, REFERENCED_TABLE_NAME, REFERENCED_TABLE_COLUMNS, UPDATE_RULE, DELETE_RULE" .
246                " FROM DATA_DICTIONARY.FOREIGN_KEYS" .
247                " WHERE CONSTRAINT_SCHEMA=" . $database . " AND CONSTRAINT_TABLE='" . $table . "'";
248     }
249
250     /**
251      * {@inheritDoc}
252      */
253     public function getListTableIndexesSQL($table, $database = null)
254     {
255         if ($database) {
256             $database = "'" . $database . "'";
257         } else {
258             $database = 'DATABASE()';
259         }
260
261         return "SELECT INDEX_NAME AS 'key_name', COLUMN_NAME AS 'column_name', IS_USED_IN_PRIMARY AS 'primary', IS_UNIQUE=0 AS 'non_unique'" .
262                " FROM DATA_DICTIONARY.INDEX_PARTS" .
263                " WHERE TABLE_SCHEMA=" . $database . " AND TABLE_NAME='" . $table . "'";
264     }
265
266     /**
267      * {@inheritDoc}
268      */
269     public function prefersIdentityColumns()
270     {
271         return true;
272     }
273
274     /**
275      * {@inheritDoc}
276      */
277     public function supportsIdentityColumns()
278     {
279         return true;
280     }
281
282     /**
283      * {@inheritDoc}
284      */
285     public function supportsInlineColumnComments()
286     {
287         return true;
288     }
289
290     /**
291      * {@inheritDoc}
292      */
293     public function supportsViews()
294     {
295         return false;
296     }
297
298     /**
299      * {@inheritDoc}
300      */
301     public function getDropIndexSQL($index, $table=null)
302     {
303         if ($index instanceof Index) {
304             $indexName = $index->getQuotedName($this);
305         } else if (is_string($index)) {
306             $indexName = $index;
307         } else {
308             throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $index parameter to be string or \Doctrine\DBAL\Schema\Index.');
309         }
310
311         if ($table instanceof Table) {
312             $table = $table->getQuotedName($this);
313         } else if(!is_string($table)) {
314             throw new \InvalidArgumentException('DrizzlePlatform::getDropIndexSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
315         }
316
317         if ($index instanceof Index && $index->isPrimary()) {
318             // drizzle primary keys are always named "PRIMARY",
319             // so we cannot use them in statements because of them being keyword.
320             return $this->getDropPrimaryKeySQL($table);
321         }
322
323         return 'DROP INDEX ' . $indexName . ' ON ' . $table;
324     }
325
326     /**
327      * @param Index $index
328      * @param Table $table
329      *
330      * @return string
331      */
332     protected function getDropPrimaryKeySQL($table)
333     {
334         return 'ALTER TABLE ' . $table . ' DROP PRIMARY KEY';
335     }
336
337     /**
338      * {@inheritDoc}
339      */
340     public function getDateTimeTypeDeclarationSQL(array $fieldDeclaration)
341     {
342         if (isset($fieldDeclaration['version']) && $fieldDeclaration['version'] == true) {
343             return 'TIMESTAMP';
344         }
345
346         return 'DATETIME';
347     }
348
349     /**
350      * {@inheritDoc}
351      */
352     public function getTimeTypeDeclarationSQL(array $fieldDeclaration)
353     {
354         return 'TIME';
355     }
356
357     /**
358      * {@inheritDoc}
359      */
360     public function getDateTypeDeclarationSQL(array $fieldDeclaration)
361     {
362         return 'DATE';
363     }
364
365     /**
366      * {@inheritDoc}
367      */
368     public function getAlterTableSQL(TableDiff $diff)
369     {
370         $columnSql = array();
371         $queryParts = array();
372
373         if ($diff->newName !== false) {
374             $queryParts[] =  'RENAME TO ' . $diff->newName;
375         }
376
377         foreach ($diff->addedColumns as $column) {
378             if ($this->onSchemaAlterTableAddColumn($column, $diff, $columnSql)) {
379                 continue;
380             }
381
382             $columnArray = $column->toArray();
383             $columnArray['comment'] = $this->getColumnComment($column);
384             $queryParts[] = 'ADD ' . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
385         }
386
387         foreach ($diff->removedColumns as $column) {
388             if ($this->onSchemaAlterTableRemoveColumn($column, $diff, $columnSql)) {
389                 continue;
390             }
391
392             $queryParts[] =  'DROP ' . $column->getQuotedName($this);
393         }
394
395         foreach ($diff->changedColumns as $columnDiff) {
396             if ($this->onSchemaAlterTableChangeColumn($columnDiff, $diff, $columnSql)) {
397                 continue;
398             }
399
400             /* @var $columnDiff \Doctrine\DBAL\Schema\ColumnDiff */
401             $column = $columnDiff->column;
402             $columnArray = $column->toArray();
403             $columnArray['comment'] = $this->getColumnComment($column);
404             $queryParts[] =  'CHANGE ' . ($columnDiff->oldColumnName) . ' '
405                     . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
406         }
407
408         foreach ($diff->renamedColumns as $oldColumnName => $column) {
409             if ($this->onSchemaAlterTableRenameColumn($oldColumnName, $column, $diff, $columnSql)) {
410                 continue;
411             }
412
413             $columnArray = $column->toArray();
414             $columnArray['comment'] = $this->getColumnComment($column);
415             $queryParts[] =  'CHANGE ' . $oldColumnName . ' '
416                     . $this->getColumnDeclarationSQL($column->getQuotedName($this), $columnArray);
417         }
418
419         $sql = array();
420         $tableSql = array();
421
422         if ( ! $this->onSchemaAlterTable($diff, $tableSql)) {
423             if (count($queryParts) > 0) {
424                 $sql[] = 'ALTER TABLE ' . $diff->name . ' ' . implode(", ", $queryParts);
425             }
426             $sql = array_merge(
427                 $this->getPreAlterTableIndexForeignKeySQL($diff),
428                 $sql,
429                 $this->getPostAlterTableIndexForeignKeySQL($diff)
430             );
431         }
432
433         return array_merge($sql, $tableSql, $columnSql);
434     }
435
436     /**
437      * {@inheritDoc}
438      */
439     public function getDropTemporaryTableSQL($table)
440     {
441         if ($table instanceof Table) {
442             $table = $table->getQuotedName($this);
443         } else if(!is_string($table)) {
444             throw new \InvalidArgumentException('getDropTableSQL() expects $table parameter to be string or \Doctrine\DBAL\Schema\Table.');
445         }
446
447         return 'DROP TEMPORARY TABLE ' . $table;
448     }
449
450     /**
451      * {@inheritDoc}
452      */
453     public function convertBooleans($item)
454     {
455         if (is_array($item)) {
456             foreach ($item as $key => $value) {
457                 if (is_bool($value) || is_numeric($item)) {
458                     $item[$key] = ($value) ? 'true' : 'false';
459                 }
460             }
461         } else if (is_bool($item) || is_numeric($item)) {
462            $item = ($item) ? 'true' : 'false';
463         }
464
465         return $item;
466     }
467
468     /**
469      * {@inheritDoc}
470      */
471     public function getLocateExpression($str, $substr, $startPos = false)
472     {
473         if ($startPos == false) {
474             return 'LOCATE(' . $substr . ', ' . $str . ')';
475         }
476
477         return 'LOCATE(' . $substr . ', ' . $str . ', '.$startPos.')';
478     }
479
480     /**
481      * {@inheritDoc}
482      */
483     public function getGuidExpression()
484     {
485         return 'UUID()';
486     }
487
488     /**
489      * {@inheritDoc}
490      */
491     public function getRegexpExpression()
492     {
493         return 'RLIKE';
494     }
495 }