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.137 2008/02/17 18:53:40 afz 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.         $notnull = empty($field['notnull']'' ' NOT NULL';
  500.         $default '';
  501.         if (array_key_exists('default'$field)) {
  502.             if ($field['default'=== ''{
  503.                 $db =$this->getDBInstance();
  504.                 if (PEAR::isError($db)) {
  505.                     return $db;
  506.                 }
  507.                 $valid_default_values $this->getValidTypes();
  508.                 $field['default'$valid_default_values[$field['type']];
  509.                 if ($field['default'=== ''&& ($db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL)) {
  510.                     $field['default'' ';
  511.                 }
  512.             }
  513.             if (!is_null($field['default'])) {
  514.                 $default ' DEFAULT ' $this->quote($field['default']$field['type']);
  515.             }
  516.         }
  517.  
  518.         $collation = empty($field['collation']'' :
  519.             ' '.$this->_getCollationFieldDeclaration($field['collation']);
  520.  
  521.         return $charset.$default.$notnull.$collation;
  522.     }
  523.  
  524.     // }}}
  525.     // {{{ _getCharsetFieldDeclaration()
  526.     
  527.     /**
  528.      * Obtain DBMS specific SQL code portion needed to set the CHARACTER SET
  529.      * of a field declaration to be used in statements like CREATE TABLE.
  530.      *
  531.      * @param string $charset   name of the charset
  532.      * @return string  DBMS specific SQL code portion needed to set the CHARACTER SET
  533.      *                  of a field declaration.
  534.      */
  535.     function _getCharsetFieldDeclaration($charset)
  536.     {
  537.         return '';
  538.     }
  539.  
  540.     // }}}
  541.     // {{{ _getCollationFieldDeclaration()
  542.  
  543.     /**
  544.      * Obtain DBMS specific SQL code portion needed to set the COLLATION
  545.      * of a field declaration to be used in statements like CREATE TABLE.
  546.      *
  547.      * @param string $collation   name of the collation
  548.      * @return string  DBMS specific SQL code portion needed to set the COLLATION
  549.      *                  of a field declaration.
  550.      */
  551.     function _getCollationFieldDeclaration($collation)
  552.     {
  553.         return '';
  554.     }
  555.  
  556.     // }}}
  557.     // {{{ _getIntegerDeclaration()
  558.  
  559.     /**
  560.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  561.      * field to be used in statements like CREATE TABLE.
  562.      *
  563.      * @param string $name name the field to be declared.
  564.      * @param array $field associative array with the name of the properties
  565.      *        of the field being declared as array indexes. Currently, the types
  566.      *        of supported field properties are as follows:
  567.      *
  568.      *        unsigned
  569.      *            Boolean flag that indicates whether the field should be
  570.      *            declared as unsigned integer if possible.
  571.      *
  572.      *        default
  573.      *            Integer value to be used as default for this field.
  574.      *
  575.      *        notnull
  576.      *            Boolean flag that indicates whether this field is constrained
  577.      *            to not be set to null.
  578.      * @return string DBMS specific SQL code portion that should be used to
  579.      *        declare the specified field.
  580.      * @access protected
  581.      */
  582.     function _getIntegerDeclaration($name$field)
  583.     {
  584.         if (!empty($field['unsigned'])) {
  585.             $db =$this->getDBInstance();
  586.             if (PEAR::isError($db)) {
  587.                 return $db;
  588.             }
  589.  
  590.             $db->warnings[= "unsigned integer field \"$name\" is being declared as signed integer";
  591.         }
  592.         return $this->_getDeclaration($name$field);
  593.     }
  594.  
  595.     // }}}
  596.     // {{{ _getTextDeclaration()
  597.  
  598.     /**
  599.      * Obtain DBMS specific SQL code portion needed to declare an text type
  600.      * field to be used in statements like CREATE TABLE.
  601.      *
  602.      * @param string $name name the field to be declared.
  603.      * @param array $field associative array with the name of the properties
  604.      *        of the field being declared as array indexes. Currently, the types
  605.      *        of supported field properties are as follows:
  606.      *
  607.      *        length
  608.      *            Integer value that determines the maximum length of the text
  609.      *            field. If this argument is missing the field should be
  610.      *            declared to have the longest length allowed by the DBMS.
  611.      *
  612.      *        default
  613.      *            Text value to be used as default for this field.
  614.      *
  615.      *        notnull
  616.      *            Boolean flag that indicates whether this field is constrained
  617.      *            to not be set to null.
  618.      * @return string DBMS specific SQL code portion that should be used to
  619.      *        declare the specified field.
  620.      * @access protected
  621.      */
  622.     function _getTextDeclaration($name$field)
  623.     {
  624.         return $this->_getDeclaration($name$field);
  625.     }
  626.  
  627.     // }}}
  628.     // {{{ _getCLOBDeclaration()
  629.  
  630.     /**
  631.      * Obtain DBMS specific SQL code portion needed to declare an character
  632.      * large object type field to be used in statements like CREATE TABLE.
  633.      *
  634.      * @param string $name name the field to be declared.
  635.      * @param array $field associative array with the name of the properties
  636.      *         of the field being declared as array indexes. Currently, the types
  637.      *         of supported field properties are as follows:
  638.      *
  639.      *         length
  640.      *             Integer value that determines the maximum length of the large
  641.      *             object field. If this argument is missing the field should be
  642.      *             declared to have the longest length allowed by the DBMS.
  643.      *
  644.      *         notnull
  645.      *             Boolean flag that indicates whether this field is constrained
  646.      *             to not be set to null.
  647.      * @return string DBMS specific SQL code portion that should be used to
  648.      *         declare the specified field.
  649.      * @access public
  650.      */
  651.     function _getCLOBDeclaration($name$field)
  652.     {
  653.         $db =$this->getDBInstance();
  654.         if (PEAR::isError($db)) {
  655.             return $db;
  656.         }
  657.  
  658.         $notnull = empty($field['notnull']'' ' NOT NULL';
  659.         $name $db->quoteIdentifier($nametrue);
  660.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  661.     }
  662.  
  663.     // }}}
  664.     // {{{ _getBLOBDeclaration()
  665.  
  666.     /**
  667.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  668.      * object type field to be used in statements like CREATE TABLE.
  669.      *
  670.      * @param string $name name the field to be declared.
  671.      * @param array $field associative array with the name of the properties
  672.      *         of the field being declared as array indexes. Currently, the types
  673.      *         of supported field properties are as follows:
  674.      *
  675.      *         length
  676.      *             Integer value that determines the maximum length of the large
  677.      *             object field. If this argument is missing the field should be
  678.      *             declared to have the longest length allowed by the DBMS.
  679.      *
  680.      *         notnull
  681.      *             Boolean flag that indicates whether this field is constrained
  682.      *             to not be set to null.
  683.      * @return string DBMS specific SQL code portion that should be used to
  684.      *         declare the specified field.
  685.      * @access protected
  686.      */
  687.     function _getBLOBDeclaration($name$field)
  688.     {
  689.         $db =$this->getDBInstance();
  690.         if (PEAR::isError($db)) {
  691.             return $db;
  692.         }
  693.  
  694.         $notnull = empty($field['notnull']'' ' NOT NULL';
  695.         $name $db->quoteIdentifier($nametrue);
  696.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  697.     }
  698.  
  699.     // }}}
  700.     // {{{ _getBooleanDeclaration()
  701.  
  702.     /**
  703.      * Obtain DBMS specific SQL code portion needed to declare a boolean type
  704.      * field to be used in statements like CREATE TABLE.
  705.      *
  706.      * @param string $name name the field to be declared.
  707.      * @param array $field associative array with the name of the properties
  708.      *        of the field being declared as array indexes. Currently, the types
  709.      *        of supported field properties are as follows:
  710.      *
  711.      *        default
  712.      *            Boolean value to be used as default for this field.
  713.      *
  714.      *        notnullL
  715.      *            Boolean flag that indicates whether this field is constrained
  716.      *            to not be set to null.
  717.      * @return string DBMS specific SQL code portion that should be used to
  718.      *        declare the specified field.
  719.      * @access protected
  720.      */
  721.     function _getBooleanDeclaration($name$field)
  722.     {
  723.         return $this->_getDeclaration($name$field);
  724.     }
  725.  
  726.     // }}}
  727.     // {{{ _getDateDeclaration()
  728.  
  729.     /**
  730.      * Obtain DBMS specific SQL code portion needed to declare a date type
  731.      * field to be used in statements like CREATE TABLE.
  732.      *
  733.      * @param string $name name the field to be declared.
  734.      * @param array $field associative array with the name of the properties
  735.      *        of the field being declared as array indexes. Currently, the types
  736.      *        of supported field properties are as follows:
  737.      *
  738.      *        default
  739.      *            Date value to be used as default for this field.
  740.      *
  741.      *        notnull
  742.      *            Boolean flag that indicates whether this field is constrained
  743.      *            to not be set to null.
  744.      * @return string DBMS specific SQL code portion that should be used to
  745.      *        declare the specified field.
  746.      * @access protected
  747.      */
  748.     function _getDateDeclaration($name$field)
  749.     {
  750.         return $this->_getDeclaration($name$field);
  751.     }
  752.  
  753.     // }}}
  754.     // {{{ _getTimestampDeclaration()
  755.  
  756.     /**
  757.      * Obtain DBMS specific SQL code portion needed to declare a timestamp
  758.      * field to be used in statements like CREATE TABLE.
  759.      *
  760.      * @param string $name name the field to be declared.
  761.      * @param array $field associative array with the name of the properties
  762.      *        of the field being declared as array indexes. Currently, the types
  763.      *        of supported field properties are as follows:
  764.      *
  765.      *        default
  766.      *            Timestamp value to be used as default for this field.
  767.      *
  768.      *        notnull
  769.      *            Boolean flag that indicates whether this field is constrained
  770.      *            to not be set to null.
  771.      * @return string DBMS specific SQL code portion that should be used to
  772.      *        declare the specified field.
  773.      * @access protected
  774.      */
  775.     function _getTimestampDeclaration($name$field)
  776.     {
  777.         return $this->_getDeclaration($name$field);
  778.     }
  779.  
  780.     // }}}
  781.     // {{{ _getTimeDeclaration()
  782.  
  783.     /**
  784.      * Obtain DBMS specific SQL code portion needed to declare a time
  785.      * field to be used in statements like CREATE TABLE.
  786.      *
  787.      * @param string $name name the field to be declared.
  788.      * @param array $field associative array with the name of the properties
  789.      *        of the field being declared as array indexes. Currently, the types
  790.      *        of supported field properties are as follows:
  791.      *
  792.      *        default
  793.      *            Time value to be used as default for this field.
  794.      *
  795.      *        notnull
  796.      *            Boolean flag that indicates whether this field is constrained
  797.      *            to not be set to null.
  798.      * @return string DBMS specific SQL code portion that should be used to
  799.      *        declare the specified field.
  800.      * @access protected
  801.      */
  802.     function _getTimeDeclaration($name$field)
  803.     {
  804.         return $this->_getDeclaration($name$field);
  805.     }
  806.  
  807.     // }}}
  808.     // {{{ _getFloatDeclaration()
  809.  
  810.     /**
  811.      * Obtain DBMS specific SQL code portion needed to declare a float type
  812.      * field to be used in statements like CREATE TABLE.
  813.      *
  814.      * @param string $name name the field to be declared.
  815.      * @param array $field associative array with the name of the properties
  816.      *        of the field being declared as array indexes. Currently, the types
  817.      *        of supported field properties are as follows:
  818.      *
  819.      *        default
  820.      *            Float value to be used as default for this field.
  821.      *
  822.      *        notnull
  823.      *            Boolean flag that indicates whether this field is constrained
  824.      *            to not be set to null.
  825.      * @return string DBMS specific SQL code portion that should be used to
  826.      *        declare the specified field.
  827.      * @access protected
  828.      */
  829.     function _getFloatDeclaration($name$field)
  830.     {
  831.         return $this->_getDeclaration($name$field);
  832.     }
  833.  
  834.     // }}}
  835.     // {{{ _getDecimalDeclaration()
  836.  
  837.     /**
  838.      * Obtain DBMS specific SQL code portion needed to declare a decimal type
  839.      * field to be used in statements like CREATE TABLE.
  840.      *
  841.      * @param string $name name the field to be declared.
  842.      * @param array $field associative array with the name of the properties
  843.      *        of the field being declared as array indexes. Currently, the types
  844.      *        of supported field properties are as follows:
  845.      *
  846.      *        default
  847.      *            Decimal value to be used as default for this field.
  848.      *
  849.      *        notnull
  850.      *            Boolean flag that indicates whether this field is constrained
  851.      *            to not be set to null.
  852.      * @return string DBMS specific SQL code portion that should be used to
  853.      *        declare the specified field.
  854.      * @access protected
  855.      */
  856.     function _getDecimalDeclaration($name$field)
  857.     {
  858.         return $this->_getDeclaration($name$field);
  859.     }
  860.  
  861.     // }}}
  862.     // {{{ compareDefinition()
  863.  
  864.     /**
  865.      * Obtain an array of changes that may need to applied
  866.      *
  867.      * @param array $current new definition
  868.      * @param array  $previous old definition
  869.      * @return array  containing all changes that will need to be applied
  870.      * @access public
  871.      */
  872.     function compareDefinition($current$previous)
  873.     {
  874.         $type !empty($current['type']$current['type': null;
  875.  
  876.         if (!method_exists($this"_compare{$type}Definition")) {
  877.             $db =$this->getDBInstance();
  878.             if (PEAR::isError($db)) {
  879.                 return $db;
  880.             }
  881.             if (!empty($db->options['datatype_map_callback'][$type])) {
  882.                 $parameter = array('current' => $current'previous' => $previous);
  883.                 $change =  call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  884.                 return $change;
  885.             }
  886.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  887.                 'type "'.$current['type'].'" is not yet supported'__FUNCTION__);
  888.         }
  889.  
  890.         if (empty($previous['type']|| $previous['type'!= $type{
  891.             return $current;
  892.         }
  893.  
  894.         $change $this->{"_compare{$type}Definition"}($current$previous);
  895.  
  896.         if ($previous['type'!= $type{
  897.             $change['type'= true;
  898.         }
  899.  
  900.         $previous_notnull !empty($previous['notnull']$previous['notnull': false;
  901.         $notnull !empty($current['notnull']$current['notnull': false;
  902.         if ($previous_notnull != $notnull{
  903.             $change['notnull'= true;
  904.         }
  905.  
  906.         $previous_default array_key_exists('default'$previous$previous['default':
  907.             ($previous_notnull '' : null);
  908.         $default array_key_exists('default'$current$current['default':
  909.             ($notnull '' : null);
  910.         if ($previous_default !== $default{
  911.             $change['default'= true;
  912.         }
  913.  
  914.         return $change;
  915.     }
  916.  
  917.     // }}}
  918.     // {{{ _compareIntegerDefinition()
  919.  
  920.     /**
  921.      * Obtain an array of changes that may need to applied to an integer field
  922.      *
  923.      * @param array $current new definition
  924.      * @param array  $previous old definition
  925.      * @return array  containing all changes that will need to be applied
  926.      * @access protected
  927.      */
  928.     function _compareIntegerDefinition($current$previous)
  929.     {
  930.         $change = array();
  931.         $previous_unsigned !empty($previous['unsigned']$previous['unsigned': false;
  932.         $unsigned !empty($current['unsigned']$current['unsigned': false;
  933.         if ($previous_unsigned != $unsigned{
  934.             $change['unsigned'= true;
  935.         }
  936.         $previous_autoincrement !empty($previous['autoincrement']$previous['autoincrement': false;
  937.         $autoincrement !empty($current['autoincrement']$current['autoincrement': false;
  938.         if ($previous_autoincrement != $autoincrement{
  939.             $change['autoincrement'= true;
  940.         }
  941.         return $change;
  942.     }
  943.  
  944.     // }}}
  945.     // {{{ _compareTextDefinition()
  946.  
  947.     /**
  948.      * Obtain an array of changes that may need to applied to an text field
  949.      *
  950.      * @param array $current new definition
  951.      * @param array  $previous old definition
  952.      * @return array  containing all changes that will need to be applied
  953.      * @access protected
  954.      */
  955.     function _compareTextDefinition($current$previous)
  956.     {
  957.         $change = array();
  958.         $previous_length !empty($previous['length']$previous['length': 0;
  959.         $length !empty($current['length']$current['length': 0;
  960.         if ($previous_length != $length{
  961.             $change['length'= true;
  962.         }
  963.         $previous_fixed !empty($previous['fixed']$previous['fixed': 0;
  964.         $fixed !empty($current['fixed']$current['fixed': 0;
  965.         if ($previous_fixed != $fixed{
  966.             $change['fixed'= true;
  967.         }
  968.         return $change;
  969.     }
  970.  
  971.     // }}}
  972.     // {{{ _compareCLOBDefinition()
  973.  
  974.     /**
  975.      * Obtain an array of changes that may need to applied to an CLOB field
  976.      *
  977.      * @param array $current new definition
  978.      * @param array  $previous old definition
  979.      * @return array  containing all changes that will need to be applied
  980.      * @access protected
  981.      */
  982.     function _compareCLOBDefinition($current$previous)
  983.     {
  984.         return $this->_compareTextDefinition($current$previous);
  985.     }
  986.  
  987.     // }}}
  988.     // {{{ _compareBLOBDefinition()
  989.  
  990.     /**
  991.      * Obtain an array of changes that may need to applied to an BLOB field
  992.      *
  993.      * @param array $current new definition
  994.      * @param array  $previous old definition
  995.      * @return array  containing all changes that will need to be applied
  996.      * @access protected
  997.      */
  998.     function _compareBLOBDefinition($current$previous)
  999.     {
  1000.         return $this->_compareTextDefinition($current$previous);
  1001.     }
  1002.  
  1003.     // }}}
  1004.     // {{{ _compareDateDefinition()
  1005.  
  1006.     /**
  1007.      * Obtain an array of changes that may need to applied to an date field
  1008.      *
  1009.      * @param array $current new definition
  1010.      * @param array  $previous old definition
  1011.      * @return array  containing all changes that will need to be applied
  1012.      * @access protected
  1013.      */
  1014.     function _compareDateDefinition($current$previous)
  1015.     {
  1016.         return array();
  1017.     }
  1018.  
  1019.     // }}}
  1020.     // {{{ _compareTimeDefinition()
  1021.  
  1022.     /**
  1023.      * Obtain an array of changes that may need to applied to an time field
  1024.      *
  1025.      * @param array $current new definition
  1026.      * @param array  $previous old definition
  1027.      * @return array  containing all changes that will need to be applied
  1028.      * @access protected
  1029.      */
  1030.     function _compareTimeDefinition($current$previous)
  1031.     {
  1032.         return array();
  1033.     }
  1034.  
  1035.     // }}}
  1036.     // {{{ _compareTimestampDefinition()
  1037.  
  1038.     /**
  1039.      * Obtain an array of changes that may need to applied to an timestamp field
  1040.      *
  1041.      * @param array $current new definition
  1042.      * @param array  $previous old definition
  1043.      * @return array  containing all changes that will need to be applied
  1044.      * @access protected
  1045.      */
  1046.     function _compareTimestampDefinition($current$previous)
  1047.     {
  1048.         return array();
  1049.     }
  1050.  
  1051.     // }}}
  1052.     // {{{ _compareBooleanDefinition()
  1053.  
  1054.     /**
  1055.      * Obtain an array of changes that may need to applied to an boolean field
  1056.      *
  1057.      * @param array $current new definition
  1058.      * @param array  $previous old definition
  1059.      * @return array  containing all changes that will need to be applied
  1060.      * @access protected
  1061.      */
  1062.     function _compareBooleanDefinition($current$previous)
  1063.     {
  1064.         return array();
  1065.     }
  1066.  
  1067.     // }}}
  1068.     // {{{ _compareFloatDefinition()
  1069.  
  1070.     /**
  1071.      * Obtain an array of changes that may need to applied to an float field
  1072.      *
  1073.      * @param array $current new definition
  1074.      * @param array  $previous old definition
  1075.      * @return array  containing all changes that will need to be applied
  1076.      * @access protected
  1077.      */
  1078.     function _compareFloatDefinition($current$previous)
  1079.     {
  1080.         return array();
  1081.     }
  1082.  
  1083.     // }}}
  1084.     // {{{ _compareDecimalDefinition()
  1085.  
  1086.     /**
  1087.      * Obtain an array of changes that may need to applied to an decimal field
  1088.      *
  1089.      * @param array $current new definition
  1090.      * @param array  $previous old definition
  1091.      * @return array  containing all changes that will need to be applied
  1092.      * @access protected
  1093.      */
  1094.     function _compareDecimalDefinition($current$previous)
  1095.     {
  1096.         return array();
  1097.     }
  1098.  
  1099.     // }}}
  1100.     // {{{ quote()
  1101.  
  1102.     /**
  1103.      * Convert a text value into a DBMS specific format that is suitable to
  1104.      * compose query statements.
  1105.      *
  1106.      * @param string $value text string value that is intended to be converted.
  1107.      * @param string $type type to which the value should be converted to
  1108.      * @param bool $quote determines if the value should be quoted and escaped
  1109.      * @param bool $escape_wildcards if to escape escape wildcards
  1110.      * @return string text string that represents the given argument value in
  1111.      *        a DBMS specific format.
  1112.      * @access public
  1113.      */
  1114.     function quote($value$type = null$quote = true$escape_wildcards = false)
  1115.     {
  1116.         $db =$this->getDBInstance();
  1117.         if (PEAR::isError($db)) {
  1118.             return $db;
  1119.         }
  1120.  
  1121.         if (is_null($value)
  1122.             || ($value === '' && $db->options['portability'MDB2_PORTABILITY_EMPTY_TO_NULL)
  1123.         {
  1124.             if (!$quote{
  1125.                 return null;
  1126.             }
  1127.             return 'NULL';
  1128.         }
  1129.  
  1130.         if (is_null($type)) {
  1131.             switch (gettype($value)) {
  1132.             case 'integer':
  1133.                 $type 'integer';
  1134.                 break;
  1135.             case 'double':
  1136.                 // todo: default to decimal as float is quite unusual
  1137.                 // $type = 'float';
  1138.                 $type 'decimal';
  1139.                 break;
  1140.             case 'boolean':
  1141.                 $type 'boolean';
  1142.                 break;
  1143.             case 'array':
  1144.                  $value serialize($value);
  1145.             case 'object':
  1146.                  $type 'text';
  1147.                 break;
  1148.             default:
  1149.                 if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}$/'$value)) {
  1150.                     $type 'timestamp';
  1151.                 elseif (preg_match('/^\d{2}:\d{2}$/'$value)) {
  1152.                     $type 'time';
  1153.                 elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/'$value)) {
  1154.                     $type 'date';
  1155.                 else {
  1156.                     $type 'text';
  1157.                 }
  1158.                 break;
  1159.             }
  1160.         elseif (!empty($db->options['datatype_map'][$type])) {
  1161.             $type $db->options['datatype_map'][$type];
  1162.             if (!empty($db->options['datatype_map_callback'][$type])) {
  1163.                 $parameter = array('type' => $type'value' => $value'quote' => $quote'escape_wildcards' => $escape_wildcards);
  1164.                 return call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  1165.             }
  1166.         }
  1167.  
  1168.         if (!method_exists($this"_quote{$type}")) {
  1169.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1170.                 'type not defined: '.$type__FUNCTION__);
  1171.         }
  1172.         $value $this->{"_quote{$type}"}($value$quote$escape_wildcards);
  1173.         if ($quote && $escape_wildcards && $db->string_quoting['escape_pattern']
  1174.             && $db->string_quoting['escape'!== $db->string_quoting['escape_pattern']
  1175.         {
  1176.             $value.= $this->patternEscapeString();
  1177.         }
  1178.         return $value;
  1179.     }
  1180.  
  1181.     // }}}
  1182.     // {{{ _quoteInteger()
  1183.  
  1184.     /**
  1185.      * Convert a text value into a DBMS specific format that is suitable to
  1186.      * compose query statements.
  1187.      *
  1188.      * @param string $value text string value that is intended to be converted.
  1189.      * @param bool $quote determines if the value should be quoted and escaped
  1190.      * @param bool $escape_wildcards if to escape escape wildcards
  1191.      * @return string text string that represents the given argument value in
  1192.      *        a DBMS specific format.
  1193.      * @access protected
  1194.      */
  1195.     function _quoteInteger($value$quote$escape_wildcards)
  1196.     {
  1197.         return (int)$value;
  1198.     }
  1199.  
  1200.     // }}}
  1201.     // {{{ _quoteText()
  1202.  
  1203.     /**
  1204.      * Convert a text value into a DBMS specific format that is suitable to
  1205.      * compose query statements.
  1206.      *
  1207.      * @param string $value text string value that is intended to be converted.
  1208.      * @param bool $quote determines if the value should be quoted and escaped
  1209.      * @param bool $escape_wildcards if to escape escape wildcards
  1210.      * @return string text string that already contains any DBMS specific
  1211.      *        escaped character sequences.
  1212.      * @access protected
  1213.      */
  1214.     function _quoteText($value$quote$escape_wildcards)
  1215.     {
  1216.         if (!$quote{
  1217.             return $value;
  1218.         }
  1219.  
  1220.         $db =$this->getDBInstance();
  1221.         if (PEAR::isError($db)) {
  1222.             return $db;
  1223.         }
  1224.  
  1225.         $value $db->escape($value$escape_wildcards);
  1226.         if (PEAR::isError($value)) {
  1227.             return $value;
  1228.         }
  1229.         return "'".$value."'";
  1230.     }
  1231.  
  1232.     // }}}
  1233.     // {{{ _readFile()
  1234.  
  1235.     /**
  1236.      * Convert a text value into a DBMS specific format that is suitable to
  1237.      * compose query statements.
  1238.      *
  1239.      * @param string $value text string value that is intended to be converted.
  1240.      * @return string text string that represents the given argument value in
  1241.      *        a DBMS specific format.
  1242.      * @access protected
  1243.      */
  1244.     function _readFile($value)
  1245.     {
  1246.         $close = false;
  1247.         if (preg_match('/^(\w+:\/\/)(.*)$/'$value$match)) {
  1248.             $close = true;
  1249.             if ($match[1== 'file://'{
  1250.                 $value $match[2];
  1251.             }
  1252.             $value @fopen($value'r');
  1253.         }
  1254.  
  1255.         if (is_resource($value)) {
  1256.             $db =$this->getDBInstance();
  1257.             if (PEAR::isError($db)) {
  1258.                 return $db;
  1259.             }
  1260.  
  1261.             $fp $value;
  1262.             $value '';
  1263.             while (!@feof($fp)) {
  1264.                 $value.= @fread($fp$db->options['lob_buffer_length']);
  1265.             }
  1266.             if ($close{
  1267.                 @fclose($fp);
  1268.             }
  1269.         }
  1270.  
  1271.         return $value;
  1272.     }
  1273.  
  1274.     // }}}
  1275.     // {{{ _quoteLOB()
  1276.  
  1277.     /**
  1278.      * Convert a text value into a DBMS specific format that is suitable to
  1279.      * compose query statements.
  1280.      *
  1281.      * @param string $value text string value that is intended to be converted.
  1282.      * @param bool $quote determines if the value should be quoted and escaped
  1283.      * @param bool $escape_wildcards if to escape escape wildcards
  1284.      * @return string text string that represents the given argument value in
  1285.      *        a DBMS specific format.
  1286.      * @access protected
  1287.      */
  1288.     function _quoteLOB($value$quote$escape_wildcards)
  1289.     {
  1290.         $value $this->_readFile($value);
  1291.         if (PEAR::isError($value)) {
  1292.             return $value;
  1293.         }
  1294.         return $this->_quoteText($value$quote$escape_wildcards);
  1295.     }
  1296.  
  1297.     // }}}
  1298.     // {{{ _quoteCLOB()
  1299.  
  1300.     /**
  1301.      * Convert a text value into a DBMS specific format that is suitable to
  1302.      * compose query statements.
  1303.      *
  1304.      * @param string $value text string value that is intended to be converted.
  1305.      * @param bool $quote determines if the value should be quoted and escaped
  1306.      * @param bool $escape_wildcards if to escape escape wildcards
  1307.      * @return string text string that represents the given argument value in
  1308.      *        a DBMS specific format.
  1309.      * @access protected
  1310.      */
  1311.     function _quoteCLOB($value$quote$escape_wildcards)
  1312.     {
  1313.         return $this->_quoteLOB($value$quote$escape_wildcards);
  1314.     }
  1315.  
  1316.     // }}}
  1317.     // {{{ _quoteBLOB()
  1318.  
  1319.     /**
  1320.      * Convert a text value into a DBMS specific format that is suitable to
  1321.      * compose query statements.
  1322.      *
  1323.      * @param string $value text string value that is intended to be converted.
  1324.      * @param bool $quote determines if the value should be quoted and escaped
  1325.      * @param bool $escape_wildcards if to escape escape wildcards
  1326.      * @return string text string that represents the given argument value in
  1327.      *        a DBMS specific format.
  1328.      * @access protected
  1329.      */
  1330.     function _quoteBLOB($value$quote$escape_wildcards)
  1331.     {
  1332.         return $this->_quoteLOB($value$quote$escape_wildcards);
  1333.     }
  1334.  
  1335.     // }}}
  1336.     // {{{ _quoteBoolean()
  1337.  
  1338.     /**
  1339.      * Convert a text value into a DBMS specific format that is suitable to
  1340.      * compose query statements.
  1341.      *
  1342.      * @param string $value text string value that is intended to be converted.
  1343.      * @param bool $quote determines if the value should be quoted and escaped
  1344.      * @param bool $escape_wildcards if to escape escape wildcards
  1345.      * @return string text string that represents the given argument value in
  1346.      *        a DBMS specific format.
  1347.      * @access protected
  1348.      */
  1349.     function _quoteBoolean($value$quote$escape_wildcards)
  1350.     {
  1351.         return ($value ? 1 : 0);
  1352.     }
  1353.  
  1354.     // }}}
  1355.     // {{{ _quoteDate()
  1356.  
  1357.     /**
  1358.      * Convert a text value into a DBMS specific format that is suitable to
  1359.      * compose query statements.
  1360.      *
  1361.      * @param string $value text string value that is intended to be converted.
  1362.      * @param bool $quote determines if the value should be quoted and escaped
  1363.      * @param bool $escape_wildcards if to escape escape wildcards
  1364.      * @return string text string that represents the given argument value in
  1365.      *        a DBMS specific format.
  1366.      * @access protected
  1367.      */
  1368.     function _quoteDate($value$quote$escape_wildcards)
  1369.     {
  1370.         if ($value === 'CURRENT_DATE'{
  1371.             $db =$this->getDBInstance();
  1372.             if (PEAR::isError($db)) {
  1373.                 return $db;
  1374.             }
  1375.             if (isset($db->function&& is_a($db->function'MDB2_Driver_Function_Common')) {
  1376.                 return $db->function->now('date');
  1377.             }
  1378.             return 'CURRENT_DATE';
  1379.         }
  1380.         return $this->_quoteText($value$quote$escape_wildcards);
  1381.     }
  1382.  
  1383.     // }}}
  1384.     // {{{ _quoteTimestamp()
  1385.  
  1386.     /**
  1387.      * Convert a text value into a DBMS specific format that is suitable to
  1388.      * compose query statements.
  1389.      *
  1390.      * @param string $value text string value that is intended to be converted.
  1391.      * @param bool $quote determines if the value should be quoted and escaped
  1392.      * @param bool $escape_wildcards if to escape escape wildcards
  1393.      * @return string text string that represents the given argument value in
  1394.      *        a DBMS specific format.
  1395.      * @access protected
  1396.      */
  1397.     function _quoteTimestamp($value$quote$escape_wildcards)
  1398.     {
  1399.         if ($value === 'CURRENT_TIMESTAMP'{
  1400.             $db =$this->getDBInstance();
  1401.             if (PEAR::isError($db)) {
  1402.                 return $db;
  1403.             }
  1404.             if (isset($db->function&& is_a($db->function'MDB2_Driver_Function_Common')) {
  1405.                 return $db->function->now('timestamp');
  1406.             }
  1407.             return 'CURRENT_TIMESTAMP';
  1408.         }
  1409.         return $this->_quoteText($value$quote$escape_wildcards);
  1410.     }
  1411.  
  1412.     // }}}
  1413.     // {{{ _quoteTime()
  1414.  
  1415.     /**
  1416.      * Convert a text value into a DBMS specific format that is suitable to
  1417.      *       compose query statements.
  1418.      *
  1419.      * @param string $value text string value that is intended to be converted.
  1420.      * @param bool $quote determines if the value should be quoted and escaped
  1421.      * @param bool $escape_wildcards if to escape escape wildcards
  1422.      * @return string text string that represents the given argument value in
  1423.      *        a DBMS specific format.
  1424.      * @access protected
  1425.      */
  1426.     function _quoteTime($value$quote$escape_wildcards)
  1427.     {
  1428.         if ($value === 'CURRENT_TIME'{
  1429.             $db =$this->getDBInstance();
  1430.             if (PEAR::isError($db)) {
  1431.                 return $db;
  1432.             }
  1433.             if (isset($db->function&& is_a($db->function'MDB2_Driver_Function_Common')) {
  1434.                 return $db->function->now('time');
  1435.             }
  1436.             return 'CURRENT_TIME';
  1437.         }
  1438.         return $this->_quoteText($value$quote$escape_wildcards);
  1439.     }
  1440.  
  1441.     // }}}
  1442.     // {{{ _quoteFloat()
  1443.  
  1444.     /**
  1445.      * Convert a text value into a DBMS specific format that is suitable to
  1446.      * compose query statements.
  1447.      *
  1448.      * @param string $value text string value that is intended to be converted.
  1449.      * @param bool $quote determines if the value should be quoted and escaped
  1450.      * @param bool $escape_wildcards if to escape escape wildcards
  1451.      * @return string text string that represents the given argument value in
  1452.      *        a DBMS specific format.
  1453.      * @access protected
  1454.      */
  1455.     function _quoteFloat($value$quote$escape_wildcards)
  1456.     {
  1457.         if (preg_match('/^(.*)e([-+])(\d+)$/i'$value$matches)) {
  1458.             $decimal $this->_quoteDecimal($matches[1]$quote$escape_wildcards);
  1459.             $sign $matches[2];
  1460.             $exponent str_pad($matches[3]2'0'STR_PAD_LEFT);
  1461.             $value $decimal.'E'.$sign.$exponent;
  1462.         else {
  1463.             $value $this->_quoteDecimal($value$quote$escape_wildcards);
  1464.         }
  1465.         return $value;
  1466.     }
  1467.  
  1468.     // }}}
  1469.     // {{{ _quoteDecimal()
  1470.  
  1471.     /**
  1472.      * Convert a text value into a DBMS specific format that is suitable to
  1473.      * compose query statements.
  1474.      *
  1475.      * @param string $value text string value that is intended to be converted.
  1476.      * @param bool $quote determines if the value should be quoted and escaped
  1477.      * @param bool $escape_wildcards if to escape escape wildcards
  1478.      * @return string text string that represents the given argument value in
  1479.      *        a DBMS specific format.
  1480.      * @access protected
  1481.      */
  1482.     function _quoteDecimal($value$quote$escape_wildcards)
  1483.     {
  1484.         $value = (string)$value;
  1485.         $value preg_replace('/[^\d\.,\-+eE]/'''$value);
  1486.         if (preg_match('/[^\.\d]/'$value)) {
  1487.             if (strpos($value',')) {
  1488.                 // 1000,00
  1489.                 if (!strpos($value'.')) {
  1490.                     // convert the last "," to a "."
  1491.                     $value strrev(str_replace(',''.'strrev($value)));
  1492.                 // 1.000,00
  1493.                 elseif (strpos($value'.'&& strpos($value'.'strpos($value',')) {
  1494.                     $value str_replace('.'''$value);
  1495.                     // convert the last "," to a "."
  1496.                     $value strrev(str_replace(',''.'strrev($value)));
  1497.                 // 1,000.00
  1498.                 else {
  1499.                     $value str_replace(','''$value);
  1500.                 }
  1501.             }
  1502.         }
  1503.         return $value;
  1504.     }
  1505.  
  1506.     // }}}
  1507.     // {{{ writeLOBToFile()
  1508.  
  1509.     /**
  1510.      * retrieve LOB from the database
  1511.      *
  1512.      * @param resource $lob stream handle
  1513.      * @param string $file name of the file into which the LOb should be fetched
  1514.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1515.      * @access protected
  1516.      */
  1517.     function writeLOBToFile($lob$file)
  1518.     {
  1519.         $db =$this->getDBInstance();
  1520.         if (PEAR::isError($db)) {
  1521.             return $db;
  1522.         }
  1523.  
  1524.         if (preg_match('/^(\w+:\/\/)(.*)$/'$file$match)) {
  1525.             if ($match[1== 'file://'{
  1526.                 $file $match[2];
  1527.             }
  1528.         }
  1529.  
  1530.         $fp @fopen($file'wb');
  1531.         while (!@feof($lob)) {
  1532.             $result @fread($lob$db->options['lob_buffer_length']);
  1533.             $read strlen($result);
  1534.             if (@fwrite($fp$result$read!= $read{
  1535.                 @fclose($fp);
  1536.                 return $db->raiseError(MDB2_ERRORnullnull,
  1537.                     'could not write to the output file'__FUNCTION__);
  1538.             }
  1539.         }
  1540.         @fclose($fp);
  1541.         return MDB2_OK;
  1542.     }
  1543.  
  1544.     // }}}
  1545.     // {{{ _retrieveLOB()
  1546.  
  1547.     /**
  1548.      * retrieve LOB from the database
  1549.      *
  1550.      * @param array $lob array
  1551.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1552.      * @access protected
  1553.      */
  1554.     function _retrieveLOB(&$lob)
  1555.     {
  1556.         if (is_null($lob['value'])) {
  1557.             $lob['value'$lob['resource'];
  1558.         }
  1559.         $lob['loaded'= true;
  1560.         return MDB2_OK;
  1561.     }
  1562.  
  1563.     // }}}
  1564.     // {{{ readLOB()
  1565.  
  1566.     /**
  1567.      * Read data from large object input stream.
  1568.      *
  1569.      * @param resource $lob stream handle
  1570.      * @param string $data reference to a variable that will hold data
  1571.      *                           to be read from the large object input stream
  1572.      * @param integer $length    value that indicates the largest ammount ofdata
  1573.      *                           to be read from the large object input stream.
  1574.      * @return mixed the effective number of bytes read from the large object
  1575.      *                       input stream on sucess or an MDB2 error object.
  1576.      * @access public
  1577.      * @see endOfLOB()
  1578.      */
  1579.     function _readLOB($lob$length)
  1580.     {
  1581.         return substr($lob['value']$lob['position']$length);
  1582.     }
  1583.  
  1584.     // }}}
  1585.     // {{{ _endOfLOB()
  1586.  
  1587.     /**
  1588.      * Determine whether it was reached the end of the large object and
  1589.      * therefore there is no more data to be read for the its input stream.
  1590.      *
  1591.      * @param array $lob array
  1592.      * @return mixed true or false on success, a MDB2 error on failure
  1593.      * @access protected
  1594.      */
  1595.     function _endOfLOB($lob)
  1596.     {
  1597.         return $lob['endOfLOB'];
  1598.     }
  1599.  
  1600.     // }}}
  1601.     // {{{ destroyLOB()
  1602.  
  1603.     /**
  1604.      * Free any resources allocated during the lifetime of the large object
  1605.      * handler object.
  1606.      *
  1607.      * @param resource $lob stream handle
  1608.      * @access public
  1609.      */
  1610.     function destroyLOB($lob)
  1611.     {
  1612.         $lob_data stream_get_meta_data($lob);
  1613.         $lob_index $lob_data['wrapper_data']->lob_index;
  1614.         fclose($lob);
  1615.         if (isset($this->lobs[$lob_index])) {
  1616.             $this->_destroyLOB($this->lobs[$lob_index]);
  1617.             unset($this->lobs[$lob_index]);
  1618.         }
  1619.         return MDB2_OK;
  1620.     }
  1621.  
  1622.     // }}}
  1623.     // {{{ _destroyLOB()
  1624.  
  1625.     /**
  1626.      * Free any resources allocated during the lifetime of the large object
  1627.      * handler object.
  1628.      *
  1629.      * @param array $lob array
  1630.      * @access private
  1631.      */
  1632.     function _destroyLOB(&$lob)
  1633.     {
  1634.         return MDB2_OK;
  1635.     }
  1636.  
  1637.     // }}}
  1638.     // {{{ implodeArray()
  1639.  
  1640.     /**
  1641.      * apply a type to all values of an array and return as a comma seperated string
  1642.      * useful for generating IN statements
  1643.      *
  1644.      * @access public
  1645.      *
  1646.      * @param array $array data array
  1647.      * @param string $type determines type of the field
  1648.      *
  1649.      * @return string comma seperated values
  1650.      */
  1651.     function implodeArray($array$type = false)
  1652.     {
  1653.         if (!is_array($array|| empty($array)) {
  1654.             return 'NULL';
  1655.         }
  1656.         if ($type{
  1657.             foreach ($array as $value{
  1658.                 $return[$this->quote($value$type);
  1659.             }
  1660.         else {
  1661.             $return $array;
  1662.         }
  1663.         return implode(', '$return);
  1664.     }
  1665.  
  1666.     // }}}
  1667.     // {{{ matchPattern()
  1668.  
  1669.     /**
  1670.      * build a pattern matching string
  1671.      *
  1672.      * @access public
  1673.      *
  1674.      * @param array $pattern even keys are strings, odd are patterns (% and _)
  1675.      * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
  1676.      * @param string $field optional field name that is being matched against
  1677.      *                   (might be required when emulating ILIKE)
  1678.      *
  1679.      * @return string SQL pattern
  1680.      */
  1681.     function matchPattern($pattern$operator = null$field = null)
  1682.     {
  1683.         $db =$this->getDBInstance();
  1684.         if (PEAR::isError($db)) {
  1685.             return $db;
  1686.         }
  1687.  
  1688.         $match '';
  1689.         if (!is_null($operator)) {
  1690.             $operator strtoupper($operator);
  1691.             switch ($operator{
  1692.             // case insensitive
  1693.             case 'ILIKE':
  1694.                 if (is_null($field)) {
  1695.                     return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1696.                         'case insensitive LIKE matching requires passing the field name'__FUNCTION__);
  1697.                 }
  1698.                 $db->loadModule('Function'nulltrue);
  1699.                 $match $db->function->lower($field).' LIKE ';
  1700.                 break;
  1701.             // case sensitive
  1702.             case 'LIKE':
  1703.                 $match is_null($field'LIKE ' $field.' LIKE ';
  1704.                 break;
  1705.             default:
  1706.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1707.                     'not a supported operator type:'$operator__FUNCTION__);
  1708.             }
  1709.         }
  1710.         $match.= "'";
  1711.         foreach ($pattern as $key => $value{
  1712.             if ($key % 2{
  1713.                 $match.= $value;
  1714.             else {
  1715.                 if ($operator === 'ILIKE'{
  1716.                     $value strtolower($value);
  1717.                 }
  1718.                 $escaped $db->escape($value);
  1719.                 if (PEAR::isError($escaped)) {
  1720.                     return $escaped;
  1721.                 }
  1722.                 $match.= $db->escapePattern($escaped);
  1723.             }
  1724.         }
  1725.         $match.= "'";
  1726.         $match.= $this->patternEscapeString();
  1727.         return $match;
  1728.     }
  1729.  
  1730.     // }}}
  1731.     // {{{ patternEscapeString()
  1732.  
  1733.     /**
  1734.      * build string to define pattern escape character
  1735.      *
  1736.      * @access public
  1737.      *
  1738.      * @return string define pattern escape character
  1739.      */
  1740.     function patternEscapeString()
  1741.     {
  1742.         return '';
  1743.     }
  1744.  
  1745.     // }}}
  1746.     // {{{ mapNativeDatatype()
  1747.  
  1748.     /**
  1749.      * Maps a native array description of a field to a MDB2 datatype and length
  1750.      *
  1751.      * @param array  $field native field description
  1752.      * @return array containing the various possible types, length, sign, fixed
  1753.      * @access public
  1754.      */
  1755.     function mapNativeDatatype($field)
  1756.     {
  1757.         $db =$this->getDBInstance();
  1758.         if (PEAR::isError($db)) {
  1759.             return $db;
  1760.         }
  1761.  
  1762.         // If the user has specified an option to map the native field
  1763.         // type to a custom MDB2 datatype...
  1764.         $db_type strtok($field['type']'(), ');
  1765.         if (!empty($db->options['nativetype_map_callback'][$db_type])) {
  1766.             return call_user_func_array($db->options['nativetype_map_callback'][$db_type]array($db$field));
  1767.         }
  1768.  
  1769.         // Otherwise perform the built-in (i.e. normal) MDB2 native type to
  1770.         // MDB2 datatype conversion
  1771.         return $this->_mapNativeDatatype($field);
  1772.     }
  1773.  
  1774.     // }}}
  1775.     // {{{ _mapNativeDatatype()
  1776.  
  1777.     /**
  1778.      * Maps a native array description of a field to a MDB2 datatype and length
  1779.      *
  1780.      * @param array  $field native field description
  1781.      * @return array containing the various possible types, length, sign, fixed
  1782.      * @access public
  1783.      */
  1784.     function _mapNativeDatatype($field)
  1785.     {
  1786.         $db =$this->getDBInstance();
  1787.         if (PEAR::isError($db)) {
  1788.             return $db;
  1789.         }
  1790.  
  1791.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  1792.             'method not implemented'__FUNCTION__);
  1793.     }
  1794.  
  1795.     // }}}
  1796.     // {{{ mapPrepareDatatype()
  1797.  
  1798.     /**
  1799.      * Maps an mdb2 datatype to mysqli prepare type
  1800.      *
  1801.      * @param string $type 
  1802.      * @return string 
  1803.      * @access public
  1804.      */
  1805.     function mapPrepareDatatype($type)
  1806.     {
  1807.         $db =$this->getDBInstance();
  1808.         if (PEAR::isError($db)) {
  1809.             return $db;
  1810.         }
  1811.  
  1812.         if (!empty($db->options['datatype_map'][$type])) {
  1813.             $type $db->options['datatype_map'][$type];
  1814.             if (!empty($db->options['datatype_map_callback'][$type])) {
  1815.                 $parameter = array('type' => $type);
  1816.                 return call_user_func_array($db->options['datatype_map_callback'][$type]array(&$db__FUNCTION__$parameter));
  1817.             }
  1818.         }
  1819.  
  1820.         return $type;
  1821.     }
  1822. }
  1823. ?>

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