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-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@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: ibase.php,v 1.23 2005/02/07 20:54:04 quipo Exp $
  47. //
  48.  
  49. require_once 'MDB2/Driver/Datatype/Common.php';
  50.  
  51. /**
  52.  * MDB2 MySQL driver
  53.  *
  54.  * @package MDB2
  55.  * @category Database
  56.  * @author  Lukas Smith <smith@backendmedia.com>
  57.  */
  58. {
  59.     // {{{ convertResult()
  60.  
  61.     /**
  62.      * convert a value to a RDBMS indepdenant MDB2 type
  63.      *
  64.      * @param mixed  $value   value to be converted
  65.      * @param int    $type    constant that specifies which type to convert to
  66.      * @return mixed converted value
  67.      * @access public
  68.      */
  69.     function convertResult($value$type)
  70.     {
  71.         if (is_null($value)) {
  72.             return null;
  73.         }
  74.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  75.         switch ($type{
  76.         case 'decimal':
  77.             return sprintf('%.'.$db->options['decimal_places'].'f'doubleval($value)/pow(10.0$db->options['decimal_places']));
  78.         case 'timestamp':
  79.             return substr($value0strlen('YYYY-MM-DD HH:MM:SS'));
  80.         default:
  81.             return $this->_baseConvertResult($value$type);
  82.         }
  83.     }
  84.  
  85.     // }}}
  86.     // {{{ _getTypeDeclaration()
  87.  
  88.     /**
  89.      * Obtain DBMS specific SQL code portion needed to declare an text type
  90.      * field to be used in statements like CREATE TABLE.
  91.      *
  92.      * @param string $field  associative array with the name of the properties
  93.      *       of the field being declared as array indexes. Currently, the types
  94.      *       of supported field properties are as follows:
  95.      *
  96.      *       length
  97.      *           Integer value that determines the maximum length of the text
  98.      *           field. If this argument is missing the field should be
  99.      *           declared to have the longest length allowed by the DBMS.
  100.      *
  101.      *       default
  102.      *           Text value to be used as default for this field.
  103.      *
  104.      *       notnull
  105.      *           Boolean flag that indicates whether this field is constrained
  106.      *           to not be set to null.
  107.      * @return string  DBMS specific SQL code portion that should be used to
  108.      *       declare the specified field.
  109.      * @access private
  110.      */
  111.     function _getTypeDeclaration($field)
  112.     {
  113.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  114.         switch ($field['type']{
  115.         case 'text':
  116.             $length (isset($field['length']$field['length'(!MDB2::isError($length $db->options['default_text_field_length']$length : 4000));
  117.             return 'VARCHAR ('.$length.')';
  118.         case 'clob':
  119.             return 'BLOB SUB_TYPE 1';
  120.         case 'blob':
  121.             return 'BLOB SUB_TYPE 0';
  122.         case 'integer':
  123.             return 'INTEGER';
  124.         case 'boolean':
  125.             return 'CHAR (1)';
  126.         case 'date':
  127.             return 'DATE';
  128.         case 'time':
  129.             return 'TIME';
  130.         case 'timestamp':
  131.             return 'TIMESTAMP';
  132.         case 'float':
  133.             return 'DOUBLE PRECISION';
  134.         case 'decimal':
  135.             return 'DECIMAL(18,'.$db->options['decimal_places'].')';
  136.         }
  137.         return '';
  138.     }
  139.  
  140.     // }}}
  141.     // {{{ _getTextDeclaration()
  142.  
  143.     /**
  144.      * Obtain DBMS specific SQL code portion needed to declare an text type
  145.      * field to be used in statements like CREATE TABLE.
  146.      *
  147.      * @param string $name   name the field to be declared.
  148.      * @param string $field  associative array with the name of the properties
  149.      *       of the field being declared as array indexes. Currently, the types
  150.      *       of supported field properties are as follows:
  151.      *
  152.      *       length
  153.      *           Integer value that determines the maximum length of the text
  154.      *           field. If this argument is missing the field should be
  155.      *           declared to have the longest length allowed by the DBMS.
  156.      *
  157.      *       default
  158.      *           Text value to be used as default for this field.
  159.      *
  160.      *       notnull
  161.      *           Boolean flag that indicates whether this field is constrained
  162.      *           to not be set to null.
  163.      * @return string  DBMS specific SQL code portion that should be used to
  164.      *       declare the specified field.
  165.      * @access private
  166.      */
  167.     function _getTextDeclaration($name$field)
  168.     {
  169.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  170.         $type $this->getTypeDeclaration($field);
  171.         $default = isset($field['default']' DEFAULT TIME'.
  172.             $this->quote($field['default']'text''';
  173.         $notnull = isset($field['notnull']' NOT NULL' '';
  174.         return $name.' '.$type.$default.$notnull;
  175.     }
  176.  
  177.     // }}}
  178.     // {{{ _getCLOBDeclaration()
  179.  
  180.     /**
  181.      * Obtain DBMS specific SQL code portion needed to declare an character
  182.      * large object type field to be used in statements like CREATE TABLE.
  183.      *
  184.      * @param string $name   name the field to be declared.
  185.      * @param string $field  associative array with the name of the properties
  186.      *       of the field being declared as array indexes. Currently, the types
  187.      *       of supported field properties are as follows:
  188.      *
  189.      *       length
  190.      *           Integer value that determines the maximum length of the large
  191.      *           object field. If this argument is missing the field should be
  192.      *           declared to have the longest length allowed by the DBMS.
  193.      *
  194.      *       notnull
  195.      *           Boolean flag that indicates whether this field is constrained
  196.      *           to not be set to null.
  197.      * @return string  DBMS specific SQL code portion that should be used to
  198.      *       declare the specified field.
  199.      * @access private
  200.      */
  201.     function _getCLOBDeclaration($name$field)
  202.     {
  203.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  204.         $notnull = isset($field['notnull']' NOT NULL' '';
  205.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  206.     }
  207.  
  208.     // }}}
  209.     // {{{ _getBLOBDeclaration()
  210.  
  211.     /**
  212.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  213.      * object type field to be used in statements like CREATE TABLE.
  214.      *
  215.      * @param string $name   name the field to be declared.
  216.      * @param string $field  associative array with the name of the properties
  217.      *       of the field being declared as array indexes. Currently, the types
  218.      *       of supported field properties are as follows:
  219.      *
  220.      *       length
  221.      *           Integer value that determines the maximum length of the large
  222.      *           object field. If this argument is missing the field should be
  223.      *           declared to have the longest length allowed by the DBMS.
  224.      *
  225.      *       notnull
  226.      *           Boolean flag that indicates whether this field is constrained
  227.      *           to not be set to null.
  228.      * @return string  DBMS specific SQL code portion that should be used to
  229.      *       declare the specified field.
  230.      * @access private
  231.      */
  232.     function _getBLOBDeclaration($name$field)
  233.     {
  234.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  235.         $notnull = isset($field['notnull']' NOT NULL' '';
  236.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  237.     }
  238.  
  239.     // }}}
  240.     // {{{ _getDateDeclaration()
  241.  
  242.     /**
  243.      * Obtain DBMS specific SQL code portion needed to declare a date type
  244.      * field to be used in statements like CREATE TABLE.
  245.      *
  246.      * @param string $name   name the field to be declared.
  247.      * @param string $field  associative array with the name of the properties
  248.      *       of the field being declared as array indexes. Currently, the types
  249.      *       of supported field properties are as follows:
  250.      *
  251.      *       default
  252.      *           Date value to be used as default for this field.
  253.      *
  254.      *       notnull
  255.      *           Boolean flag that indicates whether this field is constrained
  256.      *           to not be set to null.
  257.      * @return string  DBMS specific SQL code portion that should be used to
  258.      *       declare the specified field.
  259.      * @access private
  260.      */
  261.     function _getDateDeclaration($name$field)
  262.     {
  263.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  264.         $default = isset($field['default']' DEFAULT '.
  265.             $this->quote($field['default']'date''';
  266.         $notnull = isset($field['notnull']' NOT NULL' '';
  267.         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
  268.     }
  269.  
  270.     // }}}
  271.     // {{{ _getTimestampDeclaration()
  272.  
  273.     /**
  274.      * Obtain DBMS specific SQL code portion needed to declare an timestamp
  275.      * type field to be used in statements like CREATE TABLE.
  276.      *
  277.      * @param string  $name   name the field to be declared.
  278.      * @param string  $field  associative array with the name of the properties
  279.      *                         of the field being declared as array indexes.
  280.      *                         Currently, the types of supported field
  281.      *                         properties are as follows:
  282.      *
  283.      *                        default
  284.      *                         Time stamp value to be used as default for this
  285.      *                         field.
  286.      *
  287.      *                        notnull
  288.      *                         Boolean flag that indicates whether this field is
  289.      *                         constrained to not be set to null.
  290.      * @return string  DBMS specific SQL code portion that should be used to
  291.      *                  declare the specified field.
  292.      * @access private
  293.      */
  294.     function _getTimestampDeclaration($name$field)
  295.     {
  296.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  297.         $default = isset($field['default']' DEFAULT '.
  298.             $this->quote($field['default']'timestamp''';
  299.         $notnull = isset($field['notnull']' NOT NULL' '';
  300.         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
  301.     }
  302.  
  303.     // }}}
  304.     // {{{ _getTimeDeclaration()
  305.  
  306.     /**
  307.      * Obtain DBMS specific SQL code portion needed to declare a time
  308.      * field to be used in statements like CREATE TABLE.
  309.      *
  310.      * @param string $name   name the field to be declared.
  311.      * @param string $field  associative array with the name of the properties
  312.      *       of the field being declared as array indexes. Currently, the types
  313.      *       of supported field properties are as follows:
  314.      *
  315.      *       default
  316.      *           Time value to be used as default for this field.
  317.      *
  318.      *       notnull
  319.      *           Boolean flag that indicates whether this field is constrained
  320.      *           to not be set to null.
  321.      * @return string  DBMS specific SQL code portion that should be used to
  322.      *       declare the specified field.
  323.      * @access private
  324.      */
  325.     function _getTimeDeclaration($name$field)
  326.     {
  327.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  328.         $default = isset($field['default']' DEFAULT '.
  329.             $this->quote($field['default']'time''';
  330.         $notnull = isset($field['notnull']' NOT NULL' '';
  331.         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ _getFloatDeclaration()
  336.  
  337.     /**
  338.      * Obtain DBMS specific SQL code portion needed to declare a float type
  339.      * field to be used in statements like CREATE TABLE.
  340.      *
  341.      * @param string $name   name the field to be declared.
  342.      * @param string $field  associative array with the name of the properties
  343.      *       of the field being declared as array indexes. Currently, the types
  344.      *       of supported field properties are as follows:
  345.      *
  346.      *       default
  347.      *           Float value to be used as default for this field.
  348.      *
  349.      *       notnull
  350.      *           Boolean flag that indicates whether this field is constrained
  351.      *           to not be set to null.
  352.      * @return string  DBMS specific SQL code portion that should be used to
  353.      *       declare the specified field.
  354.      * @access private
  355.      */
  356.     function _getFloatDeclaration($name$field)
  357.     {
  358.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  359.         $default = isset($field['default']' DEFAULT '.
  360.             $this->quote($field['default']'float''';
  361.         $notnull = isset($field['notnull']' NOT NULL' '';
  362.         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
  363.     }
  364.  
  365.     // }}}
  366.     // {{{ _getDecimalDeclaration()
  367.  
  368.     /**
  369.      * Obtain DBMS specific SQL code portion needed to declare a decimal type
  370.      * field to be used in statements like CREATE TABLE.
  371.      *
  372.      * @param string $name   name the field to be declared.
  373.      * @param string $field  associative array with the name of the properties
  374.      *       of the field being declared as array indexes. Currently, the types
  375.      *       of supported field properties are as follows:
  376.      *
  377.      *       default
  378.      *           Decimal value to be used as default for this field.
  379.      *
  380.      *       notnull
  381.      *           Boolean flag that indicates whether this field is constrained
  382.      *           to not be set to null.
  383.      * @return string  DBMS specific SQL code portion that should be used to
  384.      *       declare the specified field.
  385.      * @access private
  386.      */
  387.     function _getDecimalDeclaration($name$field)
  388.     {
  389.  
  390.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  391.         $default = isset($field['default']' DEFAULT '.
  392.             $this->quote($field['default']'decimal''';
  393.         $notnull = isset($field['notnull']' NOT NULL' '';
  394.         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
  395.     }
  396.  
  397.     // }}}
  398.     // {{{ _quoteLOB()
  399.  
  400.     /**
  401.      * Convert a text value into a DBMS specific format that is suitable to
  402.      * compose query statements.
  403.      *
  404.      * @param  $value 
  405.      * @return string text string that represents the given argument value in
  406.      *       a DBMS specific format.
  407.      * @access private
  408.      */
  409.     function _quoteLOB($value)
  410.     {
  411.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  412.         if (MDB2::isError($connect $db->connect())) {
  413.             return $connect;
  414.         }
  415.         $close = true;
  416.         if (is_resource($value)) {
  417.             $close = false;
  418.         elseif (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  419.             if ($match[1== 'file://'{
  420.                 $value $match[2];
  421.             }
  422.             $value @fopen($value'r');
  423.         else {
  424.             $fp @tmpfile();
  425.             @fwrite($fp$value);
  426.             @rewind($fp);
  427.             $value $fp;
  428.         }
  429.         if ($db->in_transaction{
  430.             $value @ibase_blob_import($db->transaction_id$value);
  431.         else {
  432.             $value @ibase_blob_import($db->connection$value);
  433.         }
  434.         if ($close{
  435.             @fclose($value);
  436.         }
  437.         return $value;
  438.     }
  439.  
  440.     // }}}
  441.     // {{{ _quoteCLOB()
  442.  
  443.     /**
  444.      * Convert a text value into a DBMS specific format that is suitable to
  445.      * compose query statements.
  446.      *
  447.      * @param  $value 
  448.      * @return string text string that represents the given argument value in
  449.      *       a DBMS specific format.
  450.      * @access private
  451.      */
  452.     function _quoteCLOB($value)
  453.     {
  454.         return $this->_quoteLOB($value);
  455.     }
  456.  
  457.     // }}}
  458.     // {{{ _quoteBLOB()
  459.  
  460.     /**
  461.      * Convert a text value into a DBMS specific format that is suitable to
  462.      * compose query statements.
  463.      *
  464.      * @param  $value 
  465.      * @return string text string that represents the given argument value in
  466.      *       a DBMS specific format.
  467.      * @access private
  468.      */
  469.     function _quoteBLOB($value)
  470.     {
  471.         return $this->_quoteLOB($value);
  472.     }
  473.     
  474.     // }}}
  475.     // {{{ _quoteBoolean()
  476.  
  477.     /**
  478.      * Convert a text value into a DBMS specific format that is suitable to
  479.      * compose query statements.
  480.      *
  481.      * @param string $value text string value that is intended to be converted.
  482.      * @return string text string that represents the given argument value in
  483.      *        a DBMS specific format.
  484.      * @access private
  485.      */
  486.     function _quoteBoolean($value)
  487.     {
  488.         return ($value "'Y'" "'N'");
  489.     }
  490.  
  491.     // }}}
  492.     // {{{ _quoteDecimal()
  493.  
  494.     /**
  495.      * Convert a text value into a DBMS specific format that is suitable to
  496.      * compose query statements.
  497.      *
  498.      * @param string $value text string value that is intended to be converted.
  499.      * @return string text string that represents the given argument value in
  500.      *       a DBMS specific format.
  501.      * @access private
  502.      */
  503.     function _quoteDecimal($value)
  504.     {
  505.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  506.         return (strval(round($value*pow(10.0$db->options['decimal_places']))));
  507.     }
  508.  
  509.     // }}}
  510.     // {{{ _retrieveLOB()
  511.  
  512.     /**
  513.      * fetch a lob value from a result set
  514.      *
  515.      * @param int $lob handle to a lob created by the createLob() function
  516.      * @return mixed MDB2_OK on success, a MDB error on failure
  517.      * @access private
  518.      */
  519.     function _retrieveLOB($lob)
  520.     {
  521.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  522.  
  523.         if (!isset($db->lobs[$lob])) {
  524.             return $db->raiseError(MDB2_ERRORNULLNULL,
  525.                 '_retrieveLOB: it was not specified a valid lob');
  526.         }
  527.  
  528.         if (!isset($db->lobs[$lob]['handle'])) {
  529.             $db->lobs[$lob]['handle'=
  530.                 @ibase_blob_open($db->lobs[$lob]['value']);
  531.             if (!$db->lobs[$lob]['handle']{
  532.                 unset($db->lobs[$lob]['value']);
  533.                 return $db->raiseError(MDB2_ERRORNULLNULL,
  534.                     '_retrieveLOB: Could not open fetched large object field' @ibase_errmsg());
  535.             }
  536.         }
  537.  
  538.         return MDB2_OK;
  539.     }
  540.  
  541.     // }}}
  542.     // {{{ _endOfResultLOB()
  543.  
  544.     /**
  545.      * Determine whether it was reached the end of the large object and
  546.      * therefore there is no more data to be read for the its input stream.
  547.      *
  548.      * @param int    $lob handle to a lob created by the createLOB() function
  549.      * @return mixed true or false on success, a MDB2 error on failure
  550.      * @access private
  551.      */
  552.     function _endOfResultLOB($lob)
  553.     {
  554.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  555.         $lobresult $this->_retrieveLOB($lob);
  556.         if (MDB2::isError($lobresult)) {
  557.             return $lobresult;
  558.         }
  559.         return isset($db->lobs[$lob]['EndOfLOB']);
  560.     }
  561.  
  562.     // }}}
  563.     // {{{ _readResultLOB()
  564.  
  565.     /**
  566.      * Read data from large object input stream.
  567.      *
  568.      * @param int $lob handle to a lob created by the createLob() function
  569.      * @param blob $data reference to a variable that will hold data to be
  570.      *       read from the large object input stream
  571.      * @param int $length integer value that indicates the largest ammount of
  572.      *       data to be read from the large object input stream.
  573.      * @return mixed length on success, a MDB error on failure
  574.      * @access private
  575.      */
  576.     function _readResultLOB($lob&$data$length)
  577.     {
  578.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  579.         if (MDB2::isError($lobresult $this->_retrieveLOB($lob))) {
  580.             return $lobresult;
  581.         }
  582.         $data @ibase_blob_get($db->lobs[$lob]['handle']$length);
  583.         if (!is_string($data)) {
  584.             $db->raiseError(MDB2_ERRORNULLNULL,
  585.                 'Read Result LOB: ' @ibase_errmsg());
  586.         }
  587.         if (($length strlen($data)) == 0{
  588.             $db->lobs[$lob]['EndOfLOB'= 1;
  589.         }
  590.         return $length;
  591.     }
  592.  
  593.     // }}}
  594.     // {{{ _destroyResultLOB()
  595.  
  596.     /**
  597.      * Free any resources allocated during the lifetime of the large object
  598.      * handler object.
  599.      *
  600.      * @param int $lob handle to a lob created by the createLob() function
  601.      * @access private
  602.      */
  603.     function _destroyResultLOB($lob)
  604.     {
  605.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  606.         if (isset($db->lobs[$lob])) {
  607.             if (isset($db->lobs[$lob]['value'])) {
  608.                @ibase_blob_close($db->lobs[$lob]['handle']);
  609.             }
  610.             $db->lobs[$lob'';
  611.         }
  612.     }
  613.  
  614.     // }}}
  615. }
  616. ?>

Documentation generated on Mon, 11 Mar 2019 14:28:16 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.