Source for file Sqlite.php

Documentation is available at Sqlite.php

  1. <?php
  2. /*
  3.  *  $Id: Sqlite.php 2196 2007-08-10 20:29:07Z meus $
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16.  *
  17.  * This software consists of voluntary contributions made by many individuals
  18.  * and is licensed under the LGPL. For more information, see
  19.  * <http://www.phpdoctrine.com>.
  20.  */
  21. Doctrine::autoload('Doctrine_DataDict');
  22. /**
  23.  * @package     Doctrine
  24.  * @subpackage  Doctrine_DataDict
  25.  * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
  26.  * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
  27.  * @author      Lukas Smith <smith@pooteeweet.org> (PEAR MDB2 library)
  28.  * @version     $Revision: 2196 $
  29.  * @category    Object Relational Mapping
  30.  * @link        www.phpdoctrine.com
  31.  * @since       1.0
  32.  */
  33. {
  34.     /**
  35.      * Obtain DBMS specific SQL code portion needed to declare an text type
  36.      * field to be used in statements like CREATE TABLE.
  37.      *
  38.      * @param array $field  associative array with the name of the properties
  39.      *       of the field being declared as array indexes. Currently, the types
  40.      *       of supported field properties are as follows:
  41.      *
  42.      *       length
  43.      *           Integer value that determines the maximum length of the text
  44.      *           field. If this argument is missing the field should be
  45.      *           declared to have the longest length allowed by the DBMS.
  46.      *
  47.      *       default
  48.      *           Text value to be used as default for this field.
  49.      *
  50.      *       notnull
  51.      *           Boolean flag that indicates whether this field is constrained
  52.      *           to not be set to null.
  53.      * @author Lukas Smith (PEAR MDB2 library)
  54.      * @return string  DBMS specific SQL code portion that should be used to
  55.      *       declare the specified field.
  56.      */
  57.     public function getNativeDeclaration(array $field)
  58.     {
  59.         if isset($field['type'])) {
  60.             throw new Doctrine_DataDict_Exception('Missing column type.');
  61.         }
  62.         switch ($field['type']{
  63.             case 'text':
  64.             case 'object':
  65.             case 'array':
  66.             case 'string':
  67.             case 'char':
  68.             case 'gzip':
  69.             case 'varchar':
  70.                 $length (isset($field['length']&& $field['length']$field['length'null;
  71.  
  72.                 $fixed  ((isset($field['fixed']&& $field['fixed']|| $field['type'== 'char'true false;
  73.  
  74.                 return $fixed ($length 'CHAR('.$length.')' 'CHAR('.$this->conn->getAttribute(Doctrine::ATTR_DEFAULT_TEXTFLD_LENGTH).')')
  75.                     : ($length 'VARCHAR('.$length.')' 'TEXT');
  76.             case 'clob':
  77.                 if (!empty($field['length'])) {
  78.                     $length $field['length'];
  79.                     if ($length <= 255{
  80.                         return 'TINYTEXT';
  81.                     elseif ($length <= 65535{
  82.                         return 'TEXT';
  83.                     elseif ($length <= 16777215{
  84.                         return 'MEDIUMTEXT';
  85.                     }
  86.                 }
  87.                 return 'LONGTEXT';
  88.             case 'blob':
  89.                 if (!empty($field['length'])) {
  90.                     $length $field['length'];
  91.                     if ($length <= 255{
  92.                         return 'TINYBLOB';
  93.                     elseif ($length <= 65535{
  94.                         return 'BLOB';
  95.                     elseif ($length <= 16777215{
  96.                         return 'MEDIUMBLOB';
  97.                     }
  98.                 }
  99.                 return 'LONGBLOB';
  100.             case 'enum':
  101.             case 'integer':
  102.             case 'boolean':
  103.             case 'int':
  104.                 return 'INTEGER';
  105.             case 'date':
  106.                 return 'DATE';
  107.             case 'time':
  108.                 return 'TIME';
  109.             case 'timestamp':
  110.                 return 'DATETIME';
  111.             case 'float':
  112.             case 'double':
  113.                 return 'DOUBLE';//($this->conn->options['fixed_float'] ? '('.
  114.                     //($this->conn->options['fixed_float']+2).','.$this->conn->options['fixed_float'].')' : '');
  115.             case 'decimal':
  116.                 $length !empty($field['length']$field['length'18;
  117.                 $scale !empty($field['scale']$field['scale'$this->conn->getAttribute(Doctrine::ATTR_DECIMAL_PLACES);
  118.                 return 'DECIMAL('.$length.','.$scale.')';
  119.         }
  120.         throw new Doctrine_DataDict_Exception('Unknown field type \'' $field['type'.  '\'.');
  121.     }
  122.     /**
  123.      * Maps a native array description of a field to Doctrine datatype and length
  124.      *
  125.      * @param array  $field native field description
  126.      * @return array containing the various possible types, length, sign, fixed
  127.      */
  128.     public function getPortableDeclaration(array $field)
  129.     {
  130.         $dbType strtolower($field['type']);
  131.         $length (isset($field['length'])) $field['length'null;
  132.         $unsigned (isset($field['unsigned'])) $field['unsigned'null;
  133.         $fixed null;
  134.         $type array();
  135.  
  136.         if isset($field['name'])) {
  137.             $field['name''';
  138.         }
  139.  
  140.         switch ($dbType{
  141.             case 'boolean':
  142.                 $type['boolean';
  143.                 break;
  144.             case 'tinyint':
  145.                 $type['integer';
  146.                 $type['boolean';
  147.                 if (preg_match('/^(is|has)/'$field['name'])) {
  148.                     $type array_reverse($type);
  149.                 }
  150.                 $unsigned preg_match('/ unsigned/i'$field['type']);
  151.                 $length 1;
  152.                 break;
  153.             case 'smallint':
  154.                 $type['integer';
  155.                 $unsigned preg_match('/ unsigned/i'$field['type']);
  156.                 $length 2;
  157.                 break;
  158.             case 'mediumint':
  159.                 $type['integer';
  160.                 $unsigned preg_match('/ unsigned/i'$field['type']);
  161.                 $length 3;
  162.                 break;
  163.             case 'int':
  164.             case 'integer':
  165.             case 'serial':
  166.                 $type['integer';
  167.                 $unsigned preg_match('/ unsigned/i'$field['type']);
  168.                 $length 4;
  169.                 break;
  170.             case 'bigint':
  171.             case 'bigserial':
  172.                 $type['integer';
  173.                 $unsigned preg_match('/ unsigned/i'$field['type']);
  174.                 $length 8;
  175.                 break;
  176.             case 'clob':
  177.             case 'tinytext':
  178.             case 'mediumtext':
  179.             case 'longtext':
  180.             case 'text':
  181.             case 'varchar':
  182.             case 'varchar2':
  183.                 $fixed false;
  184.             case 'char':
  185.                 $type['text';
  186.                 if ($length == '1'{
  187.                     $type['boolean';
  188.                     if (preg_match('/^(is|has)/'$field['name'])) {
  189.                         $type array_reverse($type);
  190.                     }
  191.                 elseif (strstr($dbType'text')) {
  192.                     $type['clob';
  193.                 }
  194.                 if ($fixed !== false{
  195.                     $fixed true;
  196.                 }
  197.                 break;
  198.             case 'date':
  199.                 $type['date';
  200.                 $length null;
  201.                 break;
  202.             case 'datetime':
  203.             case 'timestamp':
  204.                 $type['timestamp';
  205.                 $length null;
  206.                 break;
  207.             case 'time':
  208.                 $type['time';
  209.                 $length null;
  210.                 break;
  211.             case 'float':
  212.             case 'double':
  213.             case 'real':
  214.                 $type['float';
  215.                 $length null;
  216.                 break;
  217.             case 'decimal':
  218.             case 'numeric':
  219.                 $type['decimal';
  220.                 $length null;
  221.                 break;
  222.             case 'tinyblob':
  223.             case 'mediumblob':
  224.             case 'longblob':
  225.             case 'blob':
  226.                 $type['blob';
  227.                 $length null;
  228.                 break;
  229.             case 'year':
  230.                 $type['integer';
  231.                 $type['date';
  232.                 $length null;
  233.                 break;
  234.             default:
  235.                 throw new Doctrine_DataDict_Exception('unknown database attribute type: '.$dbType);
  236.         }
  237.  
  238.         return array('type'     => $type,
  239.                      'length'   => $length,
  240.                      'unsigned' => $unsigned,
  241.                      'fixed'    => $fixed);
  242.     }
  243.     /**
  244.      * Obtain DBMS specific SQL code portion needed to declare an integer type
  245.      * field to be used in statements like CREATE TABLE.
  246.      *
  247.      * @param string  $name   name the field to be declared.
  248.      * @param array  $field   associative array with the name of the properties
  249.      *                         of the field being declared as array indexes.
  250.      *                         Currently, the types of supported field
  251.      *                         properties are as follows:
  252.      *
  253.      *                        unsigned
  254.      *                         Boolean flag that indicates whether the field
  255.      *                         should be declared as unsigned integer if
  256.      *                         possible.
  257.      *
  258.      *                        default
  259.      *                         Integer value to be used as default for this
  260.      *                         field.
  261.      *
  262.      *                        notnull
  263.      *                         Boolean flag that indicates whether this field is
  264.      *                         constrained to not be set to null.
  265.      * @return string  DBMS specific SQL code portion that should be used to
  266.      *                  declare the specified field.
  267.      * @access protected
  268.      */
  269.     public function getIntegerDeclaration($namearray $field)
  270.     {
  271.         $default $autoinc '';
  272.         $type    $this->getNativeDeclaration($field);
  273.  
  274.         $autoincrement = isset($field['autoincrement']&& $field['autoincrement'];
  275.  
  276.         if ($autoincrement){
  277.             $autoinc ' PRIMARY KEY AUTOINCREMENT';
  278.             $type    'INTEGER';
  279.         elseif (array_key_exists('default'$field)) {
  280.             if ($field['default'=== ''{
  281.                 $field['default'empty($field['notnull']null 0;
  282.             }
  283.             $default ' DEFAULT ' $this->conn->quote($field['default']$field['type']);
  284.         }/**
  285.         elseif (empty($field['notnull'])) {
  286.             $default = ' DEFAULT NULL';
  287.         }
  288.         */
  289.  
  290.         $notnull  (isset($field['notnull']&& $field['notnull']' NOT NULL' '';
  291.  
  292.         // sqlite does not support unsigned attribute for autoinremented fields
  293.         $unsigned (isset($field['unsigned']&& $field['unsigned'&& !$autoincrement' UNSIGNED' '';
  294.  
  295.         $name $this->conn->quoteIdentifier($nametrue);
  296.         return $name ' ' $type $unsigned $default $notnull $autoinc;
  297.     }
  298. }