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

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