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 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. // | Authors: Lukas Smith <smith@backendmedia.com>                        |
  44. // |          Daniel Convissor <danielc@php.net>                          |
  45. // +----------------------------------------------------------------------+
  46. //
  47. // $Id: mssql.php,v 1.17 2005/04/19 12:53:41 lsmith Exp $
  48. //
  49.  
  50. require_once 'MDB2/Driver/Datatype/Common.php';
  51.  
  52. /**
  53.  * MDB2 MS SQL driver
  54.  *
  55.  * @package MDB2
  56.  * @category Database
  57.  * @author  Lukas Smith <smith@backendmedia.com>
  58.  */
  59. {
  60.     // {{{ convertResult()
  61.  
  62.     /**
  63.      * convert a value to a RDBMS indepdenant MDB2 type
  64.      *
  65.      * @param mixed  $value   value to be converted
  66.      * @param int    $type    constant that specifies which type to convert to
  67.      * @return mixed converted value
  68.      * @access public
  69.      */
  70.     function convertResult($value$type)
  71.     {
  72.         if (is_null($value)) {
  73.             return null;
  74.         }
  75.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  76.         switch ($type{
  77.         case 'boolean':
  78.             return $value == '1';
  79.         case 'date':
  80.             if (strlen($value> 10{
  81.                 $value substr($value,0,10);
  82.             }
  83.             return $value;
  84.         case 'time':
  85.             if (strlen($value> 8{
  86.                 $value substr($value,11,8);
  87.             }
  88.             return $value;
  89.         default:
  90.             return $this->_baseConvertResult($value,$type);
  91.         }
  92.     }
  93.  
  94.     // }}}
  95.     // {{{ _getIntegerDeclaration()
  96.  
  97.     /**
  98.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  99.      * field to be used in statements like CREATE TABLE.
  100.      *
  101.      * @param string $name name the field to be declared.
  102.      * @param array $field associative array with the name of the properties
  103.      *        of the field being declared as array indexes. Currently, the types
  104.      *        of supported field properties are as follows:
  105.      *
  106.      *        unsigned
  107.      *            Boolean flag that indicates whether the field should be
  108.      *            declared as unsigned integer if possible.
  109.      *
  110.      *        default
  111.      *            Integer value to be used as default for this field.
  112.      *
  113.      *        notnull
  114.      *            Boolean flag that indicates whether this field is constrained
  115.      *            to not be set to null.
  116.      * @return string DBMS specific SQL code portion that should be used to
  117.      *        declare the specified field.
  118.      * @access protected
  119.      */
  120.     function _getIntegerDeclaration($name$field)
  121.     {
  122.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  123.         if (isset($field['unsigned'])) {
  124.             $db->warnings[= "unsigned integer field \"$name\" is being declared as signed integer";
  125.         }
  126.         $default = isset($field['default']' DEFAULT '.
  127.             $this->quote($field['default']'integer''';
  128.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  129.         return $name.' INT'.$default.$notnull;
  130.     }
  131.  
  132.     // }}}
  133.     // {{{ _getTextDeclaration()
  134.  
  135.     /**
  136.      * Obtain DBMS specific SQL code portion needed to declare an text type
  137.      * field to be used in statements like CREATE TABLE.
  138.      *
  139.      * @param string $name name the field to be declared.
  140.      * @param array $field associative array with the name of the properties
  141.      *        of the field being declared as array indexes. Currently, the types
  142.      *        of supported field properties are as follows:
  143.      *
  144.      *        length
  145.      *            Integer value that determines the maximum length of the text
  146.      *            field. If this argument is missing the field should be
  147.      *            declared to have the longest length allowed by the DBMS.
  148.      *
  149.      *        default
  150.      *            Text value to be used as default for this field.
  151.      *
  152.      *        notnull
  153.      *            Boolean flag that indicates whether this field is constrained
  154.      *            to not be set to null.
  155.      * @return string DBMS specific SQL code portion that should be used to
  156.      *        declare the specified field.
  157.      * @access protected
  158.      */
  159.     function _getTextDeclaration($name$field)
  160.     {
  161.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  162.         $type = isset($field['length']'VARCHAR ('.$field['length'].')' 'TEXT';
  163.         $default = isset($field['default']' DEFAULT '.
  164.             $this->quote($field['default']'text''';
  165.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  166.         return $name.' '.$type.$default.$notnull;
  167.     }
  168.  
  169.     // }}}
  170.     // {{{ _getCLOBDeclaration()
  171.  
  172.     /**
  173.      * Obtain DBMS specific SQL code portion needed to declare an character
  174.      * large 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
  178.      *                         properties of the field being declared as array
  179.      *                         indexes. 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
  190.      *                         is 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 _getCLOBDeclaration($name$field)
  196.     {
  197.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  198.         if (isset($field['length'])) {
  199.             $length $field['length'];
  200.             if ($length <= 8000{
  201.                 $type = "VARCHAR($length)";
  202.             else {
  203.                 $type 'TEXT';
  204.             }
  205.         else {
  206.             $type 'TEXT';
  207.         }
  208.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  209.         return $name.' '.$type.$notnull;
  210.     }
  211.  
  212.     // }}}
  213.     // {{{ _getBLOBDeclaration()
  214.  
  215.     /**
  216.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  217.      * object type field to be used in statements like CREATE TABLE.
  218.      *
  219.      * @param string  $name   name the field to be declared.
  220.      * @param string  $field  associative array with the name of the properties
  221.      *                         of the field being declared as array indexes.
  222.      *                         Currently, the types of supported field
  223.      *                         properties are as follows:
  224.      *
  225.      *                        length
  226.      *                         Integer value that determines the maximum length
  227.      *                         of the large object field. If this argument is
  228.      *                         missing the field should be declared to have the
  229.      *                         longest length allowed by the DBMS.
  230.      *
  231.      *                        notnull
  232.      *                         Boolean flag that indicates whether this field is
  233.      *                         constrained to not be set to null.
  234.      * @return string  DBMS specific SQL code portion that should be used to
  235.      *                  declare the specified field.
  236.      * @access protected
  237.      */
  238.     function _getBLOBDeclaration($name$field)
  239.     {
  240.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  241.         if (isset($field['length'])) {
  242.             $length $field['length'];
  243.             if ($length <= 8000{
  244.                 $type = "VARBINARY($length)";
  245.             else {
  246.                 $type 'IMAGE';
  247.             }
  248.         else {
  249.             $type 'IMAGE';
  250.         }
  251.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  252.         return $name.' '.$type.$notnull;
  253.     }
  254.  
  255.     // }}}
  256.     // {{{ _getBooleanDeclaration()
  257.  
  258.     /**
  259.      * Obtain DBMS specific SQL code portion needed to declare a boolean type
  260.      * field to be used in statements like CREATE TABLE.
  261.      *
  262.      * @param string $name name the field to be declared.
  263.      * @param array $field associative array with the name of the properties
  264.      *        of the field being declared as array indexes. Currently, the types
  265.      *        of supported field properties are as follows:
  266.      *
  267.      *        default
  268.      *            Boolean value to be used as default for this field.
  269.      *
  270.      *        notnull
  271.      *            Boolean flag that indicates whether this field is constrained
  272.      *            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 _getBooleanDeclaration($name$field)
  278.     {
  279.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  280.         $default = isset($field['default']' DEFAULT '.
  281.             $this->quote($field['default']'boolean''';
  282.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  283.         return $name.' BIT'.$default.$notnull;
  284.     }
  285.  
  286.     // }}}
  287.     // {{{ _getDateDeclaration()
  288.  
  289.     /**
  290.      * Obtain DBMS specific SQL code portion needed to declare a date type
  291.      * field to be used in statements like CREATE TABLE.
  292.      *
  293.      * @param string $name name the field to be declared.
  294.      * @param array $field associative array with the name of the properties
  295.      *        of the field being declared as array indexes. Currently, the types
  296.      *        of supported field properties are as follows:
  297.      *
  298.      *        default
  299.      *            Date value to be used as default for this field.
  300.      *
  301.      *        notnull
  302.      *            Boolean flag that indicates whether this field is constrained
  303.      *            to not be set to null.
  304.      * @return string DBMS specific SQL code portion that should be used to
  305.      *        declare the specified field.
  306.      * @access protected
  307.      */
  308.     function _getDateDeclaration($name$field)
  309.     {
  310.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  311.         $default = isset($field['default']' DEFAULT '.
  312.             $this->quote($field['default']'date''';
  313.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  314.         return $name.' CHAR ('.strlen('YYYY-MM-DD').')'.$default.$notnull;
  315.     }
  316.  
  317.     // }}}
  318.     // {{{ _getTimestampDeclaration()
  319.  
  320.     /**
  321.      * Obtain DBMS specific SQL code portion needed to declare a timestamp
  322.      * field to be used in statements like CREATE TABLE.
  323.      *
  324.      * @param string $name name the field to be declared.
  325.      * @param array $field associative array with the name of the properties
  326.      *        of the field being declared as array indexes. Currently, the types
  327.      *        of supported field properties are as follows:
  328.      *
  329.      *        default
  330.      *            Timestamp value to be used as default for this field.
  331.      *
  332.      *        notnull
  333.      *            Boolean flag that indicates whether this field is constrained
  334.      *            to not be set to null.
  335.      * @return string DBMS specific SQL code portion that should be used to
  336.      *        declare the specified field.
  337.      * @access protected
  338.      */
  339.     function _getTimestampDeclaration($name$field)
  340.     {
  341.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  342.         $default = isset($field['default']' DEFAULT '.
  343.             $this->quote($field['default']'timestamp''';
  344.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  345.         return $name.' CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')'.$default.$notnull;
  346.     }
  347.  
  348.     // }}}
  349.     // {{{ _getTimeDeclaration()
  350.  
  351.     /**
  352.      * Obtain DBMS specific SQL code portion needed to declare a time
  353.      * field to be used in statements like CREATE TABLE.
  354.      *
  355.      * @param string $name name the field to be declared.
  356.      * @param array $field associative array with the name of the properties
  357.      *        of the field being declared as array indexes. Currently, the types
  358.      *        of supported field properties are as follows:
  359.      *
  360.      *        default
  361.      *            Time value to be used as default for this field.
  362.      *
  363.      *        notnull
  364.      *            Boolean flag that indicates whether this field is constrained
  365.      *            to not be set to null.
  366.      * @return string DBMS specific SQL code portion that should be used to
  367.      *        declare the specified field.
  368.      * @access protected
  369.      */
  370.     function _getTimeDeclaration($name$field)
  371.     {
  372.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  373.         $default = isset($field['default']' DEFAULT '.
  374.             $this->quote($field['default']'time''';
  375.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  376.         return $name.' CHAR ('.strlen('HH:MM:SS').')'.$default.$notnull;
  377.     }
  378.  
  379.     // }}}
  380.     // {{{ _getFloatDeclaration()
  381.  
  382.     /**
  383.      * Obtain DBMS specific SQL code portion needed to declare an float type
  384.      * field to be used in statements like CREATE TABLE.
  385.      *
  386.      * @param string  $name   name the field to be declared.
  387.      * @param string  $field  associative array with the name of the properties
  388.      *                         of the field being declared as array indexes.
  389.      *                         Currently, the types of supported field
  390.      *                         properties are as follows:
  391.      *
  392.      *                        default
  393.      *                         Integer value to be used as default for this
  394.      *                         field.
  395.      *
  396.      *                        notnull
  397.      *                         Boolean flag that indicates whether this field is
  398.      *                         constrained to not be set to null.
  399.      * @return string  DBMS specific SQL code portion that should be used to
  400.      *                  declare the specified field.
  401.      * @access protected
  402.      */
  403.     function _getFloatDeclaration($name$field)
  404.     {
  405.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  406.         $default = isset($field['default']' DEFAULT '.
  407.             $this->quote($field['default']'float''';
  408.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  409.         return $name.' FLOAT'.$default.$notnull;
  410.     }
  411.  
  412.     // }}}
  413.     // {{{ _getDecimalDeclaration()
  414.  
  415.     /**
  416.      * Obtain DBMS specific SQL code portion needed to declare an decimal type
  417.      * field to be used in statements like CREATE TABLE.
  418.      *
  419.      * @param string  $name   name the field to be declared.
  420.      * @param string  $field  associative array with the name of the properties
  421.      *                         of the field being declared as array indexes.
  422.      *                         Currently, the types of supported field
  423.      *                         properties are as follows:
  424.      *
  425.      *                        default
  426.      *                         Integer value to be used as default for this
  427.      *                         field.
  428.      *
  429.      *                        notnull
  430.      *                         Boolean flag that indicates whether this field is
  431.      *                         constrained to not be set to null.
  432.      * @return string  DBMS specific SQL code portion that should be used to
  433.      *                  declare the specified field.
  434.      * @access protected
  435.      */
  436.     function _getDecimalDeclaration($name$field)
  437.     {
  438.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  439.         $type 'DECIMAL(18,'.$db->options['decimal_places'].')';
  440.         $default = isset($field['default']' DEFAULT '.
  441.             $this->quote($field['default']'decimal''';
  442.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  443.         return $name.' '.$type.$default.$notnull;
  444.     }
  445.  
  446.     // }}}
  447.     // {{{ _quoteBLOB()
  448.  
  449.     /**
  450.      * Convert a text value into a DBMS specific format that is suitable to
  451.      * compose query statements.
  452.      *
  453.      * @param           $value 
  454.      * @return string  text string that represents the given argument value in
  455.      *                  a DBMS specific format.
  456.      * @access protected
  457.      */
  458.     function _quoteBLOB($value)
  459.     {
  460.         $value $this->_readFile($value);
  461.         return bin2hex("0x".$value);
  462.     }
  463.  
  464.     // }}}
  465.     // {{{ _quoteBoolean()
  466.  
  467.     /**
  468.      * Convert a text value into a DBMS specific format that is suitable to
  469.      * compose query statements.
  470.      *
  471.      * @param string $value text string value that is intended to be converted.
  472.      * @return string text string that represents the given argument value in
  473.      *        a DBMS specific format.
  474.      * @access protected
  475.      */
  476.     function _quoteBoolean($value)
  477.     {
  478.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  479.         return ($value ? 1 : 0);
  480.     }
  481.  
  482.     // }}}
  483.     // {{{ _quoteFloat()
  484.  
  485.     /**
  486.      * Convert a text value into a DBMS specific format that is suitable to
  487.      * compose query statements.
  488.      *
  489.      * @param string  $value text string value that is intended to be converted.
  490.      * @return string  text string that represents the given argument value in
  491.      *                  a DBMS specific format.
  492.      * @access protected
  493.      */
  494.     function _quoteFloat($value)
  495.     {
  496.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  497.         return $value;
  498.     }
  499.  
  500.     // }}}
  501.     // {{{ _quoteDecimal()
  502.  
  503.     /**
  504.      * Convert a text value into a DBMS specific format that is suitable to
  505.      * compose query statements.
  506.      *
  507.      * @param string  $value text string value that is intended to be converted.
  508.      * @return string  text string that represents the given argument value in
  509.      *                  a DBMS specific format.
  510.      * @access protected
  511.      */
  512.     function _quoteDecimal($value)
  513.     {
  514.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  515.         return $value;
  516.     }
  517. }
  518.  
  519. ?>

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