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 versions 4 and 5                                                 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2006 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@pooteeweet.org>                           |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: fbsql.php 327310 2012-08-27 15:16:18Z danielc $
  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@pooteeweet.org>
  57.  */
  58. class MDB2_Driver_Datatype_fbsql extends MDB2_Driver_Datatype_Common
  59. {
  60.     // {{{ _baseConvertResult()
  61.  
  62.     /**
  63.      * general type conversion method
  64.      *
  65.      * @param mixed $value refernce to a value to be converted
  66.      * @param string $type specifies which type to convert to
  67.      * @param string $rtrim if text should be rtrimmed
  68.      * @return object MDB2 error on failure
  69.      * @access protected
  70.      */
  71.     function _baseConvertResult($value$type$rtrim = true)
  72.     {
  73.         if (null === $value{
  74.             return null;
  75.         }
  76.         switch ($type{
  77.          case 'boolean':
  78.              return $value == 'T';
  79.          case 'time':
  80.             if ($value[0== '+'{
  81.                 return substr($value1);
  82.             else {
  83.                 return $value;
  84.             }
  85.         }
  86.         return parent::_baseConvertResult($value$type$rtrim);
  87.     }
  88.  
  89.     // }}}
  90.     // {{{ getTypeDeclaration()
  91.  
  92.     /**
  93.      * Obtain DBMS specific SQL code portion needed to declare an text type
  94.      * field to be used in statements like CREATE TABLE.
  95.      *
  96.      * @param array $field  associative array with the name of the properties
  97.      *       of the field being declared as array indexes. Currently, the types
  98.      *       of supported field properties are as follows:
  99.      *
  100.      *       length
  101.      *           Integer value that determines the maximum length of the text
  102.      *           field. If this argument is missing the field should be
  103.      *           declared to have the longest length allowed by the DBMS.
  104.      *
  105.      *       default
  106.      *           Text value to be used as default for this field.
  107.      *
  108.      *       notnull
  109.      *           Boolean flag that indicates whether this field is constrained
  110.      *           to not be set to null.
  111.      * @return string  DBMS specific SQL code portion that should be used to
  112.      *       declare the specified field.
  113.      * @access public
  114.      */
  115.     function getTypeDeclaration($field)
  116.     {
  117.         $db =$this->getDBInstance();
  118.         if (MDB2::isError($db)) {
  119.             return $db;
  120.         }
  121.  
  122.         switch ($field['type']{
  123.         case 'text':
  124.             $length !empty($field['length'])
  125.                 ? $field['length'$db->options['default_text_field_length'];
  126.             $fixed !empty($field['fixed']$field['fixed': false;
  127.             return $fixed 'CHAR('.$length.')' 'VARCHAR('.$length.')';
  128.         case 'clob':
  129.             return 'CLOB';
  130.         case 'blob':
  131.             return 'BLOB';
  132.         case 'integer':
  133.             return 'INT';
  134.         case 'boolean':
  135.             return 'BOOLEAN';
  136.         case 'date':
  137.             return 'DATE';
  138.         case 'time':
  139.             return 'TIME';
  140.         case 'timestamp':
  141.             return 'TIMESTAMP';
  142.         case 'float':
  143.             return 'FLOAT';
  144.         case 'decimal':
  145.             $length !empty($field['length']$field['length': 18;
  146.             $scale !empty($field['scale']$field['scale'$db->options['decimal_places'];
  147.             return 'DECIMAL('.$length.','.$scale.')';
  148.         }
  149.         return '';
  150.     }
  151.  
  152.     // }}}
  153.     // {{{ _quoteBoolean()
  154.  
  155.     /**
  156.      * Convert a text value into a DBMS specific format that is suitable to
  157.      * compose query statements.
  158.      *
  159.      * @param string $value text string value that is intended to be converted.
  160.      * @param bool $quote determines if the value should be quoted and escaped
  161.      * @param bool $escape_wildcards if to escape escape wildcards
  162.      * @return string text string that represents the given argument value in
  163.      *        a DBMS specific format.
  164.      * @access protected
  165.      */
  166.     function _quoteBoolean($value$quote$escape_wildcards)
  167.     {
  168.         return ($value 'True' 'False');
  169.     }
  170.  
  171.     // }}}
  172.     // {{{ _quoteDate()
  173.  
  174.     /**
  175.      * Convert a text value into a DBMS specific format that is suitable to
  176.      * compose query statements.
  177.      *
  178.      * @param string $value text string value that is intended to be converted.
  179.      * @param bool $quote determines if the value should be quoted and escaped
  180.      * @param bool $escape_wildcards if to escape escape wildcards
  181.      * @return string text string that represents the given argument value in
  182.      *         a DBMS specific format.
  183.      * @access protected
  184.      */
  185.     function _quoteDate($value$quote$escape_wildcards)
  186.     {
  187.         return 'DATE'.$this->_quoteText($value$quote$escape_wildcards);
  188.     }
  189.  
  190.     // }}}
  191.     // {{{ _quoteTimestamp()
  192.  
  193.     /**
  194.      * Convert a text value into a DBMS specific format that is suitable to
  195.      * compose query statements.
  196.      *
  197.      * @param string $value text string value that is intended to be converted.
  198.      * @param bool $quote determines if the value should be quoted and escaped
  199.      * @param bool $escape_wildcards if to escape escape wildcards
  200.      * @return string text string that represents the given argument value in
  201.      *         a DBMS specific format.
  202.      * @access protected
  203.      */
  204.     function _quoteTimestamp($value$quote$escape_wildcards)
  205.     {
  206.         return 'TIMESTAMP'.$this->_quoteText($value$quote$escape_wildcards);
  207.     }
  208.  
  209.     // }}}
  210.     // {{{ _quoteTime()
  211.  
  212.     /**
  213.      * Convert a text value into a DBMS specific format that is suitable to
  214.      *        compose query statements.
  215.      *
  216.      * @param string $value text string value that is intended to be converted.
  217.      * @param bool $quote determines if the value should be quoted and escaped
  218.      * @param bool $escape_wildcards if to escape escape wildcards
  219.      * @return string text string that represents the given argument value in
  220.      *         a DBMS specific format.
  221.      * @access protected
  222.      */
  223.     function _quoteTime($value$quote$escape_wildcards)
  224.     {
  225.         return 'TIME'.$this->_quoteText($value$quote$escape_wildcards);
  226.     }
  227.     
  228.     // }}}
  229.     // {{{ _mapNativeDatatype()
  230.  
  231.     /**
  232.      * Maps a native array description of a field to a MDB2 datatype and length
  233.      *
  234.      * @param array  $field native field description
  235.      * @return array containing the various possible types, length, sign, fixed
  236.      * @access public
  237.      */
  238.     function _mapNativeDatatype($field)
  239.     {
  240.         $db_type strtolower($field['type']);
  241.         $length $field['length'];
  242.         if ($length == '-1' && !empty($field['atttypmod'])) {
  243.             $length $field['atttypmod'- 4;
  244.         }
  245.         $type = array();
  246.         $unsigned $fixed = null;
  247.         switch ($db_type{
  248.         case 'tinyint':
  249.             $type['integer';
  250.             $unsigned preg_match('/ unsigned/i'$field['type']);
  251.             $length = 1;
  252.             break;
  253.         case 'smallint':
  254.             $type['integer';
  255.             $unsigned = false;
  256.             $length = 2;
  257.             if ($length == '2'{
  258.                 $type['boolean';
  259.                 if (preg_match('/^(is|has)/'$field['name'])) {
  260.                     $type array_reverse($type);
  261.                 }
  262.             }
  263.             break;
  264.         case 'int':
  265.         case 'integer':
  266.             $type['integer';
  267.             $unsigned = false;
  268.             $length = 4;
  269.             break;
  270.         case 'longint':
  271.             $type['integer';
  272.             $unsigned = false;
  273.             $length = 8;
  274.             break;
  275.         case 'boolean':
  276.             $type['boolean';
  277.             $length = null;
  278.             break;
  279.         case 'character varying':
  280.         case 'char varying':
  281.         case 'varchar':
  282.         case 'national character varying':
  283.         case 'national char varying':
  284.         case 'nchar varying':
  285.             $fixed = false;
  286.         case 'unknown':
  287.         case 'char':
  288.         case 'character':
  289.         case 'national character':
  290.         case 'national char':
  291.         case 'nchar':
  292.             $type['text';
  293.             if (strstr($db_type'text')) {
  294.                 $type['clob';
  295.                 $type array_reverse($type);
  296.             }
  297.             if ($fixed !== false{
  298.                 $fixed = true;
  299.             }
  300.             break;
  301.         case 'date':
  302.             $type['date';
  303.             $length = null;
  304.             break;
  305.         case 'timestamp':
  306.         case 'timestamp with time zone':
  307.             $type['timestamp';
  308.             $length = null;
  309.             break;
  310.         case 'time':
  311.         case 'time with time zone':
  312.             $type['time';
  313.             $length = null;
  314.             break;
  315.         case 'float':
  316.         case 'double precision':
  317.         case 'real':
  318.             $type['float';
  319.             break;
  320.         case 'decimal':
  321.         case 'numeric':
  322.             $type['decimal';
  323.             break;
  324.         case 'blob':
  325.             $type['blob';
  326.             $length = null;
  327.             break;
  328.         case 'clob':
  329.             $type['clob';
  330.             $length = null;
  331.             break;
  332.         default:
  333.             $db =$this->getDBInstance();
  334.             if (MDB2::isError($db)) {
  335.                 return $db;
  336.             }
  337.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  338.                 'unknown database attribute type: '.$db_type__FUNCTION__);
  339.         }
  340.  
  341.         if ((int)$length <= 0{
  342.             $length = null;
  343.         }
  344.  
  345.         return array($type$length$unsigned$fixed);
  346.     }
  347.  
  348.     // }}}
  349. }
  350. ?>

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