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

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