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

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