home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / DB / Table / Manager / sqlite.php < prev   
Encoding:
PHP Script  |  2008-07-02  |  16.1 KB  |  426 lines

  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.5
  73.  * @link     http://pear.php.net/package/DB_Table
  74.  */
  75. class DB_Table_Manager_sqlite {
  76.  
  77.     /**
  78.     * 
  79.     * The PEAR DB object that connects to the database.
  80.     * 
  81.     * @access private
  82.     * 
  83.     * @var object
  84.     * 
  85.     */
  86.     
  87.     var $_db = null;
  88.  
  89.  
  90.     /**
  91.      * list all indexes in a table
  92.      *
  93.      * @param string    $table      name of table that should be used in method
  94.      * @return mixed data array on success, a PEAR error on failure
  95.      * @access public
  96.      */
  97.     function listTableIndexes($table)
  98.     {
  99.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  100.         $query.= "LOWER(tbl_name)='".strtolower($table)."'";
  101.         $query.= " AND sql NOT NULL ORDER BY name";
  102.         $indexes = $this->_db->getCol($query);
  103.         if (PEAR::isError($indexes)) {
  104.             return $indexes;
  105.         }
  106.  
  107.         $result = array();
  108.         foreach ($indexes as $sql) {
  109.             if (preg_match("/^create index ([^ ]*) on /i", $sql, $tmp)) {
  110.                 $result[$tmp[1]] = true;
  111.             }
  112.         }
  113.         $result = array_change_key_case($result, CASE_LOWER);
  114.  
  115.         return array_keys($result);
  116.     }
  117.  
  118.  
  119.     /**
  120.      * list all constraints in a table
  121.      *
  122.      * @param string    $table      name of table that should be used in method
  123.      * @return mixed data array on success, a PEAR error on failure
  124.      * @access public
  125.      */
  126.     function listTableConstraints($table)
  127.     {
  128.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  129.         $query.= "LOWER(tbl_name)='".strtolower($table)."'";
  130.         $query.= " AND sql NOT NULL ORDER BY name";
  131.         $indexes = $this->_db->getCol($query);
  132.         if (PEAR::isError($indexes)) {
  133.             return $indexes;
  134.         }
  135.  
  136.         $result = array();
  137.         foreach ($indexes as $sql) {
  138.             if (preg_match("/^create unique index ([^ ]*) on /i", $sql, $tmp)) {
  139.                 $result[$tmp[1]] = true;
  140.             }
  141.         }
  142.         $result = array_change_key_case($result, CASE_LOWER);
  143.  
  144.         return array_keys($result);
  145.     }
  146.  
  147.  
  148.     /**
  149.      * get the structure of an index into an array
  150.      *
  151.      * @param string    $table      name of table that should be used in method
  152.      * @param string    $index_name name of index that should be used in method
  153.      * @return mixed data array on success, a PEAR error on failure
  154.      * @access public
  155.      */
  156.     function getTableIndexDefinition($table, $index_name)
  157.     {
  158.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  159.         $query.= "LOWER(name)='".strtolower($index_name)."' AND LOWER(tbl_name)='".strtolower($table)."'";
  160.         $query.= " AND sql NOT NULL ORDER BY name";
  161.         $sql = $this->_db->getOne($query);
  162.         if (PEAR::isError($sql)) {
  163.             return $sql;
  164.         }
  165.  
  166.         $sql = strtolower($sql);
  167.         $start_pos = strpos($sql, '(');
  168.         $end_pos = strrpos($sql, ')');
  169.         $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  170.         $column_names = split(',', $column_names);
  171.  
  172.         $definition = array();
  173.         $count = count($column_names);
  174.         for ($i=0; $i<$count; ++$i) {
  175.             $column_name = strtok($column_names[$i]," ");
  176.             $collation = strtok(" ");
  177.             $definition['fields'][$column_name] = array();
  178.             if (!empty($collation)) {
  179.                 $definition['fields'][$column_name]['sorting'] =
  180.                     ($collation=='ASC' ? 'ascending' : 'descending');
  181.             }
  182.         }
  183.  
  184.         return $definition;
  185.     }
  186.  
  187.  
  188.     /**
  189.      * get the structure of a constraint into an array
  190.      *
  191.      * @param string    $table      name of table that should be used in method
  192.      * @param string    $index_name name of index that should be used in method
  193.      * @return mixed data array on success, a PEAR error on failure
  194.      * @access public
  195.      */
  196.     function getTableConstraintDefinition($table, $index_name)
  197.     {
  198.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  199.         $query.= "LOWER(name)='".strtolower($index_name)."' AND LOWER(tbl_name)='".strtolower($table)."'";
  200.         $query.= " AND sql NOT NULL ORDER BY name";
  201.         $sql = $this->_db->getOne($query);
  202.         if (PEAR::isError($sql)) {
  203.             return $sql;
  204.         }
  205.  
  206.         $sql = strtolower($sql);
  207.         $start_pos = strpos($sql, '(');
  208.         $end_pos = strrpos($sql, ')');
  209.         $column_names = substr($sql, $start_pos+1, $end_pos-$start_pos-1);
  210.         $column_names = split(',', $column_names);
  211.  
  212.         $definition = array();
  213.         $definition['unique'] = true;
  214.         $count = count($column_names);
  215.         for ($i=0; $i<$count; ++$i) {
  216.             $column_name = strtok($column_names[$i]," ");
  217.             $collation = strtok(" ");
  218.             $definition['fields'][$column_name] = array();
  219.             if (!empty($collation)) {
  220.                 $definition['fields'][$column_name]['sorting'] =
  221.                     ($collation=='ASC' ? 'ascending' : 'descending');
  222.             }
  223.         }
  224.  
  225.         return $definition;
  226.     }
  227.  
  228.     
  229.     /**
  230.      * drop existing index
  231.      *
  232.      * @param string    $table         name of table that should be used in method
  233.      * @param string    $name         name of the index to be dropped
  234.      * @return mixed DB_OK on success, a PEAR error on failure
  235.      * @access public
  236.      */
  237.     function dropIndex($table, $name)
  238.     {
  239.         return $this->_db->query("DROP INDEX $name");
  240.     }
  241.  
  242.  
  243.     /**
  244.      * drop existing constraint
  245.      *
  246.      * @param string    $table         name of table that should be used in method
  247.      * @param string    $name         name of the constraint to be dropped
  248.      * @return mixed DB_OK on success, a PEAR error on failure
  249.      * @access public
  250.      */
  251.     function dropConstraint($table, $name)
  252.     {
  253.         if (strtolower($name) == 'primary') {
  254.             return DB_Table::throwError(
  255.                 DB_TABLE_ERR_ALTER_INDEX_IMPOS,
  256.                 '(primary)'
  257.             );
  258.         }
  259.  
  260.         return $this->_db->query("DROP INDEX $name");
  261.     }
  262.  
  263.  
  264.     /**
  265.      * alter an existing table
  266.      *
  267.      * @param string $name         name of the table that is intended to be changed.
  268.      * @param array $changes     associative array that contains the details of each type
  269.      *                             of change that is intended to be performed. The types of
  270.      *                             changes that are currently supported are defined as follows:
  271.      *
  272.      *                             name
  273.      *
  274.      *                                New name for the table.
  275.      *
  276.      *                            add
  277.      *
  278.      *                                Associative array with the names of fields to be added as
  279.      *                                 indexes of the array. The value of each entry of the array
  280.      *                                 should be set to another associative array with the properties
  281.      *                                 of the fields to be added. The properties of the fields should
  282.      *                                 be the same as defined by the Metabase parser.
  283.      *
  284.      *
  285.      *                            remove
  286.      *
  287.      *                                Associative array with the names of fields to be removed as indexes
  288.      *                                 of the array. Currently the values assigned to each entry are ignored.
  289.      *                                 An empty array should be used for future compatibility.
  290.      *
  291.      *                            rename
  292.      *
  293.      *                                Associative array with the names of fields to be renamed as indexes
  294.      *                                 of the array. The value of each entry of the array should be set to
  295.      *                                 another associative array with the entry named name with the new
  296.      *                                 field name and the entry named Declaration that is expected to contain
  297.      *                                 the portion of the field declaration already in DBMS specific SQL code
  298.      *                                 as it is used in the CREATE TABLE statement.
  299.      *
  300.      *                            change
  301.      *
  302.      *                                Associative array with the names of the fields to be changed as indexes
  303.      *                                 of the array. Keep in mind that if it is intended to change either the
  304.      *                                 name of a field and any other properties, the change array entries
  305.      *                                 should have the new names of the fields as array indexes.
  306.      *
  307.      *                                The value of each entry of the array should be set to another associative
  308.      *                                 array with the properties of the fields to that are meant to be changed as
  309.      *                                 array entries. These entries should be assigned to the new values of the
  310.      *                                 respective properties. The properties of the fields should be the same
  311.      *                                 as defined by the Metabase parser.
  312.      *
  313.      *                            Example
  314.      *                                array(
  315.      *                                    'name' => 'userlist',
  316.      *                                    'add' => array(
  317.      *                                        'quota' => array(
  318.      *                                            'type' => 'integer',
  319.      *                                            'unsigned' => 1
  320.      *                                        )
  321.      *                                    ),
  322.      *                                    'remove' => array(
  323.      *                                        'file_limit' => array(),
  324.      *                                        'time_limit' => array()
  325.      *                                    ),
  326.      *                                    'change' => array(
  327.      *                                        'name' => array(
  328.      *                                            'length' => '20',
  329.      *                                            'definition' => array(
  330.      *                                                'type' => 'text',
  331.      *                                                'length' => 20,
  332.      *                                            ),
  333.      *                                        )
  334.      *                                    ),
  335.      *                                    'rename' => array(
  336.      *                                        'sex' => array(
  337.      *                                            'name' => 'gender',
  338.      *                                            'definition' => array(
  339.      *                                                'type' => 'text',
  340.      *                                                'length' => 1,
  341.      *                                                'default' => 'M',
  342.      *                                            ),
  343.      *                                        )
  344.      *                                    )
  345.      *                                )
  346.      *
  347.      * @param boolean $check     (ignored in DB_Table)
  348.      * @access public
  349.      *
  350.      * @return mixed DB_OK on success, a PEAR error on failure
  351.      */
  352.     function alterTable($name, $changes, $check)
  353.     {
  354.         $version = $this->_getServerVersion();
  355.         foreach ($changes as $change_name => $change) {
  356.             switch ($change_name) {
  357.             case 'add':
  358.                 if ($version['major'] >= 3 && $version['minor'] >= 1) {
  359.                     break;
  360.                 }
  361.             case 'name':
  362.                 if ($version['major'] >= 3 && $version['minor'] >= 1) {
  363.                     break;
  364.                 }
  365.             case 'remove':
  366.             case 'change':
  367.             case 'rename':
  368.             default:
  369.                 return DB_Table::throwError(DB_TABLE_ERR_ALTER_TABLE_IMPOS);
  370.             }
  371.         }
  372.  
  373.         $query = '';
  374.         if (array_key_exists('name', $changes)) {
  375.             $change_name = $this->_db->quoteIdentifier($changes['name']);
  376.             $query .= 'RENAME TO ' . $change_name;
  377.         }
  378.  
  379.         if (array_key_exists('add', $changes)) {
  380.             foreach ($changes['add'] as $field_name => $field) {
  381.                 if ($query) {
  382.                     $query.= ', ';
  383.                 }
  384.                 $query.= 'ADD COLUMN ' . $field_name . ' ' . $field;
  385.             }
  386.         }
  387.  
  388.         if (!$query) {
  389.             return DB_OK;
  390.         }
  391.  
  392.         $name = $this->_db->quoteIdentifier($name);
  393.         return $this->_db->query("ALTER TABLE $name $query");
  394.     }
  395.  
  396.  
  397.     /**
  398.      * return version information about the server
  399.      *
  400.      * @param string     $native  determines if the raw version string should be returned
  401.      * @return mixed array/string with version information or MDB2 error object
  402.      * @access private
  403.      */
  404.     function _getServerVersion($native = false)
  405.     {
  406.         if (!function_exists('sqlite_libversion')) {
  407.             return 0;  // error
  408.         }
  409.         $server_info = sqlite_libversion();
  410.         if (!$native) {
  411.             $tmp = explode('.', $server_info, 3);
  412.             $server_info = array(
  413.                 'major' => isset($tmp[0]) ? $tmp[0] : null,
  414.                 'minor' => isset($tmp[1]) ? $tmp[1] : null,
  415.                 'patch' => isset($tmp[2]) ? $tmp[2] : null,
  416.                 'extra' => null,
  417.                 'native' => $server_info,
  418.             );
  419.         }
  420.         return $server_info;
  421.     }
  422.  
  423. }
  424.  
  425. ?>
  426.