# Source for file Mysql.php

Documentation is available at Mysql.php

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