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. {
  58.     // {{{ createDatabase()
  59.  
  60.     /**
  61.      * create a new database
  62.      *
  63.      * @param string $name name of the database that should be created
  64.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  65.      * @access public
  66.      */
  67.     function createDatabase($name)
  68.     {
  69.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  70.         $database_file $db->_getDatabaseFile($name);
  71.         if (file_exists($database_file)) {
  72.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  73.                 'createDatabase: database already exists');
  74.         }
  75.         $php_errormsg '';
  76.         $handle @sqlite_open($database_file$db->dsn['mode']$php_errormsg);
  77.         if (!$handle{
  78.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  79.                 'createDatabase: '.(isset($php_errormsg$php_errormsg 'could not create the database file'));
  80.         }
  81.         @sqlite_close($handle);
  82.         return MDB2_OK;
  83.     }
  84.  
  85.     // }}}
  86.     // {{{ dropDatabase()
  87.  
  88.     /**
  89.      * drop an existing database
  90.      *
  91.      * @param string $name name of the database that should be dropped
  92.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  93.      * @access public
  94.      */
  95.     function dropDatabase($name)
  96.     {
  97.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  98.         $database_file $db->_getDatabaseFile($name);
  99.         if (!@file_exists($database_file)) {
  100.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  101.                 'dropDatabase: database does not exist');
  102.         }
  103.         $result @unlink($database_file);
  104.         if (!$result{
  105.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  106.                 'dropDatabase: '.(isset($php_errormsg$php_errormsg 'could not remove the database file'));
  107.         }
  108.         return MDB2_OK;
  109.     }
  110.  
  111.     // }}}
  112.     // {{{ listDatabases()
  113.  
  114.     /**
  115.      * list all databases
  116.      *
  117.      * @return mixed data array on success, a MDB2 error on failure
  118.      * @access public
  119.      */
  120.     function listDatabases()
  121.     {
  122.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  123.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  124.             'listDatabases: list databases is not supported');
  125.     }
  126.  
  127.     // }}}
  128.     // {{{ listUsers()
  129.  
  130.     /**
  131.      * list all users
  132.      *
  133.      * @return mixed data array on success, a MDB2 error on failure
  134.      * @access public
  135.      */
  136.     function listUsers()
  137.     {
  138.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  139.         return $db->queryCol('SELECT DISTINCT USER FROM USER');
  140.     }
  141.  
  142.     // }}}
  143.     // {{{ listTables()
  144.  
  145.     /**
  146.      * list all tables in the current database
  147.      *
  148.      * @return mixed data array on success, a MDB2 error on failure
  149.      * @access public
  150.      */
  151.     function listTables()
  152.     {
  153.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  154.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  155.         $table_names $db->queryCol($query);
  156.         if (PEAR::isError($table_names)) {
  157.             return $table_names;
  158.         }
  159.         $tables = array();
  160.         for ($i = 0$j count($table_names)$i $j; ++$i{
  161.             if (!$this->_isSequenceName($table_names[$i]))
  162.                 $tables[$table_names[$i];
  163.         }
  164.         return $tables;
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ listTableFields()
  169.  
  170.     /**
  171.      * list all fields in a tables in the current database
  172.      *
  173.      * @param string $table name of table that should be used in method
  174.      * @return mixed data array on success, a MDB2 error on failure
  175.      * @access public
  176.      */
  177.     function listTableFields($table)
  178.     {
  179.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  180.         $query = "SELECT * FROM $table";
  181.         $db->setLimit(1);
  182.         $result $db->query($query);
  183.         if (PEAR::isError($result)) {
  184.             return $result;
  185.         }
  186.         $columns $result->getColumnNames();
  187.         $result->free();
  188.         if (PEAR::isError($columns)) {
  189.             return $columns;
  190.         }
  191.         return array_flip($columns);
  192.     }
  193.  
  194.     // }}}
  195.     // {{{ createIndex()
  196.  
  197.     /**
  198.      * get the stucture of a field into an array
  199.      *
  200.      * @param string    $table         name of the table on which the index is to be created
  201.      * @param string    $name         name of the index to be created
  202.      * @param array     $definition        associative array that defines properties of the index to be created.
  203.      *                                  Currently, only one property named FIELDS is supported. This property
  204.      *                                  is also an associative with the names of the index fields as array
  205.      *                                  indexes. Each entry of this array is set to another type of associative
  206.      *                                  array that specifies properties of the index that are specific to
  207.      *                                  each field.
  208.      *
  209.      *                                 Currently, only the sorting property is supported. It should be used
  210.      *                                  to define the sorting direction of the index. It may be set to either
  211.      *                                  ascending or descending.
  212.      *
  213.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  214.      *                                  drivers of those that do not support it ignore this property. Use the
  215.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  216.  
  217.      *                                  Example
  218.      *                                     array(
  219.      *                                         'fields' => array(
  220.      *                                             'user_name' => array(
  221.      *                                                 'sorting' => 'ascending'
  222.      *                                             ),
  223.      *                                             'last_login' => array()
  224.      *                                         )
  225.      *                                     )
  226.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  227.      * @access public
  228.      */
  229.     function createIndex($table$name$definition)
  230.     {
  231.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  232.         $query 'CREATE '.(isset($definition['unique']'UNIQUE' '')." INDEX $name ON $table (";
  233.         $skipped_first = false;
  234.         foreach ($definition['fields'as $field_name => $field{
  235.             if ($skipped_first{
  236.                 $query .= ',';
  237.             }
  238.             $query .= $field_name;
  239.             $skipped_first = true;
  240.         }
  241.         $query .= ')';
  242.         return $db->query($query);
  243.     }
  244.  
  245.     // }}}
  246.     // {{{ dropIndex()
  247.  
  248.     /**
  249.      * drop existing index
  250.      *
  251.      * @param string    $table         name of table that should be used in method
  252.      * @param string    $name         name of the index to be dropped
  253.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  254.      * @access public
  255.      */
  256.     function dropIndex($table$name)
  257.     {
  258.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  259.         return $db->query("DROP INDEX $name");
  260.     }
  261.  
  262.     // }}}
  263.     // {{{ listTableIndexes()
  264.  
  265.     /**
  266.      * list all indexes in a table
  267.      *
  268.      * @param string    $table      name of table that should be used in method
  269.      * @return mixed data array on success, a MDB2 error on failure
  270.      * @access public
  271.      */
  272.     function listTableIndexes($table)
  273.     {
  274.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  275.         $query = "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  276.         $indexes_all $db->queryCol($query);
  277.         if (PEAR::isError($indexes_all)) {
  278.             return $indexes_all;
  279.         }
  280.         $found $indexes = array();
  281.         foreach ($indexes_all as $index => $index_name{
  282.             if ($indexes_all[$index!= 'PRIMARY' && !isset($found[$index_name])) {
  283.                 $indexes[$index_name;
  284.                 $found[$index_name= true;
  285.             }
  286.         }
  287.         return $indexes;
  288.     }
  289.  
  290.     // }}}
  291.     // {{{ createSequence()
  292.  
  293.     /**
  294.      * create sequence
  295.      *
  296.      * @param string    $seq_name     name of the sequence to be created
  297.      * @param string    $start         start value of the sequence; default is 1
  298.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  299.      * @access public
  300.      */
  301.     function createSequence($seq_name$start = 1)
  302.     {
  303.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  304.         $sequence_name $db->getSequenceName($seq_name);
  305.         $seqcol_name $db->options['seqcol_name'];
  306.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER PRIMARY KEY DEFAULT 0 NOT NULL)";
  307.         $res $db->query($query);
  308.         if (PEAR::isError($res)) {
  309.             return $res;
  310.         }
  311.         if ($start == 1{
  312.             return MDB2_OK;
  313.         }
  314.         $res $db->query("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  315.         if (!PEAR::isError($res)) {
  316.             return MDB2_OK;
  317.         }
  318.         // Handle error
  319.         $result $db->query("DROP TABLE $sequence_name");
  320.         if (PEAR::isError($result)) {
  321.             return $db->raiseError(MDB2_ERRORnullnull,
  322.                 'createSequence: could not drop inconsistent sequence table ('.
  323.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  324.         }
  325.         return $db->raiseError(MDB2_ERRORnullnull,
  326.             'createSequence: could not create sequence table ('.
  327.             $res->getMessage().' ('.$res->getUserinfo().'))');
  328.     }
  329.  
  330.     // }}}
  331.     // {{{ dropSequence()
  332.  
  333.     /**
  334.      * drop existing sequence
  335.      *
  336.      * @param string    $seq_name     name of the sequence to be dropped
  337.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  338.      * @access public
  339.      */
  340.     function dropSequence($seq_name)
  341.     {
  342.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  343.         $sequence_name $db->getSequenceName($seq_name);
  344.         return $db->query("DROP TABLE $sequence_name");
  345.     }
  346.  
  347.     // }}}
  348.     // {{{ listSequences()
  349.  
  350.     /**
  351.      * list all sequences in the current database
  352.      *
  353.      * @return mixed data array on success, a MDB2 error on failure
  354.      * @access public
  355.      */
  356.     function listSequences()
  357.     {
  358.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  359.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  360.         $table_names $db->queryCol($query);
  361.         if (PEAR::isError($table_names)) {
  362.             return $table_names;
  363.         }
  364.         $sequences = array();
  365.         for ($i = 0$j count($table_names)$i $j; ++$i{
  366.             if ($sqn $this->_isSequenceName($table_names[$i]))
  367.                 $sequences[$sqn;
  368.         }
  369.         return $sequences;
  370.     }
  371.  
  372.     // }}}
  373. }
  374. ?>

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