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

Source for file sqlite.php

Documentation is available at sqlite.php

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * Index, constraint and alter methods for DB_Table usage with
  7.  * PEAR::DB as backend.
  8.  * 
  9.  * The code in this class was adopted from the MDB2 PEAR package.
  10.  * 
  11.  * PHP versions 4 and 5
  12.  *
  13.  * LICENSE:
  14.  * 
  15.  * Copyright (c) 1997-2007, Lorenzo Alberton <l.alberton@quipo.it>
  16.  *                          Lukas Smith <smith@pooteeweet.org>
  17.  *                          Paul M. Jones <pmjones@php.net>
  18.  *                          David C. Morse <morse@php.net>
  19.  *                          Mark Wiesemann <wiesemann@php.net>
  20.  * All rights reserved.
  21.  *
  22.  * Redistribution and use in source and binary forms, with or without
  23.  * modification, are permitted provided that the following conditions
  24.  * are met:
  25.  *
  26.  *    * Redistributions of source code must retain the above copyright
  27.  *      notice, this list of conditions and the following disclaimer.
  28.  *    * Redistributions in binary form must reproduce the above copyright
  29.  *      notice, this list of conditions and the following disclaimer in the
  30.  *      documentation and/or other materials provided with the distribution.
  31.  *    * The names of the authors may not be used to endorse or promote products
  32.  *      derived from this software without specific prior written permission.
  33.  *
  34.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  35.  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  36.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  37.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  38.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  39.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  40.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  41.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  42.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  43.  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  44.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45.  *
  46.  * @category Database
  47.  * @package  DB_Table
  48.  * @author   Lorenzo Alberton <l.alberton@quipo.it>
  49.  * @author   Lukas Smith <smith@pooteeweet.org>
  50.  * @author   Mark Wiesemann <wiesemann@php.net>
  51.  * @license  http://opensource.org/licenses/bsd-license.php New BSD License
  52.  * @version  CVS: $Id: sqlite.php,v 1.5 2007/12/13 16:52:15 wiesemann Exp $
  53.  * @link     http://pear.php.net/package/DB_Table
  54.  */
  55.  
  56. /**
  57.  * Require DB_Table class
  58.  */
  59. require_once 'DB/Table.php';
  60.  
  61. /**
  62.  * Index, constraint and alter methods for DB_Table usage with
  63.  * PEAR::DB as backend.
  64.  * 
  65.  * The code in this class was adopted from the MDB2 PEAR package.
  66.  * 
  67.  * @category Database
  68.  * @package  DB_Table
  69.  * @author   Lorenzo Alberton <l.alberton@quipo.it>
  70.  * @author   Lukas Smith <smith@pooteeweet.org>
  71.  * @author   Mark Wiesemann <wiesemann@php.net>
  72.  * @version  Release: 1.5.6
  73.  * @link     http://pear.php.net/package/DB_Table
  74.  */
  75.  
  76.     /**
  77.     * 
  78.     * The PEAR DB object that connects to the database.
  79.     * 
  80.     * @access private
  81.     * 
  82.     * @var object 
  83.     * 
  84.     */
  85.     
  86.     var $_db = null;
  87.  
  88.  
  89.     /**
  90.      * list all indexes in a table
  91.      *
  92.      * @param string    $table      name of table that should be used in method
  93.      * @return mixed data array on success, a PEAR error on failure
  94.      * @access public
  95.      */
  96.     function listTableIndexes($table)
  97.     {
  98.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  99.         $query.= "LOWER(tbl_name)='".strtolower($table)."'";
  100.         $query.= " AND sql NOT NULL ORDER BY name";
  101.         $indexes $this->_db->getCol($query);
  102.         if (PEAR::isError($indexes)) {
  103.             return $indexes;
  104.         }
  105.  
  106.         $result = array();
  107.         foreach ($indexes as $sql{
  108.             if (preg_match("/^create index ([^ ]*) on /i"$sql$tmp)) {
  109.                 $result[$tmp[1]] = true;
  110.             }
  111.         }
  112.         $result array_change_key_case($resultCASE_LOWER);
  113.  
  114.         return array_keys($result);
  115.     }
  116.  
  117.  
  118.     /**
  119.      * list all constraints in a table
  120.      *
  121.      * @param string    $table      name of table that should be used in method
  122.      * @return mixed data array on success, a PEAR error on failure
  123.      * @access public
  124.      */
  125.     function listTableConstraints($table)
  126.     {
  127.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  128.         $query.= "LOWER(tbl_name)='".strtolower($table)."'";
  129.         $query.= " AND sql NOT NULL ORDER BY name";
  130.         $indexes $this->_db->getCol($query);
  131.         if (PEAR::isError($indexes)) {
  132.             return $indexes;
  133.         }
  134.  
  135.         $result = array();
  136.         foreach ($indexes as $sql{
  137.             if (preg_match("/^create unique index ([^ ]*) on /i"$sql$tmp)) {
  138.                 $result[$tmp[1]] = true;
  139.             }
  140.         }
  141.         $result array_change_key_case($resultCASE_LOWER);
  142.  
  143.         return array_keys($result);
  144.     }
  145.  
  146.  
  147.     /**
  148.      * get the structure of an index into an array
  149.      *
  150.      * @param string    $table      name of table that should be used in method
  151.      * @param string    $index_name name of index that should be used in method
  152.      * @return mixed data array on success, a PEAR error on failure
  153.      * @access public
  154.      */
  155.     function getTableIndexDefinition($table$index_name)
  156.     {
  157.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  158.         $query.= "LOWER(name)='".strtolower($index_name)."' AND LOWER(tbl_name)='".strtolower($table)."'";
  159.         $query.= " AND sql NOT NULL ORDER BY name";
  160.         $sql $this->_db->getOne($query);
  161.         if (PEAR::isError($sql)) {
  162.             return $sql;
  163.         }
  164.  
  165.         $sql strtolower($sql);
  166.         $start_pos strpos($sql'(');
  167.         $end_pos strrpos($sql')');
  168.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  169.         $column_names split(','$column_names);
  170.  
  171.         $definition = array();
  172.         $count count($column_names);
  173.         for ($i=0; $i<$count; ++$i{
  174.             $column_name strtok($column_names[$i]," ");
  175.             $collation strtok(" ");
  176.             $definition['fields'][$column_name= array();
  177.             if (!empty($collation)) {
  178.                 $definition['fields'][$column_name]['sorting'=
  179.                     ($collation=='ASC' 'ascending' 'descending');
  180.             }
  181.         }
  182.  
  183.         return $definition;
  184.     }
  185.  
  186.  
  187.     /**
  188.      * get the structure of a constraint into an array
  189.      *
  190.      * @param string    $table      name of table that should be used in method
  191.      * @param string    $index_name name of index that should be used in method
  192.      * @return mixed data array on success, a PEAR error on failure
  193.      * @access public
  194.      */
  195.     function getTableConstraintDefinition($table$index_name)
  196.     {
  197.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  198.         $query.= "LOWER(name)='".strtolower($index_name)."' AND LOWER(tbl_name)='".strtolower($table)."'";
  199.         $query.= " AND sql NOT NULL ORDER BY name";
  200.         $sql $this->_db->getOne($query);
  201.         if (PEAR::isError($sql)) {
  202.             return $sql;
  203.         }
  204.  
  205.         $sql strtolower($sql);
  206.         $start_pos strpos($sql'(');
  207.         $end_pos strrpos($sql')');
  208.         $column_names substr($sql$start_pos+1$end_pos-$start_pos-1);
  209.         $column_names split(','$column_names);
  210.  
  211.         $definition = array();
  212.         $definition['unique'= true;
  213.         $count count($column_names);
  214.         for ($i=0; $i<$count; ++$i{
  215.             $column_name strtok($column_names[$i]," ");
  216.             $collation strtok(" ");
  217.             $definition['fields'][$column_name= array();
  218.             if (!empty($collation)) {
  219.                 $definition['fields'][$column_name]['sorting'=
  220.                     ($collation=='ASC' 'ascending' 'descending');
  221.             }
  222.         }
  223.  
  224.         return $definition;
  225.     }
  226.  
  227.     
  228.     /**
  229.      * drop existing index
  230.      *
  231.      * @param string    $table         name of table that should be used in method
  232.      * @param string    $name         name of the index to be dropped
  233.      * @return mixed DB_OK on success, a PEAR error on failure
  234.      * @access public
  235.      */
  236.     function dropIndex($table$name)
  237.     {
  238.         return $this->_db->query("DROP INDEX $name");
  239.     }
  240.  
  241.  
  242.     /**
  243.      * drop existing constraint
  244.      *
  245.      * @param string    $table         name of table that should be used in method
  246.      * @param string    $name         name of the constraint to be dropped
  247.      * @return mixed DB_OK on success, a PEAR error on failure
  248.      * @access public
  249.      */
  250.     function dropConstraint($table$name)
  251.     {
  252.         if (strtolower($name== 'primary'{
  253.             return DB_Table::throwError(
  254.                 DB_TABLE_ERR_ALTER_INDEX_IMPOS,
  255.                 '(primary)'
  256.             );
  257.         }
  258.  
  259.         return $this->_db->query("DROP INDEX $name");
  260.     }
  261.  
  262.  
  263.     /**
  264.      * alter an existing table
  265.      *
  266.      * @param string $name         name of the table that is intended to be changed.
  267.      * @param array $changes     associative array that contains the details of each type
  268.      *                              of change that is intended to be performed. The types of
  269.      *                              changes that are currently supported are defined as follows:
  270.      *
  271.      *                              name
  272.      *
  273.      *                                 New name for the table.
  274.      *
  275.      *                             add
  276.      *
  277.      *                                 Associative array with the names of fields to be added as
  278.      *                                  indexes of the array. The value of each entry of the array
  279.      *                                  should be set to another associative array with the properties
  280.      *                                  of the fields to be added. The properties of the fields should
  281.      *                                  be the same as defined by the Metabase parser.
  282.      *
  283.      *
  284.      *                             remove
  285.      *
  286.      *                                 Associative array with the names of fields to be removed as indexes
  287.      *                                  of the array. Currently the values assigned to each entry are ignored.
  288.      *                                  An empty array should be used for future compatibility.
  289.      *
  290.      *                             rename
  291.      *
  292.      *                                 Associative array with the names of fields to be renamed as indexes
  293.      *                                  of the array. The value of each entry of the array should be set to
  294.      *                                  another associative array with the entry named name with the new
  295.      *                                  field name and the entry named Declaration that is expected to contain
  296.      *                                  the portion of the field declaration already in DBMS specific SQL code
  297.      *                                  as it is used in the CREATE TABLE statement.
  298.      *
  299.      *                             change
  300.      *
  301.      *                                 Associative array with the names of the fields to be changed as indexes
  302.      *                                  of the array. Keep in mind that if it is intended to change either the
  303.      *                                  name of a field and any other properties, the change array entries
  304.      *                                  should have the new names of the fields as array indexes.
  305.      *
  306.      *                                 The value of each entry of the array should be set to another associative
  307.      *                                  array with the properties of the fields to that are meant to be changed as
  308.      *                                  array entries. These entries should be assigned to the new values of the
  309.      *                                  respective properties. The properties of the fields should be the same
  310.      *                                  as defined by the Metabase parser.
  311.      *
  312.      *                             Example
  313.      *                                 array(
  314.      *                                     'name' => 'userlist',
  315.      *                                     'add' => array(
  316.      *                                         'quota' => array(
  317.      *                                             'type' => 'integer',
  318.      *                                             'unsigned' => 1
  319.      *                                         )
  320.      *                                     ),
  321.      *                                     'remove' => array(
  322.      *                                         'file_limit' => array(),
  323.      *                                         'time_limit' => array()
  324.      *                                     ),
  325.      *                                     'change' => array(
  326.      *                                         'name' => array(
  327.      *                                             'length' => '20',
  328.      *                                             'definition' => array(
  329.      *                                                 'type' => 'text',
  330.      *                                                 'length' => 20,
  331.      *                                             ),
  332.      *                                         )
  333.      *                                     ),
  334.      *                                     'rename' => array(
  335.      *                                         'sex' => array(
  336.      *                                             'name' => 'gender',
  337.      *                                             'definition' => array(
  338.      *                                                 'type' => 'text',
  339.      *                                                 'length' => 1,
  340.      *                                                 'default' => 'M',
  341.      *                                             ),
  342.      *                                         )
  343.      *                                     )
  344.      *                                 )
  345.      *
  346.      * @param boolean $check     (ignored in DB_Table)
  347.      * @access public
  348.      *
  349.      * @return mixed DB_OK on success, a PEAR error on failure
  350.      */
  351.     function alterTable($name$changes$check)
  352.     {
  353.         $version $this->_getServerVersion();
  354.         foreach ($changes as $change_name => $change{
  355.             switch ($change_name{
  356.             case 'add':
  357.                 if ($version['major'>= 3 && $version['minor'>= 1{
  358.                     break;
  359.                 }
  360.             case 'name':
  361.                 if ($version['major'>= 3 && $version['minor'>= 1{
  362.                     break;
  363.                 }
  364.             case 'remove':
  365.             case 'change':
  366.             case 'rename':
  367.             default:
  368.                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
  369.             }
  370.         }
  371.  
  372.         $query '';
  373.         if (array_key_exists('name'$changes)) {
  374.             $change_name $this->_db->quoteIdentifier($changes['name']);
  375.             $query .= 'RENAME TO ' $change_name;
  376.         }
  377.  
  378.         if (array_key_exists('add'$changes)) {
  379.             foreach ($changes['add'as $field_name => $field{
  380.                 if ($query{
  381.                     $query.= ', ';
  382.                 }
  383.                 $query.= 'ADD COLUMN ' $field_name ' ' $field;
  384.             }
  385.         }
  386.  
  387.         if (!$query{
  388.             return DB_OK;
  389.         }
  390.  
  391.         $name $this->_db->quoteIdentifier($name);
  392.         return $this->_db->query("ALTER TABLE $name $query");
  393.     }
  394.  
  395.  
  396.     /**
  397.      * return version information about the server
  398.      *
  399.      * @param string     $native  determines if the raw version string should be returned
  400.      * @return mixed array/string with version information or MDB2 error object
  401.      * @access private
  402.      */
  403.     function _getServerVersion($native = false)
  404.     {
  405.         if (!function_exists('sqlite_libversion')) {
  406.             return 0;  // error
  407.         }
  408.         $server_info sqlite_libversion();
  409.         if (!$native{
  410.             $tmp explode('.'$server_info3);
  411.             $server_info = array(
  412.                 'major' => isset($tmp[0]$tmp[0: null,
  413.                 'minor' => isset($tmp[1]$tmp[1: null,
  414.                 'patch' => isset($tmp[2]$tmp[2: null,
  415.                 'extra' => null,
  416.                 'native' => $server_info,
  417.             );
  418.         }
  419.         return $server_info;
  420.     }
  421.  
  422. }
  423.  
  424. ?>

Documentation generated on Thu, 25 Dec 2008 15:00:32 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.