1 |
<?php
|
2 |
/*
|
3 |
* $Id: Mysql.php 2702 2007-10-03 21:43:22Z Jonathan.Wage $
|
4 |
*
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
16 |
*
|
17 |
* This software consists of voluntary contributions made by many individuals
|
18 |
* and is licensed under the LGPL. For more information, see
|
19 |
* <http://www.phpdoctrine.com>.
|
20 |
*/
|
21 |
Doctrine::autoload('Doctrine_DataDict');
|
22 |
/**
|
23 |
* @package Doctrine
|
24 |
* @subpackage DataDict
|
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 |
* @version $Revision: 2702 $
|
29 |
* @link www.phpdoctrine.com
|
30 |
* @since 1.0
|
31 |
*/
|
32 |
class Doctrine_DataDict_Mysql extends Doctrine_DataDict
|
33 |
{
|
34 |
protected $keywords = array(
|
35 |
'ADD', 'ALL', 'ALTER',
|
36 |
'ANALYZE', 'AND', 'AS',
|
37 |
'ASC', 'ASENSITIVE', 'BEFORE',
|
38 |
'BETWEEN', 'BIGINT', 'BINARY',
|
39 |
'BLOB', 'BOTH', 'BY',
|
40 |
'CALL', 'CASCADE', 'CASE',
|
41 |
'CHANGE', 'CHAR', 'CHARACTER',
|
42 |
'CHECK', 'COLLATE', 'COLUMN',
|
43 |
'CONDITION', 'CONNECTION', 'CONSTRAINT',
|
44 |
'CONTINUE', 'CONVERT', 'CREATE',
|
45 |
'CROSS', 'CURRENT_DATE', 'CURRENT_TIME',
|
46 |
'CURRENT_TIMESTAMP', 'CURRENT_USER', 'CURSOR',
|
47 |
'DATABASE', 'DATABASES', 'DAY_HOUR',
|
48 |
'DAY_MICROSECOND', 'DAY_MINUTE', 'DAY_SECOND',
|
49 |
'DEC', 'DECIMAL', 'DECLARE',
|
50 |
'DEFAULT', 'DELAYED', 'DELETE',
|
51 |
'DESC', 'DESCRIBE', 'DETERMINISTIC',
|
52 |
'DISTINCT', 'DISTINCTROW', 'DIV',
|
53 |
'DOUBLE', 'DROP', 'DUAL',
|
54 |
'EACH', 'ELSE', 'ELSEIF',
|
55 |
'ENCLOSED', 'ESCAPED', 'EXISTS',
|
56 |
'EXIT', 'EXPLAIN', 'FALSE',
|
57 |
'FETCH', 'FLOAT', 'FLOAT4',
|
58 |
'FLOAT8', 'FOR', 'FORCE',
|
59 |
'FOREIGN', 'FROM', 'FULLTEXT',
|
60 |
'GRANT', 'GROUP', 'HAVING',
|
61 |
'HIGH_PRIORITY', 'HOUR_MICROSECOND', 'HOUR_MINUTE',
|
62 |
'HOUR_SECOND', 'IF', 'IGNORE',
|
63 |
'IN', 'INDEX', 'INFILE',
|
64 |
'INNER', 'INOUT', 'INSENSITIVE',
|
65 |
'INSERT', 'INT', 'INT1',
|
66 |
'INT2', 'INT3', 'INT4',
|
67 |
'INT8', 'INTEGER', 'INTERVAL',
|
68 |
'INTO', 'IS', 'ITERATE',
|
69 |
'JOIN', 'KEY', 'KEYS',
|
70 |
'KILL', 'LEADING', 'LEAVE',
|
71 |
'LEFT', 'LIKE', 'LIMIT',
|
72 |
'LINES', 'LOAD', 'LOCALTIME',
|
73 |
'LOCALTIMESTAMP', 'LOCK', 'LONG',
|
74 |
'LONGBLOB', 'LONGTEXT', 'LOOP',
|
75 |
'LOW_PRIORITY', 'MATCH', 'MEDIUMBLOB',
|
76 |
'MEDIUMINT', 'MEDIUMTEXT', 'MIDDLEINT',
|
77 |
'MINUTE_MICROSECOND', 'MINUTE_SECOND', 'MOD',
|
78 |
'MODIFIES', 'NATURAL', 'NOT',
|
79 |
'NO_WRITE_TO_BINLOG', 'NULL', 'NUMERIC',
|
80 |
'ON', 'OPTIMIZE', 'OPTION',
|
81 |
'OPTIONALLY', 'OR', 'ORDER',
|
82 |
'OUT', 'OUTER', 'OUTFILE',
|
83 |
'PRECISION', 'PRIMARY', 'PROCEDURE',
|
84 |
'PURGE', 'RAID0', 'READ',
|
85 |
'READS', 'REAL', 'REFERENCES',
|
86 |
'REGEXP', 'RELEASE', 'RENAME',
|
87 |
'REPEAT', 'REPLACE', 'REQUIRE',
|
88 |
'RESTRICT', 'RETURN', 'REVOKE',
|
89 |
'RIGHT', 'RLIKE', 'SCHEMA',
|
90 |
'SCHEMAS', 'SECOND_MICROSECOND', 'SELECT',
|
91 |
'SENSITIVE', 'SEPARATOR', 'SET',
|
92 |
'SHOW', 'SMALLINT', 'SONAME',
|
93 |
'SPATIAL', 'SPECIFIC', 'SQL',
|
94 |
'SQLEXCEPTION', 'SQLSTATE', 'SQLWARNING',
|
95 |
'SQL_BIG_RESULT', 'SQL_CALC_FOUND_ROWS', 'SQL_SMALL_RESULT',
|
96 |
'SSL', 'STARTING', 'STRAIGHT_JOIN',
|
97 |
'TABLE', 'TERMINATED', 'THEN',
|
98 |
'TINYBLOB', 'TINYINT', 'TINYTEXT',
|
99 |
'TO', 'TRAILING', 'TRIGGER',
|
100 |
'TRUE', 'UNDO', 'UNION',
|
101 |
'UNIQUE', 'UNLOCK', 'UNSIGNED',
|
102 |
'UPDATE', 'USAGE', 'USE',
|
103 |
'USING', 'UTC_DATE', 'UTC_TIME',
|
104 |
'UTC_TIMESTAMP', 'VALUES', 'VARBINARY',
|
105 |
'VARCHAR', 'VARCHARACTER', 'VARYING',
|
106 |
'WHEN', 'WHERE', 'WHILE',
|
107 |
'WITH', 'WRITE', 'X509',
|
108 |
'XOR', 'YEAR_MONTH', 'ZEROFILL'
|
109 |
);
|
110 |
|
111 |
/**
|
112 |
* Obtain DBMS specific SQL code portion needed to declare an text type
|
113 |
* field to be used in statements like CREATE TABLE.
|
114 |
*
|
115 |
* @param array $field associative array with the name of the properties
|
116 |
* of the field being declared as array indexes. Currently, the types
|
117 |
* of supported field properties are as follows:
|
118 |
*
|
119 |
* length
|
120 |
* Integer value that determines the maximum length of the text
|
121 |
* field. If this argument is missing the field should be
|
122 |
* declared to have the longest length allowed by the DBMS.
|
123 |
*
|
124 |
* default
|
125 |
* Text value to be used as default for this field.
|
126 |
*
|
127 |
* notnull
|
128 |
* Boolean flag that indicates whether this field is constrained
|
129 |
* to not be set to null.
|
130 |
*
|
131 |
* @return string DBMS specific SQL code portion that should be used to
|
132 |
* declare the specified field.
|
133 |
*/
|
134 |
public function getNativeDeclaration($field)
|
135 |
{
|
136 |
if ( ! isset($field['type'])) {
|
137 |
throw new Doctrine_DataDict_Exception('Missing column type.');
|
138 |
}
|
139 |
|
140 |
switch ($field['type']) {
|
141 |
case 'char':
|
142 |
$length = ( ! empty($field['length'])) ? $field['length'] : false;
|
143 |
|
144 |
return $length ? 'CHAR('.$length.')' : 'CHAR(255)';
|
145 |
case 'varchar':
|
146 |
case 'array':
|
147 |
case 'object':
|
148 |
case 'string':
|
149 |
case 'gzip':
|
150 |
if ( ! isset($field['length'])) {
|
151 |
if (array_key_exists('default', $field)) {
|
152 |
$field['length'] = $this->conn->varchar_max_length;
|
153 |
} else {
|
154 |
$field['length'] = false;
|
155 |
}
|
156 |
}
|
157 |
|
158 |
$length = ($field['length'] <= $this->conn->varchar_max_length) ? $field['length'] : false;
|
159 |
$fixed = (isset($field['fixed'])) ? $field['fixed'] : false;
|
160 |
|
161 |
return $fixed ? ($length ? 'CHAR(' . $length . ')' : 'CHAR(255)')
|
162 |
: ($length ? 'VARCHAR(' . $length . ')' : 'TEXT');
|
163 |
case 'clob':
|
164 |
if ( ! empty($field['length'])) {
|
165 |
$length = $field['length'];
|
166 |
if ($length <= 255) {
|
167 |
return 'TINYTEXT';
|
168 |
} elseif ($length <= 65532) {
|
169 |
return 'TEXT';
|
170 |
} elseif ($length <= 16777215) {
|
171 |
return 'MEDIUMTEXT';
|
172 |
}
|
173 |
}
|
174 |
return 'LONGTEXT';
|
175 |
case 'blob':
|
176 |
if ( ! empty($field['length'])) {
|
177 |
$length = $field['length'];
|
178 |
if ($length <= 255) {
|
179 |
return 'TINYBLOB';
|
180 |
} elseif ($length <= 65532) {
|
181 |
return 'BLOB';
|
182 |
} elseif ($length <= 16777215) {
|
183 |
return 'MEDIUMBLOB';
|
184 |
}
|
185 |
}
|
186 |
return 'LONGBLOB';
|
187 |
case 'enum':
|
188 |
if ($this->conn->getAttribute(Doctrine::ATTR_USE_NATIVE_ENUM)) {
|
189 |
$values = array();
|
190 |
foreach ($field['values'] as $value) {
|
191 |
$values[] = $this->conn->quote($value, 'varchar');
|
192 |
}
|
193 |
return 'ENUM('.implode(', ', $values).')';
|
194 |
}
|
195 |
// fall back to integer
|
196 |
case 'integer':
|
197 |
case 'int':
|
198 |
if ( ! empty($field['length'])) {
|
199 |
$length = $field['length'];
|
200 |
if ($length <= 1) {
|
201 |
return 'TINYINT';
|
202 |
} elseif ($length == 2) {
|
203 |
return 'SMALLINT';
|
204 |
} elseif ($length == 3) {
|
205 |
return 'MEDIUMINT';
|
206 |
} elseif ($length == 4) {
|
207 |
return 'INT';
|
208 |
} elseif ($length > 4) {
|
209 |
return 'BIGINT';
|
210 |
}
|
211 |
}
|
212 |
return 'INT';
|
213 |
case 'boolean':
|
214 |
return 'TINYINT(1)';
|
215 |
case 'date':
|
216 |
return 'DATE';
|
217 |
case 'time':
|
218 |
return 'TIME';
|
219 |
case 'timestamp':
|
220 |
return 'DATETIME';
|
221 |
case 'float':
|
222 |
case 'double':
|
223 |
return 'DOUBLE';
|
224 |
case 'decimal':
|
225 |
$length = !empty($field['length']) ? $field['length'] : 18;
|
226 |
$scale = !empty($field['scale']) ? $field['scale'] : $this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
|
227 |
return 'DECIMAL('.$length.','.$scale.')';
|
228 |
}
|
229 |
throw new Doctrine_DataDict_Exception('Unknown field type \'' . $field['type'] . '\'.');
|
230 |
}
|
231 |
/**
|
232 |
* Maps a native array description of a field to a MDB2 datatype and length
|
233 |
*
|
234 |
* @param array $field native field description
|
235 |
* @return array containing the various possible types, length, sign, fixed
|
236 |
*/
|
237 |
public function getPortableDeclaration(array $field)
|
238 |
{
|
239 |
$dbType = strtolower($field['type']);
|
240 |
$dbType = strtok($dbType, '(), ');
|
241 |
if ($dbType == 'national') {
|
242 |
$dbType = strtok('(), ');
|
243 |
}
|
244 |
if (isset($field['length'])) {
|
245 |
$length = $field['length'];
|
246 |
$decimal = '';
|
247 |
} else {
|
248 |
$length = strtok('(), ');
|
249 |
$decimal = strtok('(), ');
|
250 |
}
|
251 |
$type = array();
|
252 |
$unsigned = $fixed = null;
|
253 |
|
254 |
if ( ! isset($field['name'])) {
|
255 |
$field['name'] = '';
|
256 |
}
|
257 |
|
258 |
$values = null;
|
259 |
|
260 |
switch ($dbType) {
|
261 |
case 'tinyint':
|
262 |
$type[] = 'integer';
|
263 |
$type[] = 'boolean';
|
264 |
if (preg_match('/^(is|has)/', $field['name'])) {
|
265 |
$type = array_reverse($type);
|
266 |
}
|
267 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
268 |
$length = 1;
|
269 |
break;
|
270 |
case 'smallint':
|
271 |
$type[] = 'integer';
|
272 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
273 |
$length = 2;
|
274 |
break;
|
275 |
case 'mediumint':
|
276 |
$type[] = 'integer';
|
277 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
278 |
$length = 3;
|
279 |
break;
|
280 |
case 'int':
|
281 |
case 'integer':
|
282 |
$type[] = 'integer';
|
283 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
284 |
$length = 4;
|
285 |
break;
|
286 |
case 'bigint':
|
287 |
$type[] = 'integer';
|
288 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
289 |
$length = 8;
|
290 |
break;
|
291 |
case 'tinytext':
|
292 |
case 'mediumtext':
|
293 |
case 'longtext':
|
294 |
case 'text':
|
295 |
case 'text':
|
296 |
case 'varchar':
|
297 |
$fixed = false;
|
298 |
case 'string':
|
299 |
case 'char':
|
300 |
$type[] = 'string';
|
301 |
if ($length == '1') {
|
302 |
$type[] = 'boolean';
|
303 |
if (preg_match('/^(is|has)/', $field['name'])) {
|
304 |
$type = array_reverse($type);
|
305 |
}
|
306 |
} elseif (strstr($dbType, 'text')) {
|
307 |
$type[] = 'clob';
|
308 |
if ($decimal == 'binary') {
|
309 |
$type[] = 'blob';
|
310 |
}
|
311 |
}
|
312 |
if ($fixed !== false) {
|
313 |
$fixed = true;
|
314 |
}
|
315 |
break;
|
316 |
case 'enum':
|
317 |
$type[] = 'enum';
|
318 |
preg_match_all('/\'.+\'/U', $field['type'], $matches);
|
319 |
$length = 0;
|
320 |
$fixed = false;
|
321 |
if (is_array($matches)) {
|
322 |
foreach ($matches[0] as $value) {
|
323 |
$length = max($length, strlen($value)-2);
|
324 |
}
|
325 |
if ($length == '1' && count($matches[0]) == 2) {
|
326 |
$type[] = 'boolean';
|
327 |
if (preg_match('/^(is|has)/', $field['name'])) {
|
328 |
$type = array_reverse($type);
|
329 |
}
|
330 |
} else {
|
331 |
$values = $matches[0];
|
332 |
}
|
333 |
}
|
334 |
$type[] = 'integer';
|
335 |
case 'set':
|
336 |
$fixed = false;
|
337 |
$type[] = 'text';
|
338 |
$type[] = 'integer';
|
339 |
break;
|
340 |
case 'date':
|
341 |
$type[] = 'date';
|
342 |
$length = null;
|
343 |
break;
|
344 |
case 'datetime':
|
345 |
case 'timestamp':
|
346 |
$type[] = 'timestamp';
|
347 |
$length = null;
|
348 |
break;
|
349 |
case 'time':
|
350 |
$type[] = 'time';
|
351 |
$length = null;
|
352 |
break;
|
353 |
case 'float':
|
354 |
case 'double':
|
355 |
case 'real':
|
356 |
$type[] = 'float';
|
357 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
358 |
break;
|
359 |
case 'unknown':
|
360 |
case 'decimal':
|
361 |
case 'numeric':
|
362 |
$type[] = 'decimal';
|
363 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
364 |
break;
|
365 |
case 'tinyblob':
|
366 |
case 'mediumblob':
|
367 |
case 'longblob':
|
368 |
case 'blob':
|
369 |
$type[] = 'blob';
|
370 |
$length = null;
|
371 |
break;
|
372 |
case 'year':
|
373 |
$type[] = 'integer';
|
374 |
$type[] = 'date';
|
375 |
$length = null;
|
376 |
break;
|
377 |
default:
|
378 |
throw new Doctrine_DataDict_Exception('unknown database attribute type: ' . $dbType);
|
379 |
}
|
380 |
|
381 |
$length = ((int) $length == 0) ? null : (int) $length;
|
382 |
|
383 |
if ($values === null) {
|
384 |
return array('type' => $type, 'length' => $length, 'unsigned' => $unsigned, 'fixed' => $fixed);
|
385 |
} else {
|
386 |
return array('type' => $type, 'length' => $length, 'unsigned' => $unsigned, 'fixed' => $fixed, 'values' => $values);
|
387 |
}
|
388 |
}
|
389 |
/**
|
390 |
* Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
|
391 |
* of a field declaration to be used in statements like CREATE TABLE.
|
392 |
*
|
393 |
* @param string $charset name of the charset
|
394 |
* @return string DBMS specific SQL code portion needed to set the CHARACTER SET
|
395 |
* of a field declaration.
|
396 |
*/
|
397 |
public function getCharsetFieldDeclaration($charset)
|
398 |
{
|
399 |
return 'CHARACTER SET ' . $charset;
|
400 |
}
|
401 |
/**
|
402 |
* Obtain DBMS specific SQL code portion needed to set the COLLATION
|
403 |
* of a field declaration to be used in statements like CREATE TABLE.
|
404 |
*
|
405 |
* @param string $collation name of the collation
|
406 |
* @return string DBMS specific SQL code portion needed to set the COLLATION
|
407 |
* of a field declaration.
|
408 |
*/
|
409 |
public function getCollationFieldDeclaration($collation)
|
410 |
{
|
411 |
return 'COLLATE ' . $collation;
|
412 |
}
|
413 |
/**
|
414 |
* Obtain DBMS specific SQL code portion needed to declare an integer type
|
415 |
* field to be used in statements like CREATE TABLE.
|
416 |
*
|
417 |
* @param string $name name the field to be declared.
|
418 |
* @param string $field associative array with the name of the properties
|
419 |
* of the field being declared as array indexes.
|
420 |
* Currently, the types of supported field
|
421 |
* properties are as follows:
|
422 |
*
|
423 |
* unsigned
|
424 |
* Boolean flag that indicates whether the field
|
425 |
* should be declared as unsigned integer if
|
426 |
* possible.
|
427 |
*
|
428 |
* default
|
429 |
* Integer value to be used as default for this
|
430 |
* field.
|
431 |
*
|
432 |
* notnull
|
433 |
* Boolean flag that indicates whether this field is
|
434 |
* constrained to not be set to null.
|
435 |
* @return string DBMS specific SQL code portion that should be used to
|
436 |
* declare the specified field.
|
437 |
*/
|
438 |
public function getIntegerDeclaration($name, $field)
|
439 |
{
|
440 |
$default = $autoinc = '';
|
441 |
if ( ! empty($field['autoincrement'])) {
|
442 |
$autoinc = ' AUTO_INCREMENT';
|
443 |
} elseif (array_key_exists('default', $field)) {
|
444 |
if ($field['default'] === '') {
|
445 |
$field['default'] = empty($field['notnull']) ? null : 0;
|
446 |
}
|
447 |
$default = ' DEFAULT '.$this->conn->quote($field['default']);
|
448 |
}
|
449 |
/**
|
450 |
elseif (empty($field['notnull'])) {
|
451 |
$default = ' DEFAULT NULL';
|
452 |
}
|
453 |
*/
|
454 |
|
455 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : '';
|
456 |
$unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : '';
|
457 |
|
458 |
$name = $this->conn->quoteIdentifier($name, true);
|
459 |
|
460 |
return $name . ' ' . $this->getNativeDeclaration($field) . $unsigned . $default . $notnull . $autoinc;
|
461 |
}
|
462 |
} |