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 |
// | Authors: Lukas Smith <smith@pooteeweet.org> | |
|
44 |
// | Daniel Convissor <danielc@php.net> | |
|
45 |
// +----------------------------------------------------------------------+ |
|
46 |
// |
|
47 |
// $Id$ |
|
48 |
// |
|
49 |
|
|
50 |
require_once 'MDB2/Driver/Datatype/Common.php'; |
|
51 |
|
|
52 |
/** |
|
53 |
* MDB2 MS SQL driver |
|
54 |
* |
|
55 |
* @package MDB2 |
|
56 |
* @category Database |
|
57 |
* @author Lukas Smith <smith@pooteeweet.org> |
|
58 |
*/ |
|
59 |
class MDB2_Driver_Datatype_mssql extends MDB2_Driver_Datatype_Common |
|
60 |
{ |
|
61 |
// {{{ convertResult() |
|
62 |
|
|
63 |
/** |
|
64 |
* convert a value to a RDBMS indepdenant MDB2 type |
|
65 |
* |
|
66 |
* @param mixed $value value to be converted |
|
67 |
* @param int $type constant that specifies which type to convert to |
|
68 |
* @return mixed converted value |
|
69 |
* @access public |
|
70 |
*/ |
|
71 |
function convertResult($value, $type) |
|
72 |
{ |
|
73 |
if (is_null($value)) { |
|
74 |
return null; |
|
75 |
} |
|
76 |
switch ($type) { |
|
77 |
case 'boolean': |
|
78 |
return $value == '1'; |
|
79 |
case 'date': |
|
80 |
if (strlen($value) > 10) { |
|
81 |
$value = substr($value,0,10); |
|
82 |
} |
|
83 |
return $value; |
|
84 |
case 'time': |
|
85 |
if (strlen($value) > 8) { |
|
86 |
$value = substr($value,11,8); |
|
87 |
} |
|
88 |
return $value; |
|
89 |
default: |
|
90 |
return $this->_baseConvertResult($value,$type); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
// }}} |
|
95 |
// {{{ _getIntegerDeclaration() |
|
96 |
|
|
97 |
/** |
|
98 |
* Obtain DBMS specific SQL code portion needed to declare an integer type |
|
99 |
* field to be used in statements like CREATE TABLE. |
|
100 |
* |
|
101 |
* @param string $name name the field to be declared. |
|
102 |
* @param array $field associative array with the name of the properties |
|
103 |
* of the field being declared as array indexes. Currently, the types |
|
104 |
* of supported field properties are as follows: |
|
105 |
* |
|
106 |
* unsigned |
|
107 |
* Boolean flag that indicates whether the field should be |
|
108 |
* declared as unsigned integer if possible. |
|
109 |
* |
|
110 |
* default |
|
111 |
* Integer value to be used as default for this field. |
|
112 |
* |
|
113 |
* notnull |
|
114 |
* Boolean flag that indicates whether this field is constrained |
|
115 |
* to not be set to null. |
|
116 |
* @return string DBMS specific SQL code portion that should be used to |
|
117 |
* declare the specified field. |
|
118 |
* @access protected |
|
119 |
*/ |
|
120 |
function _getIntegerDeclaration($name, $field) |
|
121 |
{ |
|
122 |
if (array_key_exists('unsigned', $field) && $field['unsigned']) { |
|
123 |
$db =& $this->getDBInstance(); |
|
124 |
if (PEAR::isError($db)) { |
|
125 |
return $db; |
|
126 |
} |
|
127 |
|
|
128 |
$db->warnings[] = "unsigned integer field \"$name\" is being declared as signed integer"; |
|
129 |
} |
|
130 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
131 |
$this->quote($field['default'], 'integer') : ''; |
|
132 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
133 |
return $name.' INT'.$default.$notnull; |
|
134 |
} |
|
135 |
|
|
136 |
// }}} |
|
137 |
// {{{ _getTextDeclaration() |
|
138 |
|
|
139 |
/** |
|
140 |
* Obtain DBMS specific SQL code portion needed to declare an text type |
|
141 |
* field to be used in statements like CREATE TABLE. |
|
142 |
* |
|
143 |
* @param string $name name the field to be declared. |
|
144 |
* @param array $field associative array with the name of the properties |
|
145 |
* of the field being declared as array indexes. Currently, the types |
|
146 |
* of supported field properties are as follows: |
|
147 |
* |
|
148 |
* length |
|
149 |
* Integer value that determines the maximum length of the text |
|
150 |
* field. If this argument is missing the field should be |
|
151 |
* declared to have the longest length allowed by the DBMS. |
|
152 |
* |
|
153 |
* default |
|
154 |
* Text value to be used as default for this field. |
|
155 |
* |
|
156 |
* notnull |
|
157 |
* Boolean flag that indicates whether this field is constrained |
|
158 |
* to not be set to null. |
|
159 |
* @return string DBMS specific SQL code portion that should be used to |
|
160 |
* declare the specified field. |
|
161 |
* @access protected |
|
162 |
*/ |
|
163 |
function _getTextDeclaration($name, $field) |
|
164 |
{ |
|
165 |
$type = array_key_exists('length', $field) ? 'VARCHAR ('.$field['length'].')' : 'TEXT'; |
|
166 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
167 |
$this->quote($field['default'], 'text') : ''; |
|
168 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
169 |
return $name.' '.$type.$default.$notnull; |
|
170 |
} |
|
171 |
|
|
172 |
// }}} |
|
173 |
// {{{ _getCLOBDeclaration() |
|
174 |
|
|
175 |
/** |
|
176 |
* Obtain DBMS specific SQL code portion needed to declare an character |
|
177 |
* large object type field to be used in statements like CREATE TABLE. |
|
178 |
* |
|
179 |
* @param string $name name the field to be declared. |
|
180 |
* @param string $field associative array with the name of the |
|
181 |
* properties of the field being declared as array |
|
182 |
* indexes. Currently, the types of supported field |
|
183 |
* properties are as follows: |
|
184 |
* |
|
185 |
* length |
|
186 |
* Integer value that determines the maximum length |
|
187 |
* of the large object field. If this argument is |
|
188 |
* missing the field should be declared to have the |
|
189 |
* longest length allowed by the DBMS. |
|
190 |
* |
|
191 |
* notnull |
|
192 |
* Boolean flag that indicates whether this field |
|
193 |
* is constrained to not be set to null. |
|
194 |
* @return string DBMS specific SQL code portion that should be used to |
|
195 |
* declare the specified field. |
|
196 |
* @access protected |
|
197 |
*/ |
|
198 |
function _getCLOBDeclaration($name, $field) |
|
199 |
{ |
|
200 |
if (array_key_exists('length', $field)) { |
|
201 |
$length = $field['length']; |
|
202 |
if ($length <= 8000) { |
|
203 |
$type = "VARCHAR($length)"; |
|
204 |
} else { |
|
205 |
$type = 'TEXT'; |
|
206 |
} |
|
207 |
} else { |
|
208 |
$type = 'TEXT'; |
|
209 |
} |
|
210 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
211 |
return $name.' '.$type.$notnull; |
|
212 |
} |
|
213 |
|
|
214 |
// }}} |
|
215 |
// {{{ _getBLOBDeclaration() |
|
216 |
|
|
217 |
/** |
|
218 |
* Obtain DBMS specific SQL code portion needed to declare an binary large |
|
219 |
* object type field to be used in statements like CREATE TABLE. |
|
220 |
* |
|
221 |
* @param string $name name the field to be declared. |
|
222 |
* @param string $field associative array with the name of the properties |
|
223 |
* of the field being declared as array indexes. |
|
224 |
* Currently, the types of supported field |
|
225 |
* properties are as follows: |
|
226 |
* |
|
227 |
* length |
|
228 |
* Integer value that determines the maximum length |
|
229 |
* of the large object field. If this argument is |
|
230 |
* missing the field should be declared to have the |
|
231 |
* longest length allowed by the DBMS. |
|
232 |
* |
|
233 |
* notnull |
|
234 |
* Boolean flag that indicates whether this field is |
|
235 |
* constrained to not be set to null. |
|
236 |
* @return string DBMS specific SQL code portion that should be used to |
|
237 |
* declare the specified field. |
|
238 |
* @access protected |
|
239 |
*/ |
|
240 |
function _getBLOBDeclaration($name, $field) |
|
241 |
{ |
|
242 |
if (array_key_exists('length', $field)) { |
|
243 |
$length = $field['length']; |
|
244 |
if ($length <= 8000) { |
|
245 |
$type = "VARBINARY($length)"; |
|
246 |
} else { |
|
247 |
$type = 'IMAGE'; |
|
248 |
} |
|
249 |
} else { |
|
250 |
$type = 'IMAGE'; |
|
251 |
} |
|
252 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
253 |
return $name.' '.$type.$notnull; |
|
254 |
} |
|
255 |
|
|
256 |
// }}} |
|
257 |
// {{{ _getBooleanDeclaration() |
|
258 |
|
|
259 |
/** |
|
260 |
* Obtain DBMS specific SQL code portion needed to declare a boolean type |
|
261 |
* field to be used in statements like CREATE TABLE. |
|
262 |
* |
|
263 |
* @param string $name name the field to be declared. |
|
264 |
* @param array $field associative array with the name of the properties |
|
265 |
* of the field being declared as array indexes. Currently, the types |
|
266 |
* of supported field properties are as follows: |
|
267 |
* |
|
268 |
* default |
|
269 |
* Boolean value to be used as default for this field. |
|
270 |
* |
|
271 |
* notnull |
|
272 |
* Boolean flag that indicates whether this field is constrained |
|
273 |
* to not be set to null. |
|
274 |
* @return string DBMS specific SQL code portion that should be used to |
|
275 |
* declare the specified field. |
|
276 |
* @access protected |
|
277 |
*/ |
|
278 |
function _getBooleanDeclaration($name, $field) |
|
279 |
{ |
|
280 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
281 |
$this->quote($field['default'], 'boolean') : ''; |
|
282 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
283 |
return $name.' BIT'.$default.$notnull; |
|
284 |
} |
|
285 |
|
|
286 |
// }}} |
|
287 |
// {{{ _getDateDeclaration() |
|
288 |
|
|
289 |
/** |
|
290 |
* Obtain DBMS specific SQL code portion needed to declare a date type |
|
291 |
* field to be used in statements like CREATE TABLE. |
|
292 |
* |
|
293 |
* @param string $name name the field to be declared. |
|
294 |
* @param array $field associative array with the name of the properties |
|
295 |
* of the field being declared as array indexes. Currently, the types |
|
296 |
* of supported field properties are as follows: |
|
297 |
* |
|
298 |
* default |
|
299 |
* Date value to be used as default for this field. |
|
300 |
* |
|
301 |
* notnull |
|
302 |
* Boolean flag that indicates whether this field is constrained |
|
303 |
* to not be set to null. |
|
304 |
* @return string DBMS specific SQL code portion that should be used to |
|
305 |
* declare the specified field. |
|
306 |
* @access protected |
|
307 |
*/ |
|
308 |
function _getDateDeclaration($name, $field) |
|
309 |
{ |
|
310 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
311 |
$this->quote($field['default'], 'date') : ''; |
|
312 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
313 |
return $name.' CHAR ('.strlen('YYYY-MM-DD').')'.$default.$notnull; |
|
314 |
} |
|
315 |
|
|
316 |
// }}} |
|
317 |
// {{{ _getTimestampDeclaration() |
|
318 |
|
|
319 |
/** |
|
320 |
* Obtain DBMS specific SQL code portion needed to declare a timestamp |
|
321 |
* field to be used in statements like CREATE TABLE. |
|
322 |
* |
|
323 |
* @param string $name name the field to be declared. |
|
324 |
* @param array $field associative array with the name of the properties |
|
325 |
* of the field being declared as array indexes. Currently, the types |
|
326 |
* of supported field properties are as follows: |
|
327 |
* |
|
328 |
* default |
|
329 |
* Timestamp value to be used as default for this field. |
|
330 |
* |
|
331 |
* notnull |
|
332 |
* Boolean flag that indicates whether this field is constrained |
|
333 |
* to not be set to null. |
|
334 |
* @return string DBMS specific SQL code portion that should be used to |
|
335 |
* declare the specified field. |
|
336 |
* @access protected |
|
337 |
*/ |
|
338 |
function _getTimestampDeclaration($name, $field) |
|
339 |
{ |
|
340 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
341 |
$this->quote($field['default'], 'timestamp') : ''; |
|
342 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
343 |
return $name.' CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')'.$default.$notnull; |
|
344 |
} |
|
345 |
|
|
346 |
// }}} |
|
347 |
// {{{ _getTimeDeclaration() |
|
348 |
|
|
349 |
/** |
|
350 |
* Obtain DBMS specific SQL code portion needed to declare a time |
|
351 |
* field to be used in statements like CREATE TABLE. |
|
352 |
* |
|
353 |
* @param string $name name the field to be declared. |
|
354 |
* @param array $field associative array with the name of the properties |
|
355 |
* of the field being declared as array indexes. Currently, the types |
|
356 |
* of supported field properties are as follows: |
|
357 |
* |
|
358 |
* default |
|
359 |
* Time value to be used as default for this field. |
|
360 |
* |
|
361 |
* notnull |
|
362 |
* Boolean flag that indicates whether this field is constrained |
|
363 |
* to not be set to null. |
|
364 |
* @return string DBMS specific SQL code portion that should be used to |
|
365 |
* declare the specified field. |
|
366 |
* @access protected |
|
367 |
*/ |
|
368 |
function _getTimeDeclaration($name, $field) |
|
369 |
{ |
|
370 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
371 |
$this->quote($field['default'], 'time') : ''; |
|
372 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
373 |
return $name.' CHAR ('.strlen('HH:MM:SS').')'.$default.$notnull; |
|
374 |
} |
|
375 |
|
|
376 |
// }}} |
|
377 |
// {{{ _getFloatDeclaration() |
|
378 |
|
|
379 |
/** |
|
380 |
* Obtain DBMS specific SQL code portion needed to declare an float type |
|
381 |
* field to be used in statements like CREATE TABLE. |
|
382 |
* |
|
383 |
* @param string $name name the field to be declared. |
|
384 |
* @param string $field associative array with the name of the properties |
|
385 |
* of the field being declared as array indexes. |
|
386 |
* Currently, the types of supported field |
|
387 |
* properties are as follows: |
|
388 |
* |
|
389 |
* default |
|
390 |
* Integer value to be used as default for this |
|
391 |
* field. |
|
392 |
* |
|
393 |
* notnull |
|
394 |
* Boolean flag that indicates whether this field is |
|
395 |
* constrained to not be set to null. |
|
396 |
* @return string DBMS specific SQL code portion that should be used to |
|
397 |
* declare the specified field. |
|
398 |
* @access protected |
|
399 |
*/ |
|
400 |
function _getFloatDeclaration($name, $field) |
|
401 |
{ |
|
402 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
403 |
$this->quote($field['default'], 'float') : ''; |
|
404 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
405 |
return $name.' FLOAT'.$default.$notnull; |
|
406 |
} |
|
407 |
|
|
408 |
// }}} |
|
409 |
// {{{ _getDecimalDeclaration() |
|
410 |
|
|
411 |
/** |
|
412 |
* Obtain DBMS specific SQL code portion needed to declare an decimal type |
|
413 |
* field to be used in statements like CREATE TABLE. |
|
414 |
* |
|
415 |
* @param string $name name the field to be declared. |
|
416 |
* @param string $field associative array with the name of the properties |
|
417 |
* of the field being declared as array indexes. |
|
418 |
* Currently, the types of supported field |
|
419 |
* properties are as follows: |
|
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 |
* @access protected |
|
431 |
*/ |
|
432 |
function _getDecimalDeclaration($name, $field) |
|
433 |
{ |
|
434 |
$db =& $this->getDBInstance(); |
|
435 |
if (PEAR::isError($db)) { |
|
436 |
return $db; |
|
437 |
} |
|
438 |
|
|
439 |
$type = 'DECIMAL(18,'.$db->options['decimal_places'].')'; |
|
440 |
$default = array_key_exists('default', $field) ? ' DEFAULT '. |
|
441 |
$this->quote($field['default'], 'decimal') : ''; |
|
442 |
$notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : ' NULL'; |
|
443 |
return $name.' '.$type.$default.$notnull; |
|
444 |
} |
|
445 |
|
|
446 |
// }}} |
|
447 |
// {{{ _quoteBLOB() |
|
448 |
|
|
449 |
/** |
|
450 |
* Convert a text value into a DBMS specific format that is suitable to |
|
451 |
* compose query statements. |
|
452 |
* |
|
453 |
* @param $value |
|
454 |
* @return string text string that represents the given argument value in |
|
455 |
* a DBMS specific format. |
|
456 |
* @access protected |
|
457 |
*/ |
|
458 |
function _quoteBLOB($value) |
|
459 |
{ |
|
460 |
$value = $this->_readFile($value); |
|
461 |
return bin2hex("0x".$value); |
|
462 |
} |
|
463 |
|
|
464 |
// }}} |
|
465 |
// {{{ _quoteBoolean() |
|
466 |
|
|
467 |
/** |
|
468 |
* Convert a text value into a DBMS specific format that is suitable to |
|
469 |
* compose query statements. |
|
470 |
* |
|
471 |
* @param string $value text string value that is intended to be converted. |
|
472 |
* @return string text string that represents the given argument value in |
|
473 |
* a DBMS specific format. |
|
474 |
* @access protected |
|
475 |
*/ |
|
476 |
function _quoteBoolean($value) |
|
477 |
{ |
|
478 |
return ($value ? 1 : 0); |
|
479 |
} |
|
480 |
|
|
481 |
// }}} |
|
482 |
// {{{ _quoteFloat() |
|
483 |
|
|
484 |
/** |
|
485 |
* Convert a text value into a DBMS specific format that is suitable to |
|
486 |
* compose query statements. |
|
487 |
* |
|
488 |
* @param string $value text string value that is intended to be converted. |
|
489 |
* @return string text string that represents the given argument value in |
|
490 |
* a DBMS specific format. |
|
491 |
* @access protected |
|
492 |
*/ |
|
493 |
function _quoteFloat($value) |
|
494 |
{ |
|
495 |
return (float)$value; |
|
496 |
} |
|
497 |
|
|
498 |
// }}} |
|
499 |
// {{{ _quoteDecimal() |
|
500 |
|
|
501 |
/** |
|
502 |
* Convert a text value into a DBMS specific format that is suitable to |
|
503 |
* compose query statements. |
|
504 |
* |
|
505 |
* @param string $value text string value that is intended to be converted. |
|
506 |
* @return string text string that represents the given argument value in |
|
507 |
* a DBMS specific format. |
|
508 |
* @access protected |
|
509 |
*/ |
|
510 |
function _quoteDecimal($value) |
|
511 |
{ |
|
512 |
$db =& $this->getDBInstance(); |
|
513 |
if (PEAR::isError($db)) { |
|
514 |
return $db; |
|
515 |
} |
|
516 |
|
|
517 |
return $db->escape($value); |
|
518 |
} |
|
519 |
} |
|
520 |
|
|
521 |
?> |