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

Source for file sqlite.php

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

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