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

Source for file sqlite.php

Documentation is available at sqlite.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: sqlite.php,v 1.17 2005/04/16 22:27:48 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. /**
  51.  * MDB2 SQLite driver for the management modules
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@backendmedia.com>
  56.  */
  57. class MDB2_Driver_Manager_sqlite extends MDB2_Driver_Manager_Common
  58. {
  59.     // {{{ createDatabase()
  60.  
  61.     /**
  62.      * create a new database
  63.      *
  64.      * @param string $name name of the database that should be created
  65.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function createDatabase($name)
  69.     {
  70.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  71.         $database_file $db->_getDatabaseFile($name);
  72.         if (file_exists($database_file)) {
  73.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  74.                 'createDatabase: database already exists');
  75.         }
  76.         $php_errormsg '';
  77.         $handle @sqlite_open($database_file$db->dsn['mode']$php_errormsg);
  78.         if (!$handle{
  79.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  80.                 'createDatabase: '.(isset($php_errormsg$php_errormsg 'could not create the database file'));
  81.         }
  82.         @sqlite_close($handle);
  83.         return MDB2_OK;
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ dropDatabase()
  88.  
  89.     /**
  90.      * drop an existing database
  91.      *
  92.      * @param string $name name of the database that should be dropped
  93.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  94.      * @access public
  95.      */
  96.     function dropDatabase($name)
  97.     {
  98.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  99.         $database_file $db->_getDatabaseFile($name);
  100.         if (!@file_exists($database_file)) {
  101.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  102.                 'dropDatabase: database does not exist');
  103.         }
  104.         $result @unlink($database_file);
  105.         if (!$result{
  106.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  107.                 'dropDatabase: '.(isset($php_errormsg$php_errormsg 'could not remove the database file'));
  108.         }
  109.         return MDB2_OK;
  110.     }
  111.  
  112.     // }}}
  113.     // {{{ listDatabases()
  114.  
  115.     /**
  116.      * list all databases
  117.      *
  118.      * @return mixed data array on success, a MDB2 error on failure
  119.      * @access public
  120.      */
  121.     function listDatabases()
  122.     {
  123.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  124.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  125.             'listDatabases: list databases is not supported');
  126.     }
  127.  
  128.     // }}}
  129.     // {{{ listUsers()
  130.  
  131.     /**
  132.      * list all users
  133.      *
  134.      * @return mixed data array on success, a MDB2 error on failure
  135.      * @access public
  136.      */
  137.     function listUsers()
  138.     {
  139.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  140.         return $db->queryCol('SELECT DISTINCT USER FROM USER');
  141.     }
  142.  
  143.     // }}}
  144.     // {{{ listTables()
  145.  
  146.     /**
  147.      * list all tables in the current database
  148.      *
  149.      * @return mixed data array on success, a MDB2 error on failure
  150.      * @access public
  151.      */
  152.     function listTables()
  153.     {
  154.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  155.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  156.         $table_names $db->queryCol($query);
  157.         if (PEAR::isError($table_names)) {
  158.             return $table_names;
  159.         }
  160.         $tables = array();
  161.         for ($i = 0$j count($table_names)$i $j; ++$i{
  162.             if (!$this->_isSequenceName($table_names[$i]))
  163.                 $tables[$table_names[$i];
  164.         }
  165.         return $tables;
  166.     }
  167.  
  168.     // }}}
  169.     // {{{ listTableFields()
  170.  
  171.     /**
  172.      * list all fields in a tables in the current database
  173.      *
  174.      * @param string $table name of table that should be used in method
  175.      * @return mixed data array on success, a MDB2 error on failure
  176.      * @access public
  177.      */
  178.     function listTableFields($table)
  179.     {
  180.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  181.         $query = "SELECT * FROM $table";
  182.         $db->setLimit(1);
  183.         $result $db->query($query);
  184.         if (PEAR::isError($result)) {
  185.             return $result;
  186.         }
  187.         $columns $result->getColumnNames();
  188.         $result->free();
  189.         if (PEAR::isError($columns)) {
  190.             return $columns;
  191.         }
  192.         return array_flip($columns);
  193.     }
  194.  
  195.     // }}}
  196.     // {{{ createIndex()
  197.  
  198.     /**
  199.      * get the stucture of a field into an array
  200.      *
  201.      * @param string    $table         name of the table on which the index is to be created
  202.      * @param string    $name         name of the index to be created
  203.      * @param array     $definition        associative array that defines properties of the index to be created.
  204.      *                                  Currently, only one property named FIELDS is supported. This property
  205.      *                                  is also an associative with the names of the index fields as array
  206.      *                                  indexes. Each entry of this array is set to another type of associative
  207.      *                                  array that specifies properties of the index that are specific to
  208.      *                                  each field.
  209.      *
  210.      *                                 Currently, only the sorting property is supported. It should be used
  211.      *                                  to define the sorting direction of the index. It may be set to either
  212.      *                                  ascending or descending.
  213.      *
  214.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  215.      *                                  drivers of those that do not support it ignore this property. Use the
  216.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  217.  
  218.      *                                  Example
  219.      *                                     array(
  220.      *                                         'fields' => array(
  221.      *                                             'user_name' => array(
  222.      *                                                 'sorting' => 'ascending'
  223.      *                                             ),
  224.      *                                             'last_login' => array()
  225.      *                                         )
  226.      *                                     )
  227.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  228.      * @access public
  229.      */
  230.     function createIndex($table$name$definition)
  231.     {
  232.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  233.         $query 'CREATE '.(isset($definition['unique']'UNIQUE' '')." INDEX $name ON $table (";
  234.         $skipped_first = false;
  235.         foreach ($definition['fields'as $field_name => $field{
  236.             if ($skipped_first{
  237.                 $query .= ',';
  238.             }
  239.             $query .= $field_name;
  240.             $skipped_first = true;
  241.         }
  242.         $query .= ')';
  243.         return $db->query($query);
  244.     }
  245.  
  246.     // }}}
  247.     // {{{ dropIndex()
  248.  
  249.     /**
  250.      * drop existing index
  251.      *
  252.      * @param string    $table         name of table that should be used in method
  253.      * @param string    $name         name of the index to be dropped
  254.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  255.      * @access public
  256.      */
  257.     function dropIndex($table$name)
  258.     {
  259.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  260.         return $db->query("DROP INDEX $name");
  261.     }
  262.  
  263.     // }}}
  264.     // {{{ listTableIndexes()
  265.  
  266.     /**
  267.      * list all indexes in a table
  268.      *
  269.      * @param string    $table      name of table that should be used in method
  270.      * @return mixed data array on success, a MDB2 error on failure
  271.      * @access public
  272.      */
  273.     function listTableIndexes($table)
  274.     {
  275.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  276.         $query = "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  277.         $indexes_all $db->queryCol($query);
  278.         if (PEAR::isError($indexes_all)) {
  279.             return $indexes_all;
  280.         }
  281.         $found $indexes = array();
  282.         foreach ($indexes_all as $index => $index_name{
  283.             if ($indexes_all[$index!= 'PRIMARY' && !isset($found[$index_name])) {
  284.                 $indexes[$index_name;
  285.                 $found[$index_name= true;
  286.             }
  287.         }
  288.         return $indexes;
  289.     }
  290.  
  291.     // }}}
  292.     // {{{ createSequence()
  293.  
  294.     /**
  295.      * create sequence
  296.      *
  297.      * @param string    $seq_name     name of the sequence to be created
  298.      * @param string    $start         start value of the sequence; default is 1
  299.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  300.      * @access public
  301.      */
  302.     function createSequence($seq_name$start = 1)
  303.     {
  304.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  305.         $sequence_name $db->getSequenceName($seq_name);
  306.         $seqcol_name $db->options['seqcol_name'];
  307.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER PRIMARY KEY DEFAULT 0 NOT NULL)";
  308.         $res $db->query($query);
  309.         if (PEAR::isError($res)) {
  310.             return $res;
  311.         }
  312.         if ($start == 1{
  313.             return MDB2_OK;
  314.         }
  315.         $res $db->query("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  316.         if (!PEAR::isError($res)) {
  317.             return MDB2_OK;
  318.         }
  319.         // Handle error
  320.         $result $db->query("DROP TABLE $sequence_name");
  321.         if (PEAR::isError($result)) {
  322.             return $db->raiseError(MDB2_ERRORnullnull,
  323.                 'createSequence: could not drop inconsistent sequence table ('.
  324.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  325.         }
  326.         return $db->raiseError(MDB2_ERRORnullnull,
  327.             'createSequence: could not create sequence table ('.
  328.             $res->getMessage().' ('.$res->getUserinfo().'))');
  329.     }
  330.  
  331.     // }}}
  332.     // {{{ dropSequence()
  333.  
  334.     /**
  335.      * drop existing sequence
  336.      *
  337.      * @param string    $seq_name     name of the sequence to be dropped
  338.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  339.      * @access public
  340.      */
  341.     function dropSequence($seq_name)
  342.     {
  343.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  344.         $sequence_name $db->getSequenceName($seq_name);
  345.         return $db->query("DROP TABLE $sequence_name");
  346.     }
  347.  
  348.     // }}}
  349.     // {{{ listSequences()
  350.  
  351.     /**
  352.      * list all sequences in the current database
  353.      *
  354.      * @return mixed data array on success, a MDB2 error on failure
  355.      * @access public
  356.      */
  357.     function listSequences()
  358.     {
  359.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  360.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  361.         $table_names $db->queryCol($query);
  362.         if (PEAR::isError($table_names)) {
  363.             return $table_names;
  364.         }
  365.         $sequences = array();
  366.         for ($i = 0$j count($table_names)$i $j; ++$i{
  367.             if ($sqn $this->_isSequenceName($table_names[$i]))
  368.                 $sequences[$sqn;
  369.         }
  370.         return $sequences;
  371.     }
  372.  
  373.     // }}}
  374. }
  375. ?>

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