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-2007 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.124 2007/03/05 22:55:01 quipo Exp $
  46.  
  47. require_once 'MDB2/LOB.php';
  48.  
  49. /**
  50.  * @package  MDB2
  51.  * @category Database
  52.  * @author   Lukas Smith <smith@pooteeweet.org>
  53.  */
  54.  
  55. /**
  56.  * MDB2_Driver_Common: Base class that is extended by each MDB2 driver
  57.  *
  58.  * @package MDB2
  59.  * @category Database
  60.  * @author Lukas Smith <smith@pooteeweet.org>
  61.  */
  62. {
  63.     var $valid_default_values = array(
  64.         'text'      => '',
  65.         'boolean'   => true,
  66.         'integer'   => 0,
  67.         'decimal'   => 0.0,
  68.         'float'     => 0.0,
  69.         'timestamp' => '1970-01-01 00:00:00',
  70.         'time'      => '00:00:00',
  71.         'date'      => '1970-01-01',
  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.     // {{{ getValidTypes()
  85.  
  86.     /**
  87.      * Get the list of valid types
  88.      *
  89.      * This function returns an array of valid types as keys with the values
  90.      * being possible default values for all native datatypes and mapped types
  91.      * for custom datatypes.
  92.      *
  93.      * @return mixed array on success, a MDB2 error on failure
  94.      * @access public
  95.      */
  96.     function getValidTypes()
  97.     {
  98.         $types $this->valid_default_values;
  99.         $db =$this->getDBInstance();
  100.         if (PEAR::isError($db)) {
  101.             return $db;
  102.         }
  103.         if (!empty($db->options['datatype_map'])) {
  104.             foreach ($db->options['datatype_map'as $type => $mapped_type{
  105.                 if (array_key_exists($mapped_type$types)) {
  106.                     $types[$type$types[$mapped_type];
  107.                 elseif (!empty($db->options['datatype_map_callback'][$type])) {
  108.                     $parameter = array('type' => $type'mapped_type' => $mapped_type);
  109.                     $default =  call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  110.                     $types[$type$default;
  111.                 }
  112.             }
  113.         }
  114.         return $types;
  115.     }
  116.  
  117.     // }}}
  118.     // {{{ checkResultTypes()
  119.  
  120.     /**
  121.      * Define the list of types to be associated with the columns of a given
  122.      * result set.
  123.      *
  124.      * This function may be called before invoking fetchRow(), fetchOne()
  125.      * fetchCole() and fetchAll() so that the necessary data type
  126.      * conversions are performed on the data to be retrieved by them. If this
  127.      * function is not called, the type of all result set columns is assumed
  128.      * to be text, thus leading to not perform any conversions.
  129.      *
  130.      * @param array $types array variable that lists the
  131.      *        data types to be expected in the result set columns. If this array
  132.      *        contains less types than the number of columns that are returned
  133.      *        in the result set, the remaining columns are assumed to be of the
  134.      *        type text. Currently, the types clob and blob are not fully
  135.      *        supported.
  136.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  137.      * @access public
  138.      */
  139.     function checkResultTypes($types)
  140.     {
  141.         $types is_array($types$types : array($types);
  142.         foreach ($types as $key => $type{
  143.             if (!isset($this->valid_default_values[$type])) {
  144.                 $db =$this->getDBInstance();
  145.                 if (PEAR::isError($db)) {
  146.                     return $db;
  147.                 }
  148.                 if (empty($db->options['datatype_map'][$type])) {
  149.                     return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  150.                         $type.' for '.$key.' is not a supported column type'__FUNCTION__);
  151.                 }
  152.             }
  153.         }
  154.         return $types;
  155.     }
  156.  
  157.     // }}}
  158.     // {{{ _baseConvertResult()
  159.  
  160.     /**
  161.      * General type conversion method
  162.      *
  163.      * @param mixed   $value reference to a value to be converted
  164.      * @param string  $type  specifies which type to convert to
  165.      * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
  166.      * @return object an MDB2 error on failure
  167.      * @access protected
  168.      */
  169.     function _baseConvertResult($value$type$rtrim = true)
  170.     {
  171.         switch ($type{
  172.         case 'text':
  173.             if ($rtrim{
  174.                 $value rtrim($value);
  175.             }
  176.             return $value;
  177.         case 'integer':
  178.             return intval($value);
  179.         case 'boolean':
  180.             return !empty($value);
  181.         case 'decimal':
  182.             return $value;
  183.         case 'float':
  184.             return doubleval($value);
  185.         case 'date':
  186.             return $value;
  187.         case 'time':
  188.             return $value;
  189.         case 'timestamp':
  190.             return $value;
  191.         case 'clob':
  192.         case 'blob':
  193.             $this->lobs[= array(
  194.                 'buffer' => null,
  195.                 'position' => 0,
  196.                 'lob_index' => null,
  197.                 'endOfLOB' => false,
  198.                 'resource' => $value,
  199.                 'value' => null,
  200.                 'loaded' => false,
  201.             );
  202.             end($this->lobs);
  203.             $lob_index key($this->lobs);
  204.             $this->lobs[$lob_index]['lob_index'$lob_index;
  205.             return fopen('MDB2LOB://'.$lob_index.'@'.$this->db_index'r+');
  206.         }
  207.  
  208.         $db =$this->getDBInstance();
  209.         if (PEAR::isError($db)) {
  210.             return $db;
  211.         }
  212.  
  213.         return $db->raiseError(MDB2_ERROR_INVALIDnullnull,
  214.             'attempt to convert result value to an unknown type :' $type__FUNCTION__);
  215.     }
  216.  
  217.     // }}}
  218.     // {{{ convertResult()
  219.  
  220.     /**
  221.      * Convert a value to a RDBMS indipendent MDB2 type
  222.      *
  223.      * @param mixed   $value value to be converted
  224.      * @param string  $type  specifies which type to convert to
  225.      * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
  226.      * @return mixed converted value
  227.      * @access public
  228.      */
  229.     function convertResult($value$type$rtrim = true)
  230.     {
  231.         if (is_null($value)) {
  232.             return null;
  233.         }
  234.         $db =$this->getDBInstance();
  235.         if (PEAR::isError($db)) {
  236.             return $db;
  237.         }
  238.         if (!empty($db->options['datatype_map'][$type])) {
  239.             $type $db->options['datatype_map'][$type];
  240.             if (!empty($db->options['datatype_map_callback'][$type])) {
  241.                 $parameter = array('type' => $type'value' => $value'rtrim' => $rtrim);
  242.                 return call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  243.             }
  244.         }
  245.         return $this->_baseConvertResult($value$type$rtrim);
  246.     }
  247.  
  248.     // }}}
  249.     // {{{ convertResultRow()
  250.  
  251.     /**
  252.      * Convert a result row
  253.      *
  254.      * @param array   $types 
  255.      * @param array   $row   specifies the types to convert to
  256.      * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
  257.      * @return mixed MDB2_OK on success, an MDB2 error on failure
  258.      * @access public
  259.      */
  260.     function convertResultRow($types$row$rtrim = true)
  261.     {
  262.         $types $this->_sortResultFieldTypes(array_keys($row)$types);
  263.         foreach ($row as $key => $value{
  264.             if (empty($types[$key])) {
  265.                 continue;
  266.             }
  267.             $value $this->convertResult($row[$key]$types[$key]$rtrim);
  268.             if (PEAR::isError($value)) {
  269.                 return $value;
  270.             }
  271.             $row[$key$value;
  272.         }
  273.         return $row;
  274.     }
  275.  
  276.     // }}}
  277.     // {{{ _sortResultFieldTypes()
  278.  
  279.     /**
  280.      * convert a result row
  281.      *
  282.      * @param array $types 
  283.      * @param array $row specifies the types to convert to
  284.      * @param bool   $rtrim   if to rtrim text values or not
  285.      * @return mixed MDB2_OK on success,  a MDB2 error on failure
  286.      * @access public
  287.      */
  288.     function _sortResultFieldTypes($columns$types)
  289.     {
  290.         $n_cols count($columns);
  291.         $n_types count($types);
  292.         if ($n_cols $n_types{
  293.             for ($i$n_cols $n_types$i >= 0; $i--{
  294.                 $types[= null;
  295.             }
  296.         }
  297.         $sorted_types = array();
  298.         foreach ($columns as $col{
  299.             $sorted_types[$col= null;
  300.         }
  301.         foreach ($types as $name => $type{
  302.             if (array_key_exists($name$sorted_types)) {
  303.                 $sorted_types[$name$type;
  304.                 unset($types[$name]);
  305.             }
  306.         }
  307.         // if there are left types in the array, fill the null values of the
  308.         // sorted array with them, in order.
  309.         if (count($types)) {
  310.             reset($types);
  311.             foreach (array_keys($sorted_typesas $k{
  312.                 if (is_null($sorted_types[$k])) {
  313.                     $sorted_types[$kcurrent($types);
  314.                     next($types);
  315.                 }
  316.             }
  317.         }
  318.         return $sorted_types;
  319.     }
  320.  
  321.     // }}}
  322.     // {{{ getDeclaration()
  323.  
  324.     /**
  325.      * Obtain DBMS specific SQL code portion needed to declare
  326.      * of the given type
  327.      *
  328.      * @param string $type type to which the value should be converted to
  329.      * @param string  $name   name the field to be declared.
  330.      * @param string  $field  definition of the field
  331.      * @return string  DBMS specific SQL code portion that should be used to
  332.      *                  declare the specified field.
  333.      * @access public
  334.      */
  335.     function getDeclaration($type$name$field)
  336.     {
  337.         $db =$this->getDBInstance();
  338.         if (PEAR::isError($db)) {
  339.             return $db;
  340.         }
  341.  
  342.         if (!empty($db->options['datatype_map'][$type])) {
  343.             $type $db->options['datatype_map'][$type];
  344.             if (!empty($db->options['datatype_map_callback'][$type])) {
  345.                 $parameter = array('type' => $type'name' => $name'field' => $field);
  346.                 return call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  347.             }
  348.         }
  349.  
  350.         if (!method_exists($this"_get{$type}Declaration")) {
  351.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  352.                 'type not defined: '.$type__FUNCTION__);
  353.         }
  354.         return $this->{"_get{$type}Declaration"}($name$field);
  355.     }
  356.  
  357.     // }}}
  358.     // {{{ getTypeDeclaration()
  359.  
  360.     /**
  361.      * Obtain DBMS specific SQL code portion needed to declare an text type
  362.      * field to be used in statements like CREATE TABLE.
  363.      *
  364.      * @param array $field  associative array with the name of the properties
  365.      *       of the field being declared as array indexes. Currently, the types
  366.      *       of supported field properties are as follows:
  367.      *
  368.      *       length
  369.      *           Integer value that determines the maximum length of the text
  370.      *           field. If this argument is missing the field should be
  371.      *           declared to have the longest length allowed by the DBMS.
  372.      *
  373.      *       default
  374.      *           Text value to be used as default for this field.
  375.      *
  376.      *       notnull
  377.      *           Boolean flag that indicates whether this field is constrained
  378.      *           to not be set to null.
  379.      * @return string  DBMS specific SQL code portion that should be used to
  380.      *       declare the specified field.
  381.      * @access public
  382.      */
  383.     function getTypeDeclaration($field)
  384.     {
  385.         $db =$this->getDBInstance();
  386.         if (PEAR::isError($db)) {
  387.             return $db;
  388.         }
  389.  
  390.         switch ($field['type']{
  391.         case 'text':
  392.             $length !empty($field['length']$field['length'$db->options['default_text_field_length'];
  393.             $fixed !empty($field['fixed']$field['fixed': false;
  394.             return $fixed ($length 'CHAR('.$length.')' 'CHAR('.$db->options['default_text_field_length'].')')
  395.                 : ($length 'VARCHAR('.$length.')' 'TEXT');
  396.         case 'clob':
  397.             return 'TEXT';
  398.         case 'blob':
  399.             return 'TEXT';
  400.         case 'integer':
  401.             return 'INT';
  402.         case 'boolean':
  403.             return 'INT';
  404.         case 'date':
  405.             return 'CHAR ('.strlen('YYYY-MM-DD').')';
  406.         case 'time':
  407.             return 'CHAR ('.strlen('HH:MM:SS').')';
  408.         case 'timestamp':
  409.             return 'CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')';
  410.         case 'float':
  411.             return 'TEXT';
  412.         case 'decimal':
  413.             return 'TEXT';
  414.         }
  415.         return '';
  416.     }
  417.  
  418.     // }}}
  419.     // {{{ _getDeclaration()
  420.  
  421.     /**
  422.      * Obtain DBMS specific SQL code portion needed to declare a generic type
  423.      * field to be used in statements like CREATE TABLE.
  424.      *
  425.      * @param string $name   name the field to be declared.
  426.      * @param array  $field  associative array with the name of the properties
  427.      *       of the field being declared as array indexes. Currently, the types
  428.      *       of supported field properties are as follows:
  429.      *
  430.      *       length
  431.      *           Integer value that determines the maximum length of the text
  432.      *           field. If this argument is missing the field should be
  433.      *           declared to have the longest length allowed by the DBMS.
  434.      *
  435.      *       default
  436.      *           Text value to be used as default for this field.
  437.      *
  438.      *       notnull
  439.      *           Boolean flag that indicates whether this field is constrained
  440.      *           to not be set to null.
  441.      *       charset
  442.      *           Text value with the default CHARACTER SET for this field.
  443.      *       collation
  444.      *           Text value with the default COLLATION for this field.
  445.      * @return string  DBMS specific SQL code portion that should be used to
  446.      *       declare the specified field, or a MDB2_Error on failure
  447.      * @access protected
  448.      */
  449.     function _getDeclaration($name$field)
  450.     {
  451.         $db =$this->getDBInstance();
  452.         if (PEAR::isError($db)) {
  453.             return $db;
  454.         }
  455.  
  456.         $name $db->quoteIdentifier($nametrue);
  457.         $declaration_options $db->datatype->_getDeclarationOptions($field);
  458.         if (PEAR::isError($declaration_options)) {
  459.             return $declaration_options;
  460.         }
  461.         return $name.' '.$this->getTypeDeclaration($field).$declaration_options;
  462.     }
  463.  
  464.     // }}}
  465.     // {{{ _getDeclarationOptions()
  466.  
  467.     /**
  468.      * Obtain DBMS specific SQL code portion needed to declare a generic type
  469.      * field to be used in statement like CREATE TABLE, without the field name
  470.      * and type values (ie. just the character set, default value, if the
  471.      * field is permitted to be NULL or not, and the collation options).
  472.      *
  473.      * @param array  $field  associative array with the name of the properties
  474.      *       of the field being declared as array indexes. Currently, the types
  475.      *       of supported field properties are as follows:
  476.      *
  477.      *       default
  478.      *           Text value to be used as default for this field.
  479.      *       notnull
  480.      *           Boolean flag that indicates whether this field is constrained
  481.      *           to not be set to null.
  482.      *       charset
  483.      *           Text value with the default CHARACTER SET for this field.
  484.      *       collation
  485.      *           Text value with the default COLLATION for this field.
  486.      * @return string  DBMS specific SQL code portion that should be used to
  487.      *       declare the specified field's options.
  488.      * @access protected
  489.      */
  490.     function _getDeclarationOptions($field)
  491.     {
  492.         $charset = empty($field['charset']'' :
  493.             ' '.$this->_getCharsetFieldDeclaration($field['charset']);
  494.  
  495.         $default '';
  496.         if (array_key_exists('default'$field)) {
  497.             if ($field['default'=== ''{
  498.                 $db =$this->getDBInstance();
  499.                 if (PEAR::isError($db)) {
  500.                     return $db;
  501.                 }
  502.                 if (empty($field['notnull'])) {
  503.                     $field['default'= null;
  504.                 else {
  505.                     $valid_default_values $this->getValidTypes();
  506.                     $field['default'$valid_default_values[$field['type']];
  507.                 }
  508.                 if ($field['default'=== ''
  509.                     && ($db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL)
  510.                 {
  511.                     $field['default'' ';
  512.                 }
  513.             }
  514.             $default ' DEFAULT '.$this->quote($field['default']$field['type']);
  515.         elseif (empty($field['notnull'])) {
  516.             $default ' DEFAULT NULL';
  517.         }
  518.  
  519.         $notnull = empty($field['notnull']'' ' NOT NULL';
  520.         
  521.         $collation = empty($field['collation']'' :
  522.             ' '.$this->_getCollationFieldDeclaration($field['collation']);
  523.         return $charset.$default.$notnull.$collation;
  524.     }
  525.  
  526.     // }}}
  527.     // {{{ _getCharsetFieldDeclaration()
  528.     
  529.     /**
  530.      * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
  531.      * of a field declaration to be used in statements like CREATE TABLE.
  532.      *
  533.      * @param string $charset   name of the charset
  534.      * @return string  DBMS specific SQL code portion needed to set the CHARACTER SET
  535.      *                  of a field declaration.
  536.      */
  537.     function _getCharsetFieldDeclaration($charset)
  538.     {
  539.         return '';
  540.     }
  541.  
  542.     // }}}
  543.     // {{{ _getCollationFieldDeclaration()
  544.  
  545.     /**
  546.      * Obtain DBMS specific SQL code portion needed to set the COLLATION
  547.      * of a field declaration to be used in statements like CREATE TABLE.
  548.      *
  549.      * @param string $collation   name of the collation
  550.      * @return string  DBMS specific SQL code portion needed to set the COLLATION
  551.      *                  of a field declaration.
  552.      */
  553.     function _getCollationFieldDeclaration($collation)
  554.     {
  555.         return '';
  556.     }
  557.  
  558.     // }}}
  559.     // {{{ _getIntegerDeclaration()
  560.  
  561.     /**
  562.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  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.      *        unsigned
  571.      *            Boolean flag that indicates whether the field should be
  572.      *            declared as unsigned integer if possible.
  573.      *
  574.      *        default
  575.      *            Integer value to be used as default for this field.
  576.      *
  577.      *        notnull
  578.      *            Boolean flag that indicates whether this field is constrained
  579.      *            to not be set to null.
  580.      * @return string DBMS specific SQL code portion that should be used to
  581.      *        declare the specified field.
  582.      * @access protected
  583.      */
  584.     function _getIntegerDeclaration($name$field)
  585.     {
  586.         if (!empty($field['unsigned'])) {
  587.             $db =$this->getDBInstance();
  588.             if (PEAR::isError($db)) {
  589.                 return $db;
  590.             }
  591.  
  592.             $db->warnings[= "unsigned integer field \"$name\" is being declared as signed integer";
  593.         }
  594.         return $this->_getDeclaration($name$field);
  595.     }
  596.  
  597.     // }}}
  598.     // {{{ _getTextDeclaration()
  599.  
  600.     /**
  601.      * Obtain DBMS specific SQL code portion needed to declare an text type
  602.      * field to be used in statements like CREATE TABLE.
  603.      *
  604.      * @param string $name name the field to be declared.
  605.      * @param array $field associative array with the name of the properties
  606.      *        of the field being declared as array indexes. Currently, the types
  607.      *        of supported field properties are as follows:
  608.      *
  609.      *        length
  610.      *            Integer value that determines the maximum length of the text
  611.      *            field. If this argument is missing the field should be
  612.      *            declared to have the longest length allowed by the DBMS.
  613.      *
  614.      *        default
  615.      *            Text value to be used as default for this field.
  616.      *
  617.      *        notnull
  618.      *            Boolean flag that indicates whether this field is constrained
  619.      *            to not be set to null.
  620.      * @return string DBMS specific SQL code portion that should be used to
  621.      *        declare the specified field.
  622.      * @access protected
  623.      */
  624.     function _getTextDeclaration($name$field)
  625.     {
  626.         return $this->_getDeclaration($name$field);
  627.     }
  628.  
  629.     // }}}
  630.     // {{{ _getCLOBDeclaration()
  631.  
  632.     /**
  633.      * Obtain DBMS specific SQL code portion needed to declare an character
  634.      * large object type field to be used in statements like CREATE TABLE.
  635.      *
  636.      * @param string $name name the field to be declared.
  637.      * @param array $field associative array with the name of the properties
  638.      *         of the field being declared as array indexes. Currently, the types
  639.      *         of supported field properties are as follows:
  640.      *
  641.      *         length
  642.      *             Integer value that determines the maximum length of the large
  643.      *             object field. If this argument is missing the field should be
  644.      *             declared to have the longest length allowed by the DBMS.
  645.      *
  646.      *         notnull
  647.      *             Boolean flag that indicates whether this field is constrained
  648.      *             to not be set to null.
  649.      * @return string DBMS specific SQL code portion that should be used to
  650.      *         declare the specified field.
  651.      * @access public
  652.      */
  653.     function _getCLOBDeclaration($name$field)
  654.     {
  655.         $db =$this->getDBInstance();
  656.         if (PEAR::isError($db)) {
  657.             return $db;
  658.         }
  659.  
  660.         $notnull = empty($field['notnull']'' ' NOT NULL';
  661.         $name $db->quoteIdentifier($nametrue);
  662.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  663.     }
  664.  
  665.     // }}}
  666.     // {{{ _getBLOBDeclaration()
  667.  
  668.     /**
  669.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  670.      * object type field to be used in statements like CREATE TABLE.
  671.      *
  672.      * @param string $name name the field to be declared.
  673.      * @param array $field associative array with the name of the properties
  674.      *         of the field being declared as array indexes. Currently, the types
  675.      *         of supported field properties are as follows:
  676.      *
  677.      *         length
  678.      *             Integer value that determines the maximum length of the large
  679.      *             object field. If this argument is missing the field should be
  680.      *             declared to have the longest length allowed by the DBMS.
  681.      *
  682.      *         notnull
  683.      *             Boolean flag that indicates whether this field is constrained
  684.      *             to not be set to null.
  685.      * @return string DBMS specific SQL code portion that should be used to
  686.      *         declare the specified field.
  687.      * @access protected
  688.      */
  689.     function _getBLOBDeclaration($name$field)
  690.     {
  691.         $db =$this->getDBInstance();
  692.         if (PEAR::isError($db)) {
  693.             return $db;
  694.         }
  695.  
  696.         $notnull = empty($field['notnull']'' ' NOT NULL';
  697.         $name $db->quoteIdentifier($nametrue);
  698.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  699.     }
  700.  
  701.     // }}}
  702.     // {{{ _getBooleanDeclaration()
  703.  
  704.     /**
  705.      * Obtain DBMS specific SQL code portion needed to declare a boolean type
  706.      * field to be used in statements like CREATE TABLE.
  707.      *
  708.      * @param string $name name the field to be declared.
  709.      * @param array $field associative array with the name of the properties
  710.      *        of the field being declared as array indexes. Currently, the types
  711.      *        of supported field properties are as follows:
  712.      *
  713.      *        default
  714.      *            Boolean value to be used as default for this field.
  715.      *
  716.      *        notnullL
  717.      *            Boolean flag that indicates whether this field is constrained
  718.      *            to not be set to null.
  719.      * @return string DBMS specific SQL code portion that should be used to
  720.      *        declare the specified field.
  721.      * @access protected
  722.      */
  723.     function _getBooleanDeclaration($name$field)
  724.     {
  725.         return $this->_getDeclaration($name$field);
  726.     }
  727.  
  728.     // }}}
  729.     // {{{ _getDateDeclaration()
  730.  
  731.     /**
  732.      * Obtain DBMS specific SQL code portion needed to declare a date type
  733.      * field to be used in statements like CREATE TABLE.
  734.      *
  735.      * @param string $name name the field to be declared.
  736.      * @param array $field associative array with the name of the properties
  737.      *        of the field being declared as array indexes. Currently, the types
  738.      *        of supported field properties are as follows:
  739.      *
  740.      *        default
  741.      *            Date value to be used as default for this field.
  742.      *
  743.      *        notnull
  744.      *            Boolean flag that indicates whether this field is constrained
  745.      *            to not be set to null.
  746.      * @return string DBMS specific SQL code portion that should be used to
  747.      *        declare the specified field.
  748.      * @access protected
  749.      */
  750.     function _getDateDeclaration($name$field)
  751.     {
  752.         return $this->_getDeclaration($name$field);
  753.     }
  754.  
  755.     // }}}
  756.     // {{{ _getTimestampDeclaration()
  757.  
  758.     /**
  759.      * Obtain DBMS specific SQL code portion needed to declare a timestamp
  760.      * field to be used in statements like CREATE TABLE.
  761.      *
  762.      * @param string $name name the field to be declared.
  763.      * @param array $field associative array with the name of the properties
  764.      *        of the field being declared as array indexes. Currently, the types
  765.      *        of supported field properties are as follows:
  766.      *
  767.      *        default
  768.      *            Timestamp value to be used as default for this field.
  769.      *
  770.      *        notnull
  771.      *            Boolean flag that indicates whether this field is constrained
  772.      *            to not be set to null.
  773.      * @return string DBMS specific SQL code portion that should be used to
  774.      *        declare the specified field.
  775.      * @access protected
  776.      */
  777.     function _getTimestampDeclaration($name$field)
  778.     {
  779.         return $this->_getDeclaration($name$field);
  780.     }
  781.  
  782.     // }}}
  783.     // {{{ _getTimeDeclaration()
  784.  
  785.     /**
  786.      * Obtain DBMS specific SQL code portion needed to declare a time
  787.      * field to be used in statements like CREATE TABLE.
  788.      *
  789.      * @param string $name name the field to be declared.
  790.      * @param array $field associative array with the name of the properties
  791.      *        of the field being declared as array indexes. Currently, the types
  792.      *        of supported field properties are as follows:
  793.      *
  794.      *        default
  795.      *            Time value to be used as default for this field.
  796.      *
  797.      *        notnull
  798.      *            Boolean flag that indicates whether this field is constrained
  799.      *            to not be set to null.
  800.      * @return string DBMS specific SQL code portion that should be used to
  801.      *        declare the specified field.
  802.      * @access protected
  803.      */
  804.     function _getTimeDeclaration($name$field)
  805.     {
  806.         return $this->_getDeclaration($name$field);
  807.     }
  808.  
  809.     // }}}
  810.     // {{{ _getFloatDeclaration()
  811.  
  812.     /**
  813.      * Obtain DBMS specific SQL code portion needed to declare a float type
  814.      * field to be used in statements like CREATE TABLE.
  815.      *
  816.      * @param string $name name the field to be declared.
  817.      * @param array $field associative array with the name of the properties
  818.      *        of the field being declared as array indexes. Currently, the types
  819.      *        of supported field properties are as follows:
  820.      *
  821.      *        default
  822.      *            Float value to be used as default for this field.
  823.      *
  824.      *        notnull
  825.      *            Boolean flag that indicates whether this field is constrained
  826.      *            to not be set to null.
  827.      * @return string DBMS specific SQL code portion that should be used to
  828.      *        declare the specified field.
  829.      * @access protected
  830.      */
  831.     function _getFloatDeclaration($name$field)
  832.     {
  833.         return $this->_getDeclaration($name$field);
  834.     }
  835.  
  836.     // }}}
  837.     // {{{ _getDecimalDeclaration()
  838.  
  839.     /**
  840.      * Obtain DBMS specific SQL code portion needed to declare a decimal type
  841.      * field to be used in statements like CREATE TABLE.
  842.      *
  843.      * @param string $name name the field to be declared.
  844.      * @param array $field associative array with the name of the properties
  845.      *        of the field being declared as array indexes. Currently, the types
  846.      *        of supported field properties are as follows:
  847.      *
  848.      *        default
  849.      *            Decimal value to be used as default for this field.
  850.      *
  851.      *        notnull
  852.      *            Boolean flag that indicates whether this field is constrained
  853.      *            to not be set to null.
  854.      * @return string DBMS specific SQL code portion that should be used to
  855.      *        declare the specified field.
  856.      * @access protected
  857.      */
  858.     function _getDecimalDeclaration($name$field)
  859.     {
  860.         return $this->_getDeclaration($name$field);
  861.     }
  862.  
  863.     // }}}
  864.     // {{{ compareDefinition()
  865.  
  866.     /**
  867.      * Obtain an array of changes that may need to applied
  868.      *
  869.      * @param array $current new definition
  870.      * @param array  $previous old definition
  871.      * @return array  containing all changes that will need to be applied
  872.      * @access public
  873.      */
  874.     function compareDefinition($current$previous)
  875.     {
  876.         $type !empty($current['type']$current['type': null;
  877.  
  878.         if (!method_exists($this"_compare{$type}Definition")) {
  879.             $db =$this->getDBInstance();
  880.             if (PEAR::isError($db)) {
  881.                 return $db;
  882.             }
  883.             if (!empty($db->options['datatype_map_callback'][$type])) {
  884.                 $parameter = array('current' => $current'previous' => $previous);
  885.                 $change =  call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  886.                 return $change;
  887.             }
  888.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  889.                 'type "'.$current['type'].'" is not yet supported'__FUNCTION__);
  890.         }
  891.  
  892.         if (empty($previous['type']|| $previous['type'!= $type{
  893.             return $current;
  894.         }
  895.  
  896.         $change $this->{"_compare{$type}Definition"}($current$previous);
  897.  
  898.         if ($previous['type'!= $type{
  899.             $change['type'= true;
  900.         }
  901.  
  902.         $previous_notnull !empty($previous['notnull']$previous['notnull': false;
  903.         $notnull !empty($current['notnull']$current['notnull': false;
  904.         if ($previous_notnull != $notnull{
  905.             $change['notnull'= true;
  906.         }
  907.  
  908.         $previous_default array_key_exists('default'$previous$previous['default':
  909.             ($previous_notnull '' : null);
  910.         $default array_key_exists('default'$current$current['default':
  911.             ($notnull '' : null);
  912.         if ($previous_default !== $default{
  913.             $change['default'= true;
  914.         }
  915.  
  916.         return $change;
  917.     }
  918.  
  919.     // }}}
  920.     // {{{ _compareIntegerDefinition()
  921.  
  922.     /**
  923.      * Obtain an array of changes that may need to applied to an integer field
  924.      *
  925.      * @param array $current new definition
  926.      * @param array  $previous old definition
  927.      * @return array  containing all changes that will need to be applied
  928.      * @access protected
  929.      */
  930.     function _compareIntegerDefinition($current$previous)
  931.     {
  932.         $change = array();
  933.         $previous_unsigned !empty($previous['unsigned']$previous['unsigned': false;
  934.         $unsigned !empty($current['unsigned']$current['unsigned': false;
  935.         if ($previous_unsigned != $unsigned{
  936.             $change['unsigned'= true;
  937.         }
  938.         $previous_autoincrement !empty($previous['autoincrement']$previous['autoincrement': false;
  939.         $autoincrement !empty($current['autoincrement']$current['autoincrement': false;
  940.         if ($previous_autoincrement != $autoincrement{
  941.             $change['autoincrement'= true;
  942.         }
  943.         return $change;
  944.     }
  945.  
  946.     // }}}
  947.     // {{{ _compareTextDefinition()
  948.  
  949.     /**
  950.      * Obtain an array of changes that may need to applied to an text field
  951.      *
  952.      * @param array $current new definition
  953.      * @param array  $previous old definition
  954.      * @return array  containing all changes that will need to be applied
  955.      * @access protected
  956.      */
  957.     function _compareTextDefinition($current$previous)
  958.     {
  959.         $change = array();
  960.         $previous_length !empty($previous['length']$previous['length': 0;
  961.         $length !empty($current['length']$current['length': 0;
  962.         if ($previous_length != $length{
  963.             $change['length'= true;
  964.         }
  965.         $previous_fixed !empty($previous['fixed']$previous['fixed': 0;
  966.         $fixed !empty($current['fixed']$current['fixed': 0;
  967.         if ($previous_fixed != $fixed{
  968.             $change['fixed'= true;
  969.         }
  970.         return $change;
  971.     }
  972.  
  973.     // }}}
  974.     // {{{ _compareCLOBDefinition()
  975.  
  976.     /**
  977.      * Obtain an array of changes that may need to applied to an CLOB field
  978.      *
  979.      * @param array $current new definition
  980.      * @param array  $previous old definition
  981.      * @return array  containing all changes that will need to be applied
  982.      * @access protected
  983.      */
  984.     function _compareCLOBDefinition($current$previous)
  985.     {
  986.         return $this->_compareTextDefinition($current$previous);
  987.     }
  988.  
  989.     // }}}
  990.     // {{{ _compareBLOBDefinition()
  991.  
  992.     /**
  993.      * Obtain an array of changes that may need to applied to an BLOB field
  994.      *
  995.      * @param array $current new definition
  996.      * @param array  $previous old definition
  997.      * @return array  containing all changes that will need to be applied
  998.      * @access protected
  999.      */
  1000.     function _compareBLOBDefinition($current$previous)
  1001.     {
  1002.         return $this->_compareTextDefinition($current$previous);
  1003.     }
  1004.  
  1005.     // }}}
  1006.     // {{{ _compareDateDefinition()
  1007.  
  1008.     /**
  1009.      * Obtain an array of changes that may need to applied to an date field
  1010.      *
  1011.      * @param array $current new definition
  1012.      * @param array  $previous old definition
  1013.      * @return array  containing all changes that will need to be applied
  1014.      * @access protected
  1015.      */
  1016.     function _compareDateDefinition($current$previous)
  1017.     {
  1018.         return array();
  1019.     }
  1020.  
  1021.     // }}}
  1022.     // {{{ _compareTimeDefinition()
  1023.  
  1024.     /**
  1025.      * Obtain an array of changes that may need to applied to an time field
  1026.      *
  1027.      * @param array $current new definition
  1028.      * @param array  $previous old definition
  1029.      * @return array  containing all changes that will need to be applied
  1030.      * @access protected
  1031.      */
  1032.     function _compareTimeDefinition($current$previous)
  1033.     {
  1034.         return array();
  1035.     }
  1036.  
  1037.     // }}}
  1038.     // {{{ _compareTimestampDefinition()
  1039.  
  1040.     /**
  1041.      * Obtain an array of changes that may need to applied to an timestamp field
  1042.      *
  1043.      * @param array $current new definition
  1044.      * @param array  $previous old definition
  1045.      * @return array  containing all changes that will need to be applied
  1046.      * @access protected
  1047.      */
  1048.     function _compareTimestampDefinition($current$previous)
  1049.     {
  1050.         return array();
  1051.     }
  1052.  
  1053.     // }}}
  1054.     // {{{ _compareBooleanDefinition()
  1055.  
  1056.     /**
  1057.      * Obtain an array of changes that may need to applied to an boolean field
  1058.      *
  1059.      * @param array $current new definition
  1060.      * @param array  $previous old definition
  1061.      * @return array  containing all changes that will need to be applied
  1062.      * @access protected
  1063.      */
  1064.     function _compareBooleanDefinition($current$previous)
  1065.     {
  1066.         return array();
  1067.     }
  1068.  
  1069.     // }}}
  1070.     // {{{ _compareFloatDefinition()
  1071.  
  1072.     /**
  1073.      * Obtain an array of changes that may need to applied to an float field
  1074.      *
  1075.      * @param array $current new definition
  1076.      * @param array  $previous old definition
  1077.      * @return array  containing all changes that will need to be applied
  1078.      * @access protected
  1079.      */
  1080.     function _compareFloatDefinition($current$previous)
  1081.     {
  1082.         return array();
  1083.     }
  1084.  
  1085.     // }}}
  1086.     // {{{ _compareDecimalDefinition()
  1087.  
  1088.     /**
  1089.      * Obtain an array of changes that may need to applied to an decimal field
  1090.      *
  1091.      * @param array $current new definition
  1092.      * @param array  $previous old definition
  1093.      * @return array  containing all changes that will need to be applied
  1094.      * @access protected
  1095.      */
  1096.     function _compareDecimalDefinition($current$previous)
  1097.     {
  1098.         return array();
  1099.     }
  1100.  
  1101.     // }}}
  1102.     // {{{ quote()
  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.      * @param string $type type to which the value should be converted to
  1110.      * @param bool $quote determines if the value should be quoted and escaped
  1111.      * @param bool $escape_wildcards if to escape escape wildcards
  1112.      * @return string text string that represents the given argument value in
  1113.      *        a DBMS specific format.
  1114.      * @access public
  1115.      */
  1116.     function quote($value$type = null$quote = true$escape_wildcards = false)
  1117.     {
  1118.         $db =$this->getDBInstance();
  1119.         if (PEAR::isError($db)) {
  1120.             return $db;
  1121.         }
  1122.  
  1123.         if (is_null($value)
  1124.             || ($value === '' && $db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL)
  1125.         {
  1126.             if (!$quote{
  1127.                 return null;
  1128.             }
  1129.             return 'NULL';
  1130.         }
  1131.  
  1132.         if (is_null($type)) {
  1133.             switch (gettype($value)) {
  1134.             case 'integer':
  1135.                 $type 'integer';
  1136.                 break;
  1137.             case 'double':
  1138.                 // todo: default to decimal as float is quite unusual
  1139.                 // $type = 'float';
  1140.                 $type 'decimal';
  1141.                 break;
  1142.             case 'boolean':
  1143.                 $type 'boolean';
  1144.                 break;
  1145.             case 'array':
  1146.                  $value serialize($value);
  1147.             case 'object':
  1148.                  $type 'text';
  1149.                 break;
  1150.             default:
  1151.                 if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/'$value)) {
  1152.                     $type 'timestamp';
  1153.                 elseif (preg_match('/^\d{2}:\d{2}$/'$value)) {
  1154.                     $type 'time';
  1155.                 elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/'$value)) {
  1156.                     $type 'date';
  1157.                 else {
  1158.                     $type 'text';
  1159.                 }
  1160.                 break;
  1161.             }
  1162.         elseif (!empty($db->options['datatype_map'][$type])) {
  1163.             $type $db->options['datatype_map'][$type];
  1164.             if (!empty($db->options['datatype_map_callback'][$type])) {
  1165.                 $parameter = array('type' => $type'value' => $value'quote' => $quote'escape_wildcards' => $escape_wildcards);
  1166.                 return call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  1167.             }
  1168.         }
  1169.  
  1170.         if (!method_exists($this"_quote{$type}")) {
  1171.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1172.                 'type not defined: '.$type__FUNCTION__);
  1173.         }
  1174.         $value $this->{"_quote{$type}"}($value$quote$escape_wildcards);
  1175.         if ($quote && $escape_wildcards && $db->string_quoting['escape_pattern']
  1176.             && $db->string_quoting['escape'!== $db->string_quoting['escape_pattern']
  1177.         {
  1178.             $value.= $this->patternEscapeString();
  1179.         }
  1180.         return $value;
  1181.     }
  1182.  
  1183.     // }}}
  1184.     // {{{ _quoteInteger()
  1185.  
  1186.     /**
  1187.      * Convert a text value into a DBMS specific format that is suitable to
  1188.      * compose query statements.
  1189.      *
  1190.      * @param string $value text string value that is intended to be converted.
  1191.      * @param bool $quote determines if the value should be quoted and escaped
  1192.      * @param bool $escape_wildcards if to escape escape wildcards
  1193.      * @return string text string that represents the given argument value in
  1194.      *        a DBMS specific format.
  1195.      * @access protected
  1196.      */
  1197.     function _quoteInteger($value$quote$escape_wildcards)
  1198.     {
  1199.         return (int)$value;
  1200.     }
  1201.  
  1202.     // }}}
  1203.     // {{{ _quoteText()
  1204.  
  1205.     /**
  1206.      * Convert a text value into a DBMS specific format that is suitable to
  1207.      * compose query statements.
  1208.      *
  1209.      * @param string $value text string value that is intended to be converted.
  1210.      * @param bool $quote determines if the value should be quoted and escaped
  1211.      * @param bool $escape_wildcards if to escape escape wildcards
  1212.      * @return string text string that already contains any DBMS specific
  1213.      *        escaped character sequences.
  1214.      * @access protected
  1215.      */
  1216.     function _quoteText($value$quote$escape_wildcards)
  1217.     {
  1218.         if (!$quote{
  1219.             return $value;
  1220.         }
  1221.  
  1222.         $db =$this->getDBInstance();
  1223.         if (PEAR::isError($db)) {
  1224.             return $db;
  1225.         }
  1226.  
  1227.         $value $db->escape($value$escape_wildcards);
  1228.         if (PEAR::isError($value)) {
  1229.             return $value;
  1230.         }
  1231.         return "'".$value."'";
  1232.     }
  1233.  
  1234.     // }}}
  1235.     // {{{ _readFile()
  1236.  
  1237.     /**
  1238.      * Convert a text value into a DBMS specific format that is suitable to
  1239.      * compose query statements.
  1240.      *
  1241.      * @param string $value text string value that is intended to be converted.
  1242.      * @return string text string that represents the given argument value in
  1243.      *        a DBMS specific format.
  1244.      * @access protected
  1245.      */
  1246.     function _readFile($value)
  1247.     {
  1248.         $close = false;
  1249.         if (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1250.             $close = true;
  1251.             if ($match[1== 'file://'{
  1252.                 $value $match[2];
  1253.             }
  1254.             $value @fopen($value'r');
  1255.         }
  1256.  
  1257.         if (is_resource($value)) {
  1258.             $db =$this->getDBInstance();
  1259.             if (PEAR::isError($db)) {
  1260.                 return $db;
  1261.             }
  1262.  
  1263.             $fp $value;
  1264.             $value '';
  1265.             while (!@feof($fp)) {
  1266.                 $value.= @fread($fp$db->options['lob_buffer_length']);
  1267.             }
  1268.             if ($close{
  1269.                 @fclose($fp);
  1270.             }
  1271.         }
  1272.  
  1273.         return $value;
  1274.     }
  1275.  
  1276.     // }}}
  1277.     // {{{ _quoteLOB()
  1278.  
  1279.     /**
  1280.      * Convert a text value into a DBMS specific format that is suitable to
  1281.      * compose query statements.
  1282.      *
  1283.      * @param string $value text string value that is intended to be converted.
  1284.      * @param bool $quote determines if the value should be quoted and escaped
  1285.      * @param bool $escape_wildcards if to escape escape wildcards
  1286.      * @return string text string that represents the given argument value in
  1287.      *        a DBMS specific format.
  1288.      * @access protected
  1289.      */
  1290.     function _quoteLOB($value$quote$escape_wildcards)
  1291.     {
  1292.         $value $this->_readFile($value);
  1293.         if (PEAR::isError($value)) {
  1294.             return $value;
  1295.         }
  1296.         return $this->_quoteText($value$quote$escape_wildcards);
  1297.     }
  1298.  
  1299.     // }}}
  1300.     // {{{ _quoteCLOB()
  1301.  
  1302.     /**
  1303.      * Convert a text value into a DBMS specific format that is suitable to
  1304.      * compose query statements.
  1305.      *
  1306.      * @param string $value text string value that is intended to be converted.
  1307.      * @param bool $quote determines if the value should be quoted and escaped
  1308.      * @param bool $escape_wildcards if to escape escape wildcards
  1309.      * @return string text string that represents the given argument value in
  1310.      *        a DBMS specific format.
  1311.      * @access protected
  1312.      */
  1313.     function _quoteCLOB($value$quote$escape_wildcards)
  1314.     {
  1315.         return $this->_quoteLOB($value$quote$escape_wildcards);
  1316.     }
  1317.  
  1318.     // }}}
  1319.     // {{{ _quoteBLOB()
  1320.  
  1321.     /**
  1322.      * Convert a text value into a DBMS specific format that is suitable to
  1323.      * compose query statements.
  1324.      *
  1325.      * @param string $value text string value that is intended to be converted.
  1326.      * @param bool $quote determines if the value should be quoted and escaped
  1327.      * @param bool $escape_wildcards if to escape escape wildcards
  1328.      * @return string text string that represents the given argument value in
  1329.      *        a DBMS specific format.
  1330.      * @access protected
  1331.      */
  1332.     function _quoteBLOB($value$quote$escape_wildcards)
  1333.     {
  1334.         return $this->_quoteLOB($value$quote$escape_wildcards);
  1335.     }
  1336.  
  1337.     // }}}
  1338.     // {{{ _quoteBoolean()
  1339.  
  1340.     /**
  1341.      * Convert a text value into a DBMS specific format that is suitable to
  1342.      * compose query statements.
  1343.      *
  1344.      * @param string $value text string value that is intended to be converted.
  1345.      * @param bool $quote determines if the value should be quoted and escaped
  1346.      * @param bool $escape_wildcards if to escape escape wildcards
  1347.      * @return string text string that represents the given argument value in
  1348.      *        a DBMS specific format.
  1349.      * @access protected
  1350.      */
  1351.     function _quoteBoolean($value$quote$escape_wildcards)
  1352.     {
  1353.         return ($value ? 1 : 0);
  1354.     }
  1355.  
  1356.     // }}}
  1357.     // {{{ _quoteDate()
  1358.  
  1359.     /**
  1360.      * Convert a text value into a DBMS specific format that is suitable to
  1361.      * compose query statements.
  1362.      *
  1363.      * @param string $value text string value that is intended to be converted.
  1364.      * @param bool $quote determines if the value should be quoted and escaped
  1365.      * @param bool $escape_wildcards if to escape escape wildcards
  1366.      * @return string text string that represents the given argument value in
  1367.      *        a DBMS specific format.
  1368.      * @access protected
  1369.      */
  1370.     function _quoteDate($value$quote$escape_wildcards)
  1371.     {
  1372.         if ($value === 'CURRENT_DATE'{
  1373.             $db =$this->getDBInstance();
  1374.             if (PEAR::isError($db)) {
  1375.                 return $db;
  1376.             }
  1377.             if (isset($db->function&& is_a($db->function'MDB2_Driver_Function_Common')) {
  1378.                 return $db->function->now('date');
  1379.             }
  1380.             return 'CURRENT_DATE';
  1381.         }
  1382.         return $this->_quoteText($value$quote$escape_wildcards);
  1383.     }
  1384.  
  1385.     // }}}
  1386.     // {{{ _quoteTimestamp()
  1387.  
  1388.     /**
  1389.      * Convert a text value into a DBMS specific format that is suitable to
  1390.      * compose query statements.
  1391.      *
  1392.      * @param string $value text string value that is intended to be converted.
  1393.      * @param bool $quote determines if the value should be quoted and escaped
  1394.      * @param bool $escape_wildcards if to escape escape wildcards
  1395.      * @return string text string that represents the given argument value in
  1396.      *        a DBMS specific format.
  1397.      * @access protected
  1398.      */
  1399.     function _quoteTimestamp($value$quote$escape_wildcards)
  1400.     {
  1401.         if ($value === 'CURRENT_TIMESTAMP'{
  1402.             $db =$this->getDBInstance();
  1403.             if (PEAR::isError($db)) {
  1404.                 return $db;
  1405.             }
  1406.             if (isset($db->function&& is_a($db->function'MDB2_Driver_Function_Common')) {
  1407.                 return $db->function->now('timestamp');
  1408.             }
  1409.             return 'CURRENT_TIMESTAMP';
  1410.         }
  1411.         return $this->_quoteText($value$quote$escape_wildcards);
  1412.     }
  1413.  
  1414.     // }}}
  1415.     // {{{ _quoteTime()
  1416.  
  1417.     /**
  1418.      * Convert a text value into a DBMS specific format that is suitable to
  1419.      *       compose query statements.
  1420.      *
  1421.      * @param string $value text string value that is intended to be converted.
  1422.      * @param bool $quote determines if the value should be quoted and escaped
  1423.      * @param bool $escape_wildcards if to escape escape wildcards
  1424.      * @return string text string that represents the given argument value in
  1425.      *        a DBMS specific format.
  1426.      * @access protected
  1427.      */
  1428.     function _quoteTime($value$quote$escape_wildcards)
  1429.     {
  1430.         if ($value === 'CURRENT_TIME'{
  1431.             $db =$this->getDBInstance();
  1432.             if (PEAR::isError($db)) {
  1433.                 return $db;
  1434.             }
  1435.             if (isset($db->function&& is_a($db->function'MDB2_Driver_Function_Common')) {
  1436.                 return $db->function->now('time');
  1437.             }
  1438.             return 'CURRENT_TIME';
  1439.         }
  1440.         return $this->_quoteText($value$quote$escape_wildcards);
  1441.     }
  1442.  
  1443.     // }}}
  1444.     // {{{ _quoteFloat()
  1445.  
  1446.     /**
  1447.      * Convert a text value into a DBMS specific format that is suitable to
  1448.      * compose query statements.
  1449.      *
  1450.      * @param string $value text string value that is intended to be converted.
  1451.      * @param bool $quote determines if the value should be quoted and escaped
  1452.      * @param bool $escape_wildcards if to escape escape wildcards
  1453.      * @return string text string that represents the given argument value in
  1454.      *        a DBMS specific format.
  1455.      * @access protected
  1456.      */
  1457.     function _quoteFloat($value$quote$escape_wildcards)
  1458.     {
  1459.         if (preg_match('/^(.*)e([-+])(\d+)$/i'$value$matches)) {
  1460.             $decimal $this->_quoteDecimal($matches[1]$quote$escape_wildcards);
  1461.             $sign $matches[2];
  1462.             $exponent str_pad($matches[3]2'0'STR_PAD_LEFT);
  1463.             $value $decimal.'E'.$sign.$exponent;
  1464.         else {
  1465.             $value $this->_quoteDecimal($value$quote$escape_wildcards);
  1466.         }
  1467.         return $value;
  1468.     }
  1469.  
  1470.     // }}}
  1471.     // {{{ _quoteDecimal()
  1472.  
  1473.     /**
  1474.      * Convert a text value into a DBMS specific format that is suitable to
  1475.      * compose query statements.
  1476.      *
  1477.      * @param string $value text string value that is intended to be converted.
  1478.      * @param bool $quote determines if the value should be quoted and escaped
  1479.      * @param bool $escape_wildcards if to escape escape wildcards
  1480.      * @return string text string that represents the given argument value in
  1481.      *        a DBMS specific format.
  1482.      * @access protected
  1483.      */
  1484.     function _quoteDecimal($value$quote$escape_wildcards)
  1485.     {
  1486.         $value = (string)$value;
  1487.         if (preg_match('/[^.0-9]/'$value)) {
  1488.             if (strpos($value',')) {
  1489.                 // 1000,00
  1490.                 if (!strpos($value'.')) {
  1491.                     // convert the last "," to a "."
  1492.                     $value strrev(str_replace(',''.'strrev($value)));
  1493.                 // 1.000,00
  1494.                 elseif (strpos($value'.'&& strpos($value'.'strpos($value',')) {
  1495.                     $value str_replace('.'''$value);
  1496.                     // convert the last "," to a "."
  1497.                     $value strrev(str_replace(',''.'strrev($value)));
  1498.                 // 1,000.00
  1499.                 else {
  1500.                     $value str_replace(','''$value);
  1501.                 }
  1502.             }
  1503.         }
  1504.         return $value;
  1505.     }
  1506.  
  1507.     // }}}
  1508.     // {{{ writeLOBToFile()
  1509.  
  1510.     /**
  1511.      * retrieve LOB from the database
  1512.      *
  1513.      * @param resource $lob stream handle
  1514.      * @param string $file name of the file into which the LOb should be fetched
  1515.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1516.      * @access protected
  1517.      */
  1518.     function writeLOBToFile($lob$file)
  1519.     {
  1520.         $db =$this->getDBInstance();
  1521.         if (PEAR::isError($db)) {
  1522.             return $db;
  1523.         }
  1524.  
  1525.         if (preg_match('/^(\w+:\/\/)(.*)$/'$file$match)) {
  1526.             if ($match[1== 'file://'{
  1527.                 $file $match[2];
  1528.             }
  1529.         }
  1530.  
  1531.         $fp @fopen($file'wb');
  1532.         while (!@feof($lob)) {
  1533.             $result @fread($lob$db->options['lob_buffer_length']);
  1534.             $read strlen($result);
  1535.             if (@fwrite($fp$result$read!= $read{
  1536.                 @fclose($fp);
  1537.                 return $db->raiseError(MDB2_ERRORnullnull,
  1538.                     'could not write to the output file'__FUNCTION__);
  1539.             }
  1540.         }
  1541.         @fclose($fp);
  1542.         return MDB2_OK;
  1543.     }
  1544.  
  1545.     // }}}
  1546.     // {{{ _retrieveLOB()
  1547.  
  1548.     /**
  1549.      * retrieve LOB from the database
  1550.      *
  1551.      * @param array $lob array
  1552.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1553.      * @access protected
  1554.      */
  1555.     function _retrieveLOB(&$lob)
  1556.     {
  1557.         if (is_null($lob['value'])) {
  1558.             $lob['value'$lob['resource'];
  1559.         }
  1560.         $lob['loaded'= true;
  1561.         return MDB2_OK;
  1562.     }
  1563.  
  1564.     // }}}
  1565.     // {{{ readLOB()
  1566.  
  1567.     /**
  1568.      * Read data from large object input stream.
  1569.      *
  1570.      * @param resource $lob stream handle
  1571.      * @param string $data reference to a variable that will hold data
  1572.      *                           to be read from the large object input stream
  1573.      * @param integer $length    value that indicates the largest ammount ofdata
  1574.      *                           to be read from the large object input stream.
  1575.      * @return mixed the effective number of bytes read from the large object
  1576.      *                       input stream on sucess or an MDB2 error object.
  1577.      * @access public
  1578.      * @see endOfLOB()
  1579.      */
  1580.     function _readLOB($lob$length)
  1581.     {
  1582.         return substr($lob['value']$lob['position']$length);
  1583.     }
  1584.  
  1585.     // }}}
  1586.     // {{{ _endOfLOB()
  1587.  
  1588.     /**
  1589.      * Determine whether it was reached the end of the large object and
  1590.      * therefore there is no more data to be read for the its input stream.
  1591.      *
  1592.      * @param array $lob array
  1593.      * @return mixed true or false on success, a MDB2 error on failure
  1594.      * @access protected
  1595.      */
  1596.     function _endOfLOB($lob)
  1597.     {
  1598.         return $lob['endOfLOB'];
  1599.     }
  1600.  
  1601.     // }}}
  1602.     // {{{ destroyLOB()
  1603.  
  1604.     /**
  1605.      * Free any resources allocated during the lifetime of the large object
  1606.      * handler object.
  1607.      *
  1608.      * @param resource $lob stream handle
  1609.      * @access public
  1610.      */
  1611.     function destroyLOB($lob)
  1612.     {
  1613.         $lob_data stream_get_meta_data($lob);
  1614.         $lob_index $lob_data['wrapper_data']->lob_index;
  1615.         fclose($lob);
  1616.         if (isset($this->lobs[$lob_index])) {
  1617.             $this->_destroyLOB($this->lobs[$lob_index]);
  1618.             unset($this->lobs[$lob_index]);
  1619.         }
  1620.         return MDB2_OK;
  1621.     }
  1622.  
  1623.     // }}}
  1624.     // {{{ _destroyLOB()
  1625.  
  1626.     /**
  1627.      * Free any resources allocated during the lifetime of the large object
  1628.      * handler object.
  1629.      *
  1630.      * @param array $lob array
  1631.      * @access private
  1632.      */
  1633.     function _destroyLOB(&$lob)
  1634.     {
  1635.         return MDB2_OK;
  1636.     }
  1637.  
  1638.     // }}}
  1639.     // {{{ implodeArray()
  1640.  
  1641.     /**
  1642.      * apply a type to all values of an array and return as a comma seperated string
  1643.      * useful for generating IN statements
  1644.      *
  1645.      * @access public
  1646.      *
  1647.      * @param array $array data array
  1648.      * @param string $type determines type of the field
  1649.      *
  1650.      * @return string comma seperated values
  1651.      */
  1652.     function implodeArray($array$type = false)
  1653.     {
  1654.         if (!is_array($array|| empty($array)) {
  1655.             return 'NULL';
  1656.         }
  1657.         if ($type{
  1658.             foreach ($array as $value{
  1659.                 $return[$this->quote($value$type);
  1660.             }
  1661.         else {
  1662.             $return $array;
  1663.         }
  1664.         return implode(', '$return);
  1665.     }
  1666.  
  1667.     // }}}
  1668.     // {{{ matchPattern()
  1669.  
  1670.     /**
  1671.      * build a pattern matching string
  1672.      *
  1673.      * EXPERIMENTAL
  1674.      *
  1675.      * WARNING: this function is experimental and may change signature at
  1676.      * any time until labelled as non-experimental
  1677.      *
  1678.      * @access public
  1679.      *
  1680.      * @param array $pattern even keys are strings, odd are patterns (% and _)
  1681.      * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
  1682.      * @param string $field optional field name that is being matched against
  1683.      *                   (might be required when emulating ILIKE)
  1684.      *
  1685.      * @return string SQL pattern
  1686.      */
  1687.     function matchPattern($pattern$operator = null$field = null)
  1688.     {
  1689.         $db =$this->getDBInstance();
  1690.         if (PEAR::isError($db)) {
  1691.             return $db;
  1692.         }
  1693.  
  1694.         $match '';
  1695.         if (!is_null($operator)) {
  1696.             $operator strtoupper($operator);
  1697.             switch ($operator{
  1698.             // case insensitive
  1699.             case 'ILIKE':
  1700.                 if (is_null($field)) {
  1701.                     return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1702.                         'case insensitive LIKE matching requires passing the field name'__FUNCTION__);
  1703.                 }
  1704.                 $db->loadModule('Function'nulltrue);
  1705.                 $match $db->function->lower($field).' LIKE ';
  1706.                 break;
  1707.             // case sensitive
  1708.             case 'LIKE':
  1709.                 $match is_null($field'LIKE ' $field.' LIKE ';
  1710.                 break;
  1711.             default:
  1712.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1713.                     'not a supported operator type:'$operator__FUNCTION__);
  1714.             }
  1715.         }
  1716.         $match.= "'";
  1717.         foreach ($pattern as $key => $value{
  1718.             if ($key % 2{
  1719.                 $match.= $value;
  1720.             else {
  1721.                 if ($operator === 'ILIKE'{
  1722.                     $value strtolower($value);
  1723.                 }
  1724.                 $escaped $db->escape($value);
  1725.                 if (PEAR::isError($escaped)) {
  1726.                     return $escaped;
  1727.                 }
  1728.                 $match.= $db->escapePattern($escaped);
  1729.             }
  1730.         }
  1731.         $match.= "'";
  1732.         $match.= $this->patternEscapeString();
  1733.         return $match;
  1734.     }
  1735.  
  1736.     // }}}
  1737.     // {{{ patternEscapeString()
  1738.  
  1739.     /**
  1740.      * build string to define pattern escape character
  1741.      *
  1742.      * EXPERIMENTAL
  1743.      *
  1744.      * WARNING: this function is experimental and may change signature at
  1745.      * any time until labelled as non-experimental
  1746.      *
  1747.      * @access public
  1748.      *
  1749.      * @return string define pattern escape character
  1750.      */
  1751.     function patternEscapeString()
  1752.     {
  1753.         return '';
  1754.     }
  1755.  
  1756.     // }}}
  1757.     // {{{ mapNativeDatatype()
  1758.  
  1759.     /**
  1760.      * Maps a native array description of a field to a MDB2 datatype and length
  1761.      *
  1762.      * @param array  $field native field description
  1763.      * @return array containing the various possible types, length, sign, fixed
  1764.      * @access public
  1765.      */
  1766.     function mapNativeDatatype($field)
  1767.     {
  1768.         $db =$this->getDBInstance();
  1769.         if (PEAR::isError($db)) {
  1770.             return $db;
  1771.         }
  1772.  
  1773.         // If the user has specified an option to map the native field
  1774.         // type to a custom MDB2 datatype...
  1775.         $db_type strtok($field['type']'(), ');
  1776.         if (!empty($db->options['nativetype_map_callback'][$db_type])) {
  1777.             return call_user_func_array($db->options['nativetype_map_callback'][$db_type]array($db$field));
  1778.         }
  1779.  
  1780.         // Otherwise perform the built-in (i.e. normal) MDB2 native type to
  1781.         // MDB2 datatype conversion
  1782.         return $this->_mapNativeDatatype($field);
  1783.     }
  1784.  
  1785.     // }}}
  1786.     // {{{ _mapNativeDatatype()
  1787.  
  1788.     /**
  1789.      * Maps a native array description of a field to a MDB2 datatype and length
  1790.      *
  1791.      * @param array  $field native field description
  1792.      * @return array containing the various possible types, length, sign, fixed
  1793.      * @access public
  1794.      */
  1795.     function _mapNativeDatatype($field)
  1796.     {
  1797.         $db =$this->getDBInstance();
  1798.         if (PEAR::isError($db)) {
  1799.             return $db;
  1800.         }
  1801.  
  1802.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1803.             'method not implemented'__FUNCTION__);
  1804.     }
  1805.  
  1806.     // }}}
  1807.     // {{{ mapPrepareDatatype()
  1808.  
  1809.     /**
  1810.      * Maps an mdb2 datatype to mysqli prepare type
  1811.      *
  1812.      * @param string $type 
  1813.      * @return string 
  1814.      * @access public
  1815.      */
  1816.     function mapPrepareDatatype($type)
  1817.     {
  1818.         $db =$this->getDBInstance();
  1819.         if (PEAR::isError($db)) {
  1820.             return $db;
  1821.         }
  1822.  
  1823.         if (!empty($db->options['datatype_map'][$type])) {
  1824.             $type $db->options['datatype_map'][$type];
  1825.             if (!empty($db->options['datatype_map_callback'][$type])) {
  1826.                 $parameter = array('type' => $type);
  1827.                 return call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  1828.             }
  1829.         }
  1830.  
  1831.         return $type;
  1832.     }
  1833. }
  1834. ?>

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