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

Source for file ibase.php

Documentation is available at ibase.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5                                                 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith, Lorenzo Alberton                       |
  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. // |         Lorenzo Alberton <l.alberton@quipo.it>                       |
  45. // +----------------------------------------------------------------------+
  46. //
  47. // $Id: ibase.php 327310 2012-08-27 15:16:18Z danielc $
  48.  
  49. require_once 'MDB2/Driver/Datatype/Common.php';
  50.  
  51. /**
  52.  * MDB2 Firebird/Interbase driver
  53.  *
  54.  * @package MDB2
  55.  * @category Database
  56.  * @author  Lukas Smith <smith@pooteeweet.org>
  57.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  58.  */
  59. class MDB2_Driver_Datatype_ibase extends MDB2_Driver_Datatype_Common
  60. {
  61.     // {{{ _baseConvertResult()
  62.  
  63.     /**
  64.      * General type conversion method
  65.      *
  66.      * @param mixed   $value refernce to a value to be converted
  67.      * @param string  $type  specifies which type to convert to
  68.      * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
  69.      * @return object MDB2 error on failure
  70.      * @access protected
  71.      */
  72.     function _baseConvertResult($value$type$rtrim = true)
  73.     {
  74.         if (null === $value{
  75.             return null;
  76.         }
  77.         $db $this->getDBInstance();
  78.         if (MDB2::isError($db)) {
  79.             return $db;
  80.         }
  81.         if (MDB2::isError($connection)) {
  82.             return $connection;
  83.         }
  84.  
  85.         switch ($type{
  86.         case 'text':
  87.             $blob_info @ibase_blob_info($connection$value);
  88.             if (is_array($blob_info&& $blob_info['length'> 0{
  89.                 //LOB => fetch into variable
  90.                 $clob $this->_baseConvertResult($value'clob'$rtrim);
  91.                 if (!MDB2::isError($clob&& is_resource($clob)) {
  92.                     $clob_value '';
  93.                     while (!feof($clob)) {
  94.                         $clob_value .= fread($clob8192);
  95.                     }
  96.                     $this->destroyLOB($clob);
  97.                 }
  98.                 $value $clob_value;
  99.             }
  100.             if ($rtrim{
  101.                 $value rtrim($value);
  102.             }
  103.             return $value;
  104.         case 'timestamp':
  105.             return substr($value0strlen('YYYY-MM-DD HH:MM:SS'));
  106.         }
  107.         return parent::_baseConvertResult($value$type$rtrim);
  108.     }
  109.  
  110.     // }}}
  111.     // {{{ _getCharsetFieldDeclaration()
  112.  
  113.     /**
  114.      * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
  115.      * of a field declaration to be used in statements like CREATE TABLE.
  116.      *
  117.      * @param string $charset   name of the charset
  118.      * @return string  DBMS specific SQL code portion needed to set the CHARACTER SET
  119.      *                  of a field declaration.
  120.      */
  121.     function _getCharsetFieldDeclaration($charset)
  122.     {
  123.         return 'CHARACTER SET '.$charset;
  124.     }
  125.  
  126.     // }}}
  127.     // {{{ _getCollationFieldDeclaration()
  128.  
  129.     /**
  130.      * Obtain DBMS specific SQL code portion needed to set the COLLATION
  131.      * of a field declaration to be used in statements like CREATE TABLE.
  132.      *
  133.      * @param string $collation   name of the collation
  134.      * @return string  DBMS specific SQL code portion needed to set the COLLATION
  135.      *                  of a field declaration.
  136.      */
  137.     function _getCollationFieldDeclaration($collation)
  138.     {
  139.         return 'COLLATE '.$collation;
  140.     }
  141.  
  142.     // }}}
  143.     // {{{ getTypeDeclaration()
  144.  
  145.     /**
  146.      * Obtain DBMS specific SQL code portion needed to declare an text type
  147.      * field to be used in statements like CREATE TABLE.
  148.      *
  149.      * @param array $field  associative array with the name of the properties
  150.      *       of the field being declared as array indexes. Currently, the types
  151.      *       of supported field properties are as follows:
  152.      *
  153.      *       length
  154.      *           Integer value that determines the maximum length of the text
  155.      *           field. If this argument is missing the field should be
  156.      *           declared to have the longest length allowed by the DBMS.
  157.      *
  158.      *       default
  159.      *           Text value to be used as default for this field.
  160.      *
  161.      *       notnull
  162.      *           Boolean flag that indicates whether this field is constrained
  163.      *           to not be set to null.
  164.      * @return string  DBMS specific SQL code portion that should be used to
  165.      *       declare the specified field.
  166.      * @access public
  167.      */
  168.     function getTypeDeclaration($field)
  169.     {
  170.         $db $this->getDBInstance();
  171.         if (MDB2::isError($db)) {
  172.             return $db;
  173.         }
  174.  
  175.         switch ($field['type']{
  176.         case 'text':
  177.             $length !empty($field['length'])
  178.                 ? $field['length'$db->options['default_text_field_length'];
  179.             $fixed !empty($field['fixed']$field['fixed': false;
  180.             return $fixed 'CHAR('.$length.')' 'VARCHAR('.$length.')';
  181.         case 'clob':
  182.             return 'BLOB SUB_TYPE 1';
  183.         case 'blob':
  184.             return 'BLOB SUB_TYPE 0';
  185.         case 'integer':
  186.             return 'INT';
  187.         case 'boolean':
  188.             return 'SMALLINT';
  189.         case 'date':
  190.             return 'DATE';
  191.         case 'time':
  192.             return 'TIME';
  193.         case 'timestamp':
  194.             return 'TIMESTAMP';
  195.         case 'float':
  196.             return 'DOUBLE PRECISION';
  197.         case 'decimal':
  198.             $length !empty($field['length']$field['length': 18;
  199.             $scale !empty($field['scale']$field['scale'$db->options['decimal_places'];
  200.             return 'DECIMAL('.$length.','.$scale.')';
  201.         }
  202.         return '';
  203.     }
  204.  
  205.     // }}}
  206.     // {{{ _quoteLOB()
  207.  
  208.     /**
  209.      * Convert a text value into a DBMS specific format that is suitable to
  210.      * compose query statements.
  211.      *
  212.      * @param string $value text string value that is intended to be converted.
  213.      * @param bool $quote determines if the value should be quoted and escaped
  214.      * @param bool $escape_wildcards if to escape escape wildcards
  215.      * @return string text string that represents the given argument value in
  216.      *       a DBMS specific format.
  217.      * @access protected
  218.      */
  219.     function _quoteLOB($value$quote$escape_wildcards)
  220.     {
  221.         $db $this->getDBInstance();
  222.         if (MDB2::isError($db)) {
  223.             return $db;
  224.         }
  225.  
  226.         $connection $db->getConnection();
  227.         if (MDB2::isError($connection)) {
  228.             return $connection;
  229.         }
  230.  
  231.         $close = true;
  232.         if (is_resource($value)) {
  233.             $close = false;
  234.         elseif (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  235.             if ($match[1== 'file://'{
  236.                 $value $match[2];
  237.             }
  238.             $value @fopen($value'r');
  239.         else {
  240.             $fp @tmpfile();
  241.             @fwrite($fp$value);
  242.             @rewind($fp);
  243.             $value $fp;
  244.         }
  245.         $blob_id @ibase_blob_import($connection$value);
  246.  
  247.         if ($close{
  248.             @fclose($value);
  249.         }
  250.         return $blob_id;
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ _retrieveLOB()
  255.  
  256.     /**
  257.      * retrieve LOB from the database
  258.      *
  259.      * @param array $lob array
  260.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  261.      * @access protected
  262.      */
  263.     function _retrieveLOB(&$lob)
  264.     {
  265.         if (empty($lob['handle'])) {
  266.             $db $this->getDBInstance();
  267.             if (MDB2::isError($db)) {
  268.                 return $db;
  269.             }
  270.             $connection $db->getConnection();
  271.             if (MDB2::isError($connection)) {
  272.                 return $connection;
  273.             }
  274.             $lob['handle'@ibase_blob_open($connection$lob['resource']);
  275.             if (!$lob['handle']{
  276.                 $db $this->getDBInstance();
  277.                 if (MDB2::isError($db)) {
  278.                     return $db;
  279.                 }
  280.  
  281.                 return $db->raiseError(nullnullnull,
  282.                     'Could not open fetched large object field'__FUNCTION__);
  283.             }
  284.         }
  285.         $lob['loaded'= true;
  286.         return MDB2_OK;
  287.     }
  288.  
  289.     // }}}
  290.     // {{{ _readLOB()
  291.  
  292.     /**
  293.      * Read data from large object input stream.
  294.      *
  295.      * @param array $lob array
  296.      * @param blob $data reference to a variable that will hold data to be
  297.      *       read from the large object input stream
  298.      * @param int $length integer value that indicates the largest ammount of
  299.      *       data to be read from the large object input stream.
  300.      * @return mixed length on success, a MDB2 error on failure
  301.      * @access protected
  302.      */
  303.     function _readLOB(&$lob$length)
  304.     {
  305.         $data @ibase_blob_get($lob['handle']$length);
  306.         if (!is_string($data)) {
  307.             $db $this->getDBInstance();
  308.             if (MDB2::isError($db)) {
  309.                 return $db;
  310.             }
  311.  
  312.             return $db->raiseError(nullnullnull,
  313.                     'Unable to read LOB'__FUNCTION__);
  314.         }
  315.         return $data;
  316.     }
  317.  
  318.     // }}}
  319.     // {{{ _destroyLOB()
  320.  
  321.     /**
  322.      * Free any resources allocated during the lifetime of the large object
  323.      * handler object.
  324.      *
  325.      * @param array $lob array
  326.      * @access protected
  327.      */
  328.     function _destroyLOB(&$lob)
  329.     {
  330.         if (isset($lob['handle'])) {
  331.            @ibase_blob_close($lob['handle']);
  332.             unset($lob['handle']);
  333.         }
  334.     }
  335.  
  336.     // }}}
  337.     // {{{ patternEscapeString()
  338.  
  339.     /**
  340.      * build string to define escape pattern string
  341.      *
  342.      * @access public
  343.      *
  344.      * @return string define escape pattern
  345.      */
  346.     function patternEscapeString()
  347.     {
  348.         $db $this->getDBInstance();
  349.         if (MDB2::isError($db)) {
  350.             return $db;
  351.         }
  352.         return " ESCAPE '"$db->string_quoting['escape_pattern'."'";
  353.     }
  354.  
  355.     // }}}
  356.     // {{{ _mapNativeDatatype()
  357.  
  358.     /**
  359.      * Maps a native array description of a field to a MDB2 datatype and length
  360.      *
  361.      * @param array  $field native field description
  362.      * @return array containing the various possible types, length, sign, fixed
  363.      * @access public
  364.      */
  365.     function _mapNativeDatatype($field)
  366.     {
  367.         $length $field['length'];
  368.         if ((int)$length <= 0{
  369.             $length = null;
  370.         }
  371.         $type = array();
  372.         $unsigned $fixed = null;
  373.         $db_type strtolower($field['type']);
  374.         $field['field_sub_type'!empty($field['field_sub_type'])
  375.             ? strtolower($field['field_sub_type']: null;
  376.         switch ($db_type{
  377.         case 'smallint':
  378.         case 'integer':
  379.         case 'int64':
  380.             //these may be 'numeric' or 'decimal'
  381.             if (isset($field['field_sub_type'])) {
  382.                 $field['type'$field['field_sub_type'];
  383.                 return $this->mapNativeDatatype($field);
  384.             }
  385.         case 'bigint':
  386.         case 'quad':
  387.             $type['integer';
  388.             if ($length == '1'{
  389.                 $type['boolean';
  390.                 if (preg_match('/^(is|has)/'$field['name'])) {
  391.                     $type array_reverse($type);
  392.                 }
  393.             }
  394.             break;
  395.         case 'varchar':
  396.             $fixed = false;
  397.         case 'char':
  398.         case 'cstring':
  399.             $type['text';
  400.             if ($length == '1'{
  401.                 $type['boolean';
  402.                 if (preg_match('/^(is|has)/'$field['name'])) {
  403.                     $type array_reverse($type);
  404.                 }
  405.             }
  406.             if ($fixed !== false{
  407.                 $fixed = true;
  408.             }
  409.             break;
  410.         case 'date':
  411.             $type['date';
  412.             $length = null;
  413.             break;
  414.         case 'timestamp':
  415.             $type['timestamp';
  416.             $length = null;
  417.             break;
  418.         case 'time':
  419.             $type['time';
  420.             $length = null;
  421.             break;
  422.         case 'float':
  423.         case 'double':
  424.         case 'double precision':
  425.         case 'd_float':
  426.             $type['float';
  427.             break;
  428.         case 'decimal':
  429.         case 'numeric':
  430.             $type['decimal';
  431.             $length $field['precision'].','.$field['scale'];
  432.             break;
  433.         case 'blob':
  434.             $type[($field['field_sub_type'== 'text''clob' 'blob';
  435.             $length = null;
  436.             break;
  437.         default:
  438.             $db $this->getDBInstance();
  439.             if (MDB2::isError($db)) {
  440.                 return $db;
  441.             }
  442.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  443.                 'unknown database attribute type: '.$db_type__FUNCTION__);
  444.         }
  445.  
  446.         if ((int)$length <= 0{
  447.             $length = null;
  448.         }
  449.  
  450.         return array($type$length$unsigned$fixed);
  451.     }
  452.  
  453.     // }}}
  454. }
  455. ?>

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