thomascube
2006-03-04 f5121b5639992fc9e51fd551bac2254429b638fa
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@pooteeweet.org>                           |
44 // +----------------------------------------------------------------------+
45 //
46 // $Id$
47 //
48
49 require_once 'MDB2/Driver/Datatype/Common.php';
50
51 /**
52  * MDB2 FrontbaseSQL driver
53  *
54  * @package MDB2
55  * @category Database
56  * @author  Lukas Smith <smith@pooteeweet.org>
57  */
58 class MDB2_Driver_Datatype_fbsql extends MDB2_Driver_Datatype_Common
59 {
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 == 'T';
79          case 'time':
80             if ($value[0] == '+') {
81                 return substr($value, 1);
82             } else {
83                 return $value;
84             }
85         default:
86             return $this->_baseConvertResult($value, $type);
87         }
88         return $this->_baseConvertResult($value, $type);
89     }
90
91     // }}}
92     // {{{ _getIntegerDeclaration()
93
94     /**
95      * Obtain DBMS specific SQL code portion needed to declare an integer type
96      * field to be used in statements like CREATE TABLE.
97      *
98      * @param string  $name   name the field to be declared.
99      * @param string  $field  associative array with the name of the properties
100      *                        of the field being declared as array indexes.
101      *                        Currently, the types of supported field
102      *                        properties are as follows:
103      *
104      *                       unsigned
105      *                        Boolean flag that indicates whether the field
106      *                        should be declared as unsigned integer if
107      *                        possible.
108      *
109      *                       default
110      *                        Integer value to be used as default for this
111      *                        field.
112      *
113      *                       notnull
114      *                        Boolean flag that indicates whether this field is
115      *                        constrained 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
129                 declared as signed integer";
130         }
131         $default = array_key_exists('default', $field) ? ' DEFAULT '.
132             $this->quote($field['default'], 'integer') : '';
133         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
134         return $name.' INT'.$default.$notnull;
135     }
136
137     // }}}
138     // {{{ _getTextDeclaration()
139
140     /**
141      * Obtain DBMS specific SQL code portion needed to declare an text type
142      * field to be used in statements like CREATE TABLE.
143      *
144      * @param string $name name the field to be declared.
145     * @param array $field associative array with the name of the properties
146      *       of the field being declared as array indexes. Currently, the types
147      *       of supported field properties are as follows:
148     *
149      *       length
150      *           Integer value that determines the maximum length of the text
151      *           field. If this argument is missing the field should be
152      *           declared to have the longest length allowed by the DBMS.
153      *
154      *       default
155      *           Text value to be used as default for this field.
156      *
157      *       notnull
158      *           Boolean flag that indicates whether this field is constrained
159      *           to not be set to NULL.
160      * @return string DBMS specific SQL code portion that should be used to
161      *       declare the specified field.
162      * @access protected
163      */
164     function _getTextDeclaration($name, $field)
165     {
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' : '';
169         $length = array_key_exists('length', $field) ? $field['length'] : 32768;
170         return $name.' VARCHAR ('.$length.')'.$default.$notnull;
171     }
172
173     // }}}
174     // {{{ _getCLOBDeclaration()
175
176     /**
177      * Obtain DBMS specific SQL code portion needed to declare an character
178      * large object type field to be used in statements like CREATE TABLE.
179      *
180      * @param string  $name   name the field to be declared.
181      * @param string  $field  associative array with the name of the
182      *                        properties of the field being declared as array
183      *                        indexes. Currently, the types of supported field
184      *                        properties are as follows:
185      *
186      *                       length
187      *                        Integer value that determines the maximum length
188      *                        of the large object field. If this argument is
189      *                        missing the field should be declared to have the
190      *                        longest length allowed by the DBMS.
191      *
192      *                       notnull
193      *                        Boolean flag that indicates whether this field
194      *                        is constrained to not be set to null.
195      * @return string  DBMS specific SQL code portion that should be used to
196      *                 declare the specified field.
197      * @access protected
198      */
199     function _getCLOBDeclaration($name, $field)
200     {
201         return "$name CLOB".((array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '');
202     }
203
204     // }}}
205     // {{{ _getBLOBDeclaration()
206
207     /**
208      * Obtain DBMS specific SQL code portion needed to declare an binary large
209      * object type field to be used in statements like CREATE TABLE.
210      *
211      * @param string  $name   name the field to be declared.
212      * @param string  $field  associative array with the name of the properties
213      *                        of the field being declared as array indexes.
214      *                        Currently, the types of supported field
215      *                        properties are as follows:
216      *
217      *                       length
218      *                        Integer value that determines the maximum length
219      *                        of the large object field. If this argument is
220      *                        missing the field should be declared to have the
221      *                        longest length allowed by the DBMS.
222      *
223      *                       notnull
224      *                        Boolean flag that indicates whether this field is
225      *                        constrained to not be set to null.
226      * @return string  DBMS specific SQL code portion that should be used to
227      *                 declare the specified field.
228      * @access protected
229      */
230     function _getBLOBDeclaration($name, $field)
231     {
232         return "$name BLOB".((array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '');
233     }
234
235     // }}}
236     // {{{ _getBooleanDeclaration()
237
238     /**
239      * Obtain DBMS specific SQL code portion needed to declare a boolean type
240      * field to be used in statements like CREATE TABLE.
241      *
242      * @param string $name name the field to be declared.
243      * @param array $field associative array with the name of the properties
244      *       of the field being declared as array indexes. Currently, the types
245      *       of supported field properties are as follows:
246      *
247      *       default
248      *           Boolean value to be used as default for this field.
249      *
250      *       notnullL
251      *           Boolean flag that indicates whether this field is constrained
252      *           to not be set to NULL.
253      * @return string DBMS specific SQL code portion that should be used to
254      *       declare the specified field.
255      * @access protected
256      */
257     function _getBooleanDeclaration($name, $field)
258     {
259         $default = array_key_exists('default', $field) ? ' DEFAULT '.
260             $this->quote($field['default'], 'boolean') : '';
261         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
262         return $name.' BOOLEAN)'.$default.$notnull;
263     }
264
265     // }}}
266     // {{{ _getDateDeclaration()
267
268     /**
269      * Obtain DBMS specific SQL code portion needed to declare an date type
270      * field to be used in statements like CREATE TABLE.
271      *
272      * @param string  $name   name the field to be declared.
273      * @param string  $field  associative array with the name of the properties
274      *                        of the field being declared as array indexes.
275      *                        Currently, the types of supported field properties
276      *                        are as follows:
277      *
278      *                       default
279      *                        Date value to be used as default for this field.
280      *
281      *                       notnull
282      *                        Boolean flag that indicates whether this field is
283      *                        constrained to not be set to null.
284      * @return string  DBMS specific SQL code portion that should be used to
285      *                 declare the specified field.
286      * @access protected
287      */
288     function _getDateDeclaration($name, $field)
289     {
290         $default = array_key_exists('default', $field) ? ' DEFAULT DATE '.
291             $this->quote($field['default'], 'date') : '';
292         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
293         return $name.' DATE'.$default.$notnull;
294     }
295
296     // }}}
297     // {{{ _getTimestampDeclaration()
298
299     /**
300      * Obtain DBMS specific SQL code portion needed to declare an timestamp
301      * type field to be used in statements like CREATE TABLE.
302      *
303      * @param string  $name   name the field to be declared.
304      * @param string  $field  associative array with the name of the properties
305      *                        of the field being declared as array indexes.
306      *                        Currently, the types of supported field
307      *                        properties are as follows:
308      *
309      *                       default
310      *                        Time stamp value to be used as default for this
311      *                        field.
312      *
313      *                       notnull
314      *                        Boolean flag that indicates whether this field is
315      *                        constrained to not be set to null.
316      * @return string  DBMS specific SQL code portion that should be used to
317      *                 declare the specified field.
318      * @access protected
319      */
320     function _getTimestampDeclaration($name, $field)
321     {
322         $default = array_key_exists('default', $field) ? ' DEFAULT TIMESTAMP '.
323             $this->quote($field['default'], 'timestamp') : '';
324         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
325         return $name.' TIMESTAMP'.$default.$notnull;
326     }
327
328     // }}}
329     // {{{ _getTimeDeclaration()
330
331     /**
332      * Obtain DBMS specific SQL code portion needed to declare an time type
333      * field to be used in statements like CREATE TABLE.
334      *
335      * @param string  $name   name the field to be declared.
336      * @param string  $field  associative array with the name of the properties
337      *                        of the field being declared as array indexes.
338      *                        Currently, the types of supported field
339      *                        properties are as follows:
340      *
341      *                       default
342      *                        Time value to be used as default for this field.
343      *
344      *                       notnull
345      *                        Boolean flag that indicates whether this field is
346      *                        constrained to not be set to null.
347      * @return string  DBMS specific SQL code portion that should be used to
348      *                 declare the specified field.
349      * @access protected
350      */
351     function _getTimeDeclaration($name, $field)
352     {
353         $default = array_key_exists('default', $field) ? ' DEFAULT TIME '.
354             $this->quote($field['default'], 'time') : '';
355         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
356         return $name.' TIME'.$default.$notnull;
357     }
358
359     // }}}
360     // {{{ _getFloatDeclaration()
361
362     /**
363      * Obtain DBMS specific SQL code portion needed to declare an float type
364      * field to be used in statements like CREATE TABLE.
365      *
366      * @param string  $name   name the field to be declared.
367      * @param string  $field  associative array with the name of the properties
368      *                        of the field being declared as array indexes.
369      *                        Currently, the types of supported field
370      *                        properties are as follows:
371      *
372      *                       default
373      *                        Integer value to be used as default for this
374      *                        field.
375      *
376      *                       notnull
377      *                        Boolean flag that indicates whether this field is
378      *                        constrained to not be set to null.
379      * @return string  DBMS specific SQL code portion that should be used to
380      *                 declare the specified field.
381      * @access protected
382      */
383     function _getFloatDeclaration($name, $field)
384     {
385         $default = array_key_exists('default', $field) ? ' DEFAULT '.
386             $this->quote($field['default'], 'float') : '';
387         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
388         return $name.' FLOAT'.$default.$notnull;
389     }
390
391     // }}}
392     // {{{ _getDecimalDeclaration()
393
394     /**
395      * Obtain DBMS specific SQL code portion needed to declare an decimal type
396      * field to be used in statements like CREATE TABLE.
397      *
398      * @param string  $name   name the field to be declared.
399      * @param string  $field  associative array with the name of the properties
400      *                        of the field being declared as array indexes.
401      *                        Currently, the types of supported field
402      *                        properties are as follows:
403      *
404      *                       default
405      *                        Integer value to be used as default for this
406      *                        field.
407      *
408      *                       notnull
409      *                        Boolean flag that indicates whether this field is
410      *                        constrained to not be set to null.
411      * @return string  DBMS specific SQL code portion that should be used to
412      *                 declare the specified field.
413      * @access protected
414      */
415     function _getDecimalDeclaration($name, $field)
416     {
417         $db =& $this->getDBInstance();
418         if (PEAR::isError($db)) {
419             return $db;
420         }
421
422         $type = 'DECIMAL(18,'.$db->options['decimal_places'].')';
423         $default = array_key_exists('default', $field) ? ' DEFAULT '.
424             $this->quote($field['default'], 'decimal') : '';
425         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
426         return $name.' '.$type.$default.$notnull;
427     }
428
429     // }}}
430     // {{{ _quoteBLOB()
431
432     /**
433      * Convert a text value into a DBMS specific format that is suitable to
434      * compose query statements.
435      *
436      * @param           $value
437      * @return string  text string that represents the given argument value in
438      *                 a DBMS specific format.
439      * @access protected
440      */
441     function _quoteBLOB($value)
442     {
443         $value = $this->_readFile($value);
444         return "'".addslashes($value)."'";
445     }
446
447     // }}}
448     // {{{ _quoteBoolean()
449
450     /**
451      * Convert a text value into a DBMS specific format that is suitable to
452      * compose query statements.
453      *
454      * @param string $value text string value that is intended to be converted.
455      * @return string text string that represents the given argument value in
456      *       a DBMS specific format.
457      * @access protected
458      */
459     function _quoteBoolean($value)
460     {
461         return ($value ? 'True' : 'False');
462     }
463
464     // }}}
465     // {{{ _quoteDate()
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 _quoteDate($value)
477     {
478         return 'DATE'.$this->_quoteText($value);
479     }
480
481     // }}}
482     // {{{ _quoteTimestamp()
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 _quoteTimestamp($value)
494     {
495         return 'TIMESTAMP'.$this->_quoteText($value);
496     }
497
498     // }}}
499     // {{{ _quoteTime()
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 _quoteTime($value)
511     {
512         return 'TIME'.$this->_quoteText($value);
513     }
514
515     // }}}
516     // {{{ _quoteFloat()
517
518     /**
519      * Convert a text value into a DBMS specific format that is suitable to
520      * compose query statements.
521      *
522      * @param string  $value text string value that is intended to be converted.
523      * @return string  text string that represents the given argument value in
524      *                 a DBMS specific format.
525      * @access protected
526      */
527     function _quoteFloat($value)
528     {
529         return (float)$value;
530     }
531
532     // }}}
533     // {{{ _quoteDecimal()
534
535     /**
536      * Convert a text value into a DBMS specific format that is suitable to
537      * compose query statements.
538      *
539      * @param string  $value text string value that is intended to be converted.
540      * @return string  text string that represents the given argument value in
541      *                 a DBMS specific format.
542      * @access protected
543      */
544     function _quoteDecimal($value)
545     {
546         $db =& $this->getDBInstance();
547         if (PEAR::isError($db)) {
548             return $db;
549         }
550
551         return $db->escape($value);
552     }
553 }
554
555 ?>