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

Source for file Common.php

Documentation is available at Common.php

  1. <?php
  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: Common.php,v 1.52 2006/01/18 15:18:14 quipo Exp $
  46.  
  47. require_once 'MDB2/LOB.php';
  48.  
  49. /**
  50.  * @package  MDB2
  51.  * @category Database
  52.  * @author   Lukas Smith <smith@pooteeweet.org>
  53.  */
  54.  
  55. /**
  56.  * MDB2_Driver_Common: Base class that is extended by each MDB2 driver
  57.  *
  58.  * @package MDB2
  59.  * @category Database
  60.  * @author Lukas Smith <smith@pooteeweet.org>
  61.  */
  62. {
  63.     var $valid_types = array(
  64.         'text'      => '',
  65.         'boolean'   => true,
  66.         'integer'   => 0,
  67.         'decimal'   => 0.0,
  68.         'float'     => 0.0,
  69.         'date'      => '0000-00-00 00:00:00',
  70.         'time'      => '00:00:00',
  71.         'timestamp' => '0000-00-00',
  72.         'clob'      => '',
  73.         'blob'      => '',
  74.     );
  75.  
  76.     /**
  77.      * contains all LOB objects created with this MDB2 instance
  78.      * @var array 
  79.      * @access protected
  80.      */
  81.     var $lobs = array();
  82.  
  83.     // }}}
  84.     // {{{ checkResultTypes()
  85.  
  86.     /**
  87.      * Define the list of types to be associated with the columns of a given
  88.      * result set.
  89.      *
  90.      * This function may be called before invoking fetchRow(), fetchOne()
  91.      * fetchCole() and fetchAll() so that the necessary data type
  92.      * conversions are performed on the data to be retrieved by them. If this
  93.      * function is not called, the type of all result set columns is assumed
  94.      * to be text, thus leading to not perform any conversions.
  95.      *
  96.      * @param string $types array variable that lists the
  97.      *        data types to be expected in the result set columns. If this array
  98.      *        contains less types than the number of columns that are returned
  99.      *        in the result set, the remaining columns are assumed to be of the
  100.      *        type text. Currently, the types clob and blob are not fully
  101.      *        supported.
  102.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  103.      * @access public
  104.      */
  105.     function checkResultTypes($types)
  106.     {
  107.         $types is_array($types$types : array($types);
  108.         foreach ($types as $key => $type{
  109.             if (!isset($this->valid_types[$type])) {
  110.                 $db =$this->getDBInstance();
  111.                 if (PEAR::isError($db)) {
  112.                     return $db;
  113.                 }
  114.  
  115.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  116.                     'setResultTypes: ' $type ' for '$key .' is not a supported column type');
  117.             }
  118.         }
  119.         return $types;
  120.     }
  121.  
  122.     // }}}
  123.     // {{{ _baseConvertResult()
  124.  
  125.     /**
  126.      * general type conversion method
  127.      *
  128.      * @param mixed $value refernce to a value to be converted
  129.      * @param int $type constant that specifies which type to convert to
  130.      * @return object MDB2 error on failure
  131.      * @access protected
  132.      */
  133.     function _baseConvertResult($value$type)
  134.     {
  135.         switch ($type{
  136.         case 'text':
  137.             return $value;
  138.         case 'integer':
  139.             return intval($value);
  140.         case 'boolean':
  141.             return !empty($value);
  142.         case 'decimal':
  143.             return $value;
  144.         case 'float':
  145.             return doubleval($value);
  146.         case 'date':
  147.             return $value;
  148.         case 'time':
  149.             return $value;
  150.         case 'timestamp':
  151.             return $value;
  152.         case 'clob':
  153.         case 'blob':
  154.             $this->lobs[= array(
  155.                 'buffer' => null,
  156.                 'position' => 0,
  157.                 'lob_index' => null,
  158.                 'endOfLOB' => false,
  159.                 'ressource' => $value,
  160.                 'value' => null,
  161.             );
  162.             end($this->lobs);
  163.             $lob_index key($this->lobs);
  164.             $this->lobs[$lob_index]['lob_index'$lob_index;
  165.             return fopen('MDB2LOB://'.$lob_index.'@'.$this->db_index'r+');
  166.         }
  167.  
  168.         $db =$this->getDBInstance();
  169.         if (PEAR::isError($db)) {
  170.             return $db;
  171.         }
  172.  
  173.         return $db->raiseError(MDB2_ERROR_INVALIDnullnull,
  174.             'attempt to convert result value to an unknown type ' $type);
  175.     }
  176.  
  177.     // }}}
  178.     // {{{ convertResult()
  179.  
  180.     /**
  181.      * convert a value to a RDBMS indepdenant MDB2 type
  182.      *
  183.      * @param mixed $value value to be converted
  184.      * @param int $type constant that specifies which type to convert to
  185.      * @return mixed converted value or a MDB2 error on failure
  186.      * @access public
  187.      */
  188.     function convertResult($value$type)
  189.     {
  190.         if (is_null($value)) {
  191.             return null;
  192.         }
  193.         return $this->_baseConvertResult($value$type);
  194.     }
  195.  
  196.     // }}}
  197.     // {{{ convertResultRow()
  198.  
  199.     /**
  200.      * convert a result row
  201.      *
  202.      * @param resource $result result identifier
  203.      * @param array $row array with data
  204.      * @return mixed MDB2_OK on success,  a MDB2 error on failure
  205.      * @access public
  206.      */
  207.     function convertResultRow($types$row)
  208.     {
  209.         if (is_array($types)) {
  210.             reset($types);
  211.             $current_column = -1;
  212.             foreach ($row as $key => $column{
  213.                 ++$current_column;
  214.                 if (!isset($column)) {
  215.                     continue;
  216.                 }
  217.                 if (isset($types[$current_column])) {
  218.                     $type $types[$current_column];
  219.                 elseif (isset($types[$key])) {
  220.                     $type $types[$key];
  221.                 elseif (current($types)) {
  222.                     $type current($types);
  223.                     next($types);
  224.                 else {
  225.                     continue;
  226.                 }
  227.                 $value $this->convertResult($row[$key]$type);
  228.                 if (PEAR::isError($value)) {
  229.                     return $value;
  230.                 }
  231.                 $row[$key$value;
  232.             }
  233.         }
  234.         return $row;
  235.     }
  236.  
  237.     // }}}
  238.     // {{{ getDeclaration()
  239.  
  240.     /**
  241.      * Obtain DBMS specific SQL code portion needed to declare
  242.      * of the given type
  243.      *
  244.      * @param string $type type to which the value should be converted to
  245.      * @param string  $name   name the field to be declared.
  246.      * @param string  $field  definition of the field
  247.      * @return string  DBMS specific SQL code portion that should be used to
  248.      *                  declare the specified field.
  249.      * @access public
  250.      */
  251.     function getDeclaration($type$name$field)
  252.     {
  253.         if (!method_exists($this"_get{$type}Declaration")) {
  254.             $db =$this->getDBInstance();
  255.             if (PEAR::isError($db)) {
  256.                 return $db;
  257.             }
  258.  
  259.             return $db->raiseError('type not defined: '.$type);
  260.         }
  261.         return $this->{"_get{$type}Declaration"}($name$field);
  262.     }
  263.  
  264.     // }}}
  265.     // {{{ getTypeDeclaration()
  266.  
  267.     /**
  268.      * Obtain DBMS specific SQL code portion needed to declare an text type
  269.      * field to be used in statements like CREATE TABLE.
  270.      *
  271.      * @param array $field  associative array with the name of the properties
  272.      *       of the field being declared as array indexes. Currently, the types
  273.      *       of supported field properties are as follows:
  274.      *
  275.      *       length
  276.      *           Integer value that determines the maximum length of the text
  277.      *           field. If this argument is missing the field should be
  278.      *           declared to have the longest length allowed by the DBMS.
  279.      *
  280.      *       default
  281.      *           Text value to be used as default for this field.
  282.      *
  283.      *       notnull
  284.      *           Boolean flag that indicates whether this field is constrained
  285.      *           to not be set to null.
  286.      * @return string  DBMS specific SQL code portion that should be used to
  287.      *       declare the specified field.
  288.      * @access public
  289.      */
  290.     function getTypeDeclaration($field)
  291.     {
  292.         $db =$this->getDBInstance();
  293.         if (PEAR::isError($db)) {
  294.             return $db;
  295.         }
  296.  
  297.         switch ($field['type']{
  298.         case 'text':
  299.             return array_key_exists('length'$field'CHAR ('.$field['length'].')' 'TEXT';
  300.         case 'clob':
  301.             return 'TEXT';
  302.         case 'blob':
  303.             return 'TEXT';
  304.         case 'integer':
  305.             return 'INT';
  306.         case 'boolean':
  307.             return 'INT';
  308.         case 'date':
  309.             return 'CHAR ('.strlen('YYYY-MM-DD').')';
  310.         case 'time':
  311.             return 'CHAR ('.strlen('HH:MM:SS').')';
  312.         case 'timestamp':
  313.             return 'CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')';
  314.         case 'float':
  315.             return 'TEXT';
  316.         case 'decimal':
  317.             return 'TEXT';
  318.         }
  319.         return '';
  320.     }
  321.  
  322.     // }}}
  323.     // {{{ _getDeclaration()
  324.  
  325.     /**
  326.      * Obtain DBMS specific SQL code portion needed to declare a generic type
  327.      * field to be used in statements like CREATE TABLE.
  328.      *
  329.      * @param string $name   name the field to be declared.
  330.      * @param array  $field  associative array with the name of the properties
  331.      *       of the field being declared as array indexes. Currently, the types
  332.      *       of supported field properties are as follows:
  333.      *
  334.      *       length
  335.      *           Integer value that determines the maximum length of the text
  336.      *           field. If this argument is missing the field should be
  337.      *           declared to have the longest length allowed by the DBMS.
  338.      *
  339.      *       default
  340.      *           Text value to be used as default for this field.
  341.      *
  342.      *       notnull
  343.      *           Boolean flag that indicates whether this field is constrained
  344.      *           to not be set to null.
  345.      * @return string  DBMS specific SQL code portion that should be used to
  346.      *       declare the specified field.
  347.      * @access protected
  348.      */
  349.     function _getDeclaration($name$field)
  350.     {
  351.         $db =$this->getDBInstance();
  352.         if (PEAR::isError($db)) {
  353.             return $db;
  354.         }
  355.  
  356.         $default '';
  357.         if (array_key_exists('default'$field)) {
  358.             if ($field['default'=== ''{
  359.                 $field['default'(array_key_exists('notnull'$field&& $field['notnull'])
  360.                     ? $this->valid_types[$field['type']] : null;
  361.             }
  362.             $default ' DEFAULT '.$this->quote($field['default']$field['type']);
  363.         }
  364.  
  365.         $notnull (array_key_exists('notnull'$field&& $field['notnull']' NOT NULL' '';
  366.         $name $db->quoteIdentifier($nametrue);
  367.         return $name.' '.$this->getTypeDeclaration($field).$default.$notnull;
  368.     }
  369.  
  370.     // }}}
  371.     // {{{ _getIntegerDeclaration()
  372.  
  373.     /**
  374.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  375.      * field to be used in statements like CREATE TABLE.
  376.      *
  377.      * @param string $name name the field to be declared.
  378.      * @param array $field associative array with the name of the properties
  379.      *        of the field being declared as array indexes. Currently, the types
  380.      *        of supported field properties are as follows:
  381.      *
  382.      *        unsigned
  383.      *            Boolean flag that indicates whether the field should be
  384.      *            declared as unsigned integer if possible.
  385.      *
  386.      *        default
  387.      *            Integer value to be used as default for this field.
  388.      *
  389.      *        notnull
  390.      *            Boolean flag that indicates whether this field is constrained
  391.      *            to not be set to null.
  392.      * @return string DBMS specific SQL code portion that should be used to
  393.      *        declare the specified field.
  394.      * @access protected
  395.      */
  396.     function _getIntegerDeclaration($name$field)
  397.     {
  398.         if (array_key_exists('unsigned'$field&& $field['unsigned']{
  399.             $db =$this->getDBInstance();
  400.             if (PEAR::isError($db)) {
  401.                 return $db;
  402.             }
  403.  
  404.             $db->warnings[= "unsigned integer field \"$name\" is being declared as signed integer";
  405.         }
  406.         return $this->_getDeclaration($name$field);
  407.     }
  408.  
  409.     // }}}
  410.     // {{{ _getTextDeclaration()
  411.  
  412.     /**
  413.      * Obtain DBMS specific SQL code portion needed to declare an text type
  414.      * field to be used in statements like CREATE TABLE.
  415.      *
  416.      * @param string $name name the field to be declared.
  417.      * @param array $field associative array with the name of the properties
  418.      *        of the field being declared as array indexes. Currently, the types
  419.      *        of supported field properties are as follows:
  420.      *
  421.      *        length
  422.      *            Integer value that determines the maximum length of the text
  423.      *            field. If this argument is missing the field should be
  424.      *            declared to have the longest length allowed by the DBMS.
  425.      *
  426.      *        default
  427.      *            Text value to be used as default for this field.
  428.      *
  429.      *        notnull
  430.      *            Boolean flag that indicates whether this field is constrained
  431.      *            to not be set to null.
  432.      * @return string DBMS specific SQL code portion that should be used to
  433.      *        declare the specified field.
  434.      * @access protected
  435.      */
  436.     function _getTextDeclaration($name$field)
  437.     {
  438.         return $this->_getDeclaration($name$field);
  439.     }
  440.  
  441.     // }}}
  442.     // {{{ _getCLOBDeclaration()
  443.  
  444.     /**
  445.      * Obtain DBMS specific SQL code portion needed to declare an character
  446.      * large object type field to be used in statements like CREATE TABLE.
  447.      *
  448.      * @param string $name name the field to be declared.
  449.      * @param array $field associative array with the name of the properties
  450.      *         of the field being declared as array indexes. Currently, the types
  451.      *         of supported field properties are as follows:
  452.      *
  453.      *         length
  454.      *             Integer value that determines the maximum length of the large
  455.      *             object field. If this argument is missing the field should be
  456.      *             declared to have the longest length allowed by the DBMS.
  457.      *
  458.      *         notnull
  459.      *             Boolean flag that indicates whether this field is constrained
  460.      *             to not be set to null.
  461.      * @return string DBMS specific SQL code portion that should be used to
  462.      *         declare the specified field.
  463.      * @access public
  464.      */
  465.     function _getCLOBDeclaration($name$field)
  466.     {
  467.         $db =$this->getDBInstance();
  468.         if (PEAR::isError($db)) {
  469.             return $db;
  470.         }
  471.  
  472.         $notnull (array_key_exists('notnull'$field&& $field['notnull']' NOT NULL' '';
  473.         $name $db->quoteIdentifier($nametrue);
  474.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  475.     }
  476.  
  477.     // }}}
  478.     // {{{ _getBLOBDeclaration()
  479.  
  480.     /**
  481.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  482.      * object type field to be used in statements like CREATE TABLE.
  483.      *
  484.      * @param string $name name the field to be declared.
  485.      * @param array $field associative array with the name of the properties
  486.      *         of the field being declared as array indexes. Currently, the types
  487.      *         of supported field properties are as follows:
  488.      *
  489.      *         length
  490.      *             Integer value that determines the maximum length of the large
  491.      *             object field. If this argument is missing the field should be
  492.      *             declared to have the longest length allowed by the DBMS.
  493.      *
  494.      *         notnull
  495.      *             Boolean flag that indicates whether this field is constrained
  496.      *             to not be set to null.
  497.      * @return string DBMS specific SQL code portion that should be used to
  498.      *         declare the specified field.
  499.      * @access protected
  500.      */
  501.     function _getBLOBDeclaration($name$field)
  502.     {
  503.         $db =$this->getDBInstance();
  504.         if (PEAR::isError($db)) {
  505.             return $db;
  506.         }
  507.  
  508.         $notnull (array_key_exists('notnull'$field&& $field['notnull']' NOT NULL' '';
  509.         $name $db->quoteIdentifier($nametrue);
  510.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  511.     }
  512.  
  513.     // }}}
  514.     // {{{ _getBooleanDeclaration()
  515.  
  516.     /**
  517.      * Obtain DBMS specific SQL code portion needed to declare a boolean type
  518.      * field to be used in statements like CREATE TABLE.
  519.      *
  520.      * @param string $name name the field to be declared.
  521.      * @param array $field associative array with the name of the properties
  522.      *        of the field being declared as array indexes. Currently, the types
  523.      *        of supported field properties are as follows:
  524.      *
  525.      *        default
  526.      *            Boolean value to be used as default for this field.
  527.      *
  528.      *        notnullL
  529.      *            Boolean flag that indicates whether this field is constrained
  530.      *            to not be set to null.
  531.      * @return string DBMS specific SQL code portion that should be used to
  532.      *        declare the specified field.
  533.      * @access protected
  534.      */
  535.     function _getBooleanDeclaration($name$field)
  536.     {
  537.         return $this->_getDeclaration($name$field);
  538.     }
  539.  
  540.     // }}}
  541.     // {{{ _getDateDeclaration()
  542.  
  543.     /**
  544.      * Obtain DBMS specific SQL code portion needed to declare a date type
  545.      * field to be used in statements like CREATE TABLE.
  546.      *
  547.      * @param string $name name the field to be declared.
  548.      * @param array $field associative array with the name of the properties
  549.      *        of the field being declared as array indexes. Currently, the types
  550.      *        of supported field properties are as follows:
  551.      *
  552.      *        default
  553.      *            Date value to be used as default for this field.
  554.      *
  555.      *        notnull
  556.      *            Boolean flag that indicates whether this field is constrained
  557.      *            to not be set to null.
  558.      * @return string DBMS specific SQL code portion that should be used to
  559.      *        declare the specified field.
  560.      * @access protected
  561.      */
  562.     function _getDateDeclaration($name$field)
  563.     {
  564.         return $this->_getDeclaration($name$field);
  565.     }
  566.  
  567.     // }}}
  568.     // {{{ _getTimestampDeclaration()
  569.  
  570.     /**
  571.      * Obtain DBMS specific SQL code portion needed to declare a timestamp
  572.      * field to be used in statements like CREATE TABLE.
  573.      *
  574.      * @param string $name name the field to be declared.
  575.      * @param array $field associative array with the name of the properties
  576.      *        of the field being declared as array indexes. Currently, the types
  577.      *        of supported field properties are as follows:
  578.      *
  579.      *        default
  580.      *            Timestamp value to be used as default for this field.
  581.      *
  582.      *        notnull
  583.      *            Boolean flag that indicates whether this field is constrained
  584.      *            to not be set to null.
  585.      * @return string DBMS specific SQL code portion that should be used to
  586.      *        declare the specified field.
  587.      * @access protected
  588.      */
  589.     function _getTimestampDeclaration($name$field)
  590.     {
  591.         return $this->_getDeclaration($name$field);
  592.     }
  593.  
  594.     // }}}
  595.     // {{{ _getTimeDeclaration()
  596.  
  597.     /**
  598.      * Obtain DBMS specific SQL code portion needed to declare a time
  599.      * field to be used in statements like CREATE TABLE.
  600.      *
  601.      * @param string $name name the field to be declared.
  602.      * @param array $field associative array with the name of the properties
  603.      *        of the field being declared as array indexes. Currently, the types
  604.      *        of supported field properties are as follows:
  605.      *
  606.      *        default
  607.      *            Time value to be used as default for this field.
  608.      *
  609.      *        notnull
  610.      *            Boolean flag that indicates whether this field is constrained
  611.      *            to not be set to null.
  612.      * @return string DBMS specific SQL code portion that should be used to
  613.      *        declare the specified field.
  614.      * @access protected
  615.      */
  616.     function _getTimeDeclaration($name$field)
  617.     {
  618.         return $this->_getDeclaration($name$field);
  619.     }
  620.  
  621.     // }}}
  622.     // {{{ _getFloatDeclaration()
  623.  
  624.     /**
  625.      * Obtain DBMS specific SQL code portion needed to declare a float type
  626.      * field to be used in statements like CREATE TABLE.
  627.      *
  628.      * @param string $name name the field to be declared.
  629.      * @param array $field associative array with the name of the properties
  630.      *        of the field being declared as array indexes. Currently, the types
  631.      *        of supported field properties are as follows:
  632.      *
  633.      *        default
  634.      *            Float value to be used as default for this field.
  635.      *
  636.      *        notnull
  637.      *            Boolean flag that indicates whether this field is constrained
  638.      *            to not be set to null.
  639.      * @return string DBMS specific SQL code portion that should be used to
  640.      *        declare the specified field.
  641.      * @access protected
  642.      */
  643.     function _getFloatDeclaration($name$field)
  644.     {
  645.         return $this->_getDeclaration($name$field);
  646.     }
  647.  
  648.     // }}}
  649.     // {{{ _getDecimalDeclaration()
  650.  
  651.     /**
  652.      * Obtain DBMS specific SQL code portion needed to declare a decimal type
  653.      * field to be used in statements like CREATE TABLE.
  654.      *
  655.      * @param string $name name the field to be declared.
  656.      * @param array $field associative array with the name of the properties
  657.      *        of the field being declared as array indexes. Currently, the types
  658.      *        of supported field properties are as follows:
  659.      *
  660.      *        default
  661.      *            Decimal value to be used as default for this field.
  662.      *
  663.      *        notnull
  664.      *            Boolean flag that indicates whether this field is constrained
  665.      *            to not be set to null.
  666.      * @return string DBMS specific SQL code portion that should be used to
  667.      *        declare the specified field.
  668.      * @access protected
  669.      */
  670.     function _getDecimalDeclaration($name$field)
  671.     {
  672.         return $this->_getDeclaration($name$field);
  673.     }
  674.  
  675.     // }}}
  676.     // {{{ compareDefinition()
  677.  
  678.     /**
  679.      * Obtain an array of changes that may need to applied
  680.      *
  681.      * @param array $current new definition
  682.      * @param array  $previous old definition
  683.      * @return array  containg all changes that will need to be applied
  684.      * @access public
  685.      */
  686.     function compareDefinition($current$previous)
  687.     {
  688.         $type array_key_exists('type'$current$current['type': null;
  689.  
  690.         if (!method_exists($this"_compare{$type}Definition")) {
  691.             $db =$this->getDBInstance();
  692.             if (PEAR::isError($db)) {
  693.                 return $db;
  694.             }
  695.  
  696.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  697.                 'type "'.$current['type'].'" is not yet supported');
  698.         }
  699.  
  700.         if (!array_key_exists('type'$previous|| $previous['type'!= $type{
  701.             return $current;
  702.         }
  703.  
  704.         $change $this->{"_compare{$type}Definition"}($current$previous);
  705.  
  706.         if ($previous['type'!= $type{
  707.             $change['type'= true;
  708.         }
  709.  
  710.         $previous_notnull array_key_exists('notnull'$previous$previous['notnull': false;
  711.         $notnull array_key_exists('notnull'$current$current['notnull': false;
  712.         if ($previous_notnull != $notnull{
  713.             $change['notnull'= true;
  714.         }
  715.  
  716.         $previous_default array_key_exists('default'$previous$previous['default':
  717.             ($previous_notnull '' : null);
  718.         $default array_key_exists('default'$current$current['default':
  719.             ($notnull '' : null);
  720.         if ($previous_default !== $default{
  721.             $change['default'= true;
  722.         }
  723.  
  724.         return $change;
  725.     }
  726.  
  727.     // }}}
  728.     // {{{ _compareIntegerDefinition()
  729.  
  730.     /**
  731.      * Obtain an array of changes that may need to applied to an integer field
  732.      *
  733.      * @param array $current new definition
  734.      * @param array  $previous old definition
  735.      * @return array  containg all changes that will need to be applied
  736.      * @access protected
  737.      */
  738.     function _compareIntegerDefinition($current$previous)
  739.     {
  740.         $change = array();
  741.         $previous_unsigned array_key_exists('unsigned'$previous$previous['unsigned': false;
  742.         $unsigned array_key_exists('unsigned'$current$current['unsigned': false;
  743.         if ($previous_unsigned != $unsigned{
  744.             $change['unsigned'= true;
  745.         }
  746.         $previous_autoincrement array_key_exists('autoincrement'$previous$previous['autoincrement': false;
  747.         $autoincrement array_key_exists('autoincrement'$current$current['autoincrement': false;
  748.         if ($previous_autoincrement != $autoincrement{
  749.             $change['autoincrement'= true;
  750.         }
  751.         return $change;
  752.     }
  753.  
  754.     // }}}
  755.     // {{{ _compareTextDefinition()
  756.  
  757.     /**
  758.      * Obtain an array of changes that may need to applied to an text field
  759.      *
  760.      * @param array $current new definition
  761.      * @param array  $previous old definition
  762.      * @return array  containg all changes that will need to be applied
  763.      * @access protected
  764.      */
  765.     function _compareTextDefinition($current$previous)
  766.     {
  767.         $change = array();
  768.         $previous_length array_key_exists('length'$previous$previous['length': 0;
  769.         $length array_key_exists('length'$current$current['length': 0;
  770.         if ($previous_length != $length{
  771.             $change['length'= true;
  772.         }
  773.         return $change;
  774.     }
  775.  
  776.     // }}}
  777.     // {{{ _compareCLOBDefinition()
  778.  
  779.     /**
  780.      * Obtain an array of changes that may need to applied to an CLOB field
  781.      *
  782.      * @param array $current new definition
  783.      * @param array  $previous old definition
  784.      * @return array  containg all changes that will need to be applied
  785.      * @access protected
  786.      */
  787.     function _compareCLOBDefinition($current$previous)
  788.     {
  789.         return $this->_compareTextDefinition($current$previous);
  790.     }
  791.  
  792.     // }}}
  793.     // {{{ _compareBLOBDefinition()
  794.  
  795.     /**
  796.      * Obtain an array of changes that may need to applied to an BLOB field
  797.      *
  798.      * @param array $current new definition
  799.      * @param array  $previous old definition
  800.      * @return array  containg all changes that will need to be applied
  801.      * @access protected
  802.      */
  803.     function _compareBLOBDefinition($current$previous)
  804.     {
  805.         return $this->_compareTextDefinition($current$previous);
  806.     }
  807.  
  808.     // }}}
  809.     // {{{ _compareDateDefinition()
  810.  
  811.     /**
  812.      * Obtain an array of changes that may need to applied to an date field
  813.      *
  814.      * @param array $current new definition
  815.      * @param array  $previous old definition
  816.      * @return array  containg all changes that will need to be applied
  817.      * @access protected
  818.      */
  819.     function _compareDateDefinition($current$previous)
  820.     {
  821.         return array();
  822.     }
  823.  
  824.     // }}}
  825.     // {{{ _compareTimeDefinition()
  826.  
  827.     /**
  828.      * Obtain an array of changes that may need to applied to an time field
  829.      *
  830.      * @param array $current new definition
  831.      * @param array  $previous old definition
  832.      * @return array  containg all changes that will need to be applied
  833.      * @access protected
  834.      */
  835.     function _compareTimeDefinition($current$previous)
  836.     {
  837.         return array();
  838.     }
  839.  
  840.     // }}}
  841.     // {{{ _compareTimestampDefinition()
  842.  
  843.     /**
  844.      * Obtain an array of changes that may need to applied to an timestamp field
  845.      *
  846.      * @param array $current new definition
  847.      * @param array  $previous old definition
  848.      * @return array  containg all changes that will need to be applied
  849.      * @access protected
  850.      */
  851.     function _compareTimestampDefinition($current$previous)
  852.     {
  853.         return array();
  854.     }
  855.  
  856.     // }}}
  857.     // {{{ _compareBooleanDefinition()
  858.  
  859.     /**
  860.      * Obtain an array of changes that may need to applied to an boolean field
  861.      *
  862.      * @param array $current new definition
  863.      * @param array  $previous old definition
  864.      * @return array  containg all changes that will need to be applied
  865.      * @access protected
  866.      */
  867.     function _compareBooleanDefinition($current$previous)
  868.     {
  869.         return array();
  870.     }
  871.  
  872.     // }}}
  873.     // {{{ _compareFloatDefinition()
  874.  
  875.     /**
  876.      * Obtain an array of changes that may need to applied to an float field
  877.      *
  878.      * @param array $current new definition
  879.      * @param array  $previous old definition
  880.      * @return array  containg all changes that will need to be applied
  881.      * @access protected
  882.      */
  883.     function _compareFloatDefinition($current$previous)
  884.     {
  885.         return array();
  886.     }
  887.  
  888.     // }}}
  889.     // {{{ _compareDecimalDefinition()
  890.  
  891.     /**
  892.      * Obtain an array of changes that may need to applied to an decimal field
  893.      *
  894.      * @param array $current new definition
  895.      * @param array  $previous old definition
  896.      * @return array  containg all changes that will need to be applied
  897.      * @access protected
  898.      */
  899.     function _compareDecimalDefinition($current$previous)
  900.     {
  901.         return array();
  902.     }
  903.  
  904.     // }}}
  905.     // {{{ quote()
  906.  
  907.     /**
  908.      * Convert a text value into a DBMS specific format that is suitable to
  909.      * compose query statements.
  910.      *
  911.      * @param string $value text string value that is intended to be converted.
  912.      * @param string $type type to which the value should be converted to
  913.      * @param bool $quote determines if the value should be quoted and escaped
  914.      * @return string text string that represents the given argument value in
  915.      *        a DBMS specific format.
  916.      * @access public
  917.      */
  918.     function quote($value$type = null$quote = true)
  919.     {
  920.         $db =$this->getDBInstance();
  921.         if (PEAR::isError($db)) {
  922.             return $db;
  923.         }
  924.  
  925.         if (is_null($value)
  926.             || ($value === '' && $db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL)
  927.         {
  928.             if (!$quote{
  929.                 return null;
  930.             }
  931.             return 'NULL';
  932.         }
  933.  
  934.         if (is_null($type)) {
  935.             switch (gettype($value)) {
  936.             case 'integer':
  937.                 $type 'integer';
  938.                 break;
  939.             case 'double':
  940.                 // todo
  941.                 $type 'decimal';
  942.                 $type 'float';
  943.                 break;
  944.             case 'boolean':
  945.                 $type 'boolean';
  946.                 break;
  947.             case 'array':
  948.             case 'object':
  949.                  $type 'text';
  950.                 break;
  951.             default:
  952.                 if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/'$value)) {
  953.                     $type 'timestamp';
  954.                 elseif (preg_match('/^\d{2}:\d{2}$/'$value)) {
  955.                     $type 'time';
  956.                 elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/'$value)) {
  957.                     $type 'date';
  958.                 else {
  959.                     $type 'text';
  960.                 }
  961.                 break;
  962.             }
  963.         }
  964.  
  965.         if (!method_exists($this"_quote{$type}")) {
  966.             return $db->raiseError('type not defined: '.$type);
  967.         }
  968.         $value $this->{"_quote{$type}"}($value$quote);
  969.  
  970.         return $value;
  971.     }
  972.  
  973.     // }}}
  974.     // {{{ _quoteInteger()
  975.  
  976.     /**
  977.      * Convert a text value into a DBMS specific format that is suitable to
  978.      * compose query statements.
  979.      *
  980.      * @param string $value text string value that is intended to be converted.
  981.      * @param bool $quote determines if the value should be quoted and escaped
  982.      * @return string text string that represents the given argument value in
  983.      *        a DBMS specific format.
  984.      * @access protected
  985.      */
  986.     function _quoteInteger($value$quote)
  987.     {
  988.         return (int)$value;
  989.     }
  990.  
  991.     // }}}
  992.     // {{{ _quoteText()
  993.  
  994.     /**
  995.      * Convert a text value into a DBMS specific format that is suitable to
  996.      * compose query statements.
  997.      *
  998.      * @param string $value text string value that is intended to be converted.
  999.      * @param bool $quote determines if the value should be quoted and escaped
  1000.      * @return string text string that already contains any DBMS specific
  1001.      *        escaped character sequences.
  1002.      * @access protected
  1003.      */
  1004.     function _quoteText($value$quote)
  1005.     {
  1006.         if (!$quote{
  1007.             return $value;
  1008.         }
  1009.         $db =$this->getDBInstance();
  1010.         if (PEAR::isError($db)) {
  1011.             return $db;
  1012.         }
  1013.  
  1014.         return "'".$db->escape($value)."'";
  1015.     }
  1016.  
  1017.     // }}}
  1018.     // {{{ _readFile()
  1019.  
  1020.     /**
  1021.      * Convert a text value into a DBMS specific format that is suitable to
  1022.      * compose query statements.
  1023.      *
  1024.      * @param string $value text string value that is intended to be converted.
  1025.      * @return string text string that represents the given argument value in
  1026.      *        a DBMS specific format.
  1027.      * @access protected
  1028.      */
  1029.     function _readFile($value)
  1030.     {
  1031.         $close = false;
  1032.         if (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1033.             $close = true;
  1034.             if ($match[1== 'file://'{
  1035.                 $value $match[2];
  1036.             }
  1037.             $value @fopen($value'r');
  1038.         }
  1039.  
  1040.         if (is_resource($value)) {
  1041.             $db =$this->getDBInstance();
  1042.             if (PEAR::isError($db)) {
  1043.                 return $db;
  1044.             }
  1045.  
  1046.             $fp $value;
  1047.             $value '';
  1048.             while (!@feof($fp)) {
  1049.                 $value.= @fread($fp$db->options['lob_buffer_length']);
  1050.             }
  1051.             if ($close{
  1052.                 @fclose($fp);
  1053.             }
  1054.         }
  1055.  
  1056.         return $value;
  1057.     }
  1058.  
  1059.     // }}}
  1060.     // {{{ _quoteLOB()
  1061.  
  1062.     /**
  1063.      * Convert a text value into a DBMS specific format that is suitable to
  1064.      * compose query statements.
  1065.      *
  1066.      * @param string $value text string value that is intended to be converted.
  1067.      * @param bool $quote determines if the value should be quoted and escaped
  1068.      * @return string text string that represents the given argument value in
  1069.      *        a DBMS specific format.
  1070.      * @access protected
  1071.      */
  1072.     function _quoteLOB($value$quote)
  1073.     {
  1074.         $value $this->_readFile($value);
  1075.         return $this->_quoteText($value$quote);
  1076.     }
  1077.  
  1078.     // }}}
  1079.     // {{{ _quoteCLOB()
  1080.  
  1081.     /**
  1082.      * Convert a text value into a DBMS specific format that is suitable to
  1083.      * compose query statements.
  1084.      *
  1085.      * @param string $value text string value that is intended to be converted.
  1086.      * @param bool $quote determines if the value should be quoted and escaped
  1087.      * @return string text string that represents the given argument value in
  1088.      *        a DBMS specific format.
  1089.      * @access protected
  1090.      */
  1091.     function _quoteCLOB($value$quote)
  1092.     {
  1093.         return $this->_quoteLOB($value$quote);
  1094.     }
  1095.  
  1096.     // }}}
  1097.     // {{{ _quoteBLOB()
  1098.  
  1099.     /**
  1100.      * Convert a text value into a DBMS specific format that is suitable to
  1101.      * compose query statements.
  1102.      *
  1103.      * @param string $value text string value that is intended to be converted.
  1104.      * @param bool $quote determines if the value should be quoted and escaped
  1105.      * @return string text string that represents the given argument value in
  1106.      *        a DBMS specific format.
  1107.      * @access protected
  1108.      */
  1109.     function _quoteBLOB($value$quote)
  1110.     {
  1111.         return $this->_quoteLOB($value$quote);
  1112.     }
  1113.  
  1114.     // }}}
  1115.     // {{{ _quoteBoolean()
  1116.  
  1117.     /**
  1118.      * Convert a text value into a DBMS specific format that is suitable to
  1119.      * compose query statements.
  1120.      *
  1121.      * @param string $value text string value that is intended to be converted.
  1122.      * @param bool $quote determines if the value should be quoted and escaped
  1123.      * @return string text string that represents the given argument value in
  1124.      *        a DBMS specific format.
  1125.      * @access protected
  1126.      */
  1127.     function _quoteBoolean($value$quote)
  1128.     {
  1129.         return ($value ? 1 : 0);
  1130.     }
  1131.  
  1132.     // }}}
  1133.     // {{{ _quoteDate()
  1134.  
  1135.     /**
  1136.      * Convert a text value into a DBMS specific format that is suitable to
  1137.      * compose query statements.
  1138.      *
  1139.      * @param string $value text string value that is intended to be converted.
  1140.      * @param bool $quote determines if the value should be quoted and escaped
  1141.      * @return string text string that represents the given argument value in
  1142.      *        a DBMS specific format.
  1143.      * @access protected
  1144.      */
  1145.     function _quoteDate($value$quote)
  1146.     {
  1147.        return $this->_quoteText($value$quote);
  1148.     }
  1149.  
  1150.     // }}}
  1151.     // {{{ _quoteTimestamp()
  1152.  
  1153.     /**
  1154.      * Convert a text value into a DBMS specific format that is suitable to
  1155.      * compose query statements.
  1156.      *
  1157.      * @param string $value text string value that is intended to be converted.
  1158.      * @param bool $quote determines if the value should be quoted and escaped
  1159.      * @return string text string that represents the given argument value in
  1160.      *        a DBMS specific format.
  1161.      * @access protected
  1162.      */
  1163.     function _quoteTimestamp($value$quote)
  1164.     {
  1165.        return $this->_quoteText($value$quote);
  1166.     }
  1167.  
  1168.     // }}}
  1169.     // {{{ _quoteTime()
  1170.  
  1171.     /**
  1172.      * Convert a text value into a DBMS specific format that is suitable to
  1173.      *       compose query statements.
  1174.      *
  1175.      * @param string $value text string value that is intended to be converted.
  1176.      * @param bool $quote determines if the value should be quoted and escaped
  1177.      * @return string text string that represents the given argument value in
  1178.      *        a DBMS specific format.
  1179.      * @access protected
  1180.      */
  1181.     function _quoteTime($value$quote)
  1182.     {
  1183.        return $this->_quoteText($value$quote);
  1184.     }
  1185.  
  1186.     // }}}
  1187.     // {{{ _quoteFloat()
  1188.  
  1189.     /**
  1190.      * Convert a text value into a DBMS specific format that is suitable to
  1191.      * compose query statements.
  1192.      *
  1193.      * @param string $value text string value that is intended to be converted.
  1194.      * @param bool $quote determines if the value should be quoted and escaped
  1195.      * @return string text string that represents the given argument value in
  1196.      *        a DBMS specific format.
  1197.      * @access protected
  1198.      */
  1199.     function _quoteFloat($value$quote)
  1200.     {
  1201.         if (!$quote{
  1202.             return $value;
  1203.         }
  1204.         $db =$this->getDBInstance();
  1205.         if (PEAR::isError($db)) {
  1206.             return $db;
  1207.         }
  1208.  
  1209.         return $db->escape($value);
  1210.     }
  1211.  
  1212.     // }}}
  1213.     // {{{ _quoteDecimal()
  1214.  
  1215.     /**
  1216.      * Convert a text value into a DBMS specific format that is suitable to
  1217.      * compose query statements.
  1218.      *
  1219.      * @param string $value text string value that is intended to be converted.
  1220.      * @param bool $quote determines if the value should be quoted and escaped
  1221.      * @return string text string that represents the given argument value in
  1222.      *        a DBMS specific format.
  1223.      * @access protected
  1224.      */
  1225.     function _quoteDecimal($value$quote)
  1226.     {
  1227.         if (!$quote{
  1228.             return $value;
  1229.         }
  1230.         $db =$this->getDBInstance();
  1231.         if (PEAR::isError($db)) {
  1232.             return $db;
  1233.         }
  1234.  
  1235.         return $db->escape($value);
  1236.     }
  1237.  
  1238.     // }}}
  1239.     // {{{ writeLOBToFile()
  1240.  
  1241.     /**
  1242.      * retrieve LOB from the database
  1243.      *
  1244.      * @param resource $lob stream handle
  1245.      * @param string $file name of the file into which the LOb should be fetched
  1246.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1247.      * @access protected
  1248.      */
  1249.     function writeLOBToFile($lob$file)
  1250.     {
  1251.         $db =$this->getDBInstance();
  1252.         if (PEAR::isError($db)) {
  1253.             return $db;
  1254.         }
  1255.  
  1256.         $fp fopen($file'wb');
  1257.         while (!feof($lob)) {
  1258.             $result fread($lob$db->options['lob_buffer_length']);
  1259.             $read strlen($result);
  1260.             if (fwrite($fp$result$read!= $read{
  1261.                 fclose($fp);
  1262.                 return $db->raiseError(MDB2_ERRORnullnull,
  1263.                     'writeLOBToFile: could not write to the output file');
  1264.             }
  1265.         }
  1266.         fclose($fp);
  1267.         return MDB2_OK;
  1268.     }
  1269.  
  1270.     // }}}
  1271.     // {{{ _retrieveLOB()
  1272.  
  1273.     /**
  1274.      * retrieve LOB from the database
  1275.      *
  1276.      * @param resource $lob stream handle
  1277.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1278.      * @access protected
  1279.      */
  1280.     function _retrieveLOB(&$lob)
  1281.     {
  1282.         if (is_null($lob['value'])) {
  1283.             $lob['value'$lob['ressource'];
  1284.         }
  1285.         return MDB2_OK;
  1286.     }
  1287.  
  1288.     // }}}
  1289.     // {{{ readLOB()
  1290.  
  1291.     /**
  1292.      * Read data from large object input stream.
  1293.      *
  1294.      * @param resource $lob stream handle
  1295.      * @param string $data reference to a variable that will hold data
  1296.      *                           to be read from the large object input stream
  1297.      * @param integer $length    value that indicates the largest ammount ofdata
  1298.      *                           to be read from the large object input stream.
  1299.      * @return mixed the effective number of bytes read from the large object
  1300.      *                       input stream on sucess or an MDB2 error object.
  1301.      * @access public
  1302.      * @see endOfLOB()
  1303.      */
  1304.     function _readLOB($lob$length)
  1305.     {
  1306.         return substr($lob['value']$lob['position']$length);
  1307.     }
  1308.  
  1309.     // }}}
  1310.     // {{{ _endOfLOB()
  1311.  
  1312.     /**
  1313.      * Determine whether it was reached the end of the large object and
  1314.      * therefore there is no more data to be read for the its input stream.
  1315.      *
  1316.      * @param resource $lob stream handle
  1317.      * @return mixed true or false on success, a MDB2 error on failure
  1318.      * @access protected
  1319.      */
  1320.     function _endOfLOB($lob)
  1321.     {
  1322.         return $lob['endOfLOB'];
  1323.     }
  1324.  
  1325.     // }}}
  1326.     // {{{ destroyLOB()
  1327.  
  1328.     /**
  1329.      * Free any resources allocated during the lifetime of the large object
  1330.      * handler object.
  1331.      *
  1332.      * @param resource $lob stream handle
  1333.      * @access public
  1334.      */
  1335.     function destroyLOB($lob)
  1336.     {
  1337.         $lob_data stream_get_meta_data($lob);
  1338.         $lob_index $lob_data['wrapper_data']->lob_index;
  1339.         fclose($lob);
  1340.         if (isset($this->lobs[$lob_index])) {
  1341.             $this->_destroyLOB($lob_index);
  1342.             unset($this->lobs[$lob_index]);
  1343.         }
  1344.         return MDB2_OK;
  1345.     }
  1346.  
  1347.     // }}}
  1348.     // {{{ _destroyLOB()
  1349.  
  1350.     /**
  1351.      * Free any resources allocated during the lifetime of the large object
  1352.      * handler object.
  1353.      *
  1354.      * @param int $lob_index from the lob array
  1355.      * @access private
  1356.      */
  1357.     function _destroyLOB($lob_index)
  1358.     {
  1359.         return MDB2_OK;
  1360.     }
  1361.  
  1362.     // }}}
  1363.     // {{{ implodeArray()
  1364.  
  1365.     /**
  1366.      * apply a type to all values of an array and return as a comma seperated string
  1367.      * useful for generating IN statements
  1368.      *
  1369.      * @access public
  1370.      *
  1371.      * @param array $array data array
  1372.      * @param string $type determines type of the field
  1373.      *
  1374.      * @return string comma seperated values
  1375.      */
  1376.     function implodeArray($array$type = false)
  1377.     {
  1378.         if (!is_array($array|| empty($array)) {
  1379.             return 'NULL';
  1380.         }
  1381.         if ($type{
  1382.             foreach ($array as $value{
  1383.                 $return[$this->quote($value$type);
  1384.             }
  1385.         else {
  1386.             $return $array;
  1387.         }
  1388.         return implode(', '$return);
  1389.     }
  1390. }
  1391.  
  1392. ?>

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