Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema / MySqlSchemaManager.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\Schema;
21
22 /**
23  * Schema manager for the MySql RDBMS.
24  *
25  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
26  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
27  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
28  * @author      Roman Borschel <roman@code-factory.org>
29  * @author      Benjamin Eberlei <kontakt@beberlei.de>
30  * @version     $Revision$
31  * @since       2.0
32  */
33 class MySqlSchemaManager extends AbstractSchemaManager
34 {
35     protected function _getPortableViewDefinition($view)
36     {
37         return new View($view['TABLE_NAME'], $view['VIEW_DEFINITION']);
38     }
39
40     protected function _getPortableTableDefinition($table)
41     {
42         return array_shift($table);
43     }
44
45     protected function _getPortableUserDefinition($user)
46     {
47         return array(
48             'user' => $user['User'],
49             'password' => $user['Password'],
50         );
51     }
52
53     protected function _getPortableTableIndexesList($tableIndexes, $tableName=null)
54     {
55         foreach($tableIndexes as $k => $v) {
56             $v = array_change_key_case($v, CASE_LOWER);
57             if($v['key_name'] == 'PRIMARY') {
58                 $v['primary'] = true;
59             } else {
60                 $v['primary'] = false;
61             }
62             if (strpos($v['index_type'], 'FULLTEXT') !== false) {
63                 $v['flags'] = array('FULLTEXT');
64             }
65             $tableIndexes[$k] = $v;
66         }
67
68         return parent::_getPortableTableIndexesList($tableIndexes, $tableName);
69     }
70
71     protected function _getPortableSequenceDefinition($sequence)
72     {
73         return end($sequence);
74     }
75
76     protected function _getPortableDatabaseDefinition($database)
77     {
78         return $database['Database'];
79     }
80
81     /**
82      * Gets a portable column definition.
83      *
84      * The database type is mapped to a corresponding Doctrine mapping type.
85      *
86      * @param $tableColumn
87      * @return array
88      */
89     protected function _getPortableTableColumnDefinition($tableColumn)
90     {
91         $tableColumn = array_change_key_case($tableColumn, CASE_LOWER);
92
93         $dbType = strtolower($tableColumn['type']);
94         $dbType = strtok($dbType, '(), ');
95         if (isset($tableColumn['length'])) {
96             $length = $tableColumn['length'];
97             $decimal = '';
98         } else {
99             $length = strtok('(), ');
100             $decimal = strtok('(), ') ? strtok('(), '):null;
101         }
102         $type = array();
103         $fixed = null;
104
105         if ( ! isset($tableColumn['name'])) {
106             $tableColumn['name'] = '';
107         }
108
109         $scale = null;
110         $precision = null;
111
112         $type = $this->_platform->getDoctrineTypeMapping($dbType);
113         $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type);
114         $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type);
115
116         switch ($dbType) {
117             case 'char':
118                 $fixed = true;
119                 break;
120             case 'float':
121             case 'double':
122             case 'real':
123             case 'numeric':
124             case 'decimal':
125                 if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) {
126                     $precision = $match[1];
127                     $scale = $match[2];
128                     $length = null;
129                 }
130                 break;
131             case 'tinyint':
132             case 'smallint':
133             case 'mediumint':
134             case 'int':
135             case 'integer':
136             case 'bigint':
137             case 'tinyblob':
138             case 'mediumblob':
139             case 'longblob':
140             case 'blob':
141             case 'year':
142                 $length = null;
143                 break;
144         }
145
146         $length = ((int) $length == 0) ? null : (int) $length;
147
148         $options = array(
149             'length'        => $length,
150             'unsigned'      => (bool) (strpos($tableColumn['type'], 'unsigned') !== false),
151             'fixed'         => (bool) $fixed,
152             'default'       => isset($tableColumn['default']) ? $tableColumn['default'] : null,
153             'notnull'       => (bool) ($tableColumn['null'] != 'YES'),
154             'scale'         => null,
155             'precision'     => null,
156             'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false),
157             'comment'       => (isset($tableColumn['comment'])) ? $tableColumn['comment'] : null
158         );
159
160         if ($scale !== null && $precision !== null) {
161             $options['scale'] = $scale;
162             $options['precision'] = $precision;
163         }
164
165         return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options);
166     }
167
168     protected function _getPortableTableForeignKeysList($tableForeignKeys)
169     {
170         $list = array();
171         foreach ($tableForeignKeys as $key => $value) {
172             $value = array_change_key_case($value, CASE_LOWER);
173             if (!isset($list[$value['constraint_name']])) {
174                 if (!isset($value['delete_rule']) || $value['delete_rule'] == "RESTRICT") {
175                     $value['delete_rule'] = null;
176                 }
177                 if (!isset($value['update_rule']) || $value['update_rule'] == "RESTRICT") {
178                     $value['update_rule'] = null;
179                 }
180
181                 $list[$value['constraint_name']] = array(
182                     'name' => $value['constraint_name'],
183                     'local' => array(),
184                     'foreign' => array(),
185                     'foreignTable' => $value['referenced_table_name'],
186                     'onDelete' => $value['delete_rule'],
187                     'onUpdate' => $value['update_rule'],
188                 );
189             }
190             $list[$value['constraint_name']]['local'][] = $value['column_name'];
191             $list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name'];
192         }
193
194         $result = array();
195         foreach($list as $constraint) {
196             $result[] = new ForeignKeyConstraint(
197                 array_values($constraint['local']), $constraint['foreignTable'],
198                 array_values($constraint['foreign']), $constraint['name'],
199                 array(
200                     'onDelete' => $constraint['onDelete'],
201                     'onUpdate' => $constraint['onUpdate'],
202                 )
203             );
204         }
205
206         return $result;
207     }
208 }