MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file oci8.php

Documentation is available at oci8.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 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: oci8.php 327310 2012-08-27 15:16:18Z danielc $
  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.     // {{{ _baseConvertResult()
  59.  
  60.     /**
  61.      * general type conversion method
  62.      *
  63.      * @param mixed   $value refernce to a value to be converted
  64.      * @param string  $type  specifies which type to convert to
  65.      * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
  66.      * @return object MDB2 error on failure
  67.      * @access protected
  68.      */
  69.     function _baseConvertResult($value$type$rtrim = true)
  70.     {
  71.         if (null === $value{
  72.             return null;
  73.         }
  74.         switch ($type{
  75.         case 'text':
  76.             if (is_object($value&& is_a($value'OCI-Lob')) {
  77.                 //LOB => fetch into variable
  78.                 $clob $this->_baseConvertResult($value'clob'$rtrim);
  79.                 if (!MDB2::isError($clob&& is_resource($clob)) {
  80.                     $clob_value '';
  81.                     while (!feof($clob)) {
  82.                         $clob_value .= fread($clob8192);
  83.                     }
  84.                     $this->destroyLOB($clob);
  85.                 }
  86.                 $value $clob_value;
  87.             }
  88.             if ($rtrim{
  89.                 $value rtrim($value);
  90.             }
  91.             return $value;
  92.         case 'date':
  93.             return substr($value0strlen('YYYY-MM-DD'));
  94.         case 'time':
  95.             return substr($valuestrlen('YYYY-MM-DD ')strlen('HH:MI:SS'));
  96.         }
  97.         return parent::_baseConvertResult($value$type$rtrim);
  98.     }
  99.  
  100.     // }}}
  101.     // {{{ getTypeDeclaration()
  102.  
  103.     /**
  104.      * Obtain DBMS specific SQL code portion needed to declare an text type
  105.      * field to be used in statements like CREATE TABLE.
  106.      *
  107.      * @param array $field  associative array with the name of the properties
  108.      *       of the field being declared as array indexes. Currently, the types
  109.      *       of supported field properties are as follows:
  110.      *
  111.      *       length
  112.      *           Integer value that determines the maximum length of the text
  113.      *           field. If this argument is missing the field should be
  114.      *           declared to have the longest length allowed by the DBMS.
  115.      *
  116.      *       default
  117.      *           Text value to be used as default for this field.
  118.      *
  119.      *       notnull
  120.      *           Boolean flag that indicates whether this field is constrained
  121.      *           to not be set to null.
  122.      * @return string  DBMS specific SQL code portion that should be used to
  123.      *       declare the specified field.
  124.      * @access public
  125.      */
  126.     function getTypeDeclaration($field)
  127.     {
  128.         $db $this->getDBInstance();
  129.         if (MDB2::isError($db)) {
  130.             return $db;
  131.         }
  132.  
  133.         switch ($field['type']{
  134.         case 'text':
  135.             $length !empty($field['length'])
  136.                 ? $field['length'$db->options['default_text_field_length'];
  137.             $fixed !empty($field['fixed']$field['fixed': false;
  138.             return $fixed 'CHAR('.$length.')' 'VARCHAR2('.$length.')';
  139.         case 'clob':
  140.             return 'CLOB';
  141.         case 'blob':
  142.             return 'BLOB';
  143.         case 'integer':
  144.             if (!empty($field['length'])) {
  145.                 switch((int)$field['length']{
  146.                     case 1:  $digit = 3;  break;
  147.                     case 2:  $digit = 5;  break;
  148.                     case 3:  $digit = 8;  break;
  149.                     case 4:  $digit = 10; break;
  150.                     case 5:  $digit = 13; break;
  151.                     case 6:  $digit = 15; break;
  152.                     case 7:  $digit = 17; break;
  153.                     case 8:  $digit = 20; break;
  154.                     default: $digit = 10;
  155.                 }
  156.                 return 'NUMBER('.$digit.')';
  157.             }
  158.             return 'INT';
  159.         case 'boolean':
  160.             return 'NUMBER(1)';
  161.         case 'date':
  162.         case 'time':
  163.         case 'timestamp':
  164.             return 'DATE';
  165.         case 'float':
  166.             return 'NUMBER';
  167.         case 'decimal':
  168.             $scale !empty($field['scale']$field['scale'$db->options['decimal_places'];
  169.             return 'NUMBER(*,'.$scale.')';
  170.         }
  171.     }
  172.  
  173.     // }}}
  174.     // {{{ _quoteCLOB()
  175.  
  176.     /**
  177.      * Convert a text value into a DBMS specific format that is suitable to
  178.      * compose query statements.
  179.      *
  180.      * @param string $value text string value that is intended to be converted.
  181.      * @param bool $quote determines if the value should be quoted and escaped
  182.      * @param bool $escape_wildcards if to escape escape wildcards
  183.      * @return string text string that represents the given argument value in
  184.      *         a DBMS specific format.
  185.      * @access protected
  186.      */
  187.     function _quoteCLOB($value$quote$escape_wildcards)
  188.     {
  189.         return 'EMPTY_CLOB()';
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ _quoteBLOB()
  194.  
  195.     /**
  196.      * Convert a text value into a DBMS specific format that is suitable to
  197.      * compose query statements.
  198.      *
  199.      * @param string $value text string value that is intended to be converted.
  200.      * @param bool $quote determines if the value should be quoted and escaped
  201.      * @param bool $escape_wildcards if to escape escape wildcards
  202.      * @return string text string that represents the given argument value in
  203.      *         a DBMS specific format.
  204.      * @access protected
  205.      */
  206.     function _quoteBLOB($value$quote$escape_wildcards)
  207.     {
  208.         return 'EMPTY_BLOB()';
  209.     }
  210.  
  211.     // }}}
  212.     // {{{ _quoteDate()
  213.  
  214.     /**
  215.      * Convert a text value into a DBMS specific format that is suitable to
  216.      * compose query statements.
  217.      *
  218.      * @param string $value text string value that is intended to be converted.
  219.      * @param bool $quote determines if the value should be quoted and escaped
  220.      * @param bool $escape_wildcards if to escape escape wildcards
  221.      * @return string text string that represents the given argument value in
  222.      *         a DBMS specific format.
  223.      * @access protected
  224.      */
  225.     function _quoteDate($value$quote$escape_wildcards)
  226.     {
  227.        return $this->_quoteText("$value 00:00:00"$quote$escape_wildcards);
  228.     }
  229.  
  230.     // }}}
  231.     // {{{ _quoteTimestamp()
  232.  
  233.     /**
  234.      * Convert a text value into a DBMS specific format that is suitable to
  235.      * compose query statements.
  236.      *
  237.      * @param string $value text string value that is intended to be converted.
  238.      * @param bool $quote determines if the value should be quoted and escaped
  239.      * @param bool $escape_wildcards if to escape escape wildcards
  240.      * @return string text string that represents the given argument value in
  241.      *         a DBMS specific format.
  242.      * @access protected
  243.      */
  244.     function _quoteTimestamp($value$quote$escape_wildcards)
  245.     {
  246.        return $this->_quoteText($value$quote$escape_wildcards);
  247.     }
  248.  
  249.     // }}}
  250.     // {{{ _quoteTime()
  251.  
  252.     /**
  253.      * Convert a text value into a DBMS specific format that is suitable to
  254.      *        compose query statements.
  255.      *
  256.      * @param string $value text string value that is intended to be converted.
  257.      * @param bool $quote determines if the value should be quoted and escaped
  258.      * @param bool $escape_wildcards if to escape escape wildcards
  259.      * @return string text string that represents the given argument value in
  260.      *         a DBMS specific format.
  261.      * @access protected
  262.      */
  263.     function _quoteTime($value$quote$escape_wildcards)
  264.     {
  265.        return $this->_quoteText("0001-01-01 $value"$quote$escape_wildcards);
  266.     }
  267.  
  268.     // }}}
  269.     // {{{ writeLOBToFile()
  270.  
  271.     /**
  272.      * retrieve LOB from the database
  273.      *
  274.      * @param array $lob array
  275.      * @param string $file name of the file into which the LOb should be fetched
  276.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  277.      * @access protected
  278.      */
  279.     function writeLOBToFile($lob$file)
  280.     {
  281.         if (preg_match('/^(\w+:\/\/)(.*)$/'$file$match)) {
  282.             if ($match[1== 'file://'{
  283.                 $file $match[2];
  284.             }
  285.         }
  286.         $lob_data stream_get_meta_data($lob);
  287.         $lob_index $lob_data['wrapper_data']->lob_index;
  288.         $result $this->lobs[$lob_index]['resource']->writetofile($file);
  289.         $this->lobs[$lob_index]['resource']->seek(0);
  290.         if (!$result{
  291.             $db $this->getDBInstance();
  292.             if (MDB2::isError($db)) {
  293.                 return $db;
  294.             }
  295.  
  296.             return $db->raiseError(nullnullnull,
  297.                 'Unable to write LOB to file'__FUNCTION__);
  298.         }
  299.         return MDB2_OK;
  300.     }
  301.  
  302.     // }}}
  303.     // {{{ _retrieveLOB()
  304.  
  305.     /**
  306.      * retrieve LOB from the database
  307.      *
  308.      * @param array $lob array
  309.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  310.      * @access protected
  311.      */
  312.     function _retrieveLOB(&$lob)
  313.     {
  314.         if (!is_object($lob['resource'])) {
  315.             $db $this->getDBInstance();
  316.             if (MDB2::isError($db)) {
  317.                 return $db;
  318.             }
  319.  
  320.            return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  321.                'attemped to retrieve LOB from non existing or NULL column'__FUNCTION__);
  322.         }
  323.  
  324.         if (!$lob['loaded']
  325. #            && !method_exists($lob['resource'], 'read')
  326.         {
  327.             $lob['value'$lob['resource']->load();
  328.             $lob['resource']->seek(0);
  329.         }
  330.         $lob['loaded'= true;
  331.         return MDB2_OK;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ _readLOB()
  336.  
  337.     /**
  338.      * Read data from large object input stream.
  339.      *
  340.      * @param array $lob array
  341.      * @param blob $data reference to a variable that will hold data to be
  342.      *       read from the large object input stream
  343.      * @param int $length integer value that indicates the largest ammount of
  344.      *       data to be read from the large object input stream.
  345.      * @return mixed length on success, a MDB2 error on failure
  346.      * @access protected
  347.      */
  348.     function _readLOB($lob$length)
  349.     {
  350.         if ($lob['loaded']{
  351.             return parent::_readLOB($lob$length);
  352.         }
  353.  
  354.         if (!is_object($lob['resource'])) {
  355.             $db $this->getDBInstance();
  356.             if (MDB2::isError($db)) {
  357.                 return $db;
  358.             }
  359.  
  360.            return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  361.                'attemped to retrieve LOB from non existing or NULL column'__FUNCTION__);
  362.         }
  363.  
  364.         $data $lob['resource']->read($length);
  365.         if (!is_string($data)) {
  366.             $db $this->getDBInstance();
  367.             if (MDB2::isError($db)) {
  368.                 return $db;
  369.             }
  370.  
  371.             return $db->raiseError(nullnullnull,
  372.                     'Unable to read LOB'__FUNCTION__);
  373.         }
  374.         return $data;
  375.     }
  376.  
  377.     // }}}
  378.     // {{{ patternEscapeString()
  379.  
  380.     /**
  381.      * build string to define escape pattern string
  382.      *
  383.      * @access public
  384.      *
  385.      *
  386.      * @return string define escape pattern
  387.      */
  388.     function patternEscapeString()
  389.     {
  390.         $db $this->getDBInstance();
  391.         if (MDB2::isError($db)) {
  392.             return $db;
  393.         }
  394.         return " ESCAPE '"$db->string_quoting['escape_pattern'."'";
  395.     }
  396.  
  397.     // }}}
  398.     // {{{ _mapNativeDatatype()
  399.  
  400.     /**
  401.      * Maps a native array description of a field to a MDB2 datatype and length
  402.      *
  403.      * @param array  $field native field description
  404.      * @return array containing the various possible types, length, sign, fixed
  405.      * @access public
  406.      */
  407.     function _mapNativeDatatype($field)
  408.     {
  409.         $db_type strtolower($field['type']);
  410.         $type = array();
  411.         $length $unsigned $fixed = null;
  412.         if (!empty($field['length'])) {
  413.             $length $field['length'];
  414.         }
  415.         switch ($db_type{
  416.         case 'integer':
  417.         case 'pls_integer':
  418.         case 'binary_integer':
  419.             $type['integer';
  420.             if ($length == '1'{
  421.                 $type['boolean';
  422.                 if (preg_match('/^(is|has)/'$field['name'])) {
  423.                     $type array_reverse($type);
  424.                 }
  425.             }
  426.             break;
  427.         case 'varchar':
  428.         case 'varchar2':
  429.         case 'nvarchar2':
  430.             $fixed = false;
  431.         case 'char':
  432.         case 'nchar':
  433.             $type['text';
  434.             if ($length == '1'{
  435.                 $type['boolean';
  436.                 if (preg_match('/^(is|has)/'$field['name'])) {
  437.                     $type array_reverse($type);
  438.                 }
  439.             }
  440.             if ($fixed !== false{
  441.                 $fixed = true;
  442.             }
  443.             break;
  444.         case 'date':
  445.         case 'timestamp':
  446.             $type['timestamp';
  447.             $length = null;
  448.             break;
  449.         case 'float':
  450.             $type['float';
  451.             break;
  452.         case 'number':
  453.             if (!empty($field['scale'])) {
  454.                 $type['decimal';
  455.                 $length $length.','.$field['scale'];
  456.             else {
  457.                 $type['integer';
  458.                 if ($length == '1'{
  459.                     $type['boolean';
  460.                     if (preg_match('/^(is|has)/'$field['name'])) {
  461.                         $type array_reverse($type);
  462.                     }
  463.                 }
  464.             }
  465.             break;
  466.         case 'long':
  467.             $type['text';
  468.         case 'clob':
  469.         case 'nclob':
  470.             $type['clob';
  471.             break;
  472.         case 'blob':
  473.         case 'raw':
  474.         case 'long raw':
  475.         case 'bfile':
  476.             $type['blob';
  477.             $length = null;
  478.             break;
  479.         case 'rowid':
  480.         case 'urowid':
  481.         default:
  482.             $db $this->getDBInstance();
  483.             if (MDB2::isError($db)) {
  484.                 return $db;
  485.             }
  486.  
  487.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  488.                 'unknown database attribute type: '.$db_type__FUNCTION__);
  489.         }
  490.  
  491.         if ((int)$length <= 0{
  492.             $length = null;
  493.         }
  494.  
  495.         return array($type$length$unsigned$fixed);
  496.     }
  497. }
  498.  
  499. ?>

Documentation generated on Mon, 11 Mar 2019 15:51:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.