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

Source for file sqlsrv.php

Documentation is available at sqlsrv.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-2007 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@pooteeweet.org>                          |
  44. // |          Daniel Convissor <danielc@php.net>                          |
  45. // +----------------------------------------------------------------------+
  46.  
  47. require_once 'MDB2/Driver/Datatype/Common.php';
  48.  
  49. /**
  50.  * MDB2 MS SQL driver
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  */
  55. class MDB2_Driver_Datatype_sqlsrv extends MDB2_Driver_Datatype_Common
  56. {
  57.     // {{{ _baseConvertResult()
  58.  
  59.     /**
  60.      * general type conversion method
  61.      *
  62.      * @param mixed   $value refernce to a value to be converted
  63.      * @param string  $type  specifies which type to convert to
  64.      * @param boolean $rtrim [optional] when TRUE [default], apply rtrim() to text
  65.      * @return object MDB2 error on failure
  66.      * @access protected
  67.      */
  68.     function _baseConvertResult($value$type$rtrim = true)
  69.     {
  70.         if (null === $value{
  71.             return null;
  72.         }
  73.         switch ($type{
  74.         case 'boolean':
  75.             return ($value === 0)? false : !empty($value);
  76.         case 'date':
  77.             if (strlen($value> 10{
  78.                 $value substr($value,0,10);
  79.             }
  80.             return $value;
  81.         case 'time':
  82.             if (strlen($value> 8{
  83.                 $value substr($value,11,8);
  84.             }
  85.             return $value;
  86.         }
  87.         return parent::_baseConvertResult($value$type$rtrim);
  88.     }
  89.  
  90.     // }}}
  91.     // {{{ _getCollationFieldDeclaration()
  92.  
  93.     /**
  94.      * Obtain DBMS specific SQL code portion needed to set the COLLATION
  95.      * of a field declaration to be used in statements like CREATE TABLE.
  96.      *
  97.      * @param string $collation name of the collation
  98.      *
  99.      * @return string DBMS specific SQL code portion needed to set the COLLATION
  100.      *                 of a field declaration.
  101.      */
  102.     function _getCollationFieldDeclaration($collation)
  103.     {
  104.         return 'COLLATE '.$collation;
  105.     }
  106.  
  107.     // }}}
  108.     // {{{ getTypeDeclaration()
  109.  
  110.     /**
  111.      * Obtain DBMS specific SQL code portion needed to declare an text type
  112.      * field to be used in statements like CREATE TABLE.
  113.      *
  114.      * @param array $field  associative array with the name of the properties
  115.      *       of the field being declared as array indexes. Currently, the types
  116.      *       of supported field properties are as follows:
  117.      *
  118.      *       length
  119.      *           Integer value that determines the maximum length of the text
  120.      *           field. If this argument is missing the field should be
  121.      *           declared to have the longest length allowed by the DBMS.
  122.      *
  123.      *       default
  124.      *           Text value to be used as default for this field.
  125.      *
  126.      *       notnull
  127.      *           Boolean flag that indicates whether this field is constrained
  128.      *           to not be set to null.
  129.      * @return string  DBMS specific SQL code portion that should be used to
  130.      *       declare the specified field.
  131.      * @access public
  132.      */
  133.     function getTypeDeclaration($field)
  134.     {
  135.         $db $this->getDBInstance();
  136.         if (MDB2::isError($db)) {
  137.             return $db;
  138.         }
  139.  
  140.         switch ($field['type']{
  141.         case 'text':
  142.             $length !empty($field['length'])
  143.                 ? $field['length': false;
  144.             $fixed !empty($field['fixed']$field['fixed': false;
  145.             return $fixed ($length 'CHAR('.$length.')' 'CHAR('.$db->options['default_text_field_length'].')')
  146.                 : ($length 'VARCHAR('.$length.')' 'VARCHAR(MAX)');
  147.         case 'clob':
  148.             if (!empty($field['length'])) {
  149.                 $length $field['length'];
  150.                 if ($length <= 8000{
  151.                     return 'VARCHAR('.$length.')';
  152.                 }
  153.              }
  154.              return 'VARCHAR(MAX)';
  155.         case 'blob':
  156.             if (!empty($field['length'])) {
  157.                 $length $field['length'];
  158.                 if ($length <= 8000{
  159.                     return "VARBINARY($length)";
  160.                 }
  161.             }
  162.             return 'IMAGE';
  163.         case 'integer':
  164.             return 'INT';
  165.         case 'boolean':
  166.             return 'BIT';
  167.         case 'date':
  168.             return 'CHAR ('.strlen('YYYY-MM-DD').')';
  169.         case 'time':
  170.             return 'CHAR ('.strlen('HH:MM:SS').')';
  171.         case 'timestamp':
  172.             return 'CHAR ('.strlen('YYYY-MM-DD HH:MM:SS').')';
  173.         case 'float':
  174.             return 'FLOAT';
  175.         case 'decimal':
  176.             $length !empty($field['length']$field['length': 18;
  177.             $scale !empty($field['scale']$field['scale'$db->options['decimal_places'];
  178.             return 'DECIMAL('.$length.','.$scale.')';
  179.         }
  180.         return '';
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ _getIntegerDeclaration()
  185.  
  186.     /**
  187.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  188.      * field to be used in statements like CREATE TABLE.
  189.      *
  190.      * @param string  $name   name the field to be declared.
  191.      * @param string  $field  associative array with the name of the properties
  192.      *                         of the field being declared as array indexes.
  193.      *                         Currently, the types of supported field
  194.      *                         properties are as follows:
  195.      *
  196.      *                        unsigned
  197.      *                         Boolean flag that indicates whether the field
  198.      *                         should be declared as unsigned integer if
  199.      *                         possible.
  200.      *
  201.      *                        default
  202.      *                         Integer value to be used as default for this
  203.      *                         field.
  204.      *
  205.      *                        notnull
  206.      *                         Boolean flag that indicates whether this field is
  207.      *                         constrained to not be set to null.
  208.      * @return string  DBMS specific SQL code portion that should be used to
  209.      *                  declare the specified field.
  210.      * @access protected
  211.      */
  212.     function _getIntegerDeclaration($name$field)
  213.     {
  214.         $db $this->getDBInstance();
  215.         if (MDB2::isError($db)) {
  216.             return $db;
  217.         }
  218.  
  219.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  220.         $default $autoinc '';
  221.         if (!empty($field['autoincrement'])) {
  222.             $autoinc ' IDENTITY PRIMARY KEY';
  223.         elseif (array_key_exists('default'$field)) {
  224.             if ($field['default'=== ''{
  225.                 $field['default'= 0;
  226.             }
  227.             if (null === $field['default']{
  228.                 $default ' DEFAULT (NULL)';
  229.             else {
  230.                 $default ' DEFAULT (' $this->quote($field['default']'integer'')';
  231.             }
  232.         }
  233.  
  234.         if (!empty($field['unsigned'])) {
  235.             $db->warnings[= "unsigned integer field \"$name\" is being declared as signed integer";
  236.         }
  237.  
  238.         $name $db->quoteIdentifier($nametrue);
  239.         return $name.' '.$this->getTypeDeclaration($field).$notnull.$default.$autoinc;
  240.     }
  241.  
  242.     // }}}
  243.     // {{{ _getCLOBDeclaration()
  244.  
  245.     /**
  246.      * Obtain DBMS specific SQL code portion needed to declare an character
  247.      * large object type field to be used in statements like CREATE TABLE.
  248.      *
  249.      * @param string $name name the field to be declared.
  250.      * @param array $field associative array with the name of the properties
  251.      *         of the field being declared as array indexes. Currently, the types
  252.      *         of supported field properties are as follows:
  253.      *
  254.      *         length
  255.      *             Integer value that determines the maximum length of the large
  256.      *             object field. If this argument is missing the field should be
  257.      *             declared to have the longest length allowed by the DBMS.
  258.      *
  259.      *         notnull
  260.      *             Boolean flag that indicates whether this field is constrained
  261.      *             to not be set to null.
  262.      * @return string DBMS specific SQL code portion that should be used to
  263.      *         declare the specified field.
  264.      * @access public
  265.      */
  266.     function _getCLOBDeclaration($name$field)
  267.     {
  268.         $db $this->getDBInstance();
  269.         if (MDB2::isError($db)) {
  270.             return $db;
  271.         }
  272.  
  273.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  274.         $name $db->quoteIdentifier($nametrue);
  275.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ _getBLOBDeclaration()
  280.  
  281.     /**
  282.      * Obtain DBMS specific SQL code portion needed to declare an binary large
  283.      * object type field to be used in statements like CREATE TABLE.
  284.      *
  285.      * @param string $name name the field to be declared.
  286.      * @param array $field associative array with the name of the properties
  287.      *         of the field being declared as array indexes. Currently, the types
  288.      *         of supported field properties are as follows:
  289.      *
  290.      *         length
  291.      *             Integer value that determines the maximum length of the large
  292.      *             object field. If this argument is missing the field should be
  293.      *             declared to have the longest length allowed by the DBMS.
  294.      *
  295.      *         notnull
  296.      *             Boolean flag that indicates whether this field is constrained
  297.      *             to not be set to null.
  298.      * @return string DBMS specific SQL code portion that should be used to
  299.      *         declare the specified field.
  300.      * @access protected
  301.      */
  302.     function _getBLOBDeclaration($name$field)
  303.     {
  304.         $db $this->getDBInstance();
  305.         if (MDB2::isError($db)) {
  306.             return $db;
  307.         }
  308.  
  309.         $notnull = empty($field['notnull']' NULL' ' NOT NULL';
  310.         $name $db->quoteIdentifier($nametrue);
  311.         return $name.' '.$this->getTypeDeclaration($field).$notnull;
  312.     }
  313.  
  314.     // }}}
  315.     // {{{ _quoteBLOB()
  316.  
  317.     /**
  318.      * Convert a text value into a DBMS specific format that is suitable to
  319.      * compose query statements.
  320.      *
  321.      * @param string $value text string value that is intended to be converted.
  322.      * @param bool $quote determines if the value should be quoted and escaped
  323.      * @param bool $escape_wildcards if to escape escape wildcards
  324.      * @return string  text string that represents the given argument value in
  325.      *                  a DBMS specific format.
  326.      * @access protected
  327.      */
  328.     function _quoteBLOB($value$quote$escape_wildcards)
  329.     {
  330.         if (!$quote{
  331.             return $value;
  332.         }
  333.         $db $this->getDBInstance();
  334.         if (MDB2::isError($db)) {
  335.             return $db;
  336.         }
  337.         if ($db->options['lob_allow_url_include']{
  338.             $value '0x'.bin2hex($this->_readFile($value));
  339.         }
  340.         return "'".$value."'";
  341.     }
  342.  
  343.     // }}}
  344.     // {{{ _mapNativeDatatype()
  345.  
  346.     /**
  347.      * Maps a native array description of a field to a MDB2 datatype and length
  348.      *
  349.      * @param array  $field native field description
  350.      * @return array containing the various possible types, length, sign, fixed
  351.      * @access public
  352.      */
  353.     function _mapNativeDatatype($field)
  354.     {
  355.         // todo: handle length of various int variations
  356.         $db_type $field['type'];
  357.         $length $field['length'];
  358.         $type = array();
  359.         // todo: unsigned handling seems to be missing
  360.         $unsigned $fixed = null;
  361.         switch ($db_type{
  362.         case 'bit':
  363.         case SQLSRV_SQLTYPE_BIT:
  364.             $type[0'boolean';
  365.             break;
  366.         case 'tinyint':
  367.         case SQLSRV_SQLTYPE_TINYINT:
  368.             $type[0'integer';
  369.             $length = 1;
  370.             break;
  371.         case 'smallint':
  372.         case SQLSRV_SQLTYPE_SMALLINT:
  373.             $type[0'integer';
  374.             $length = 2;
  375.             break;
  376.         case 'int':
  377.         case SQLSRV_SQLTYPE_INT:
  378.             $type[0'integer';
  379.             $length = 4;
  380.             break;
  381.         case 'bigint':
  382.         case SQLSRV_SQLTYPE_BIGINT:
  383.             $type[0'integer';
  384.             $length = 8;
  385.             break;
  386.         case 'datetime':
  387.         case SQLSRV_SQLTYPE_DATETIME:
  388.             $type[0'timestamp';
  389.             break;
  390.         case 'float':
  391.         case SQLSRV_SQLTYPE_FLOAT:
  392.         case 'real':
  393.         case SQLSRV_SQLTYPE_REAL:
  394.             $type[0'float';
  395.             break;
  396.         case 'numeric':
  397.         case SQLSRV_SQLTYPE_NUMERIC:
  398.         case 'decimal':
  399.         case SQLSRV_SQLTYPE_DECIMAL:
  400.         case 'money':
  401.         case SQLSRV_SQLTYPE_MONEY:
  402.             $type[0'decimal';
  403.             $length $field['numeric_precision'].','.$field['numeric_scale'];
  404.             break;
  405.         case 'text':
  406.         case SQLSRV_SQLTYPE_TEXT:
  407.         case 'ntext':
  408.         case SQLSRV_SQLTYPE_NTEXT:
  409.         case 'varchar':
  410.         case SQLSRV_SQLTYPE_VARCHAR:
  411.         case 'nvarchar':
  412.         case SQLSRV_SQLTYPE_NVARCHAR:
  413.             $fixed = false;
  414.         case 'char':
  415.         case SQLSRV_SQLTYPE_CHAR:
  416.         case 'nchar':
  417.         case SQLSRV_SQLTYPE_NCHAR:
  418.             $type[0'text';
  419.             if ($length == '1'{
  420.                 $type['boolean';
  421.                 if (preg_match('/^(is|has)/'$field['name'])) {
  422.                     $type array_reverse($type);
  423.                 }
  424.             elseif (strstr($db_type'text'|| strstr($db_typeSQLSRV_SQLTYPE_TEXT)) {
  425.                 $type['clob';
  426.                 $type array_reverse($type);
  427.             }
  428.             if ($fixed !== false{
  429.                 $fixed = true;
  430.             }
  431.             break;
  432.         case 'image':
  433.         case SQLSRV_SQLTYPE_IMAGE:
  434.         case 'varbinary':
  435.         case SQLSRV_SQLTYPE_VARBINARY:
  436.         case 'timestamp':
  437.         case SQLSRV_SQLTYPE_TIMESTAMP:
  438.             $type['blob';
  439.             $length = null;
  440.             break;
  441.         default:
  442.             $db $this->getDBInstance();
  443.             if (MDB2::isError($db)) {
  444.                 return $db;
  445.             }
  446.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  447.                 'unknown database attribute type: '.$db_type__FUNCTION__);
  448.         }
  449.  
  450.         if ((int)$length <= 0{
  451.             $length = null;
  452.         }
  453.  
  454.         return array($type$length$unsigned$fixed);
  455.     }
  456.     // }}}
  457. }
  458.  
  459. ?>

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