thomascube
2006-05-01 6204390af16bcf50f82da61a1aefc2ad0c0adf94
commit | author | age
c9462d 1 <?php
S 2 // +----------------------------------------------------------------------+
3 // | PHP versions 4 and 5                                                 |
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
6 // | Stig. S. Bakken, Lukas Smith                                         |
7 // | All rights reserved.                                                 |
8 // +----------------------------------------------------------------------+
9 // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
10 // | API as well as database abstraction for PHP applications.            |
11 // | This LICENSE is in the BSD license style.                            |
12 // |                                                                      |
13 // | Redistribution and use in source and binary forms, with or without   |
14 // | modification, are permitted provided that the following conditions   |
15 // | are met:                                                             |
16 // |                                                                      |
17 // | Redistributions of source code must retain the above copyright       |
18 // | notice, this list of conditions and the following disclaimer.        |
19 // |                                                                      |
20 // | Redistributions in binary form must reproduce the above copyright    |
21 // | notice, this list of conditions and the following disclaimer in the  |
22 // | documentation and/or other materials provided with the distribution. |
23 // |                                                                      |
24 // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
25 // | Lukas Smith nor the names of his contributors may be used to endorse |
26 // | or promote products derived from this software without specific prior|
27 // | written permission.                                                  |
28 // |                                                                      |
29 // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
30 // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
31 // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
32 // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
33 // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
34 // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
35 // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
36 // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
37 // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
38 // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
39 // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
40 // | POSSIBILITY OF SUCH DAMAGE.                                          |
41 // +----------------------------------------------------------------------+
42 // | Author: Lukas Smith <smith@pooteeweet.org>                           |
43 // +----------------------------------------------------------------------+
44
45 // $Id$
46
47 require_once 'MDB2/Driver/Datatype/Common.php';
48
49 /**
50  * MDB2 OCI8 driver
51  *
52  * @package MDB2
53  * @category Database
54  * @author Lukas Smith <smith@pooteeweet.org>
55  */
56 class MDB2_Driver_Datatype_oci8 extends MDB2_Driver_Datatype_Common
57 {
58     // {{{ convertResult()
59
60     /**
61      * convert a value to a RDBMS indepdenant MDB2 type
62      *
63      * @param mixed $value value to be converted
64      * @param int $type constant that specifies which type to convert to
65      * @return mixed converted value
66      * @access public
67      */
68     function convertResult($value, $type)
69     {
70         if (is_null($value)) {
71             return null;
72         }
73         switch ($type) {
74         case 'date':
75             return substr($value, 0, strlen('YYYY-MM-DD'));
76         case 'time':
77             return substr($value, strlen('YYYY-MM-DD '), strlen('HH:MI:SS'));
78         default:
79             return $this->_baseConvertResult($value, $type);
80         }
81     }
82
83     // }}}
84     // {{{ getTypeDeclaration()
85
86     /**
87      * Obtain DBMS specific SQL code portion needed to declare an text type
88      * field to be used in statements like CREATE TABLE.
89      *
90      * @param array $field  associative array with the name of the properties
91      *      of the field being declared as array indexes. Currently, the types
92      *      of supported field properties are as follows:
93      *
94      *      length
95      *          Integer value that determines the maximum length of the text
96      *          field. If this argument is missing the field should be
97      *          declared to have the longest length allowed by the DBMS.
98      *
99      *      default
100      *          Text value to be used as default for this field.
101      *
102      *      notnull
103      *          Boolean flag that indicates whether this field is constrained
104      *          to not be set to null.
105      * @return string  DBMS specific SQL code portion that should be used to
106      *      declare the specified field.
107      * @access public
108      */
109     function getTypeDeclaration($field)
110     {
111         $db =& $this->getDBInstance();
112         if (PEAR::isError($db)) {
113             return $db;
114         }
115
116         switch ($field['type']) {
117         case 'text':
118             $length = (array_key_exists('length', $field) ? $field['length'] : (($length = $db->options['default_text_field_length']) ? $length : 4000));
119             return 'VARCHAR ('.$length.')';
120         case 'clob':
121             return 'CLOB';
122         case 'blob':
123             return 'BLOB';
124         case 'integer':
125             return 'INT';
126         case 'boolean':
127             return 'CHAR (1)';
128         case 'date':
129         case 'time':
130         case 'timestamp':
131             return 'DATE';
132         case 'float':
133             return 'NUMBER';
134         case 'decimal':
135             return 'NUMBER(*,'.$db->options['decimal_places'].')';
136         }
137     }
138
139     // }}}
140     // {{{ _getIntegerDeclaration()
141
142     /**
143      * Obtain DBMS specific SQL code portion needed to declare an integer type
144      * field to be used in statements like CREATE TABLE.
145      *
146      * @param string $name name the field to be declared.
147      * @param array $field associative array with the name of the properties
148      *       of the field being declared as array indexes. Currently, the types
149      *       of supported field properties are as follows:
150      *
151      *       unsigned
152      *           Boolean flag that indicates whether the field should be
153      *           declared as unsigned integer if possible.
154      *
155      *       default
156      *           Integer value to be used as default for this field.
157      *
158      *       notnull
159      *           Boolean flag that indicates whether this field is constrained
160      *           to not be set to null.
161      * @param string $table name of the current table being processed
162      *           by alterTable(), used for autoincrement emulation
163      * @return string DBMS specific SQL code portion that should be used to
164      *       declare the specified field.
165      * @access protected
166      */
167     function _getIntegerDeclaration($name, $field, $table = null)
168     {
169         if (array_key_exists('unsigned', $field) && $field['unsigned']) {
170             $db =& $this->getDBInstance();
171             if (PEAR::isError($db)) {
172                 return $db;
173             }
174             $db->warning[] = "unsigned integer field \"$name\" is being declared as signed integer";
175         }
176
177         $default = array_key_exists('default', $field) ? ' DEFAULT '.
178             $this->quote($field['default'], 'integer') : '';
179         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
180         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
181     }
182
183     // }}}
184     // {{{ _getTextDeclaration()
185
186     /**
187      * Obtain DBMS specific SQL code portion needed to declare an text type
188      * field to be used in statements like CREATE TABLE.
189      *
190      * @param string $name name the field to be declared.
191      * @param array $field associative array with the name of the properties
192      *        of the field being declared as array indexes. Currently, the types
193      *        of supported field properties are as follows:
194      *
195      *        length
196      *            Integer value that determines the maximum length of the text
197      *            field. If this argument is missing the field should be
198      *            declared to have the longest length allowed by the DBMS.
199      *
200      *        default
201      *            Text value to be used as default for this field.
202      *
203      *        notnull
204      *            Boolean flag that indicates whether this field is constrained
205      *            to not be set to null.
206      * @return string DBMS specific SQL code portion that should be used to
207      *        declare the specified field.
208      * @access protected
209      */
210     function _getTextDeclaration($name, $field)
211     {
212         $type = $this->getTypeDeclaration($field);
213         $default = array_key_exists('default', $field) ? ' DEFAULT '.
214             $this->quote($field['default'], 'text') : '';
215         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
216         return $name.' '.$type.$default.$notnull;
217     }
218
219     // }}}
220     // {{{ _getCLOBDeclaration()
221
222     /**
223      * Obtain DBMS specific SQL code portion needed to declare an character
224      * large object type field to be used in statements like CREATE TABLE.
225      *
226      * @param string $name name the field to be declared.
227      * @param array $field associative array with the name of the properties
228      *        of the field being declared as array indexes. Currently, the types
229      *        of supported field properties are as follows:
230      *
231      *        length
232      *            Integer value that determines the maximum length of the large
233      *            object field. If this argument is missing the field should be
234      *            declared to have the longest length allowed by the DBMS.
235      *
236      *        notnull
237      *            Boolean flag that indicates whether this field is constrained
238      *            to not be set to null.
239      * @return string DBMS specific SQL code portion that should be used to
240      *        declare the specified field.
241      * @access public
242      */
243     function _getCLOBDeclaration($name, $field)
244     {
245         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
246         return $name.' '.$this->getTypeDeclaration($field).$notnull;
247     }
248
249     // }}}
250     // {{{ _getBLOBDeclaration()
251
252     /**
253      * Obtain DBMS specific SQL code portion needed to declare an binary large
254      * object type field to be used in statements like CREATE TABLE.
255      *
256      * @param string $name name the field to be declared.
257      * @param array $field associative array with the name of the properties
258      *        of the field being declared as array indexes. Currently, the types
259      *        of supported field properties are as follows:
260      *
261      *        length
262      *            Integer value that determines the maximum length of the large
263      *            object field. If this argument is missing the field should be
264      *            declared to have the longest length allowed by the DBMS.
265      *
266      *        notnull
267      *            Boolean flag that indicates whether this field is constrained
268      *            to not be set to null.
269      * @return string DBMS specific SQL code portion that should be used to
270      *        declare the specified field.
271      * @access protected
272      */
273     function _getBLOBDeclaration($name, $field)
274     {
275         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
276         return $name.' '.$this->getTypeDeclaration($field).$notnull;
277     }
278
279     // }}}
280     // {{{ _getDateDeclaration()
281
282     /**
283      * Obtain DBMS specific SQL code portion needed to declare a date type
284      * field to be used in statements like CREATE TABLE.
285      *
286      * @param string $name name the field to be declared.
287      * @param array $field associative array with the name of the properties
288      *        of the field being declared as array indexes. Currently, the types
289      *        of supported field properties are as follows:
290      *
291      *        default
292      *            Date value to be used as default for this field.
293      *
294      *        notnull
295      *            Boolean flag that indicates whether this field is constrained
296      *            to not be set to null.
297      * @return string DBMS specific SQL code portion that should be used to
298      *        declare the specified field.
299      * @access protected
300      */
301     function _getDateDeclaration($name, $field)
302     {
303         $default = array_key_exists('default', $field) ? ' DEFAULT '.
304             $this->quote($field['default'], 'date') : '';
305         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
306         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
307     }
308
309     // }}}
310     // {{{ _getTimestampDeclaration()
311
312     /**
313      * Obtain DBMS specific SQL code portion needed to declare a timestamp
314      * field to be used in statements like CREATE TABLE.
315      *
316      * @param string $name name the field to be declared.
317      * @param array $field associative array with the name of the properties
318      *        of the field being declared as array indexes. Currently, the types
319      *        of supported field properties are as follows:
320      *
321      *        default
322      *            Timestamp value to be used as default for this field.
323      *
324      *        notnull
325      *            Boolean flag that indicates whether this field is constrained
326      *            to not be set to null.
327      * @return string DBMS specific SQL code portion that should be used to
328      *        declare the specified field.
329      * @access protected
330      */
331     function _getTimestampDeclaration($name, $field)
332     {
333         $default = array_key_exists('default', $field) ? ' DEFAULT '.
334             $this->quote($field['default'], 'timestamp') : '';
335         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
336         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;    }
337
338     // }}}
339     // {{{ _getTimeDeclaration()
340
341     /**
342      * Obtain DBMS specific SQL code portion needed to declare a time
343      * field to be used in statements like CREATE TABLE.
344      *
345      * @param string $name name the field to be declared.
346      * @param array $field associative array with the name of the properties
347      *        of the field being declared as array indexes. Currently, the types
348      *        of supported field properties are as follows:
349      *
350      *        default
351      *            Time value to be used as default for this field.
352      *
353      *        notnull
354      *            Boolean flag that indicates whether this field is constrained
355      *            to not be set to null.
356      * @return string DBMS specific SQL code portion that should be used to
357      *        declare the specified field.
358      * @access protected
359      */
360     function _getTimeDeclaration($name, $field)
361     {
362         $default = array_key_exists('default', $field) ? ' DEFAULT '.
363             $this->quote($field['default'], 'time') : '';
364         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
365         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
366     }
367
368     // }}}
369     // {{{ _getFloatDeclaration()
370
371     /**
372      * Obtain DBMS specific SQL code portion needed to declare a float type
373      * field to be used in statements like CREATE TABLE.
374      *
375      * @param string $name name the field to be declared.
376      * @param array $field associative array with the name of the properties
377      *        of the field being declared as array indexes. Currently, the types
378      *        of supported field properties are as follows:
379      *
380      *        default
381      *            Float value to be used as default for this field.
382      *
383      *        notnull
384      *            Boolean flag that indicates whether this field is constrained
385      *            to not be set to null.
386      * @return string DBMS specific SQL code portion that should be used to
387      *        declare the specified field.
388      * @access protected
389      */
390     function _getFloatDeclaration($name, $field)
391     {
392         $default = array_key_exists('default', $field) ? ' DEFAULT '.
393             $this->quote($field['default'], 'float') : '';
394         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
395         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
396     }
397
398     // }}}
399     // {{{ _getDecimalDeclaration()
400
401     /**
402      * Obtain DBMS specific SQL code portion needed to declare a decimal type
403      * field to be used in statements like CREATE TABLE.
404      *
405      * @param string $name name the field to be declared.
406      * @param array $field associative array with the name of the properties
407      *        of the field being declared as array indexes. Currently, the types
408      *        of supported field properties are as follows:
409      *
410      *        default
411      *            Decimal value to be used as default for this field.
412      *
413      *        notnull
414      *            Boolean flag that indicates whether this field is constrained
415      *            to not be set to null.
416      * @return string DBMS specific SQL code portion that should be used to
417      *        declare the specified field.
418      * @access protected
419      */
420     function _getDecimalDeclaration($name, $field)
421     {
422         $default = array_key_exists('default', $field) ? ' DEFAULT '.
423             $this->quote($field['default'], 'decimal') : '';
424         $notnull = (array_key_exists('notnull', $field) && $field['notnull']) ? ' NOT NULL' : '';
425         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
426     }
427
428     // }}}
429     // {{{ _quoteCLOB()
430
431     /**
432      * Convert a text value into a DBMS specific format that is suitable to
433      * compose query statements.
434      *
435      * @param  $value
436      * @return string text string that represents the given argument value in
437      *        a DBMS specific format.
438      * @access protected
439      */
440     function _quoteCLOB($value)
441     {
442         return 'EMPTY_CLOB()';
443     }
444
445     // }}}
446     // {{{ _quoteBLOB()
447
448     /**
449      * Convert a text value into a DBMS specific format that is suitable to
450      * compose query statements.
451      *
452      * @param  $value
453      * @return string text string that represents the given argument value in
454      *        a DBMS specific format.
455      * @access protected
456      */
457     function _quoteBLOB($value)
458     {
459         return 'EMPTY_BLOB()';
460     }
461
462     // }}}
463     // {{{ _quoteDate()
464
465     /**
466      * Convert a text value into a DBMS specific format that is suitable to
467      * compose query statements.
468      *
469      * @param string $value text string value that is intended to be converted.
470      * @return string text string that represents the given argument value in
471      *        a DBMS specific format.
472      * @access protected
473      */
474     function _quoteDate($value)
475     {
476        return $this->_quoteText("$value 00:00:00");
477     }
478
479     // }}}
480     // {{{ _quoteTimestamp()
481
482     /**
483      * Convert a text value into a DBMS specific format that is suitable to
484      * compose query statements.
485      *
486      * @param string $value text string value that is intended to be converted.
487      * @return string text string that represents the given argument value in
488      *        a DBMS specific format.
489      * @access protected
490      */
491     function _quoteTimestamp($value)
492     {
493        return $this->_quoteText($value);
494     }
495
496     // }}}
497     // {{{ _quoteTime()
498
499     /**
500      * Convert a text value into a DBMS specific format that is suitable to
501      *        compose query statements.
502      *
503      * @param string $value text string value that is intended to be converted.
504      * @return string text string that represents the given argument value in
505      *        a DBMS specific format.
506      * @access protected
507      */
508     function _quoteTime($value)
509     {
510        return $this->_quoteText("0001-01-01 $value");
511     }
512
513     // }}}
514     // {{{ _quoteFloat()
515
516     /**
517      * Convert a text value into a DBMS specific format that is suitable to
518      * compose query statements.
519      *
520      * @param string $value text string value that is intended to be converted.
521      * @return string text string that represents the given argument value in
522      *        a DBMS specific format.
523      * @access protected
524      */
525     function _quoteFloat($value)
526     {
527         return (float)$value;
528     }
529
530     // }}}
531     // {{{ _quoteDecimal()
532
533     /**
534      * Convert a text value into a DBMS specific format that is suitable to
535      * compose query statements.
536      *
537      * @param string $value text string value that is intended to be converted.
538      * @return string text string that represents the given argument value in
539      *        a DBMS specific format.
540      * @access protected
541      */
542     function _quoteDecimal($value)
543     {
544         $db =& $this->getDBInstance();
545         if (PEAR::isError($db)) {
546             return $db;
547         }
548
549         return $db->escape($value);
550     }
551
552     // }}}
553     // {{{ writeLOBToFile()
554
555     /**
556      * retrieve LOB from the database
557      *
558      * @param resource $lob stream handle
559      * @param string $file name of the file into which the LOb should be fetched
560      * @return mixed MDB2_OK on success, a MDB2 error on failure
561      * @access protected
562      */
563     function writeLOBToFile($lob, $file)
564     {
565         $lob_data = stream_get_meta_data($lob);
566         $lob_index = $lob_data['wrapper_data']->lob_index;
567         if (!@$this->lobs[$lob_index]['value']->writelobtofile($file)) {
568             $db =& $this->getDBInstance();
569             if (PEAR::isError($db)) {
570                 return $db;
571             }
572
573             return $db->raiseError();
574         }
575         return MDB2_OK;
576     }
577
578     // }}}
579     // {{{ _retrieveLOB()
580
581     /**
582      * retrieve LOB from the database
583      *
584      * @param int $lob_index from the lob array
585      * @return mixed MDB2_OK on success, a MDB2 error on failure
586      * @access protected
587      */
588     function _retrieveLOB(&$lob)
589     {
590         if (!array_key_exists('loaded', $lob)) {
591             if (!is_object($lob['ressource'])) {
592                 $db =& $this->getDBInstance();
593                 if (PEAR::isError($db)) {
594                     return $db;
595                 }
596
597                return $db->raiseError(MDB2_ERROR, null, null,
598                    'attemped to retrieve LOB from non existing or NULL column');
599             }
600             $lob['value'] = $lob['ressource']->load();
601             $lob['loaded'] = true;
602         }
603         return MDB2_OK;
604     }
605 }
606
607 ?>