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

Source for file mssql.php

Documentation is available at mssql.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: mssql.php,v 1.5 2004/04/09 10:41:21 lsmith Exp $
  47. //
  48.  
  49. require_once 'MDB2/Driver/Datatype/Common.php';
  50.  
  51. /**
  52.  * MDB2 MS SQL 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_mssql($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 == '1';
  87.             case MDB2_TYPE_DATE:
  88.                 if (strlen($value> 10{
  89.                     $value substr($value,0,10);
  90.                 }
  91.                 return $value;
  92.             case MDB2_TYPE_TIME:
  93.                 if (strlen($value> 8{
  94.                     $value substr($value,11,8);
  95.                 }
  96.                 return $value;
  97.             default:
  98.                 return $this->_baseConvertResult($value,$type);
  99.         }
  100.     }
  101.  
  102.     // }}}
  103.     // {{{ getTextDeclaration()
  104.  
  105.     /**
  106.      * Obtain DBMS specific SQL code portion needed to declare an text type
  107.      * field to be used in statements like CREATE TABLE.
  108.      *
  109.      * @param string $name name the field to be declared.
  110.      * @param string $field associative array with the name of the properties
  111.      *        of the field being declared as array indexes. Currently, the types
  112.      *        of supported field properties are as follows:
  113.      *
  114.      *        length
  115.      *            Integer value that determines the maximum length of the text
  116.      *            field. If this argument is missing the field should be
  117.      *            declared to have the longest length allowed by the DBMS.
  118.      *
  119.      *        default
  120.      *            Text value to be used as default for this field.
  121.      *
  122.      *        notnull
  123.      *            Boolean flag that indicates whether this field is constrained
  124.      *            to not be set to null.
  125.      * @return string DBMS specific SQL code portion that should be used to
  126.      *        declare the specified field.
  127.      * @access public
  128.      */
  129.     function getTextDeclaration($name$field)
  130.     {
  131.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  132.         $type = isset($field['length']'VARCHAR ('.$field['length'].')' 'TEXT';
  133.         $default = isset($field['default']' DEFAULT '.
  134.             $this->quoteText($field['default']'';
  135.         $notnull = isset($field['notnull']' NOT NULL' '';
  136.         return $name.' '.$type.$default.$notnull;
  137.     }
  138.  
  139.     // }}}
  140.     // {{{ getCLOBDeclaration()
  141.  
  142.     /**
  143.      * Obtain DBMS specific SQL code portion needed to declare an character
  144.      * large object type field to be used in statements like CREATE TABLE.
  145.      *
  146.      * @param string  $name   name the field to be declared.
  147.      * @param string  $field  associative array with the name of the
  148.      *                         properties of the field being declared as array
  149.      *                         indexes. Currently, the types of supported field
  150.      *                         properties are as follows:
  151.      *
  152.      *                        length
  153.      *                         Integer value that determines the maximum length
  154.      *                         of the large object field. If this argument is
  155.      *                         missing the field should be declared to have the
  156.      *                         longest length allowed by the DBMS.
  157.      *
  158.      *                        notnull
  159.      *                         Boolean flag that indicates whether this field
  160.      *                         is constrained to not be set to null.
  161.      * @return string  DBMS specific SQL code portion that should be used to
  162.      *                  declare the specified field.
  163.      * @access public
  164.      */
  165.     function getCLOBDeclaration($name$field)
  166.     {
  167.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  168.         if (isset($field['length'])) {
  169.             $length $field['length'];
  170.             if ($length <= 8000{
  171.                 $type = "VARCHAR($length)";
  172.             else {
  173.                 $type 'TEXT';
  174.             }
  175.         else {
  176.             $type 'TEXT';
  177.         }
  178.         $notnull = isset($field['notnull']' NOT NULL' '';
  179.         return $name.' '.$type.$notnull;
  180.     }
  181.  
  182.     // }}}
  183.     // {{{ getBLOBDeclaration()
  184.  
  185.     /**
  186.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  187.      * object type field to be used in statements like CREATE TABLE.
  188.      *
  189.      * @param string  $name   name the field to be declared.
  190.      * @param string  $field  associative array with the name of the properties
  191.      *                         of the field being declared as array indexes.
  192.      *                         Currently, the types of supported field
  193.      *                         properties are as follows:
  194.      *
  195.      *                        length
  196.      *                         Integer value that determines the maximum length
  197.      *                         of the large object field. If this argument is
  198.      *                         missing the field should be declared to have the
  199.      *                         longest length allowed by the DBMS.
  200.      *
  201.      *                        notnull
  202.      *                         Boolean flag that indicates whether this field is
  203.      *                         constrained to not be set to null.
  204.      * @return string  DBMS specific SQL code portion that should be used to
  205.      *                  declare the specified field.
  206.      * @access public
  207.      */
  208.     function getBLOBDeclaration($name$field)
  209.     {
  210.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  211.         if (isset($field['length'])) {
  212.             $length $field['length'];
  213.             if ($length <= 8000{
  214.                 $type = "VARBINARY($length)";
  215.             else {
  216.                 $type 'IMAGE';
  217.             }
  218.         else {
  219.             $type 'IMAGE';
  220.         }
  221.         $notnull = isset($field['notnull']' NOT NULL' '';
  222.         return $name.' '.$type.$notnull;
  223.     }
  224.  
  225.     // }}}
  226.     // {{{ getBooleanDeclaration()
  227.  
  228.     /**
  229.      * Obtain DBMS specific SQL code portion needed to declare a boolean type
  230.      * field to be used in statements like CREATE TABLE.
  231.      *
  232.      * @param string $name name the field to be declared.
  233.      * @param string $field associative array with the name of the properties
  234.      *        of the field being declared as array indexes. Currently, the types
  235.      *        of supported field properties are as follows:
  236.      *
  237.      *        default
  238.      *            Boolean value to be used as default for this field.
  239.      *
  240.      *        notnull
  241.      *            Boolean flag that indicates whether this field is constrained
  242.      *            to not be set to null.
  243.      * @return string DBMS specific SQL code portion that should be used to
  244.      *        declare the specified field.
  245.      * @access public
  246.      */
  247.     function getBooleanDeclaration($name$field)
  248.     {
  249.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  250.         $default = isset($field['default']' DEFAULT '.
  251.             $this->quoteBoolean($field['default']'';
  252.         $notnull = isset($field['notnull']' NOT NULL' '';
  253.         return $name.' BIT'.$default.$notnull;
  254.     }
  255.  
  256.     // }}}
  257.     // {{{ getFloatDeclaration()
  258.  
  259.     /**
  260.      * Obtain DBMS specific SQL code portion needed to declare an float type
  261.      * field to be used in statements like CREATE TABLE.
  262.      *
  263.      * @param string  $name   name the field to be declared.
  264.      * @param string  $field  associative array with the name of the properties
  265.      *                         of the field being declared as array indexes.
  266.      *                         Currently, the types of supported field
  267.      *                         properties are as follows:
  268.      *
  269.      *                        default
  270.      *                         Integer value to be used as default for this
  271.      *                         field.
  272.      *
  273.      *                        notnull
  274.      *                         Boolean flag that indicates whether this field is
  275.      *                         constrained to not be set to null.
  276.      * @return string  DBMS specific SQL code portion that should be used to
  277.      *                  declare the specified field.
  278.      * @access public
  279.      */
  280.     function getFloatDeclaration($name$field)
  281.     {
  282.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  283.         $default = isset($field['default']' DEFAULT '.
  284.             $this->quoteFloat($field['default']'';
  285.         $notnull = isset($field['notnull']' NOT NULL' '';
  286.         return $name.' FLOAT'.$default.$notnull;
  287.     }
  288.  
  289.     // }}}
  290.     // {{{ getDecimalDeclaration()
  291.  
  292.     /**
  293.      * Obtain DBMS specific SQL code portion needed to declare an decimal type
  294.      * field to be used in statements like CREATE TABLE.
  295.      *
  296.      * @param string  $name   name the field to be declared.
  297.      * @param string  $field  associative array with the name of the properties
  298.      *                         of the field being declared as array indexes.
  299.      *                         Currently, the types of supported field
  300.      *                         properties are as follows:
  301.      *
  302.      *                        default
  303.      *                         Integer value to be used as default for this
  304.      *                         field.
  305.      *
  306.      *                        notnull
  307.      *                         Boolean flag that indicates whether this field is
  308.      *                         constrained to not be set to null.
  309.      * @return string  DBMS specific SQL code portion that should be used to
  310.      *                  declare the specified field.
  311.      * @access public
  312.      */
  313.     function getDecimalDeclaration($name$field)
  314.     {
  315.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  316.         $type 'DECIMAL(18,'.$db->options['decimal_places'].')';
  317.         $default = isset($field['default']' DEFAULT '.
  318.             $this->quoteDecimal($field['default']'';
  319.         $notnull = isset($field['notnull']' NOT NULL' '';
  320.         return $name.' '.$type.$default.$notnull;
  321.     }
  322.  
  323.     // }}}
  324.     // {{{ quoteCLOB()
  325.  
  326.     /**
  327.      * Convert a text value into a DBMS specific format that is suitable to
  328.      * compose query statements.
  329.      *
  330.      * @param           $clob 
  331.      * @return string  text string that represents the given argument value in
  332.      *                  a DBMS specific format.
  333.      * @access public
  334.      */
  335.     function quoteCLOB($clob)
  336.     {
  337.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  338.         if ($clob === null{
  339.             return 'NULL';
  340.         }
  341.         $value "'";
  342.         $data = null;
  343.         while (!$this->endOfLOB($clob)) {
  344.             $result $this->readLOB($clob$data$db->options['lob_buffer_length']);
  345.             if (MDB2::isError($result)) {
  346.                 return $result;
  347.             }
  348.             $value .= $db->escape($data);
  349.         }
  350.         $value .= "'";
  351.         return $value;
  352.     }
  353.  
  354.     // }}}
  355.     // {{{ quoteBLOB()
  356.  
  357.     /**
  358.      * Convert a text value into a DBMS specific format that is suitable to
  359.      * compose query statements.
  360.      *
  361.      * @param           $blob 
  362.      * @return string  text string that represents the given argument value in
  363.      *                  a DBMS specific format.
  364.      * @access public
  365.      */
  366.     function quoteBLOB($blob)
  367.     {
  368.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  369.         if ($blob === null{
  370.             return 'NULL';
  371.         }
  372.         $value "0x";
  373.         $data = null;
  374.         while (!$this->endOfLOB($blob)) {
  375.         $result $this->readLOB($blob$data$db->options['lob_buffer_length']);
  376.             if (MDB2::isError($result)) {
  377.                 return $result;
  378.             }
  379.             $value .= bin2hex($data);
  380.         }
  381.         return $value;
  382.     }
  383.  
  384.     // }}}
  385.     // {{{ quoteBoolean()
  386.  
  387.     /**
  388.      * Convert a text value into a DBMS specific format that is suitable to
  389.      * compose query statements.
  390.      *
  391.      * @param string $value text string value that is intended to be converted.
  392.      * @return string text string that represents the given argument value in
  393.      *        a DBMS specific format.
  394.      * @access public
  395.      */
  396.     function quoteBoolean($value)
  397.     {
  398.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  399.         return ($value === null'NULL' ($value ? 1 : 0);
  400.     }
  401.  
  402.     // }}}
  403.     // {{{ quoteFloat()
  404.  
  405.     /**
  406.      * Convert a text value into a DBMS specific format that is suitable to
  407.      * compose query statements.
  408.      *
  409.      * @param string  $value text string value that is intended to be converted.
  410.      * @return string  text string that represents the given argument value in
  411.      *                  a DBMS specific format.
  412.      * @access public
  413.      */
  414.     function quoteFloat($value)
  415.     {
  416.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  417.         return ($value === null'NULL' $value;
  418.     }
  419.  
  420.     // }}}
  421.     // {{{ quoteDecimal()
  422.  
  423.     /**
  424.      * Convert a text value into a DBMS specific format that is suitable to
  425.      * compose query statements.
  426.      *
  427.      * @param string  $value text string value that is intended to be converted.
  428.      * @return string  text string that represents the given argument value in
  429.      *                  a DBMS specific format.
  430.      * @access public
  431.      */
  432.     function quoteDecimal($value)
  433.     {
  434.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  435.         return ($value === null'NULL' $value;
  436.     }
  437. }
  438.  
  439. ?>

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