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

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