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

Source for file fbsql.php

Documentation is available at fbsql.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: fbsql.php,v 1.5 2004/03/29 09:20:38 quipo Exp $
  47. //
  48.  
  49. require_once 'MDB2/Driver/Datatype/Common.php';
  50.  
  51. /**
  52.  * MDB2 FrontbaseSQL 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_fbsql($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.         switch ($type{
  85.              case MDB2_TYPE_BOOLEAN:
  86.                  return $value == 'T';
  87.              case MDB2_TYPE_TIME:
  88.                 if ($value[0== '+'{
  89.                     return substr($value1);
  90.                 else {
  91.                     return $value;
  92.                 }
  93.             default:
  94.                 return $this->_baseConvertResult($value$type);
  95.         }
  96.         return $this->_baseConvertResult($value$type);
  97.     }
  98.  
  99.     // }}}
  100.     // {{{ getIntegerDeclaration()
  101.  
  102.     /**
  103.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  104.      * field to be used in statements like CREATE TABLE.
  105.      *
  106.      * @param string  $name   name the field to be declared.
  107.      * @param string  $field  associative array with the name of the properties
  108.      *                         of the field being declared as array indexes.
  109.      *                         Currently, the types of supported field
  110.      *                         properties are as follows:
  111.      *
  112.      *                        unsigned
  113.      *                         Boolean flag that indicates whether the field
  114.      *                         should be declared as unsigned integer if
  115.      *                         possible.
  116.      *
  117.      *                        default
  118.      *                         Integer value to be used as default for this
  119.      *                         field.
  120.      *
  121.      *                        notnull
  122.      *                         Boolean flag that indicates whether this field is
  123.      *                         constrained to not be set to null.
  124.      * @return string  DBMS specific SQL code portion that should be used to
  125.      *                  declare the specified field.
  126.      * @access public
  127.      */
  128.     function getIntegerDeclaration($name$field)
  129.     {
  130.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  131.         if (isset($field['unsigned'])) {
  132.             $this->warnings[= "unsigned integer field \"$name\" is being
  133.                 declared as signed integer";
  134.         }
  135.         $default = isset($field['default']' DEFAULT '.
  136.             $this->quoteInteger($field['default']'';
  137.         $notnull = isset($field['notnull']' NOT NULL' '';
  138.         return $name.' INT'.$default.$notnull;
  139.     }
  140.  
  141.     // }}}
  142.     // {{{ getTextDeclaration()
  143.  
  144.     /**
  145.      * Obtain DBMS specific SQL code portion needed to declare an text type
  146.      * field to be used in statements like CREATE TABLE.
  147.      *
  148.      * @param string $name name the field to be declared.
  149.     * @param string $field associative array with the name of the properties
  150.      *        of the field being declared as array indexes. Currently, the types
  151.      *        of supported field properties are as follows:
  152.     *
  153.      *        length
  154.      *            Integer value that determines the maximum length of the text
  155.      *            field. If this argument is missing the field should be
  156.      *            declared to have the longest length allowed by the DBMS.
  157.      *
  158.      *        default
  159.      *            Text value to be used as default for this field.
  160.      *
  161.      *        notnull
  162.      *            Boolean flag that indicates whether this field is constrained
  163.      *            to not be set to NULL.
  164.      * @return string DBMS specific SQL code portion that should be used to
  165.      *        declare the specified field.
  166.      * @access public
  167.      */
  168.     function getTextDeclaration($name$field)
  169.     {
  170.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  171.         $default = isset($field['default']' DEFAULT '.
  172.             $this->quoteText($field['default']'';
  173.         $notnull = isset($field['notnull']' NOT NULL' '';
  174.         $length = isset($field['length']$field['length'$db->max_text_length;
  175.         return $name.' VARCHAR ('.$length.')'.$default.$notnull;
  176.     }
  177.  
  178.     // }}}
  179.     // {{{ getCLOBDeclaration()
  180.  
  181.     /**
  182.      * Obtain DBMS specific SQL code portion needed to declare an character
  183.      * large 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
  187.      *                         properties of the field being declared as array
  188.      *                         indexes. 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
  199.      *                         is 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 getCLOBDeclaration($name$field)
  205.     {
  206.         return "$name CLOB".(isset($field['notnull']' NOT NULL' '');
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ getBLOBDeclaration()
  211.  
  212.     /**
  213.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  214.      * object type field to be used in statements like CREATE TABLE.
  215.      *
  216.      * @param string  $name   name the field to be declared.
  217.      * @param string  $field  associative array with the name of the properties
  218.      *                         of the field being declared as array indexes.
  219.      *                         Currently, the types of supported field
  220.      *                         properties are as follows:
  221.      *
  222.      *                        length
  223.      *                         Integer value that determines the maximum length
  224.      *                         of the large object field. If this argument is
  225.      *                         missing the field should be declared to have the
  226.      *                         longest length allowed by the DBMS.
  227.      *
  228.      *                        notnull
  229.      *                         Boolean flag that indicates whether this field is
  230.      *                         constrained to not be set to null.
  231.      * @return string  DBMS specific SQL code portion that should be used to
  232.      *                  declare the specified field.
  233.      * @access public
  234.      */
  235.     function getBLOBDeclaration($name$field)
  236.     {
  237.         return "$name BLOB".(isset($field['notnull']' NOT NULL' '');
  238.     }
  239.  
  240.     // }}}
  241.     // {{{ getBooleanDeclaration()
  242.  
  243.     /**
  244.      * Obtain DBMS specific SQL code portion needed to declare a boolean type
  245.      * field to be used in statements like CREATE TABLE.
  246.      *
  247.      * @param string $name name the field to be declared.
  248.      * @param string $field associative array with the name of the properties
  249.      *        of the field being declared as array indexes. Currently, the types
  250.      *        of supported field properties are as follows:
  251.      *
  252.      *        default
  253.      *            Boolean value to be used as default for this field.
  254.      *
  255.      *        notnullL
  256.      *            Boolean flag that indicates whether this field is constrained
  257.      *            to not be set to NULL.
  258.      * @return string DBMS specific SQL code portion that should be used to
  259.      *        declare the specified field.
  260.      * @access public
  261.      */
  262.     function getBooleanDeclaration($name$field)
  263.     {
  264.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  265.         $default = isset($field['default']' DEFAULT '.
  266.             $this->quoteBoolean($field['default']'';
  267.         $notnull = isset($field['notnull']' NOT NULL' '';
  268.         return $name.' BOOLEAN)'.$default.$notnull;
  269.     }
  270.  
  271.     // }}}
  272.     // {{{ getDateDeclaration()
  273.  
  274.     /**
  275.      * Obtain DBMS specific SQL code portion needed to declare an date type
  276.      * field to be used in statements like CREATE TABLE.
  277.      *
  278.      * @param string  $name   name the field to be declared.
  279.      * @param string  $field  associative array with the name of the properties
  280.      *                         of the field being declared as array indexes.
  281.      *                         Currently, the types of supported field properties
  282.      *                         are as follows:
  283.      *
  284.      *                        default
  285.      *                         Date value to be used as default for this field.
  286.      *
  287.      *                        notnull
  288.      *                         Boolean flag that indicates whether this field is
  289.      *                         constrained to not be set to null.
  290.      * @return string  DBMS specific SQL code portion that should be used to
  291.      *                  declare the specified field.
  292.      * @access public
  293.      */
  294.     function getDateDeclaration($name$field)
  295.     {
  296.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  297.         $default = isset($field['default']' DEFAULT DATE '.
  298.             $this->quoteDate($field['default']'';
  299.         $notnull = isset($field['notnull']' NOT NULL' '';
  300.         return $name.' DATE'.$default.$notnull;
  301.     }
  302.  
  303.     // }}}
  304.     // {{{ getTimestampDeclaration()
  305.  
  306.     /**
  307.      * Obtain DBMS specific SQL code portion needed to declare an timestamp
  308.      * type field to be used in statements like CREATE TABLE.
  309.      *
  310.      * @param string  $name   name the field to be declared.
  311.      * @param string  $field  associative array with the name of the properties
  312.      *                         of the field being declared as array indexes.
  313.      *                         Currently, the types of supported field
  314.      *                         properties are as follows:
  315.      *
  316.      *                        default
  317.      *                         Time stamp value to be used as default for this
  318.      *                         field.
  319.      *
  320.      *                        notnull
  321.      *                         Boolean flag that indicates whether this field is
  322.      *                         constrained to not be set to null.
  323.      * @return string  DBMS specific SQL code portion that should be used to
  324.      *                  declare the specified field.
  325.      * @access public
  326.      */
  327.     function getTimestampDeclaration($name$field)
  328.     {
  329.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  330.         $default = isset($field['default']' DEFAULT TIMESTAMP '.
  331.             $this->quoteTimestamp($field['default']'';
  332.         $notnull = isset($field['notnull']' NOT NULL' '';
  333.         return $name.' TIMESTAMP'.$default.$notnull;
  334.     }
  335.  
  336.     // }}}
  337.     // {{{ getTimeDeclaration()
  338.  
  339.     /**
  340.      * Obtain DBMS specific SQL code portion needed to declare an time type
  341.      * field to be used in statements like CREATE TABLE.
  342.      *
  343.      * @param string  $name   name the field to be declared.
  344.      * @param string  $field  associative array with the name of the properties
  345.      *                         of the field being declared as array indexes.
  346.      *                         Currently, the types of supported field
  347.      *                         properties are as follows:
  348.      *
  349.      *                        default
  350.      *                         Time value to be used as default for this field.
  351.      *
  352.      *                        notnull
  353.      *                         Boolean flag that indicates whether this field is
  354.      *                         constrained to not be set to null.
  355.      * @return string  DBMS specific SQL code portion that should be used to
  356.      *                  declare the specified field.
  357.      * @access public
  358.      */
  359.     function getTimeDeclaration($name$field)
  360.     {
  361.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  362.         $default = isset($field['default']' DEFAULT TIME '.
  363.             $this->quoteTime($field['default']'';
  364.         $notnull = isset($field['notnull']' NOT NULL' '';
  365.         return $name.' TIME'.$default.$notnull;
  366.     }
  367.  
  368.     // }}}
  369.     // {{{ getFloatDeclaration()
  370.  
  371.     /**
  372.      * Obtain DBMS specific SQL code portion needed to declare an float type
  373.      * field to be used in statements like CREATE TABLE.
  374.      *
  375.      * @param string  $name   name the field to be declared.
  376.      * @param string  $field  associative array with the name of the properties
  377.      *                         of the field being declared as array indexes.
  378.      *                         Currently, the types of supported field
  379.      *                         properties are as follows:
  380.      *
  381.      *                        default
  382.      *                         Integer value to be used as default for this
  383.      *                         field.
  384.      *
  385.      *                        notnull
  386.      *                         Boolean flag that indicates whether this field is
  387.      *                         constrained to not be set to null.
  388.      * @return string  DBMS specific SQL code portion that should be used to
  389.      *                  declare the specified field.
  390.      * @access public
  391.      */
  392.     function getFloatDeclaration($name$field)
  393.     {
  394.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  395.         $default = isset($field['default']' DEFAULT '.
  396.             $this->quoteFloat($field['default']'';
  397.         $notnull = isset($field['notnull']' NOT NULL' '';
  398.         return $name.' FLOAT'.$default.$notnull;
  399.     }
  400.  
  401.     // }}}
  402.     // {{{ getDecimalDeclaration()
  403.  
  404.     /**
  405.      * Obtain DBMS specific SQL code portion needed to declare an decimal type
  406.      * field to be used in statements like CREATE TABLE.
  407.      *
  408.      * @param string  $name   name the field to be declared.
  409.      * @param string  $field  associative array with the name of the properties
  410.      *                         of the field being declared as array indexes.
  411.      *                         Currently, the types of supported field
  412.      *                         properties are as follows:
  413.      *
  414.      *                        default
  415.      *                         Integer value to be used as default for this
  416.      *                         field.
  417.      *
  418.      *                        notnull
  419.      *                         Boolean flag that indicates whether this field is
  420.      *                         constrained to not be set to null.
  421.      * @return string  DBMS specific SQL code portion that should be used to
  422.      *                  declare the specified field.
  423.      * @access public
  424.      */
  425.     function getDecimalDeclaration($name$field)
  426.     {
  427.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  428.         $type 'DECIMAL(18,'.$db->options['decimal_places'].')';
  429.         $default = isset($field['default']' DEFAULT '.
  430.             $this->quoteDecimal($field['default']'';
  431.         $notnull = isset($field['notnull']' NOT NULL' '';
  432.         return $name.' '.$type.$default.$notnull;
  433.     }
  434.  
  435.     // }}}
  436.     // {{{ quoteCLOB()
  437.  
  438.     /**
  439.      * Convert a text value into a DBMS specific format that is suitable to
  440.      * compose query statements.
  441.      *
  442.      * @param           $clob 
  443.      * @return string  text string that represents the given argument value in
  444.      *                  a DBMS specific format.
  445.      * @access public
  446.      */
  447.     function quoteCLOB($clob)
  448.     {
  449.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  450.         if ($clob === null{
  451.             return 'NULL';
  452.         }
  453.         $value "'";
  454.         $data = null;
  455.         while (!$this->endOfLOB($clob)) {
  456.             $result $this->readLOB($clob$data$db->options['lob_buffer_length']);
  457.             if (MDB2::isError($result)) {
  458.                 return $result;
  459.             }
  460.             $value .= $db->escape($data);
  461.         }
  462.         $value .= "'";
  463.         return $value;
  464.     }
  465.  
  466.     // }}}
  467.     // {{{ quoteBLOB()
  468.  
  469.     /**
  470.      * Convert a text value into a DBMS specific format that is suitable to
  471.      * compose query statements.
  472.      *
  473.      * @param           $blob 
  474.      * @return string  text string that represents the given argument value in
  475.      *                  a DBMS specific format.
  476.      * @access public
  477.      */
  478.     function quoteBLOB($blob)
  479.     {
  480.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  481.         if ($blob === null{
  482.             return 'NULL';
  483.         }
  484.         $value "'";
  485.         $data = null;
  486.         while (!$this->endOfLOB($blob)) {
  487.             $result $this->readLOB($blob$data$db->options['lob_buffer_length']);
  488.             if (MDB2::isError($result)) {
  489.                 return $result;
  490.             }
  491.             $value .= addslashes($data);
  492.         }
  493.         $value .= "'";
  494.         return $value;
  495.     }
  496.  
  497.     // }}}
  498.     // {{{ quoteBoolean()
  499.  
  500.     /**
  501.      * Convert a text value into a DBMS specific format that is suitable to
  502.      * compose query statements.
  503.      *
  504.      * @param string $value text string value that is intended to be converted.
  505.      * @return string text string that represents the given argument value in
  506.      *        a DBMS specific format.
  507.      * @access public
  508.      */
  509.     function quoteBoolean($value)
  510.     {
  511.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  512.         return ($value === null'NULL' ($value 'True' 'False');
  513.     }
  514.  
  515.     // }}}
  516.     // {{{ quoteDate()
  517.  
  518.     /**
  519.      * Convert a text value into a DBMS specific format that is suitable to
  520.      * compose query statements.
  521.      *
  522.      * @param string $value text string value that is intended to be converted.
  523.      * @return string text string that represents the given argument value in
  524.      *         a DBMS specific format.
  525.      * @access public
  526.      */
  527.     function quoteDate($value)
  528.     {
  529.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  530.         return ($value === null'NULL' : "DATE'$value'";
  531.     }
  532.  
  533.     // }}}
  534.     // {{{ quoteTimestamp()
  535.  
  536.     /**
  537.      * Convert a text value into a DBMS specific format that is suitable to
  538.      * compose query statements.
  539.      *
  540.      * @param string $value text string value that is intended to be converted.
  541.      * @return string text string that represents the given argument value in
  542.      *         a DBMS specific format.
  543.      * @access public
  544.      */
  545.     function quoteTimestamp($value)
  546.     {
  547.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  548.         return ($value === null'NULL' : "TIMESTAMP'$value'";
  549.     }
  550.  
  551.     // }}}
  552.     // {{{ quoteTime()
  553.  
  554.     /**
  555.      * Convert a text value into a DBMS specific format that is suitable to
  556.      *        compose query statements.
  557.      *
  558.      * @param string $value text string value that is intended to be converted.
  559.      * @return string text string that represents the given argument value in
  560.      *         a DBMS specific format.
  561.      * @access public
  562.      */
  563.     function quoteTime($value)
  564.     {
  565.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  566.         return ($value === null'NULL' : "TIME'$value'";
  567.     }
  568.  
  569.     // }}}
  570.     // {{{ quoteFloat()
  571.  
  572.     /**
  573.      * Convert a text value into a DBMS specific format that is suitable to
  574.      * compose query statements.
  575.      *
  576.      * @param string  $value text string value that is intended to be converted.
  577.      * @return string  text string that represents the given argument value in
  578.      *                  a DBMS specific format.
  579.      * @access public
  580.      */
  581.     function quoteFloat($value)
  582.     {
  583.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  584.         return ($value === null'NULL' : (float)$value;
  585.     }
  586.  
  587.     // }}}
  588.     // {{{ quoteDecimal()
  589.  
  590.     /**
  591.      * Convert a text value into a DBMS specific format that is suitable to
  592.      * compose query statements.
  593.      *
  594.      * @param string  $value text string value that is intended to be converted.
  595.      * @return string  text string that represents the given argument value in
  596.      *                  a DBMS specific format.
  597.      * @access public
  598.      */
  599.     function quoteDecimal($value)
  600.     {
  601.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  602.         return ($value === null'NULL' $value;
  603.     }
  604. }
  605.  
  606. ?>

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