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-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. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  44. // +----------------------------------------------------------------------+
  45. //
  46. // $Id: sqlite.php 327310 2012-08-27 15:16:18Z danielc $
  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@pooteeweet.org>
  57.  */
  58. class MDB2_Driver_Datatype_sqlite extends MDB2_Driver_Datatype_Common
  59. {
  60.     // {{{ _getCollationFieldDeclaration()
  61.  
  62.     /**
  63.      * Obtain DBMS specific SQL code portion needed to set the COLLATION
  64.      * of a field declaration to be used in statements like CREATE TABLE.
  65.      *
  66.      * @param string $collation name of the collation
  67.      *
  68.      * @return string DBMS specific SQL code portion needed to set the COLLATION
  69.      *                 of a field declaration.
  70.      */
  71.     function _getCollationFieldDeclaration($collation)
  72.     {
  73.         return 'COLLATE '.$collation;
  74.     }
  75.  
  76.     // }}}
  77.     // {{{ getTypeDeclaration()
  78.  
  79.     /**
  80.      * Obtain DBMS specific SQL code portion needed to declare an text type
  81.      * field to be used in statements like CREATE TABLE.
  82.      *
  83.      * @param array $field  associative array with the name of the properties
  84.      *       of the field being declared as array indexes. Currently, the types
  85.      *       of supported field properties are as follows:
  86.      *
  87.      *       length
  88.      *           Integer value that determines the maximum length of the text
  89.      *           field. If this argument is missing the field should be
  90.      *           declared to have the longest length allowed by the DBMS.
  91.      *
  92.      *       default
  93.      *           Text value to be used as default for this field.
  94.      *
  95.      *       notnull
  96.      *           Boolean flag that indicates whether this field is constrained
  97.      *           to not be set to null.
  98.      * @return string  DBMS specific SQL code portion that should be used to
  99.      *       declare the specified field.
  100.      * @access public
  101.      */
  102.     function getTypeDeclaration($field)
  103.     {
  104.         $db $this->getDBInstance();
  105.         if (MDB2::isError($db)) {
  106.             return $db;
  107.         }
  108.  
  109.         switch ($field['type']{
  110.         case 'text':
  111.             $length !empty($field['length'])
  112.                 ? $field['length': false;
  113.             $fixed !empty($field['fixed']$field['fixed': false;
  114.             return $fixed ($length 'CHAR('.$length.')' 'CHAR('.$db->options['default_text_field_length'].')')
  115.                 : ($length 'VARCHAR('.$length.')' 'TEXT');
  116.         case 'clob':
  117.             if (!empty($field['length'])) {
  118.                 $length $field['length'];
  119.                 if ($length <= 255{
  120.                     return 'TINYTEXT';
  121.                 elseif ($length <= 65532{
  122.                     return 'TEXT';
  123.                 elseif ($length <= 16777215{
  124.                     return 'MEDIUMTEXT';
  125.                 }
  126.             }
  127.             return 'LONGTEXT';
  128.         case 'blob':
  129.             if (!empty($field['length'])) {
  130.                 $length $field['length'];
  131.                 if ($length <= 255{
  132.                     return 'TINYBLOB';
  133.                 elseif ($length <= 65532{
  134.                     return 'BLOB';
  135.                 elseif ($length <= 16777215{
  136.                     return 'MEDIUMBLOB';
  137.                 }
  138.             }
  139.             return 'LONGBLOB';
  140.         case 'integer':
  141.             if (!empty($field['length'])) {
  142.                 $length $field['length'];
  143.                 if ($length <= 2{
  144.                     return 'SMALLINT';
  145.                 elseif ($length == 3 || $length == 4{
  146.                     return 'INTEGER';
  147.                 elseif ($length > 4{
  148.                     return 'BIGINT';
  149.                 }
  150.             }
  151.             return 'INTEGER';
  152.         case 'boolean':
  153.             return 'BOOLEAN';
  154.         case 'date':
  155.             return 'DATE';
  156.         case 'time':
  157.             return 'TIME';
  158.         case 'timestamp':
  159.             return 'DATETIME';
  160.         case 'float':
  161.             return 'DOUBLE'.($db->options['fixed_float''('.
  162.                 ($db->options['fixed_float']+2).','.$db->options['fixed_float'].')' '');
  163.         case 'decimal':
  164.             $length !empty($field['length']$field['length': 18;
  165.             $scale !empty($field['scale']$field['scale'$db->options['decimal_places'];
  166.             return 'DECIMAL('.$length.','.$scale.')';
  167.         }
  168.         return '';
  169.     }
  170.  
  171.     // }}}
  172.     // {{{ _getIntegerDeclaration()
  173.  
  174.     /**
  175.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  176.      * field to be used in statements like CREATE TABLE.
  177.      *
  178.      * @param string  $name   name the field to be declared.
  179.      * @param string  $field  associative array with the name of the properties
  180.      *                         of the field being declared as array indexes.
  181.      *                         Currently, the types of supported field
  182.      *                         properties are as follows:
  183.      *
  184.      *                        unsigned
  185.      *                         Boolean flag that indicates whether the field
  186.      *                         should be declared as unsigned integer if
  187.      *                         possible.
  188.      *
  189.      *                        default
  190.      *                         Integer value to be used as default for this
  191.      *                         field.
  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 _getIntegerDeclaration($name$field)
  201.     {
  202.         $db $this->getDBInstance();
  203.         if (MDB2::isError($db)) {
  204.             return $db;
  205.         }
  206.  
  207.         $default $autoinc '';
  208.         if (!empty($field['autoincrement'])) {
  209.             $autoinc ' PRIMARY KEY';
  210.         elseif (array_key_exists('default'$field)) {
  211.             if ($field['default'=== ''{
  212.                 $field['default'= empty($field['notnull']? null : 0;
  213.             }
  214.             $default ' DEFAULT '.$this->quote($field['default']'integer');
  215.         }
  216.  
  217.         $notnull = empty($field['notnull']'' ' NOT NULL';
  218.         $unsigned = empty($field['unsigned']'' ' UNSIGNED';
  219.         if (empty($default&& empty($notnull)) {
  220.             $default ' DEFAULT NULL';
  221.         }
  222.         $name $db->quoteIdentifier($nametrue);
  223.         return $name.' '.$this->getTypeDeclaration($field).$unsigned.$default.$notnull.$autoinc;
  224.     }
  225.  
  226.     // }}}
  227.     // {{{ matchPattern()
  228.  
  229.     /**
  230.      * build a pattern matching string
  231.      *
  232.      * @access public
  233.      *
  234.      * @param array $pattern even keys are strings, odd are patterns (% and _)
  235.      * @param string $operator optional pattern operator (LIKE, ILIKE and maybe others in the future)
  236.      * @param string $field optional field name that is being matched against
  237.      *                   (might be required when emulating ILIKE)
  238.      *
  239.      * @return string SQL pattern
  240.      */
  241.     function matchPattern($pattern$operator = null$field = null)
  242.     {
  243.         $db $this->getDBInstance();
  244.         if (MDB2::isError($db)) {
  245.             return $db;
  246.         }
  247.  
  248.         $match '';
  249.         if (null !== $operator{
  250.             $field (null === $field'' $field.' ';
  251.             $operator strtoupper($operator);
  252.             switch ($operator{
  253.             // case insensitive
  254.             case 'ILIKE':
  255.                 $match $field.'LIKE ';
  256.                 break;
  257.             case 'NOT ILIKE':
  258.                 $match $field.'NOT LIKE ';
  259.                 break;
  260.             // case sensitive
  261.             case 'LIKE':
  262.                 $match $field.'LIKE ';
  263.                 break;
  264.             case 'NOT LIKE':
  265.                 $match $field.'NOT LIKE ';
  266.                 break;
  267.             default:
  268.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  269.                     'not a supported operator type:'$operator__FUNCTION__);
  270.             }
  271.         }
  272.         $match.= "'";
  273.         foreach ($pattern as $key => $value{
  274.             if ($key % 2{
  275.                 $match.= $value;
  276.             else {
  277.                 $match.= $db->escapePattern($db->escape($value));
  278.             }
  279.         }
  280.         $match.= "'";
  281.         $match.= $this->patternEscapeString();
  282.         return $match;
  283.     }
  284.  
  285.     // }}}
  286.     // {{{ _mapNativeDatatype()
  287.  
  288.     /**
  289.      * Maps a native array description of a field to a MDB2 datatype and length
  290.      *
  291.      * @param array  $field native field description
  292.      * @return array containing the various possible types, length, sign, fixed
  293.      * @access public
  294.      */
  295.     function _mapNativeDatatype($field)
  296.     {
  297.         $db_type strtolower($field['type']);
  298.         $length !empty($field['length']$field['length': null;
  299.         $unsigned !empty($field['unsigned']$field['unsigned': null;
  300.         $fixed = null;
  301.         $type = array();
  302.         switch ($db_type{
  303.         case 'boolean':
  304.             $type['boolean';
  305.             break;
  306.         case 'tinyint':
  307.             $type['integer';
  308.             $type['boolean';
  309.             if (preg_match('/^(is|has)/'$field['name'])) {
  310.                 $type array_reverse($type);
  311.             }
  312.             $unsigned preg_match('/ unsigned/i'$field['type']);
  313.             $length = 1;
  314.             break;
  315.         case 'smallint':
  316.             $type['integer';
  317.             $unsigned preg_match('/ unsigned/i'$field['type']);
  318.             $length = 2;
  319.             break;
  320.         case 'mediumint':
  321.             $type['integer';
  322.             $unsigned preg_match('/ unsigned/i'$field['type']);
  323.             $length = 3;
  324.             break;
  325.         case 'int':
  326.         case 'integer':
  327.         case 'serial':
  328.             $type['integer';
  329.             $unsigned preg_match('/ unsigned/i'$field['type']);
  330.             $length = 4;
  331.             break;
  332.         case 'bigint':
  333.         case 'bigserial':
  334.             $type['integer';
  335.             $unsigned preg_match('/ unsigned/i'$field['type']);
  336.             $length = 8;
  337.             break;
  338.         case 'clob':
  339.             $type['clob';
  340.             $fixed  = false;
  341.             break;
  342.         case 'tinytext':
  343.         case 'mediumtext':
  344.         case 'longtext':
  345.         case 'text':
  346.         case 'varchar':
  347.         case 'varchar2':
  348.             $fixed = false;
  349.         case 'char':
  350.             $type['text';
  351.             if ($length == '1'{
  352.                 $type['boolean';
  353.                 if (preg_match('/^(is|has)/'$field['name'])) {
  354.                     $type array_reverse($type);
  355.                 }
  356.             elseif (strstr($db_type'text')) {
  357.                 $type['clob';
  358.                 $type array_reverse($type);
  359.             }
  360.             if ($fixed !== false{
  361.                 $fixed = true;
  362.             }
  363.             break;
  364.         case 'date':
  365.             $type['date';
  366.             $length = null;
  367.             break;
  368.         case 'datetime':
  369.         case 'timestamp':
  370.             $type['timestamp';
  371.             $length = null;
  372.             break;
  373.         case 'time':
  374.             $type['time';
  375.             $length = null;
  376.             break;
  377.         case 'float':
  378.         case 'double':
  379.         case 'real':
  380.             $type['float';
  381.             break;
  382.         case 'decimal':
  383.         case 'numeric':
  384.             $type['decimal';
  385.             $length $length.','.$field['decimal'];
  386.             break;
  387.         case 'tinyblob':
  388.         case 'mediumblob':
  389.         case 'longblob':
  390.         case 'blob':
  391.             $type['blob';
  392.             $length = null;
  393.             break;
  394.         case 'year':
  395.             $type['integer';
  396.             $type['date';
  397.             $length = null;
  398.             break;
  399.         default:
  400.             $db $this->getDBInstance();
  401.             if (MDB2::isError($db)) {
  402.                 return $db;
  403.             }
  404.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  405.                 'unknown database attribute type: '.$db_type__FUNCTION__);
  406.         }
  407.  
  408.         if ((int)$length <= 0{
  409.             $length = null;
  410.         }
  411.  
  412.         return array($type$length$unsigned$fixed);
  413.     }
  414.  
  415.     // }}}
  416. }
  417.  
  418. ?>

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