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

Source for file odbc.php

Documentation is available at odbc.php

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

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