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 LGPL. 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 | $tableIndexes[$k] = $v; |
---|
63 | } |
---|
64 | |
---|
65 | return parent::_getPortableTableIndexesList($tableIndexes, $tableName); |
---|
66 | } |
---|
67 | |
---|
68 | protected function _getPortableSequenceDefinition($sequence) |
---|
69 | { |
---|
70 | return end($sequence); |
---|
71 | } |
---|
72 | |
---|
73 | protected function _getPortableDatabaseDefinition($database) |
---|
74 | { |
---|
75 | return $database['Database']; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Gets a portable column definition. |
---|
80 | * |
---|
81 | * The database type is mapped to a corresponding Doctrine mapping type. |
---|
82 | * |
---|
83 | * @param $tableColumn |
---|
84 | * @return array |
---|
85 | */ |
---|
86 | protected function _getPortableTableColumnDefinition($tableColumn) |
---|
87 | { |
---|
88 | $tableColumn = array_change_key_case($tableColumn, CASE_LOWER); |
---|
89 | |
---|
90 | $dbType = strtolower($tableColumn['type']); |
---|
91 | $dbType = strtok($dbType, '(), '); |
---|
92 | if (isset($tableColumn['length'])) { |
---|
93 | $length = $tableColumn['length']; |
---|
94 | $decimal = ''; |
---|
95 | } else { |
---|
96 | $length = strtok('(), '); |
---|
97 | $decimal = strtok('(), ') ? strtok('(), '):null; |
---|
98 | } |
---|
99 | $type = array(); |
---|
100 | $unsigned = $fixed = null; |
---|
101 | |
---|
102 | if ( ! isset($tableColumn['name'])) { |
---|
103 | $tableColumn['name'] = ''; |
---|
104 | } |
---|
105 | |
---|
106 | $scale = null; |
---|
107 | $precision = null; |
---|
108 | |
---|
109 | $type = $this->_platform->getDoctrineTypeMapping($dbType); |
---|
110 | $type = $this->extractDoctrineTypeFromComment($tableColumn['comment'], $type); |
---|
111 | $tableColumn['comment'] = $this->removeDoctrineTypeFromComment($tableColumn['comment'], $type); |
---|
112 | |
---|
113 | switch ($dbType) { |
---|
114 | case 'char': |
---|
115 | $fixed = true; |
---|
116 | break; |
---|
117 | case 'float': |
---|
118 | case 'double': |
---|
119 | case 'real': |
---|
120 | case 'numeric': |
---|
121 | case 'decimal': |
---|
122 | if(preg_match('([A-Za-z]+\(([0-9]+)\,([0-9]+)\))', $tableColumn['type'], $match)) { |
---|
123 | $precision = $match[1]; |
---|
124 | $scale = $match[2]; |
---|
125 | $length = null; |
---|
126 | } |
---|
127 | break; |
---|
128 | case 'tinyint': |
---|
129 | case 'smallint': |
---|
130 | case 'mediumint': |
---|
131 | case 'int': |
---|
132 | case 'integer': |
---|
133 | case 'bigint': |
---|
134 | case 'tinyblob': |
---|
135 | case 'mediumblob': |
---|
136 | case 'longblob': |
---|
137 | case 'blob': |
---|
138 | case 'binary': |
---|
139 | case 'varbinary': |
---|
140 | case 'year': |
---|
141 | $length = null; |
---|
142 | break; |
---|
143 | } |
---|
144 | |
---|
145 | $length = ((int) $length == 0) ? null : (int) $length; |
---|
146 | $def = array( |
---|
147 | 'type' => $type, |
---|
148 | 'length' => $length, |
---|
149 | 'unsigned' => (bool) $unsigned, |
---|
150 | 'fixed' => (bool) $fixed |
---|
151 | ); |
---|
152 | |
---|
153 | $options = array( |
---|
154 | 'length' => $length, |
---|
155 | 'unsigned' => (bool)$unsigned, |
---|
156 | 'fixed' => (bool)$fixed, |
---|
157 | 'default' => isset($tableColumn['default']) ? $tableColumn['default'] : null, |
---|
158 | 'notnull' => (bool) ($tableColumn['null'] != 'YES'), |
---|
159 | 'scale' => null, |
---|
160 | 'precision' => null, |
---|
161 | 'autoincrement' => (bool) (strpos($tableColumn['extra'], 'auto_increment') !== false), |
---|
162 | 'comment' => (isset($tableColumn['comment'])) ? $tableColumn['comment'] : null |
---|
163 | ); |
---|
164 | |
---|
165 | if ($scale !== null && $precision !== null) { |
---|
166 | $options['scale'] = $scale; |
---|
167 | $options['precision'] = $precision; |
---|
168 | } |
---|
169 | |
---|
170 | return new Column($tableColumn['field'], \Doctrine\DBAL\Types\Type::getType($type), $options); |
---|
171 | } |
---|
172 | |
---|
173 | protected function _getPortableTableForeignKeysList($tableForeignKeys) |
---|
174 | { |
---|
175 | $list = array(); |
---|
176 | foreach ($tableForeignKeys as $key => $value) { |
---|
177 | $value = array_change_key_case($value, CASE_LOWER); |
---|
178 | if (!isset($list[$value['constraint_name']])) { |
---|
179 | if (!isset($value['delete_rule']) || $value['delete_rule'] == "RESTRICT") { |
---|
180 | $value['delete_rule'] = null; |
---|
181 | } |
---|
182 | if (!isset($value['update_rule']) || $value['update_rule'] == "RESTRICT") { |
---|
183 | $value['update_rule'] = null; |
---|
184 | } |
---|
185 | |
---|
186 | $list[$value['constraint_name']] = array( |
---|
187 | 'name' => $value['constraint_name'], |
---|
188 | 'local' => array(), |
---|
189 | 'foreign' => array(), |
---|
190 | 'foreignTable' => $value['referenced_table_name'], |
---|
191 | 'onDelete' => $value['delete_rule'], |
---|
192 | 'onUpdate' => $value['update_rule'], |
---|
193 | ); |
---|
194 | } |
---|
195 | $list[$value['constraint_name']]['local'][] = $value['column_name']; |
---|
196 | $list[$value['constraint_name']]['foreign'][] = $value['referenced_column_name']; |
---|
197 | } |
---|
198 | |
---|
199 | $result = array(); |
---|
200 | foreach($list AS $constraint) { |
---|
201 | $result[] = new ForeignKeyConstraint( |
---|
202 | array_values($constraint['local']), $constraint['foreignTable'], |
---|
203 | array_values($constraint['foreign']), $constraint['name'], |
---|
204 | array( |
---|
205 | 'onDelete' => $constraint['onDelete'], |
---|
206 | 'onUpdate' => $constraint['onUpdate'], |
---|
207 | ) |
---|
208 | ); |
---|
209 | } |
---|
210 | |
---|
211 | return $result; |
---|
212 | } |
---|
213 | } |
---|