MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file mysqli.php

Documentation is available at mysqli.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5                                                 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  7. // | Stig. S. Bakken, Lukas Smith                                         |
  8. // | All rights reserved.                                                 |
  9. // +----------------------------------------------------------------------+
  10. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  11. // | API as well as database abstraction for PHP applications.            |
  12. // | This LICENSE is in the BSD license style.                            |
  13. // |                                                                      |
  14. // | Redistribution and use in source and binary forms, with or without   |
  15. // | modification, are permitted provided that the following conditions   |
  16. // | are met:                                                             |
  17. // |                                                                      |
  18. // | Redistributions of source code must retain the above copyright       |
  19. // | notice, this list of conditions and the following disclaimer.        |
  20. // |                                                                      |
  21. // | Redistributions in binary form must reproduce the above copyright    |
  22. // | notice, this list of conditions and the following disclaimer in the  |
  23. // | documentation and/or other materials provided with the distribution. |
  24. // |                                                                      |
  25. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  26. // | Lukas Smith nor the names of his contributors may be used to endorse |
  27. // | or promote products derived from this software without specific prior|
  28. // | written permission.                                                  |
  29. // |                                                                      |
  30. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  31. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  32. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  33. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  34. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  35. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  36. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  37. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  38. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  39. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  40. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  41. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  42. // +----------------------------------------------------------------------+
  43. // | Author: Lukas Smith <smith@backendmedia.com>                         |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: mysqli.php,v 1.4 2005/04/21 15:30:11 lsmith Exp $
  47. //
  48.  
  49. require_once 'MDB2/Driver/Datatype/Common.php';
  50.  
  51. /**
  52.  * MDB2 MySQL driver
  53.  *
  54.  * @package MDB2
  55.  * @category Database
  56.  * @author  Lukas Smith <smith@backendmedia.com>
  57.  */
  58. {
  59.     // {{{ convertResult()
  60.  
  61.     /**
  62.      * convert a value to a RDBMS indepdenant MDB2 type
  63.      *
  64.      * @param mixed  $value   value to be converted
  65.      * @param int    $type    constant that specifies which type to convert to
  66.      * @return mixed converted value
  67.      * @access public
  68.      */
  69.     function convertResult($value$type)
  70.     {
  71.         if (is_null($value)) {
  72.             return null;
  73.         }
  74.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  75.         return $this->_baseConvertResult($value$type);
  76.     }
  77.  
  78.     // }}}
  79.     // {{{ _getIntegerDeclaration()
  80.  
  81.     /**
  82.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  83.      * field to be used in statements like CREATE TABLE.
  84.      *
  85.      * @param string  $name   name the field to be declared.
  86.      * @param string  $field  associative array with the name of the properties
  87.      *                         of the field being declared as array indexes.
  88.      *                         Currently, the types of supported field
  89.      *                         properties are as follows:
  90.      *
  91.      *                        unsigned
  92.      *                         Boolean flag that indicates whether the field
  93.      *                         should be declared as unsigned integer if
  94.      *                         possible.
  95.      *
  96.      *                        default
  97.      *                         Integer value to be used as default for this
  98.      *                         field.
  99.      *
  100.      *                        notnull
  101.      *                         Boolean flag that indicates whether this field is
  102.      *                         constrained to not be set to null.
  103.      * @return string  DBMS specific SQL code portion that should be used to
  104.      *                  declare the specified field.
  105.      * @access protected
  106.      */
  107.     function _getIntegerDeclaration($name$field)
  108.     {
  109.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  110.         $unsigned = isset($field['unsigned']' UNSIGNED' '';
  111.         $default = isset($field['default']' DEFAULT '.
  112.             $this->quote($field['default']'integer''';
  113.         $notnull = isset($field['notnull']' NOT NULL' '';
  114.         return $name.' INT'.$unsigned.$default.$notnull;
  115.        ;
  116.     }
  117.  
  118.     // }}}
  119.     // {{{ _getCLOBDeclaration()
  120.  
  121.     /**
  122.      * Obtain DBMS specific SQL code portion needed to declare an character
  123.      * large object type field to be used in statements like CREATE TABLE.
  124.      *
  125.      * @param string  $name   name the field to be declared.
  126.      * @param string  $field  associative array with the name of the
  127.      *                         properties of the field being declared as array
  128.      *                         indexes. Currently, the types of supported field
  129.      *                         properties are as follows:
  130.      *
  131.      *                        length
  132.      *                         Integer value that determines the maximum length
  133.      *                         of the large object field. If this argument is
  134.      *                         missing the field should be declared to have the
  135.      *                         longest length allowed by the DBMS.
  136.      *
  137.      *                        notnull
  138.      *                         Boolean flag that indicates whether this field
  139.      *                         is constrained to not be set to null.
  140.      * @return string  DBMS specific SQL code portion that should be used to
  141.      *                  declare the specified field.
  142.      * @access protected
  143.      */
  144.     function _getCLOBDeclaration($name$field)
  145.     {
  146.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  147.         if (isset($field['length'])) {
  148.             $length $field['length'];
  149.             if ($length <= 255{
  150.                 $type 'TINYTEXT';
  151.             else {
  152.                 if ($length <= 65535{
  153.                     $type 'TEXT';
  154.                 else {
  155.                     if ($length <= 16777215{
  156.                         $type 'MEDIUMTEXT';
  157.                     else {
  158.                         $type 'LONGTEXT';
  159.                     }
  160.                 }
  161.             }
  162.         else {
  163.             $type 'LONGTEXT';
  164.         }
  165.         $notnull = isset($field['notnull']' NOT NULL' '';
  166.         return $name.' '.$type.$notnull;
  167.     }
  168.  
  169.     // }}}
  170.     // {{{ _getBLOBDeclaration()
  171.  
  172.     /**
  173.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  174.      * object type field to be used in statements like CREATE TABLE.
  175.      *
  176.      * @param string  $name   name the field to be declared.
  177.      * @param string  $field  associative array with the name of the properties
  178.      *                         of the field being declared as array indexes.
  179.      *                         Currently, the types of supported field
  180.      *                         properties are as follows:
  181.      *
  182.      *                        length
  183.      *                         Integer value that determines the maximum length
  184.      *                         of the large object field. If this argument is
  185.      *                         missing the field should be declared to have the
  186.      *                         longest length allowed by the DBMS.
  187.      *
  188.      *                        notnull
  189.      *                         Boolean flag that indicates whether this field is
  190.      *                         constrained to not be set to null.
  191.      * @return string  DBMS specific SQL code portion that should be used to
  192.      *                  declare the specified field.
  193.      * @access protected
  194.      */
  195.     function _getBLOBDeclaration($name$field)
  196.     {
  197.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  198.         if (isset($field['length'])) {
  199.             $length $field['length'];
  200.             if ($length <= 255{
  201.                 $type 'TINYBLOB';
  202.             else {
  203.                 if ($length <= 65535{
  204.                     $type 'BLOB';
  205.                 else {
  206.                     if ($length <= 16777215{
  207.                         $type 'MEDIUMBLOB';
  208.                     else {
  209.                         $type 'LONGBLOB';
  210.                     }
  211.                 }
  212.             }
  213.         }
  214.         else {
  215.             $type 'LONGBLOB';
  216.         }
  217.         $notnull = isset($field['notnull']' NOT NULL' '';
  218.         return $name.' '.$type.$notnull;
  219.     }
  220.  
  221.     // }}}
  222.     // {{{ _getDateDeclaration()
  223.  
  224.     /**
  225.      * Obtain DBMS specific SQL code portion needed to declare an date type
  226.      * field to be used in statements like CREATE TABLE.
  227.      *
  228.      * @param string  $name   name the field to be declared.
  229.      * @param string  $field  associative array with the name of the properties
  230.      *                         of the field being declared as array indexes.
  231.      *                         Currently, the types of supported field properties
  232.      *                         are as follows:
  233.      *
  234.      *                        default
  235.      *                         Date value to be used as default for this field.
  236.      *
  237.      *                        notnull
  238.      *                         Boolean flag that indicates whether this field is
  239.      *                         constrained to not be set to null.
  240.      * @return string  DBMS specific SQL code portion that should be used to
  241.      *                  declare the specified field.
  242.      * @access protected
  243.      */
  244.     function _getDateDeclaration($name$field)
  245.     {
  246.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  247.         $default = isset($field['default']' DEFAULT '.
  248.             $this->quote($field['default']'date''';
  249.         $notnull = isset($field['notnull']' NOT NULL' '';
  250.         return $name.' DATE'.$default.$notnull;
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ _getTimestampDeclaration()
  255.  
  256.     /**
  257.      * Obtain DBMS specific SQL code portion needed to declare an timestamp
  258.      * type field to be used in statements like CREATE TABLE.
  259.      *
  260.      * @param string  $name   name the field to be declared.
  261.      * @param string  $field  associative array with the name of the properties
  262.      *                         of the field being declared as array indexes.
  263.      *                         Currently, the types of supported field
  264.      *                         properties are as follows:
  265.      *
  266.      *                        default
  267.      *                         Time stamp value to be used as default for this
  268.      *                         field.
  269.      *
  270.      *                        notnull
  271.      *                         Boolean flag that indicates whether this field is
  272.      *                         constrained to not be set to null.
  273.      * @return string  DBMS specific SQL code portion that should be used to
  274.      *                  declare the specified field.
  275.      * @access protected
  276.      */
  277.     function _getTimestampDeclaration($name$field)
  278.     {
  279.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  280.         $default = isset($field['default']' DEFAULT '.
  281.             $this->quote($field['default']'timestamp''';
  282.         $notnull = isset($field['notnull']' NOT NULL' '';
  283.         return $name.' DATETIME'.$default.$notnull;
  284.     }
  285.  
  286.     // }}}
  287.     // {{{ _getTimeDeclaration()
  288.  
  289.     /**
  290.      * Obtain DBMS specific SQL code portion needed to declare an time type
  291.      * field to be used in statements like CREATE TABLE.
  292.      *
  293.      * @param string  $name   name the field to be declared.
  294.      * @param string  $field  associative array with the name of the properties
  295.      *                         of the field being declared as array indexes.
  296.      *                         Currently, the types of supported field
  297.      *                         properties are as follows:
  298.      *
  299.      *                        default
  300.      *                         Time value to be used as default for this field.
  301.      *
  302.      *                        notnull
  303.      *                         Boolean flag that indicates whether this field is
  304.      *                         constrained to not be set to null.
  305.      * @return string  DBMS specific SQL code portion that should be used to
  306.      *                  declare the specified field.
  307.      * @access protected
  308.      */
  309.     function _getTimeDeclaration($name$field)
  310.     {
  311.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  312.         $default = isset($field['default']' DEFAULT '.
  313.             $this->quote($field['default']'time''';
  314.         $notnull = isset($field['notnull']' NOT NULL' '';
  315.         return $name.' TIME'.$default.$notnull;
  316.     }
  317.  
  318.     // }}}
  319.     // {{{ _getFloatDeclaration()
  320.  
  321.     /**
  322.      * Obtain DBMS specific SQL code portion needed to declare an float type
  323.      * field to be used in statements like CREATE TABLE.
  324.      *
  325.      * @param string  $name   name the field to be declared.
  326.      * @param string  $field  associative array with the name of the properties
  327.      *                         of the field being declared as array indexes.
  328.      *                         Currently, the types of supported field
  329.      *                         properties are as follows:
  330.      *
  331.      *                        default
  332.      *                         Integer value to be used as default for this
  333.      *                         field.
  334.      *
  335.      *                        notnull
  336.      *                         Boolean flag that indicates whether this field is
  337.      *                         constrained to not be set to null.
  338.      * @return string  DBMS specific SQL code portion that should be used to
  339.      *                  declare the specified field.
  340.      * @access protected
  341.      */
  342.     function _getFloatDeclaration($name$field)
  343.     {
  344.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  345.         $type 'DOUBLE';
  346.         $default = isset($field['default']' DEFAULT '.
  347.             $this->quote($field['default']'float''';
  348.         $notnull = isset($field['notnull']' NOT NULL' '';
  349.         return $name.' '.$type.$default.$notnull;
  350.     }
  351.  
  352.     // }}}
  353.     // {{{ _getDecimalDeclaration()
  354.  
  355.     /**
  356.      * Obtain DBMS specific SQL code portion needed to declare an decimal type
  357.      * field to be used in statements like CREATE TABLE.
  358.      *
  359.      * @param string  $name   name the field to be declared.
  360.      * @param string  $field  associative array with the name of the properties
  361.      *                         of the field being declared as array indexes.
  362.      *                         Currently, the types of supported field
  363.      *                         properties are as follows:
  364.      *
  365.      *                        default
  366.      *                         Integer value to be used as default for this
  367.      *                         field.
  368.      *
  369.      *                        notnull
  370.      *                         Boolean flag that indicates whether this field is
  371.      *                         constrained to not be set to null.
  372.      * @return string  DBMS specific SQL code portion that should be used to
  373.      *                  declare the specified field.
  374.      * @access protected
  375.      */
  376.     function _getDecimalDeclaration($name$field)
  377.     {
  378.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  379.         $type 'DECIMAL(18,'.$db->options['decimal_places'].')';
  380.         $default = isset($field['default']' DEFAULT '.
  381.             $this->quote($field['default']'decimal''';
  382.         $notnull = isset($field['notnull']' NOT NULL' '';
  383.         return $name.' '.$type.$default.$notnull;
  384.     }
  385.  
  386.     // }}}
  387.     // {{{ _quoteBLOB()
  388.  
  389.     /**
  390.      * Convert a text value into a DBMS specific format that is suitable to
  391.      * compose query statements.
  392.      *
  393.      * @param           $value 
  394.      * @return string  text string that represents the given argument value in
  395.      *                  a DBMS specific format.
  396.      * @access protected
  397.      */
  398.     function _quoteBLOB($value)
  399.     {
  400.         $value $this->_readFile($value);
  401.         return "'".addslashes($value)."'";
  402.     }
  403.  
  404.     // }}}
  405.     // {{{ _quoteFloat()
  406.  
  407.     /**
  408.      * Convert a text value into a DBMS specific format that is suitable to
  409.      * compose query statements.
  410.      *
  411.      * @param string  $value text string value that is intended to be converted.
  412.      * @return string  text string that represents the given argument value in
  413.      *                  a DBMS specific format.
  414.      * @access protected
  415.      */
  416.     function _quoteFloat($value)
  417.     {
  418.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  419.         return (float)$value;
  420.     }
  421.  
  422.     // }}}
  423.     // {{{ _quoteDecimal()
  424.  
  425.     /**
  426.      * Convert a text value into a DBMS specific format that is suitable to
  427.      * compose query statements.
  428.      *
  429.      * @param string  $value text string value that is intended to be converted.
  430.      * @return string  text string that represents the given argument value in
  431.      *                  a DBMS specific format.
  432.      * @access protected
  433.      */
  434.     function _quoteDecimal($value)
  435.     {
  436.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  437.         return $value;
  438.     }
  439.  
  440.     // }}}
  441.     // {{{ mapNativeDatatype()
  442.  
  443.     /**
  444.      * Maps a native array description of a field to a MDB2 datatype and length
  445.      *
  446.      * @param array  $field native field description
  447.      * @return array containing the various possible types and the length
  448.      * @access public
  449.      */
  450.     function mapNativeDatatype($field)
  451.     {
  452.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  453.         $db_type strtolower($field['type']);
  454.         $db_type strtok($db_type'(), ');
  455.         if ($db_type == 'national'{
  456.             $db_type strtok('(), ');
  457.         }
  458.         $length strtok('(), ');
  459.         $decimal strtok('(), ');
  460.         $type = array();
  461.         switch ($db_type{
  462.         case 'tinyint':
  463.         case 'smallint':
  464.         case 'mediumint':
  465.         case 'int':
  466.         case 'integer':
  467.         case 'bigint':
  468.             $type['integer';
  469.             if ($length == '1'{
  470.                 $type['boolean';
  471.                 if (preg_match('/^[is|has]/'$field['field'])) {
  472.                     $type array_reverse($type);
  473.                 }
  474.             }
  475.             break;
  476.         case 'char':
  477.         case 'varchar':
  478.             $type['text';
  479.             if ($length == '1'{
  480.                 $type['boolean';
  481.                 if (preg_match('/[is|has]/'$field['field'])) {
  482.                     $type array_reverse($type);
  483.                 }
  484.             }
  485.             break;
  486.         case 'enum':
  487.             preg_match_all('/\'.+\'/U'$field['type']$matches);
  488.             $length = 0;
  489.             if (is_array($matches)) {
  490.                 foreach ($matches[0as $value{
  491.                     $length max($lengthstrlen($value)-2);
  492.                 }
  493.             }
  494.         case 'set':
  495.             $type['text';
  496.             $type['integer';
  497.             break;
  498.         case 'date':
  499.             $type['date';
  500.             $length = null;
  501.             break;
  502.         case 'datetime':
  503.         case 'timestamp':
  504.             $type['timestamp';
  505.             $length = null;
  506.             break;
  507.         case 'time':
  508.             $type['time';
  509.             $length = null;
  510.             break;
  511.         case 'float':
  512.         case 'double':
  513.         case 'real':
  514.             $type['float';
  515.             break;
  516.         case 'decimal':
  517.         case 'numeric':
  518.             $type['decimal';
  519.             break;
  520.         case 'tinytext':
  521.         case 'mediumtext':
  522.         case 'longtext':
  523.         case 'text':
  524.             if ($decimal == 'binary'{
  525.                 $type['blob';
  526.             }
  527.             $type['clob';
  528.             $type['text';
  529.             break;
  530.         case 'tinyblob':
  531.         case 'mediumblob':
  532.         case 'longblob':
  533.         case 'blob':
  534.             $type['blob';
  535.             $type['text';
  536.             $length = null;
  537.             break;
  538.         case 'year':
  539.             $type['integer';
  540.             $type['date';
  541.             $length = null;
  542.             break;
  543.         default:
  544.             return $db->raiseError(MDB2_ERRORnullnull,
  545.                 'getTableFieldDefinition: unknown database attribute type');
  546.         }
  547.  
  548.         return array($type$length);
  549.     }
  550. }
  551.  
  552. ?>

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