commit | author | age
|
c9462d
|
1 |
<?php |
S |
2 |
// vim: set et ts=4 sw=4 fdm=marker: |
|
3 |
// +----------------------------------------------------------------------+ |
|
4 |
// | PHP versions 4 and 5 | |
|
5 |
// +----------------------------------------------------------------------+ |
|
6 |
// | Copyright (c) 1998-2004 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@backendmedia.com> | |
|
44 |
// +----------------------------------------------------------------------+ |
|
45 |
// |
|
46 |
// $Id$ |
|
47 |
// |
|
48 |
|
|
49 |
require_once 'MDB2/Driver/Datatype/Common.php'; |
|
50 |
|
|
51 |
/** |
|
52 |
* MDB2 SQLite driver |
|
53 |
* |
|
54 |
* @package MDB2 |
|
55 |
* @category Database |
|
56 |
* @author Lukas Smith <smith@backendmedia.com> |
|
57 |
*/ |
|
58 |
class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common |
|
59 |
{ |
|
60 |
// }}} |
|
61 |
// {{{ _getIntegerDeclaration() |
|
62 |
|
|
63 |
/** |
|
64 |
* Obtain DBMS specific SQL code portion needed to declare an integer type |
|
65 |
* field to be used in statements like CREATE TABLE. |
|
66 |
* |
|
67 |
* @param string $name name the field to be declared. |
|
68 |
* @param string $field associative array with the name of the properties |
|
69 |
* of the field being declared as array indexes. |
|
70 |
* Currently, the types of supported field |
|
71 |
* properties are as follows: |
|
72 |
* |
|
73 |
* unsigned |
|
74 |
* Boolean flag that indicates whether the field |
|
75 |
* should be declared as unsigned integer if |
|
76 |
* possible. |
|
77 |
* |
|
78 |
* default |
|
79 |
* Integer value to be used as default for this |
|
80 |
* field. |
|
81 |
* |
|
82 |
* notnull |
|
83 |
* Boolean flag that indicates whether this field is |
|
84 |
* constrained to not be set to null. |
|
85 |
* @return string DBMS specific SQL code portion that should be used to |
|
86 |
* declare the specified field. |
|
87 |
* @access protected |
|
88 |
*/ |
|
89 |
function _getIntegerDeclaration($name, $field) |
|
90 |
{ |
|
91 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
92 |
$unsigned = (isset($field['unsigned']) && $field['unsigned']) ? ' UNSIGNED' : ''; |
|
93 |
$default = isset($field['default']) ? ' DEFAULT '. |
|
94 |
$this->quote($field['default'], 'integer') : ''; |
|
95 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
96 |
return $name.' INT'.$unsigned.$default.$notnull; |
|
97 |
} |
|
98 |
|
|
99 |
// }}} |
|
100 |
// {{{ _getCLOBDeclaration() |
|
101 |
|
|
102 |
/** |
|
103 |
* Obtain DBMS specific SQL code portion needed to declare an character |
|
104 |
* large object type field to be used in statements like CREATE TABLE. |
|
105 |
* |
|
106 |
* @param string $name name the field to be declared. |
|
107 |
* @param string $field associative array with the name of the |
|
108 |
* properties of the field being declared as array |
|
109 |
* indexes. Currently, the types of supported field |
|
110 |
* properties are as follows: |
|
111 |
* |
|
112 |
* length |
|
113 |
* Integer value that determines the maximum length |
|
114 |
* of the large object field. If this argument is |
|
115 |
* missing the field should be declared to have the |
|
116 |
* longest length allowed by the DBMS. |
|
117 |
* |
|
118 |
* notnull |
|
119 |
* Boolean flag that indicates whether this field |
|
120 |
* is constrained to not be set to null. |
|
121 |
* @return string DBMS specific SQL code portion that should be used to |
|
122 |
* declare the specified field. |
|
123 |
* @access protected |
|
124 |
*/ |
|
125 |
function _getCLOBDeclaration($name, $field) |
|
126 |
{ |
|
127 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
128 |
if (isset($field['length'])) { |
|
129 |
$length = $field['length']; |
|
130 |
if ($length <= 255) { |
|
131 |
$type = 'TINYTEXT'; |
|
132 |
} else { |
|
133 |
if ($length <= 65535) { |
|
134 |
$type = 'TEXT'; |
|
135 |
} else { |
|
136 |
if ($length <= 16777215) { |
|
137 |
$type = 'MEDIUMTEXT'; |
|
138 |
} else { |
|
139 |
$type = 'LONGTEXT'; |
|
140 |
} |
|
141 |
} |
|
142 |
} |
|
143 |
} else { |
|
144 |
$type = 'LONGTEXT'; |
|
145 |
} |
|
146 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
147 |
return $name.' '.$type.$notnull; |
|
148 |
} |
|
149 |
|
|
150 |
// }}} |
|
151 |
// {{{ _getBLOBDeclaration() |
|
152 |
|
|
153 |
/** |
|
154 |
* Obtain DBMS specific SQL code portion needed to declare an binary large |
|
155 |
* object type field to be used in statements like CREATE TABLE. |
|
156 |
* |
|
157 |
* @param string $name name the field to be declared. |
|
158 |
* @param string $field associative array with the name of the properties |
|
159 |
* of the field being declared as array indexes. |
|
160 |
* Currently, the types of supported field |
|
161 |
* properties are as follows: |
|
162 |
* |
|
163 |
* length |
|
164 |
* Integer value that determines the maximum length |
|
165 |
* of the large object field. If this argument is |
|
166 |
* missing the field should be declared to have the |
|
167 |
* longest length allowed by the DBMS. |
|
168 |
* |
|
169 |
* notnull |
|
170 |
* Boolean flag that indicates whether this field is |
|
171 |
* constrained to not be set to null. |
|
172 |
* @return string DBMS specific SQL code portion that should be used to |
|
173 |
* declare the specified field. |
|
174 |
* @access protected |
|
175 |
*/ |
|
176 |
function _getBLOBDeclaration($name, $field) |
|
177 |
{ |
|
178 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
179 |
if (isset($field['length'])) { |
|
180 |
$length = $field['length']; |
|
181 |
if ($length <= 255) { |
|
182 |
$type = 'TINYBLOB'; |
|
183 |
} else { |
|
184 |
if ($length <= 65535) { |
|
185 |
$type = 'BLOB'; |
|
186 |
} else { |
|
187 |
if ($length <= 16777215) { |
|
188 |
$type = 'MEDIUMBLOB'; |
|
189 |
} else { |
|
190 |
$type = 'LONGBLOB'; |
|
191 |
} |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
else { |
|
196 |
$type = 'LONGBLOB'; |
|
197 |
} |
|
198 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
199 |
return $name.' '.$type.$notnull; |
|
200 |
} |
|
201 |
|
|
202 |
// }}} |
|
203 |
// {{{ _getDateDeclaration() |
|
204 |
|
|
205 |
/** |
|
206 |
* Obtain DBMS specific SQL code portion needed to declare an date type |
|
207 |
* field to be used in statements like CREATE TABLE. |
|
208 |
* |
|
209 |
* @param string $name name the field to be declared. |
|
210 |
* @param string $field associative array with the name of the properties |
|
211 |
* of the field being declared as array indexes. |
|
212 |
* Currently, the types of supported field properties |
|
213 |
* are as follows: |
|
214 |
* |
|
215 |
* default |
|
216 |
* Date value to be used as default for this field. |
|
217 |
* |
|
218 |
* notnull |
|
219 |
* Boolean flag that indicates whether this field is |
|
220 |
* constrained to not be set to null. |
|
221 |
* @return string DBMS specific SQL code portion that should be used to |
|
222 |
* declare the specified field. |
|
223 |
* @access protected |
|
224 |
*/ |
|
225 |
function _getDateDeclaration($name, $field) |
|
226 |
{ |
|
227 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
228 |
$default = isset($field['default']) ? ' DEFAULT '. |
|
229 |
$this->quote($field['default'], 'date') : ''; |
|
230 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
231 |
return $name.' DATE'.$default.$notnull; |
|
232 |
} |
|
233 |
|
|
234 |
// }}} |
|
235 |
// {{{ _getTimestampDeclaration() |
|
236 |
|
|
237 |
/** |
|
238 |
* Obtain DBMS specific SQL code portion needed to declare an timestamp |
|
239 |
* type field to be used in statements like CREATE TABLE. |
|
240 |
* |
|
241 |
* @param string $name name the field to be declared. |
|
242 |
* @param string $field associative array with the name of the properties |
|
243 |
* of the field being declared as array indexes. |
|
244 |
* Currently, the types of supported field |
|
245 |
* properties are as follows: |
|
246 |
* |
|
247 |
* default |
|
248 |
* Time stamp value to be used as default for this |
|
249 |
* field. |
|
250 |
* |
|
251 |
* notnull |
|
252 |
* Boolean flag that indicates whether this field is |
|
253 |
* constrained to not be set to null. |
|
254 |
* @return string DBMS specific SQL code portion that should be used to |
|
255 |
* declare the specified field. |
|
256 |
* @access protected |
|
257 |
*/ |
|
258 |
function _getTimestampDeclaration($name, $field) |
|
259 |
{ |
|
260 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
261 |
$default = isset($field['default']) ? ' DEFAULT '. |
|
262 |
$this->quote($field['default'], 'timestamp') : ''; |
|
263 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
264 |
return $name.' DATETIME'.$default.$notnull; |
|
265 |
} |
|
266 |
|
|
267 |
// }}} |
|
268 |
// {{{ _getTimeDeclaration() |
|
269 |
|
|
270 |
/** |
|
271 |
* Obtain DBMS specific SQL code portion needed to declare an time type |
|
272 |
* field to be used in statements like CREATE TABLE. |
|
273 |
* |
|
274 |
* @param string $name name the field to be declared. |
|
275 |
* @param string $field associative array with the name of the properties |
|
276 |
* of the field being declared as array indexes. |
|
277 |
* Currently, the types of supported field |
|
278 |
* properties are as follows: |
|
279 |
* |
|
280 |
* default |
|
281 |
* Time value to be used as default for this field. |
|
282 |
* |
|
283 |
* notnull |
|
284 |
* Boolean flag that indicates whether this field is |
|
285 |
* constrained to not be set to null. |
|
286 |
* @return string DBMS specific SQL code portion that should be used to |
|
287 |
* declare the specified field. |
|
288 |
* @access protected |
|
289 |
*/ |
|
290 |
function _getTimeDeclaration($name, $field) |
|
291 |
{ |
|
292 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
293 |
$default = isset($field['default']) ? ' DEFAULT '. |
|
294 |
$this->quote($field['default'], 'time') : ''; |
|
295 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
296 |
return $name.' TIME'.$default.$notnull; |
|
297 |
} |
|
298 |
|
|
299 |
// }}} |
|
300 |
// {{{ _getFloatDeclaration() |
|
301 |
|
|
302 |
/** |
|
303 |
* Obtain DBMS specific SQL code portion needed to declare an float type |
|
304 |
* field to be used in statements like CREATE TABLE. |
|
305 |
* |
|
306 |
* @param string $name name the field to be declared. |
|
307 |
* @param string $field associative array with the name of the properties |
|
308 |
* of the field being declared as array indexes. |
|
309 |
* Currently, the types of supported field |
|
310 |
* properties are as follows: |
|
311 |
* |
|
312 |
* default |
|
313 |
* Integer value to be used as default for this |
|
314 |
* field. |
|
315 |
* |
|
316 |
* notnull |
|
317 |
* Boolean flag that indicates whether this field is |
|
318 |
* constrained to not be set to null. |
|
319 |
* @return string DBMS specific SQL code portion that should be used to |
|
320 |
* declare the specified field. |
|
321 |
* @access protected |
|
322 |
*/ |
|
323 |
function _getFloatDeclaration($name, $field) |
|
324 |
{ |
|
325 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
326 |
$type = 'DOUBLE'.($db->options['fixed_float'] ? '('. |
|
327 |
($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' : ''); |
|
328 |
$default = isset($field['default']) ? ' DEFAULT '. |
|
329 |
$this->quote($field['default'], 'float') : ''; |
|
330 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
331 |
return $name.' '.$type.$default.$notnull; |
|
332 |
} |
|
333 |
|
|
334 |
// }}} |
|
335 |
// {{{ _getDecimalDeclaration() |
|
336 |
|
|
337 |
/** |
|
338 |
* Obtain DBMS specific SQL code portion needed to declare an decimal type |
|
339 |
* field to be used in statements like CREATE TABLE. |
|
340 |
* |
|
341 |
* @param string $name name the field to be declared. |
|
342 |
* @param string $field associative array with the name of the properties |
|
343 |
* of the field being declared as array indexes. |
|
344 |
* Currently, the types of supported field |
|
345 |
* properties are as follows: |
|
346 |
* |
|
347 |
* default |
|
348 |
* Integer value to be used as default for this |
|
349 |
* field. |
|
350 |
* |
|
351 |
* notnull |
|
352 |
* Boolean flag that indicates whether this field is |
|
353 |
* constrained to not be set to null. |
|
354 |
* @return string DBMS specific SQL code portion that should be used to |
|
355 |
* declare the specified field. |
|
356 |
* @access protected |
|
357 |
*/ |
|
358 |
function _getDecimalDeclaration($name, $field) |
|
359 |
{ |
|
360 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
361 |
$type = 'BIGINT'; |
|
362 |
$default = isset($field['default']) ? ' DEFAULT '. |
|
363 |
$this->quote($field['default'], 'decimal') : ''; |
|
364 |
$notnull = (isset($field['notnull']) && $field['notnull']) ? ' NOT NULL' : ''; |
|
365 |
return $name.' '.$type.$default.$notnull; |
|
366 |
} |
|
367 |
|
|
368 |
// }}} |
|
369 |
// {{{ _quoteFloat() |
|
370 |
|
|
371 |
/** |
|
372 |
* Convert a text value into a DBMS specific format that is suitable to |
|
373 |
* compose query statements. |
|
374 |
* |
|
375 |
* @param string $value text string value that is intended to be converted. |
|
376 |
* @return string text string that represents the given argument value in |
|
377 |
* a DBMS specific format. |
|
378 |
* @access protected |
|
379 |
*/ |
|
380 |
function _quoteFloat($value) |
|
381 |
{ |
|
382 |
return (float)$value; |
|
383 |
} |
|
384 |
|
|
385 |
// }}} |
|
386 |
// {{{ _quoteDecimal() |
|
387 |
|
|
388 |
/** |
|
389 |
* Convert a text value into a DBMS specific format that is suitable to |
|
390 |
* compose query statements. |
|
391 |
* |
|
392 |
* @param string $value text string value that is intended to be converted. |
|
393 |
* @return string text string that represents the given argument value in |
|
394 |
* a DBMS specific format. |
|
395 |
* @access protected |
|
396 |
*/ |
|
397 |
function _quoteDecimal($value) |
|
398 |
{ |
|
399 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
400 |
return $db->escape($value); |
|
401 |
} |
|
402 |
|
|
403 |
// }}} |
|
404 |
// {{{ mapNativeDatatype() |
|
405 |
|
|
406 |
/** |
|
407 |
* Maps a native array description of a field to a MDB2 datatype and length |
|
408 |
* |
|
409 |
* @param array $field native field description |
|
410 |
* @return array containing the various possible types and the length |
|
411 |
* @access public |
|
412 |
*/ |
|
413 |
function mapNativeDatatype($field) |
|
414 |
{ |
|
415 |
$db =& $GLOBALS['_MDB2_databases'][$this->db_index]; |
|
416 |
$db_type = $field['type']; |
|
417 |
$length = isset($field['length']) ? $field['length'] : null; |
|
418 |
$type = array(); |
|
419 |
switch ($db_type) { |
|
420 |
case 'tinyint': |
|
421 |
case 'smallint': |
|
422 |
case 'mediumint': |
|
423 |
case 'int': |
|
424 |
case 'integer': |
|
425 |
case 'bigint': |
|
426 |
$type[] = 'integer'; |
|
427 |
if ($length == '1') { |
|
428 |
$type[] = 'boolean'; |
|
429 |
if (preg_match('/^[is|has]/', $field['name'])) { |
|
430 |
$type = array_reverse($type); |
|
431 |
} |
|
432 |
} |
|
433 |
$type[] = 'decimal'; |
|
434 |
break; |
|
435 |
case 'tinytext': |
|
436 |
case 'mediumtext': |
|
437 |
case 'longtext': |
|
438 |
case 'text': |
|
439 |
case 'char': |
|
440 |
case 'varchar': |
|
441 |
case "varchar2": |
|
442 |
$type[] = 'text'; |
|
443 |
if ($length == '1') { |
|
444 |
$type[] = 'boolean'; |
|
445 |
if (preg_match('/[is|has]/', $field['name'])) { |
|
446 |
$type = array_reverse($type); |
|
447 |
} |
|
448 |
} elseif (strstr($db_type, 'text')) |
|
449 |
$type[] = 'clob'; |
|
450 |
break; |
|
451 |
case 'enum': |
|
452 |
preg_match_all('/\'.+\'/U',$row[$type_column], $matches); |
|
453 |
$length = 0; |
|
454 |
if (is_array($matches)) { |
|
455 |
foreach ($matches[0] as $value) { |
|
456 |
$length = max($length, strlen($value)-2); |
|
457 |
} |
|
458 |
} |
|
459 |
case 'set': |
|
460 |
$type[] = 'text'; |
|
461 |
$type[] = 'integer'; |
|
462 |
break; |
|
463 |
case 'date': |
|
464 |
$type[] = 'date'; |
|
465 |
$length = null; |
|
466 |
break; |
|
467 |
case 'datetime': |
|
468 |
case 'timestamp': |
|
469 |
$type[] = 'timestamp'; |
|
470 |
$length = null; |
|
471 |
break; |
|
472 |
case 'time': |
|
473 |
$type[] = 'time'; |
|
474 |
$length = null; |
|
475 |
break; |
|
476 |
case 'float': |
|
477 |
case 'double': |
|
478 |
case 'real': |
|
479 |
$type[] = 'float'; |
|
480 |
break; |
|
481 |
case 'decimal': |
|
482 |
case 'numeric': |
|
483 |
$type[] = 'decimal'; |
|
484 |
break; |
|
485 |
case 'tinyblob': |
|
486 |
case 'mediumblob': |
|
487 |
case 'longblob': |
|
488 |
case 'blob': |
|
489 |
$type[] = 'text'; |
|
490 |
$length = null; |
|
491 |
break; |
|
492 |
case 'year': |
|
493 |
$type[] = 'integer'; |
|
494 |
$type[] = 'date'; |
|
495 |
$length = null; |
|
496 |
break; |
|
497 |
default: |
|
498 |
return $db->raiseError(MDB2_ERROR, null, null, |
|
499 |
'getTableFieldDefinition: unknown database attribute type'); |
|
500 |
} |
|
501 |
|
|
502 |
return array($type, $length); |
|
503 |
} |
|
504 |
} |
|
505 |
|
|
506 |
?> |