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

Source for file mysql.php

Documentation is available at mysql.php

  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  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: mysql.php,v 1.4 2004/03/29 09:20:38 quipo 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.     // }}}
  60.     // {{{ constructor
  61.  
  62.     /**
  63.      * Constructor
  64.      */
  65.     function MDB2_Driver_Datatype_mysql($db_index)
  66.     {
  67.         $this->MDB2_Driver_Datatype_Common($db_index);
  68.     }
  69.  
  70.     // }}}
  71.     // {{{ convertResult()
  72.  
  73.     /**
  74.      * convert a value to a RDBMS indepdenant MDB2 type
  75.      *
  76.      * @param mixed  $value   value to be converted
  77.      * @param int    $type    constant that specifies which type to convert to
  78.      * @return mixed converted value
  79.      * @access public
  80.      */
  81.     function convertResult($value$type)
  82.     {
  83.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  84.         return $this->_baseConvertResult($value$type);
  85.     }
  86.  
  87.     // }}}
  88.     // {{{ getIntegerDeclaration()
  89.  
  90.     /**
  91.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  92.      * field to be used in statements like CREATE TABLE.
  93.      *
  94.      * @param string  $name   name the field to be declared.
  95.      * @param string  $field  associative array with the name of the properties
  96.      *                         of the field being declared as array indexes.
  97.      *                         Currently, the types of supported field
  98.      *                         properties are as follows:
  99.      *
  100.      *                        unsigned
  101.      *                         Boolean flag that indicates whether the field
  102.      *                         should be declared as unsigned integer if
  103.      *                         possible.
  104.      *
  105.      *                        default
  106.      *                         Integer value to be used as default for this
  107.      *                         field.
  108.      *
  109.      *                        notnull
  110.      *                         Boolean flag that indicates whether this field is
  111.      *                         constrained to not be set to null.
  112.      * @return string  DBMS specific SQL code portion that should be used to
  113.      *                  declare the specified field.
  114.      * @access public
  115.      */
  116.     function getIntegerDeclaration($name$field)
  117.     {
  118.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  119.         $unsigned = isset($field['unsigned']' UNSIGNED' '';
  120.         $default = isset($field['default']' DEFAULT '.
  121.             $this->quoteInteger($field['default']'';
  122.         $notnull = isset($field['notnull']' NOT NULL' '';
  123.         return $name.' INT'.$unsigned.$default.$notnull;
  124.        ;
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ getCLOBDeclaration()
  129.  
  130.     /**
  131.      * Obtain DBMS specific SQL code portion needed to declare an character
  132.      * large object type field to be used in statements like CREATE TABLE.
  133.      *
  134.      * @param string  $name   name the field to be declared.
  135.      * @param string  $field  associative array with the name of the
  136.      *                         properties of the field being declared as array
  137.      *                         indexes. Currently, the types of supported field
  138.      *                         properties are as follows:
  139.      *
  140.      *                        length
  141.      *                         Integer value that determines the maximum length
  142.      *                         of the large object field. If this argument is
  143.      *                         missing the field should be declared to have the
  144.      *                         longest length allowed by the DBMS.
  145.      *
  146.      *                        notnull
  147.      *                         Boolean flag that indicates whether this field
  148.      *                         is constrained to not be set to null.
  149.      * @return string  DBMS specific SQL code portion that should be used to
  150.      *                  declare the specified field.
  151.      * @access public
  152.      */
  153.     function getCLOBDeclaration($name$field)
  154.     {
  155.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  156.         if (isset($field['length'])) {
  157.             $length $field['length'];
  158.             if ($length <= 255{
  159.                 $type 'TINYTEXT';
  160.             else {
  161.                 if ($length <= 65535{
  162.                     $type 'TEXT';
  163.                 else {
  164.                     if ($length <= 16777215{
  165.                         $type 'MEDIUMTEXT';
  166.                     else {
  167.                         $type 'LONGTEXT';
  168.                     }
  169.                 }
  170.             }
  171.         else {
  172.             $type 'LONGTEXT';
  173.         }
  174.         $notnull = isset($field['notnull']' NOT NULL' '';
  175.         return $name.' '.$type.$notnull;
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ getBLOBDeclaration()
  180.  
  181.     /**
  182.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  183.      * object type field to be used in statements like CREATE TABLE.
  184.      *
  185.      * @param string  $name   name the field to be declared.
  186.      * @param string  $field  associative array with the name of the properties
  187.      *                         of the field being declared as array indexes.
  188.      *                         Currently, the types of supported field
  189.      *                         properties are as follows:
  190.      *
  191.      *                        length
  192.      *                         Integer value that determines the maximum length
  193.      *                         of the large object field. If this argument is
  194.      *                         missing the field should be declared to have the
  195.      *                         longest length allowed by the DBMS.
  196.      *
  197.      *                        notnull
  198.      *                         Boolean flag that indicates whether this field is
  199.      *                         constrained to not be set to null.
  200.      * @return string  DBMS specific SQL code portion that should be used to
  201.      *                  declare the specified field.
  202.      * @access public
  203.      */
  204.     function getBLOBDeclaration($name$field)
  205.     {
  206.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  207.         if (isset($field['length'])) {
  208.             $length $field['length'];
  209.             if ($length <= 255{
  210.                 $type 'TINYBLOB';
  211.             else {
  212.                 if ($length <= 65535{
  213.                     $type 'BLOB';
  214.                 else {
  215.                     if ($length <= 16777215{
  216.                         $type 'MEDIUMBLOB';
  217.                     else {
  218.                         $type 'LONGBLOB';
  219.                     }
  220.                 }
  221.             }
  222.         }
  223.         else {
  224.             $type 'LONGBLOB';
  225.         }
  226.         $notnull = isset($field['notnull']' NOT NULL' '';
  227.         return $name.' '.$type.$notnull;
  228.     }
  229.  
  230.     // }}}
  231.     // {{{ getDateDeclaration()
  232.  
  233.     /**
  234.      * Obtain DBMS specific SQL code portion needed to declare an date type
  235.      * field to be used in statements like CREATE TABLE.
  236.      *
  237.      * @param string  $name   name the field to be declared.
  238.      * @param string  $field  associative array with the name of the properties
  239.      *                         of the field being declared as array indexes.
  240.      *                         Currently, the types of supported field properties
  241.      *                         are as follows:
  242.      *
  243.      *                        default
  244.      *                         Date value to be used as default for this field.
  245.      *
  246.      *                        notnull
  247.      *                         Boolean flag that indicates whether this field is
  248.      *                         constrained to not be set to null.
  249.      * @return string  DBMS specific SQL code portion that should be used to
  250.      *                  declare the specified field.
  251.      * @access public
  252.      */
  253.     function getDateDeclaration($name$field)
  254.     {
  255.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  256.         $default = isset($field['default']' DEFAULT '.
  257.             $this->quoteDate($field['default']'';
  258.         $notnull = isset($field['notnull']' NOT NULL' '';
  259.         return $name.' DATE'.$default.$notnull;
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ getTimestampDeclaration()
  264.  
  265.     /**
  266.      * Obtain DBMS specific SQL code portion needed to declare an timestamp
  267.      * type field to be used in statements like CREATE TABLE.
  268.      *
  269.      * @param string  $name   name the field to be declared.
  270.      * @param string  $field  associative array with the name of the properties
  271.      *                         of the field being declared as array indexes.
  272.      *                         Currently, the types of supported field
  273.      *                         properties are as follows:
  274.      *
  275.      *                        default
  276.      *                         Time stamp value to be used as default for this
  277.      *                         field.
  278.      *
  279.      *                        notnull
  280.      *                         Boolean flag that indicates whether this field is
  281.      *                         constrained to not be set to null.
  282.      * @return string  DBMS specific SQL code portion that should be used to
  283.      *                  declare the specified field.
  284.      * @access public
  285.      */
  286.     function getTimestampDeclaration($name$field)
  287.     {
  288.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  289.         $default = isset($field['default']' DEFAULT '.
  290.             $this->quoteTimestamp($field['default']'';
  291.         $notnull = isset($field['notnull']' NOT NULL' '';
  292.         return $name.' DATETIME'.$default.$notnull;
  293.     }
  294.  
  295.     // }}}
  296.     // {{{ getTimeDeclaration()
  297.  
  298.     /**
  299.      * Obtain DBMS specific SQL code portion needed to declare an time type
  300.      * field to be used in statements like CREATE TABLE.
  301.      *
  302.      * @param string  $name   name the field to be declared.
  303.      * @param string  $field  associative array with the name of the properties
  304.      *                         of the field being declared as array indexes.
  305.      *                         Currently, the types of supported field
  306.      *                         properties are as follows:
  307.      *
  308.      *                        default
  309.      *                         Time value to be used as default for this field.
  310.      *
  311.      *                        notnull
  312.      *                         Boolean flag that indicates whether this field is
  313.      *                         constrained to not be set to null.
  314.      * @return string  DBMS specific SQL code portion that should be used to
  315.      *                  declare the specified field.
  316.      * @access public
  317.      */
  318.     function getTimeDeclaration($name$field)
  319.     {
  320.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  321.         $default = isset($field['default']' DEFAULT '.
  322.             $this->quoteTime($field['default']'';
  323.         $notnull = isset($field['notnull']' NOT NULL' '';
  324.         return $name.' TIME'.$default.$notnull;
  325.     }
  326.  
  327.     // }}}
  328.     // {{{ getFloatDeclaration()
  329.  
  330.     /**
  331.      * Obtain DBMS specific SQL code portion needed to declare an float type
  332.      * field to be used in statements like CREATE TABLE.
  333.      *
  334.      * @param string  $name   name the field to be declared.
  335.      * @param string  $field  associative array with the name of the properties
  336.      *                         of the field being declared as array indexes.
  337.      *                         Currently, the types of supported field
  338.      *                         properties are as follows:
  339.      *
  340.      *                        default
  341.      *                         Integer value to be used as default for this
  342.      *                         field.
  343.      *
  344.      *                        notnull
  345.      *                         Boolean flag that indicates whether this field is
  346.      *                         constrained to not be set to null.
  347.      * @return string  DBMS specific SQL code portion that should be used to
  348.      *                  declare the specified field.
  349.      * @access public
  350.      */
  351.     function getFloatDeclaration($name$field)
  352.     {
  353.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  354.         $type 'DOUBLE';
  355.         $default = isset($field['default']' DEFAULT '.
  356.             $this->quoteFloat($field['default']'';
  357.         $notnull = isset($field['notnull']' NOT NULL' '';
  358.         return $name.' '.$type.$default.$notnull;
  359.     }
  360.  
  361.     // }}}
  362.     // {{{ getDecimalDeclaration()
  363.  
  364.     /**
  365.      * Obtain DBMS specific SQL code portion needed to declare an decimal type
  366.      * field to be used in statements like CREATE TABLE.
  367.      *
  368.      * @param string  $name   name the field to be declared.
  369.      * @param string  $field  associative array with the name of the properties
  370.      *                         of the field being declared as array indexes.
  371.      *                         Currently, the types of supported field
  372.      *                         properties are as follows:
  373.      *
  374.      *                        default
  375.      *                         Integer value to be used as default for this
  376.      *                         field.
  377.      *
  378.      *                        notnull
  379.      *                         Boolean flag that indicates whether this field is
  380.      *                         constrained to not be set to null.
  381.      * @return string  DBMS specific SQL code portion that should be used to
  382.      *                  declare the specified field.
  383.      * @access public
  384.      */
  385.     function getDecimalDeclaration($name$field)
  386.     {
  387.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  388.         $type 'DECIMAL(18,'.$db->options['decimal_places'].')';
  389.         $default = isset($field['default']' DEFAULT '.
  390.             $this->quoteDecimal($field['default']'';
  391.         $notnull = isset($field['notnull']' NOT NULL' '';
  392.         return $name.' '.$type.$default.$notnull;
  393.     }
  394.  
  395.     // }}}
  396.     // {{{ quoteCLOB()
  397.  
  398.     /**
  399.      * Convert a text value into a DBMS specific format that is suitable to
  400.      * compose query statements.
  401.      *
  402.      * @param           $clob 
  403.      * @return string  text string that represents the given argument value in
  404.      *                  a DBMS specific format.
  405.      * @access public
  406.      */
  407.     function quoteCLOB($clob)
  408.     {
  409.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  410.         if (is_null($clob)) {
  411.             return 'NULL';
  412.         }
  413.         $value "'";
  414.         $data = null;
  415.         while (!$this->endOfLOB($clob)) {
  416.             $result $this->readLOB($clob$data$db->options['lob_buffer_length']);
  417.             if (MDB2::isError($result)) {
  418.                 return $result;
  419.             }
  420.             $value .= $db->escape($data);
  421.         }
  422.         $value .= "'";
  423.         return $value;
  424.     }
  425.  
  426.     // }}}
  427.     // {{{ quoteBLOB()
  428.  
  429.     /**
  430.      * Convert a text value into a DBMS specific format that is suitable to
  431.      * compose query statements.
  432.      *
  433.      * @param           $blob 
  434.      * @return string  text string that represents the given argument value in
  435.      *                  a DBMS specific format.
  436.      * @access public
  437.      */
  438.     function quoteBLOB($blob)
  439.     {
  440.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  441.         if (is_null($blob)) {
  442.             return 'NULL';
  443.         }
  444.         $value "'";
  445.         $data = null;
  446.         while (!$this->endOfLOB($blob)) {
  447.         $result $this->readLOB($blob$data$db->options['lob_buffer_length']);
  448.             if (MDB2::isError($result)) {
  449.                 return $result;
  450.             }
  451.             $value .= addslashes($data);
  452.         }
  453.         $value .= "'";
  454.         return $value;
  455.     }
  456.  
  457.     // }}}
  458.     // {{{ quoteFloat()
  459.  
  460.     /**
  461.      * Convert a text value into a DBMS specific format that is suitable to
  462.      * compose query statements.
  463.      *
  464.      * @param string  $value text string value that is intended to be converted.
  465.      * @return string  text string that represents the given argument value in
  466.      *                  a DBMS specific format.
  467.      * @access public
  468.      */
  469.     function quoteFloat($value)
  470.     {
  471.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  472.         return (is_null($value)) 'NULL' : (float)$value;
  473.     }
  474.  
  475.     // }}}
  476.     // {{{ quoteDecimal()
  477.  
  478.     /**
  479.      * Convert a text value into a DBMS specific format that is suitable to
  480.      * compose query statements.
  481.      *
  482.      * @param string  $value text string value that is intended to be converted.
  483.      * @return string  text string that represents the given argument value in
  484.      *                  a DBMS specific format.
  485.      * @access public
  486.      */
  487.     function quoteDecimal($value)
  488.     {
  489.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  490.         return (is_null($value)) 'NULL' $value;
  491.     }
  492. }
  493.  
  494. ?>

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