| 1 |
lars |
1 |
<?php
|
|
|
2 |
// vim: set et ts=4 sw=4 fdm=marker:
|
|
|
3 |
// +----------------------------------------------------------------------+
|
|
|
4 |
// | PHP versions 4 and 5 |
|
|
|
5 |
// +----------------------------------------------------------------------+
|
|
|
6 |
// | Copyright (c) 1998-2006 Manuel Lemos, Tomas V.V.Cox, |
|
|
|
7 |
// | Stig. S. Bakken, Lukas Smith |
|
|
|
8 |
// | All rights reserved. |
|
|
|
9 |
// +----------------------------------------------------------------------+
|
|
|
10 |
// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
|
|
|
11 |
// | API as well as database abstraction for PHP applications. |
|
|
|
12 |
// | This LICENSE is in the BSD license style. |
|
|
|
13 |
// | |
|
|
|
14 |
// | Redistribution and use in source and binary forms, with or without |
|
|
|
15 |
// | modification, are permitted provided that the following conditions |
|
|
|
16 |
// | are met: |
|
|
|
17 |
// | |
|
|
|
18 |
// | Redistributions of source code must retain the above copyright |
|
|
|
19 |
// | notice, this list of conditions and the following disclaimer. |
|
|
|
20 |
// | |
|
|
|
21 |
// | Redistributions in binary form must reproduce the above copyright |
|
|
|
22 |
// | notice, this list of conditions and the following disclaimer in the |
|
|
|
23 |
// | documentation and/or other materials provided with the distribution. |
|
|
|
24 |
// | |
|
|
|
25 |
// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
|
|
|
26 |
// | Lukas Smith nor the names of his contributors may be used to endorse |
|
|
|
27 |
// | or promote products derived from this software without specific prior|
|
|
|
28 |
// | written permission. |
|
|
|
29 |
// | |
|
|
|
30 |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
|
31 |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
|
32 |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
|
|
33 |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
|
|
34 |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
|
|
35 |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
|
|
36 |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
|
|
|
37 |
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
|
|
|
38 |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
|
39 |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
|
|
|
40 |
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
|
|
41 |
// | POSSIBILITY OF SUCH DAMAGE. |
|
|
|
42 |
// +----------------------------------------------------------------------+
|
|
|
43 |
// | Author: Lukas Smith <smith@pooteeweet.org> |
|
|
|
44 |
// +----------------------------------------------------------------------+
|
|
|
45 |
//
|
|
|
46 |
// $Id: mysql.php,v 1.54 2006/10/14 13:42:02 lsmith Exp $
|
|
|
47 |
//
|
|
|
48 |
|
|
|
49 |
require_once 'MDB2/Driver/Datatype/Common.php';
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
* MDB2 MySQL driver
|
|
|
53 |
*
|
|
|
54 |
* @package MDB2
|
|
|
55 |
* @category Database
|
|
|
56 |
* @author Lukas Smith <smith@pooteeweet.org>
|
|
|
57 |
*/
|
|
|
58 |
class MDB2_Driver_Datatype_mysql extends MDB2_Driver_Datatype_Common
|
|
|
59 |
{
|
|
|
60 |
// {{{ _getCharsetFieldDeclaration()
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
|
|
|
64 |
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
65 |
*
|
|
|
66 |
* @param string $charset name of the charset
|
|
|
67 |
* @return string DBMS specific SQL code portion needed to set the CHARACTER SET
|
|
|
68 |
* of a field declaration.
|
|
|
69 |
*/
|
|
|
70 |
function _getCharsetFieldDeclaration($charset)
|
|
|
71 |
{
|
|
|
72 |
return 'CHARACTER SET '.$charset;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// }}}
|
|
|
76 |
// {{{ _getCollationFieldDeclaration()
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Obtain DBMS specific SQL code portion needed to set the COLLATION
|
|
|
80 |
* of a field declaration to be used in statements like CREATE TABLE.
|
|
|
81 |
*
|
|
|
82 |
* @param string $collation name of the collation
|
|
|
83 |
* @return string DBMS specific SQL code portion needed to set the COLLATION
|
|
|
84 |
* of a field declaration.
|
|
|
85 |
*/
|
|
|
86 |
function _getCollationFieldDeclaration($collation)
|
|
|
87 |
{
|
|
|
88 |
return 'COLLATE '.$collation;
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// }}}
|
|
|
92 |
// {{{ getTypeDeclaration()
|
|
|
93 |
|
|
|
94 |
/**
|
|
|
95 |
* Obtain DBMS specific SQL code portion needed to declare an text type
|
|
|
96 |
* field to be used in statements like CREATE TABLE.
|
|
|
97 |
*
|
|
|
98 |
* @param array $field associative array with the name of the properties
|
|
|
99 |
* of the field being declared as array indexes. Currently, the types
|
|
|
100 |
* of supported field properties are as follows:
|
|
|
101 |
*
|
|
|
102 |
* length
|
|
|
103 |
* Integer value that determines the maximum length of the text
|
|
|
104 |
* field. If this argument is missing the field should be
|
|
|
105 |
* declared to have the longest length allowed by the DBMS.
|
|
|
106 |
*
|
|
|
107 |
* default
|
|
|
108 |
* Text value to be used as default for this field.
|
|
|
109 |
*
|
|
|
110 |
* notnull
|
|
|
111 |
* Boolean flag that indicates whether this field is constrained
|
|
|
112 |
* to not be set to null.
|
|
|
113 |
* @return string DBMS specific SQL code portion that should be used to
|
|
|
114 |
* declare the specified field.
|
|
|
115 |
* @access public
|
|
|
116 |
*/
|
|
|
117 |
function getTypeDeclaration($field)
|
|
|
118 |
{
|
|
|
119 |
$db =& $this->getDBInstance();
|
|
|
120 |
if (PEAR::isError($db)) {
|
|
|
121 |
return $db;
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
switch ($field['type']) {
|
|
|
125 |
case 'text':
|
|
|
126 |
if (empty($field['length']) && array_key_exists('default', $field)) {
|
|
|
127 |
$field['length'] = $db->varchar_max_length;
|
|
|
128 |
}
|
|
|
129 |
$length = !empty($field['length']) ? $field['length'] : false;
|
|
|
130 |
$fixed = !empty($field['fixed']) ? $field['fixed'] : false;
|
|
|
131 |
return $fixed ? ($length ? 'CHAR('.$length.')' : 'CHAR(255)')
|
|
|
132 |
: ($length ? 'VARCHAR('.$length.')' : 'TEXT');
|
|
|
133 |
case 'clob':
|
|
|
134 |
if (!empty($field['length'])) {
|
|
|
135 |
$length = $field['length'];
|
|
|
136 |
if ($length <= 255) {
|
|
|
137 |
return 'TINYTEXT';
|
|
|
138 |
} elseif ($length <= 65532) {
|
|
|
139 |
return 'TEXT';
|
|
|
140 |
} elseif ($length <= 16777215) {
|
|
|
141 |
return 'MEDIUMTEXT';
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
return 'LONGTEXT';
|
|
|
145 |
case 'blob':
|
|
|
146 |
if (!empty($field['length'])) {
|
|
|
147 |
$length = $field['length'];
|
|
|
148 |
if ($length <= 255) {
|
|
|
149 |
return 'TINYBLOB';
|
|
|
150 |
} elseif ($length <= 65532) {
|
|
|
151 |
return 'BLOB';
|
|
|
152 |
} elseif ($length <= 16777215) {
|
|
|
153 |
return 'MEDIUMBLOB';
|
|
|
154 |
}
|
|
|
155 |
}
|
|
|
156 |
return 'LONGBLOB';
|
|
|
157 |
case 'integer':
|
|
|
158 |
if (!empty($field['length'])) {
|
|
|
159 |
$length = $field['length'];
|
|
|
160 |
if ($length <= 1) {
|
|
|
161 |
return 'TINYINT';
|
|
|
162 |
} elseif ($length == 2) {
|
|
|
163 |
return 'SMALLINT';
|
|
|
164 |
} elseif ($length == 3) {
|
|
|
165 |
return 'MEDIUMINT';
|
|
|
166 |
} elseif ($length == 4) {
|
|
|
167 |
return 'INT';
|
|
|
168 |
} elseif ($length > 4) {
|
|
|
169 |
return 'BIGINT';
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
return 'INT';
|
|
|
173 |
case 'boolean':
|
|
|
174 |
return 'TINYINT(1)';
|
|
|
175 |
case 'date':
|
|
|
176 |
return 'DATE';
|
|
|
177 |
case 'time':
|
|
|
178 |
return 'TIME';
|
|
|
179 |
case 'timestamp':
|
|
|
180 |
return 'DATETIME';
|
|
|
181 |
case 'float':
|
|
|
182 |
return 'DOUBLE';
|
|
|
183 |
case 'decimal':
|
|
|
184 |
$length = !empty($field['length']) ? $field['length'] : 18;
|
|
|
185 |
$scale = !empty($field['scale']) ? $field['scale'] : $db->options['decimal_places'];
|
|
|
186 |
return 'DECIMAL('.$length.','.$scale.')';
|
|
|
187 |
}
|
|
|
188 |
return '';
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
// }}}
|
|
|
192 |
// {{{ _getIntegerDeclaration()
|
|
|
193 |
|
|
|
194 |
/**
|
|
|
195 |
* Obtain DBMS specific SQL code portion needed to declare an integer type
|
|
|
196 |
* field to be used in statements like CREATE TABLE.
|
|
|
197 |
*
|
|
|
198 |
* @param string $name name the field to be declared.
|
|
|
199 |
* @param string $field associative array with the name of the properties
|
|
|
200 |
* of the field being declared as array indexes.
|
|
|
201 |
* Currently, the types of supported field
|
|
|
202 |
* properties are as follows:
|
|
|
203 |
*
|
|
|
204 |
* unsigned
|
|
|
205 |
* Boolean flag that indicates whether the field
|
|
|
206 |
* should be declared as unsigned integer if
|
|
|
207 |
* possible.
|
|
|
208 |
*
|
|
|
209 |
* default
|
|
|
210 |
* Integer value to be used as default for this
|
|
|
211 |
* field.
|
|
|
212 |
*
|
|
|
213 |
* notnull
|
|
|
214 |
* Boolean flag that indicates whether this field is
|
|
|
215 |
* constrained to not be set to null.
|
|
|
216 |
* @return string DBMS specific SQL code portion that should be used to
|
|
|
217 |
* declare the specified field.
|
|
|
218 |
* @access protected
|
|
|
219 |
*/
|
|
|
220 |
function _getIntegerDeclaration($name, $field)
|
|
|
221 |
{
|
|
|
222 |
$db =& $this->getDBInstance();
|
|
|
223 |
if (PEAR::isError($db)) {
|
|
|
224 |
return $db;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
$default = $autoinc = '';;
|
|
|
228 |
if (!empty($field['autoincrement'])) {
|
|
|
229 |
$autoinc = ' AUTO_INCREMENT PRIMARY KEY';
|
|
|
230 |
} elseif (array_key_exists('default', $field)) {
|
|
|
231 |
if ($field['default'] === '') {
|
|
|
232 |
$field['default'] = empty($field['notnull']) ? null : 0;
|
|
|
233 |
}
|
|
|
234 |
$default = ' DEFAULT '.$this->quote($field['default'], 'integer');
|
|
|
235 |
} elseif (empty($field['notnull'])) {
|
|
|
236 |
$default = ' DEFAULT NULL';
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
$notnull = empty($field['notnull']) ? '' : ' NOT NULL';
|
|
|
240 |
$unsigned = empty($field['unsigned']) ? '' : ' UNSIGNED';
|
|
|
241 |
$name = $db->quoteIdentifier($name, true);
|
|
|
242 |
return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
// }}}
|
|
|
246 |
// {{{ matchPattern()
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* build a pattern matching string
|
|
|
250 |
*
|
|
|
251 |
* EXPERIMENTAL
|
|
|
252 |
*
|
|
|
253 |
* WARNING: this function is experimental and may change signature at
|
|
|
254 |
* any time until labelled as non-experimental
|
|
|
255 |
*
|
|
|
256 |
* @access public
|
|
|
257 |
*
|
|
|
258 |
* @param array $pattern even keys are strings, odd are patterns (% and _)
|
|
|
259 |
* @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
|
|
|
260 |
* @param string $field optional field name that is being matched against
|
|
|
261 |
* (might be required when emulating ILIKE)
|
|
|
262 |
*
|
|
|
263 |
* @return string SQL pattern
|
|
|
264 |
*/
|
|
|
265 |
function matchPattern($pattern, $operator = null, $field = null)
|
|
|
266 |
{
|
|
|
267 |
$db =& $this->getDBInstance();
|
|
|
268 |
if (PEAR::isError($db)) {
|
|
|
269 |
return $db;
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
$match = '';
|
|
|
273 |
if (!is_null($operator)) {
|
|
|
274 |
$field = is_null($field) ? '' : $field.' ';
|
|
|
275 |
$operator = strtoupper($operator);
|
|
|
276 |
switch ($operator) {
|
|
|
277 |
// case insensitive
|
|
|
278 |
case 'ILIKE':
|
|
|
279 |
$match = $field.'LIKE ';
|
|
|
280 |
break;
|
|
|
281 |
// case sensitive
|
|
|
282 |
case 'LIKE':
|
|
|
283 |
$match = $field.'LIKE BINARY ';
|
|
|
284 |
break;
|
|
|
285 |
default:
|
|
|
286 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
|
|
|
287 |
'not a supported operator type:'. $operator, __FUNCTION__);
|
|
|
288 |
}
|
|
|
289 |
}
|
|
|
290 |
$match.= "'";
|
|
|
291 |
foreach ($pattern as $key => $value) {
|
|
|
292 |
if ($key % 2) {
|
|
|
293 |
$match.= $value;
|
|
|
294 |
} else {
|
|
|
295 |
$match.= $db->escapePattern($db->escape($value));
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
$match.= "'";
|
|
|
299 |
$match.= $this->patternEscapeString();
|
|
|
300 |
return $match;
|
|
|
301 |
}
|
|
|
302 |
|
|
|
303 |
// }}}
|
|
|
304 |
// {{{ mapNativeDatatype()
|
|
|
305 |
|
|
|
306 |
/**
|
|
|
307 |
* Maps a native array description of a field to a MDB2 datatype and length
|
|
|
308 |
*
|
|
|
309 |
* @param array $field native field description
|
|
|
310 |
* @return array containing the various possible types, length, sign, fixed
|
|
|
311 |
* @access public
|
|
|
312 |
*/
|
|
|
313 |
function mapNativeDatatype($field)
|
|
|
314 |
{
|
|
|
315 |
$db_type = strtolower($field['type']);
|
|
|
316 |
$db_type = strtok($db_type, '(), ');
|
|
|
317 |
if ($db_type == 'national') {
|
|
|
318 |
$db_type = strtok('(), ');
|
|
|
319 |
}
|
|
|
320 |
if (!empty($field['length'])) {
|
|
|
321 |
$length = $field['length'];
|
|
|
322 |
$decimal = '';
|
|
|
323 |
} else {
|
|
|
324 |
$length = strtok('(), ');
|
|
|
325 |
$decimal = strtok('(), ');
|
|
|
326 |
}
|
|
|
327 |
$type = array();
|
|
|
328 |
$unsigned = $fixed = null;
|
|
|
329 |
switch ($db_type) {
|
|
|
330 |
case 'tinyint':
|
|
|
331 |
$type[] = 'integer';
|
|
|
332 |
$type[] = 'boolean';
|
|
|
333 |
if (preg_match('/^(is|has)/', $field['name'])) {
|
|
|
334 |
$type = array_reverse($type);
|
|
|
335 |
}
|
|
|
336 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
337 |
$length = 1;
|
|
|
338 |
break;
|
|
|
339 |
case 'smallint':
|
|
|
340 |
$type[] = 'integer';
|
|
|
341 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
342 |
$length = 2;
|
|
|
343 |
break;
|
|
|
344 |
case 'mediumint':
|
|
|
345 |
$type[] = 'integer';
|
|
|
346 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
347 |
$length = 3;
|
|
|
348 |
break;
|
|
|
349 |
case 'int':
|
|
|
350 |
case 'integer':
|
|
|
351 |
$type[] = 'integer';
|
|
|
352 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
353 |
$length = 4;
|
|
|
354 |
break;
|
|
|
355 |
case 'bigint':
|
|
|
356 |
$type[] = 'integer';
|
|
|
357 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
358 |
$length = 8;
|
|
|
359 |
break;
|
|
|
360 |
case 'tinytext':
|
|
|
361 |
case 'mediumtext':
|
|
|
362 |
case 'longtext':
|
|
|
363 |
case 'text':
|
|
|
364 |
case 'text':
|
|
|
365 |
case 'varchar':
|
|
|
366 |
$fixed = false;
|
|
|
367 |
case 'string':
|
|
|
368 |
case 'char':
|
|
|
369 |
$type[] = 'text';
|
|
|
370 |
if ($length == '1') {
|
|
|
371 |
$type[] = 'boolean';
|
|
|
372 |
if (preg_match('/^(is|has)/', $field['name'])) {
|
|
|
373 |
$type = array_reverse($type);
|
|
|
374 |
}
|
|
|
375 |
} elseif (strstr($db_type, 'text')) {
|
|
|
376 |
$type[] = 'clob';
|
|
|
377 |
if ($decimal == 'binary') {
|
|
|
378 |
$type[] = 'blob';
|
|
|
379 |
}
|
|
|
380 |
}
|
|
|
381 |
if ($fixed !== false) {
|
|
|
382 |
$fixed = true;
|
|
|
383 |
}
|
|
|
384 |
break;
|
|
|
385 |
case 'enum':
|
|
|
386 |
$type[] = 'text';
|
|
|
387 |
preg_match_all('/\'.+\'/U', $field['type'], $matches);
|
|
|
388 |
$length = 0;
|
|
|
389 |
$fixed = false;
|
|
|
390 |
if (is_array($matches)) {
|
|
|
391 |
foreach ($matches[0] as $value) {
|
|
|
392 |
$length = max($length, strlen($value)-2);
|
|
|
393 |
}
|
|
|
394 |
if ($length == '1' && count($matches[0]) == 2) {
|
|
|
395 |
$type[] = 'boolean';
|
|
|
396 |
if (preg_match('/^(is|has)/', $field['name'])) {
|
|
|
397 |
$type = array_reverse($type);
|
|
|
398 |
}
|
|
|
399 |
}
|
|
|
400 |
}
|
|
|
401 |
$type[] = 'integer';
|
|
|
402 |
case 'set':
|
|
|
403 |
$fixed = false;
|
|
|
404 |
$type[] = 'text';
|
|
|
405 |
$type[] = 'integer';
|
|
|
406 |
break;
|
|
|
407 |
case 'date':
|
|
|
408 |
$type[] = 'date';
|
|
|
409 |
$length = null;
|
|
|
410 |
break;
|
|
|
411 |
case 'datetime':
|
|
|
412 |
case 'timestamp':
|
|
|
413 |
$type[] = 'timestamp';
|
|
|
414 |
$length = null;
|
|
|
415 |
break;
|
|
|
416 |
case 'time':
|
|
|
417 |
$type[] = 'time';
|
|
|
418 |
$length = null;
|
|
|
419 |
break;
|
|
|
420 |
case 'float':
|
|
|
421 |
case 'double':
|
|
|
422 |
case 'real':
|
|
|
423 |
$type[] = 'float';
|
|
|
424 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
425 |
break;
|
|
|
426 |
case 'unknown':
|
|
|
427 |
case 'decimal':
|
|
|
428 |
case 'numeric':
|
|
|
429 |
$type[] = 'decimal';
|
|
|
430 |
$unsigned = preg_match('/ unsigned/i', $field['type']);
|
|
|
431 |
break;
|
|
|
432 |
case 'tinyblob':
|
|
|
433 |
case 'mediumblob':
|
|
|
434 |
case 'longblob':
|
|
|
435 |
case 'blob':
|
|
|
436 |
$type[] = 'blob';
|
|
|
437 |
$length = null;
|
|
|
438 |
break;
|
|
|
439 |
case 'year':
|
|
|
440 |
$type[] = 'integer';
|
|
|
441 |
$type[] = 'date';
|
|
|
442 |
$length = null;
|
|
|
443 |
break;
|
|
|
444 |
default:
|
|
|
445 |
$db =& $this->getDBInstance();
|
|
|
446 |
if (PEAR::isError($db)) {
|
|
|
447 |
return $db;
|
|
|
448 |
}
|
|
|
449 |
|
|
|
450 |
return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
|
|
|
451 |
'unknown database attribute type: '.$db_type, __FUNCTION__);
|
|
|
452 |
}
|
|
|
453 |
|
|
|
454 |
return array($type, $length, $unsigned, $fixed);
|
|
|
455 |
}
|
|
|
456 |
|
|
|
457 |
// }}}
|
|
|
458 |
}
|
|
|
459 |
|
|
|
460 |
?>
|