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@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: sqlite.php,v 1.43 2005/12/27 10:25:20 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@pooteeweet.org>
  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 =$this->getDBInstance();
  71.         if (PEAR::isError($db)) {
  72.             return $db;
  73.         }
  74.  
  75.         $database_file $db->_getDatabaseFile($name);
  76.         if (file_exists($database_file)) {
  77.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  78.                 'createDatabase: database already exists');
  79.         }
  80.         $php_errormsg '';
  81.         $handle @sqlite_open($database_file$db->dsn['mode']$php_errormsg);
  82.         if (!$handle{
  83.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  84.                 'createDatabase: '.(isset($php_errormsg$php_errormsg 'could not create the database file'));
  85.         }
  86.         @sqlite_close($handle);
  87.         return MDB2_OK;
  88.     }
  89.  
  90.     // }}}
  91.     // {{{ dropDatabase()
  92.  
  93.     /**
  94.      * drop an existing database
  95.      *
  96.      * @param string $name name of the database that should be dropped
  97.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  98.      * @access public
  99.      */
  100.     function dropDatabase($name)
  101.     {
  102.         $db =$this->getDBInstance();
  103.         if (PEAR::isError($db)) {
  104.             return $db;
  105.         }
  106.  
  107.         $database_file $db->_getDatabaseFile($name);
  108.         if (!@file_exists($database_file)) {
  109.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  110.                 'dropDatabase: database does not exist');
  111.         }
  112.         $result @unlink($database_file);
  113.         if (!$result{
  114.             return $db->raiseError(MDB2_ERROR_CANNOT_DROPnullnull,
  115.                 'dropDatabase: '.(isset($php_errormsg$php_errormsg 'could not remove the database file'));
  116.         }
  117.         return MDB2_OK;
  118.     }
  119.  
  120.     // }}}
  121.     // {{{ alterTable()
  122.  
  123.     /**
  124.      * alter an existing table
  125.      *
  126.      * @param string $name         name of the table that is intended to be changed.
  127.      * @param array $changes     associative array that contains the details of each type
  128.      *                              of change that is intended to be performed. The types of
  129.      *                              changes that are currently supported are defined as follows:
  130.      *
  131.      *                              name
  132.      *
  133.      *                                 New name for the table.
  134.      *
  135.      *                             add
  136.      *
  137.      *                                 Associative array with the names of fields to be added as
  138.      *                                  indexes of the array. The value of each entry of the array
  139.      *                                  should be set to another associative array with the properties
  140.      *                                  of the fields to be added. The properties of the fields should
  141.      *                                  be the same as defined by the Metabase parser.
  142.      *
  143.      *
  144.      *                             remove
  145.      *
  146.      *                                 Associative array with the names of fields to be removed as indexes
  147.      *                                  of the array. Currently the values assigned to each entry are ignored.
  148.      *                                  An empty array should be used for future compatibility.
  149.      *
  150.      *                             rename
  151.      *
  152.      *                                 Associative array with the names of fields to be renamed as indexes
  153.      *                                  of the array. The value of each entry of the array should be set to
  154.      *                                  another associative array with the entry named name with the new
  155.      *                                  field name and the entry named Declaration that is expected to contain
  156.      *                                  the portion of the field declaration already in DBMS specific SQL code
  157.      *                                  as it is used in the CREATE TABLE statement.
  158.      *
  159.      *                             change
  160.      *
  161.      *                                 Associative array with the names of the fields to be changed as indexes
  162.      *                                  of the array. Keep in mind that if it is intended to change either the
  163.      *                                  name of a field and any other properties, the change array entries
  164.      *                                  should have the new names of the fields as array indexes.
  165.      *
  166.      *                                 The value of each entry of the array should be set to another associative
  167.      *                                  array with the properties of the fields to that are meant to be changed as
  168.      *                                  array entries. These entries should be assigned to the new values of the
  169.      *                                  respective properties. The properties of the fields should be the same
  170.      *                                  as defined by the Metabase parser.
  171.      *
  172.      *                             Example
  173.      *                                 array(
  174.      *                                     'name' => 'userlist',
  175.      *                                     'add' => array(
  176.      *                                         'quota' => array(
  177.      *                                             'type' => 'integer',
  178.      *                                             'unsigned' => 1
  179.      *                                         )
  180.      *                                     ),
  181.      *                                     'remove' => array(
  182.      *                                         'file_limit' => array(),
  183.      *                                         'time_limit' => array()
  184.      *                                     ),
  185.      *                                     'change' => array(
  186.      *                                         'name' => array(
  187.      *                                             'length' => '20',
  188.      *                                             'definition' => array(
  189.      *                                                 'type' => 'text',
  190.      *                                                 'length' => 20,
  191.      *                                             ),
  192.      *                                         )
  193.      *                                     ),
  194.      *                                     'rename' => array(
  195.      *                                         'sex' => array(
  196.      *                                             'name' => 'gender',
  197.      *                                             'definition' => array(
  198.      *                                                 'type' => 'text',
  199.      *                                                 'length' => 1,
  200.      *                                                 'default' => 'M',
  201.      *                                             ),
  202.      *                                         )
  203.      *                                     )
  204.      *                                 )
  205.      *
  206.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  207.      *                              can perform the requested table alterations if the value is true or
  208.      *                              actually perform them otherwise.
  209.      * @access public
  210.      *
  211.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  212.      */
  213.     function alterTable($name$changes$check)
  214.     {
  215.         $db =$this->getDBInstance();
  216.         if (PEAR::isError($db)) {
  217.             return $db;
  218.         }
  219.  
  220.         // actually sqlite 2.x supports no ALTER TABLE at all ..
  221.         // so the only solution is:
  222.         // - reverse engineer table schema
  223.         // - alter table schema in memory
  224.         // - read all data into memory (or file?)
  225.         // - drop table
  226.         // - create table
  227.         // - import data
  228.         $version $db->getServerVersion();
  229.         foreach ($changes as $change_name => $change{
  230.             switch ($change_name{
  231.             case 'add':
  232.                 if ($version['major'>= 3 && $version['minor'>= 1{
  233.                     break;
  234.                 }
  235.             case 'name':
  236.                 if ($version['major'>= 3 && $version['minor'>= 1{
  237.                     break;
  238.                 }
  239.             case 'remove':
  240.             case 'change':
  241.             case 'rename':
  242.             default:
  243.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  244.                     'alterTable: change type "'.$change_name.'" not yet supported');
  245.             }
  246.         }
  247.  
  248.         if ($check{
  249.             return MDB2_OK;
  250.         }
  251.  
  252.         $query '';
  253.         if (array_key_exists('name'$changes)) {
  254.             $change_name $db->quoteIdentifier($changes['name']true);
  255.             $query .= 'RENAME TO ' $change_name;
  256.         }
  257.  
  258.         if (array_key_exists('add'$changes)) {
  259.             foreach ($changes['add'as $field_name => $field{
  260.                 if ($query{
  261.                     $query.= ', ';
  262.                 }
  263.                 $query.= 'ADD COLUMN ' $db->getDeclaration($field['type']$field_name$field);
  264.             }
  265.         }
  266.  
  267.         if (!$query{
  268.             return MDB2_OK;
  269.         }
  270.  
  271.         $name $db->quoteIdentifier($nametrue);
  272.         return $db->exec("ALTER TABLE $name $query");
  273.     }
  274.  
  275.  
  276.     // }}}
  277.     // {{{ listDatabases()
  278.  
  279.     /**
  280.      * list all databases
  281.      *
  282.      * @return mixed data array on success, a MDB2 error on failure
  283.      * @access public
  284.      */
  285.     function listDatabases()
  286.     {
  287.         $db =$this->getDBInstance();
  288.         if (PEAR::isError($db)) {
  289.             return $db;
  290.         }
  291.  
  292.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  293.             'listDatabases: list databases is not supported');
  294.     }
  295.  
  296.     // }}}
  297.     // {{{ listUsers()
  298.  
  299.     /**
  300.      * list all users
  301.      *
  302.      * @return mixed data array on success, a MDB2 error on failure
  303.      * @access public
  304.      */
  305.     function listUsers()
  306.     {
  307.         $db =$this->getDBInstance();
  308.         if (PEAR::isError($db)) {
  309.             return $db;
  310.         }
  311.  
  312.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  313.             'listDatabases: list databases is not supported');
  314.     }
  315.  
  316.     // }}}
  317.     // {{{ listTables()
  318.  
  319.     /**
  320.      * list all tables in the current database
  321.      *
  322.      * @return mixed data array on success, a MDB2 error on failure
  323.      * @access public
  324.      */
  325.     function listTables()
  326.     {
  327.         $db =$this->getDBInstance();
  328.         if (PEAR::isError($db)) {
  329.             return $db;
  330.         }
  331.  
  332.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  333.         $table_names $db->queryCol($query);
  334.         if (PEAR::isError($table_names)) {
  335.             return $table_names;
  336.         }
  337.         $result = array();
  338.         for ($i = 0$j count($table_names)$i $j; ++$i{
  339.             if (!$this->_isSequenceName($table_names[$i])) {
  340.                 $result[$table_names[$i];
  341.             }
  342.         }
  343.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  344.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  345.         }
  346.         return $result;
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ listTableFields()
  351.  
  352.     /**
  353.      * list all fields in a tables in the current database
  354.      *
  355.      * @param string $table name of table that should be used in method
  356.      * @return mixed data array on success, a MDB2 error on failure
  357.      * @access public
  358.      */
  359.     function listTableFields($table)
  360.     {
  361.         $db =$this->getDBInstance();
  362.         if (PEAR::isError($db)) {
  363.             return $db;
  364.         }
  365.  
  366.         $table $db->quoteIdentifier($tabletrue);
  367.         $query = "SELECT * FROM $table";
  368.         $db->setLimit(1);
  369.         $result2 $db->query($query);
  370.         if (PEAR::isError($result2)) {
  371.             return $result2;
  372.         }
  373.         $result $result2->getColumnNames();
  374.         $result2->free();
  375.         if (PEAR::isError($result)) {
  376.             return $result;
  377.         }
  378.         return array_flip($result);
  379.     }
  380.  
  381.     // }}}
  382.     // {{{ createIndex()
  383.  
  384.     /**
  385.      * get the stucture of a field into an array
  386.      *
  387.      * @param string    $table         name of the table on which the index is to be created
  388.      * @param string    $name         name of the index to be created
  389.      * @param array     $definition        associative array that defines properties of the index to be created.
  390.      *                                  Currently, only one property named FIELDS is supported. This property
  391.      *                                  is also an associative with the names of the index fields as array
  392.      *                                  indexes. Each entry of this array is set to another type of associative
  393.      *                                  array that specifies properties of the index that are specific to
  394.      *                                  each field.
  395.      *
  396.      *                                 Currently, only the sorting property is supported. It should be used
  397.      *                                  to define the sorting direction of the index. It may be set to either
  398.      *                                  ascending or descending.
  399.      *
  400.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  401.      *                                  drivers of those that do not support it ignore this property. Use the
  402.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  403.  
  404.      *                                  Example
  405.      *                                     array(
  406.      *                                         'fields' => array(
  407.      *                                             'user_name' => array(
  408.      *                                                 'sorting' => 'ascending'
  409.      *                                             ),
  410.      *                                             'last_login' => array()
  411.      *                                         )
  412.      *                                     )
  413.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  414.      * @access public
  415.      */
  416.     function createIndex($table$name$definition)
  417.     {
  418.         $db =$this->getDBInstance();
  419.         if (PEAR::isError($db)) {
  420.             return $db;
  421.         }
  422.  
  423.         $table $db->quoteIdentifier($tabletrue);
  424.         $name  $db->getIndexName($name);
  425.         $query = "CREATE INDEX $name ON $table";
  426.         $fields = array();
  427.         foreach ($definition['fields'as $field_name => $field{
  428.             $field_string $field_name;
  429.             if (array_key_exists('sorting'$field)) {
  430.                 switch ($field['sorting']{
  431.                 case 'ascending':
  432.                     $field_string.= ' ASC';
  433.                     break;
  434.                 case 'descending':
  435.                     $field_string.= ' DESC';
  436.                     break;
  437.                 }
  438.             }
  439.             $fields[$field_string;
  440.         }
  441.         $query .= ' ('.implode(', '$fields')';
  442.         return $db->exec($query);
  443.     }
  444.  
  445.     // }}}
  446.     // {{{ dropIndex()
  447.  
  448.     /**
  449.      * drop existing index
  450.      *
  451.      * @param string    $table         name of table that should be used in method
  452.      * @param string    $name         name of the index to be dropped
  453.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  454.      * @access public
  455.      */
  456.     function dropIndex($table$name)
  457.     {
  458.         $db =$this->getDBInstance();
  459.         if (PEAR::isError($db)) {
  460.             return $db;
  461.         }
  462.  
  463.         $name $db->getIndexName($name);
  464.         return $db->exec("DROP INDEX $name");
  465.     }
  466.  
  467.     // }}}
  468.     // {{{ listTableIndexes()
  469.  
  470.     /**
  471.      * list all indexes in a table
  472.      *
  473.      * @param string    $table      name of table that should be used in method
  474.      * @return mixed data array on success, a MDB2 error on failure
  475.      * @access public
  476.      */
  477.     function listTableIndexes($table)
  478.     {
  479.         $db =$this->getDBInstance();
  480.         if (PEAR::isError($db)) {
  481.             return $db;
  482.         }
  483.  
  484.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  485.         $indexes $db->queryCol($query'text');
  486.         if (PEAR::isError($indexes)) {
  487.             return $indexes;
  488.         }
  489.  
  490.         $result = array();
  491.         foreach ($indexes as $sql{
  492.             $sql strtolower($sql);
  493.             if (preg_match("/^create index ([^ ]*) on /"$sql$tmp)) {
  494.                 $index $this->_isIndexName($tmp[1]);
  495.                 $result[$index= true;
  496.             }
  497.         }
  498.  
  499.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  500.             $result array_change_key_case($result$db->options['field_case']);
  501.         }
  502.         return array_keys($result);
  503.     }
  504.  
  505.     // }}}
  506.     // {{{ createConstraint()
  507.  
  508.     /**
  509.      * create a constraint on a table
  510.      *
  511.      * @param string    $table         name of the table on which the constraint is to be created
  512.      * @param string    $name         name of the constraint to be created
  513.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  514.      *                                  Currently, only one property named FIELDS is supported. This property
  515.      *                                  is also an associative with the names of the constraint fields as array
  516.      *                                  constraints. Each entry of this array is set to another type of associative
  517.      *                                  array that specifies properties of the constraint that are specific to
  518.      *                                  each field.
  519.      *
  520.      *                                  Example
  521.      *                                     array(
  522.      *                                         'fields' => array(
  523.      *                                             'user_name' => array(),
  524.      *                                             'last_login' => array()
  525.      *                                         )
  526.      *                                     )
  527.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  528.      * @access public
  529.      */
  530.     function createConstraint($table$name$definition)
  531.     {
  532.         $db =$this->getDBInstance();
  533.         if (PEAR::isError($db)) {
  534.             return $db;
  535.         }
  536.  
  537.         if (array_key_exists('primary'$definition&& $definition['primary']{
  538.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  539.                 'createConstraint: Creating Primary Constraints is not supported');
  540.         }
  541.  
  542.         $table $db->quoteIdentifier($tabletrue);
  543.         $name  $db->getIndexName($name);
  544.         $query = "CREATE UNIQUE INDEX $name ON $table";
  545.         $fields = array();
  546.         foreach ($definition['fields'as $field_name => $field{
  547.             $field_string $field_name;
  548.             if (array_key_exists('sorting'$field)) {
  549.                 switch ($field['sorting']{
  550.                 case 'ascending':
  551.                     $field_string.= ' ASC';
  552.                     break;
  553.                 case 'descending':
  554.                     $field_string.= ' DESC';
  555.                     break;
  556.                 }
  557.             }
  558.             $fields[$field_string;
  559.         }
  560.         $query .= ' ('.implode(', '$fields')';
  561.         return $db->exec($query);
  562.     }
  563.  
  564.     // }}}
  565.     // {{{ dropConstraint()
  566.  
  567.     /**
  568.      * drop existing constraint
  569.      *
  570.      * @param string    $table         name of table that should be used in method
  571.      * @param string    $name         name of the constraint to be dropped
  572.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  573.      * @access public
  574.      */
  575.     function dropConstraint($table$name)
  576.     {
  577.         $db =$this->getDBInstance();
  578.         if (PEAR::isError($db)) {
  579.             return $db;
  580.         }
  581.  
  582.         if ($name == 'PRIMARY'{
  583.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  584.                 'dropConstraints: Dropping Primary Constraints is not supported');
  585.         }
  586.  
  587.         $name $db->getIndexName($name);
  588.         return $db->exec("DROP INDEX $name");
  589.     }
  590.  
  591.     // }}}
  592.     // {{{ listTableConstraints()
  593.  
  594.     /**
  595.      * list all sonstraints in a table
  596.      *
  597.      * @param string    $table      name of table that should be used in method
  598.      * @return mixed data array on success, a MDB2 error on failure
  599.      * @access public
  600.      */
  601.     function listTableConstraints($table)
  602.     {
  603.         $db =$this->getDBInstance();
  604.         if (PEAR::isError($db)) {
  605.             return $db;
  606.         }
  607.  
  608.         $query = "SELECT sql FROM sqlite_master WHERE type='index' AND tbl_name='$table' AND sql NOT NULL ORDER BY name";
  609.         $indexes $db->queryCol($query'text');
  610.         if (PEAR::isError($indexes)) {
  611.             return $indexes;
  612.         }
  613.  
  614.         $result = array();
  615.         foreach ($indexes as $sql{
  616.             $sql strtolower($sql);
  617.             if (preg_match("/^create unique index ([^ ]*) on /"$sql$tmp)) {
  618.                 $index $this->_isIndexName($tmp[1]);
  619.                 $result[$index= true;
  620.             }
  621.         }
  622.  
  623.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  624.             $result array_change_key_case($result$db->options['field_case']);
  625.         }
  626.         return array_keys($result);
  627.     }
  628.  
  629.     // }}}
  630.     // {{{ createSequence()
  631.  
  632.     /**
  633.      * create sequence
  634.      *
  635.      * @param string    $seq_name     name of the sequence to be created
  636.      * @param string    $start         start value of the sequence; default is 1
  637.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  638.      * @access public
  639.      */
  640.     function createSequence($seq_name$start = 1)
  641.     {
  642.         $db =$this->getDBInstance();
  643.         if (PEAR::isError($db)) {
  644.             return $db;
  645.         }
  646.  
  647.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  648.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  649.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER PRIMARY KEY DEFAULT 0 NOT NULL)";
  650.         $res $db->exec($query);
  651.         if (PEAR::isError($res)) {
  652.             return $res;
  653.         }
  654.         if ($start == 1{
  655.             return MDB2_OK;
  656.         }
  657.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  658.         if (!PEAR::isError($res)) {
  659.             return MDB2_OK;
  660.         }
  661.         // Handle error
  662.         $result $db->exec("DROP TABLE $sequence_name");
  663.         if (PEAR::isError($result)) {
  664.             return $db->raiseError(MDB2_ERRORnullnull,
  665.                 'createSequence: could not drop inconsistent sequence table ('.
  666.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  667.         }
  668.         return $db->raiseError(MDB2_ERRORnullnull,
  669.             'createSequence: could not create sequence table ('.
  670.             $res->getMessage().' ('.$res->getUserinfo().'))');
  671.     }
  672.  
  673.     // }}}
  674.     // {{{ dropSequence()
  675.  
  676.     /**
  677.      * drop existing sequence
  678.      *
  679.      * @param string    $seq_name     name of the sequence to be dropped
  680.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  681.      * @access public
  682.      */
  683.     function dropSequence($seq_name)
  684.     {
  685.         $db =$this->getDBInstance();
  686.         if (PEAR::isError($db)) {
  687.             return $db;
  688.         }
  689.  
  690.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  691.         return $db->exec("DROP TABLE $sequence_name");
  692.     }
  693.  
  694.     // }}}
  695.     // {{{ listSequences()
  696.  
  697.     /**
  698.      * list all sequences in the current database
  699.      *
  700.      * @return mixed data array on success, a MDB2 error on failure
  701.      * @access public
  702.      */
  703.     function listSequences()
  704.     {
  705.         $db =$this->getDBInstance();
  706.         if (PEAR::isError($db)) {
  707.             return $db;
  708.         }
  709.  
  710.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  711.         $table_names $db->queryCol($query);
  712.         if (PEAR::isError($table_names)) {
  713.             return $table_names;
  714.         }
  715.         $result = array();
  716.         for ($i = 0$j count($table_names)$i $j; ++$i{
  717.             if ($sqn $this->_isSequenceName($table_names[$i]))
  718.                 $result[$sqn;
  719.         }
  720.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  721.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  722.         }
  723.         return $result;
  724.     }
  725.  
  726.     // }}}
  727. }
  728. ?>

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