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

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