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-2006 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.53 2006/05/31 14:38:06 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_ALREADY_EXISTSnullnull,
  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 (!empty($changes['name'])) {
  254.             $change_name $db->quoteIdentifier($changes['name']true);
  255.             $query .= 'RENAME TO ' $change_name;
  256.         }
  257.  
  258.         if (!empty($changes['add']&& is_array($changes['add'])) {
  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.         foreach ($table_names as $table_name{
  339.             if (!$this->_fixSequenceName($table_nametrue)) {
  340.                 $result[$table_name;
  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.         $db->setLimit(1);
  368.         $result2 $db->query("SELECT * FROM $table");
  369.         if (PEAR::isError($result2)) {
  370.             return $result2;
  371.         }
  372.         $result $result2->getColumnNames();
  373.         $result2->free();
  374.         if (PEAR::isError($result)) {
  375.             return $result;
  376.         }
  377.         return array_flip($result);
  378.     }
  379.  
  380.     // }}}
  381.     // {{{ createIndex()
  382.  
  383.     /**
  384.      * Get the stucture of a field into an array
  385.      *
  386.      * @param string    $table         name of the table on which the index is to be created
  387.      * @param string    $name         name of the index to be created
  388.      * @param array     $definition        associative array that defines properties of the index to be created.
  389.      *                                  Currently, only one property named FIELDS is supported. This property
  390.      *                                  is also an associative with the names of the index fields as array
  391.      *                                  indexes. Each entry of this array is set to another type of associative
  392.      *                                  array that specifies properties of the index that are specific to
  393.      *                                  each field.
  394.      *
  395.      *                                 Currently, only the sorting property is supported. It should be used
  396.      *                                  to define the sorting direction of the index. It may be set to either
  397.      *                                  ascending or descending.
  398.      *
  399.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  400.      *                                  drivers of those that do not support it ignore this property. Use the
  401.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  402.  
  403.      *                                  Example
  404.      *                                     array(
  405.      *                                         'fields' => array(
  406.      *                                             'user_name' => array(
  407.      *                                                 'sorting' => 'ascending'
  408.      *                                             ),
  409.      *                                             'last_login' => array()
  410.      *                                         )
  411.      *                                     )
  412.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  413.      * @access public
  414.      */
  415.     function createIndex($table$name$definition)
  416.     {
  417.         $db =$this->getDBInstance();
  418.         if (PEAR::isError($db)) {
  419.             return $db;
  420.         }
  421.  
  422.         $table $db->quoteIdentifier($tabletrue);
  423.         $name  $db->getIndexName($name);
  424.         $query = "CREATE INDEX $name ON $table";
  425.         $fields = array();
  426.         foreach ($definition['fields'as $field_name => $field{
  427.             $field_string $field_name;
  428.             if (!empty($field['sorting'])) {
  429.                 switch ($field['sorting']{
  430.                 case 'ascending':
  431.                     $field_string.= ' ASC';
  432.                     break;
  433.                 case 'descending':
  434.                     $field_string.= ' DESC';
  435.                     break;
  436.                 }
  437.             }
  438.             $fields[$field_string;
  439.         }
  440.         $query .= ' ('.implode(', '$fields')';
  441.         return $db->exec($query);
  442.     }
  443.  
  444.     // }}}
  445.     // {{{ dropIndex()
  446.  
  447.     /**
  448.      * drop existing index
  449.      *
  450.      * @param string    $table         name of table that should be used in method
  451.      * @param string    $name         name of the index to be dropped
  452.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  453.      * @access public
  454.      */
  455.     function dropIndex($table$name)
  456.     {
  457.         $db =$this->getDBInstance();
  458.         if (PEAR::isError($db)) {
  459.             return $db;
  460.         }
  461.  
  462.         $name $db->getIndexName($name);
  463.         return $db->exec("DROP INDEX $name");
  464.     }
  465.  
  466.     // }}}
  467.     // {{{ listTableIndexes()
  468.  
  469.     /**
  470.      * list all indexes in a table
  471.      *
  472.      * @param string    $table      name of table that should be used in method
  473.      * @return mixed data array on success, a MDB2 error on failure
  474.      * @access public
  475.      */
  476.     function listTableIndexes($table)
  477.     {
  478.         $db =$this->getDBInstance();
  479.         if (PEAR::isError($db)) {
  480.             return $db;
  481.         }
  482.  
  483.         $table $db->quote($table'text');
  484.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  485.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  486.             $query.= 'LOWER(tbl_name)='.strtolower($table);
  487.         else {
  488.             $query.= "tbl_name=$table";
  489.         }
  490.         $query.= " AND sql NOT NULL ORDER BY name";
  491.         $indexes $db->queryCol($query'text');
  492.         if (PEAR::isError($indexes)) {
  493.             return $indexes;
  494.         }
  495.  
  496.         $result = array();
  497.         foreach ($indexes as $sql{
  498.             if (preg_match("/^create index ([^ ]*) on /i"$sql$tmp)) {
  499.                 $result[$this->_fixIndexName($tmp[1])= true;
  500.             }
  501.         }
  502.  
  503.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  504.             $result array_change_key_case($result$db->options['field_case']);
  505.         }
  506.         return array_keys($result);
  507.     }
  508.  
  509.     // }}}
  510.     // {{{ createConstraint()
  511.  
  512.     /**
  513.      * create a constraint on a table
  514.      *
  515.      * @param string    $table         name of the table on which the constraint is to be created
  516.      * @param string    $name         name of the constraint to be created
  517.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  518.      *                                  Currently, only one property named FIELDS is supported. This property
  519.      *                                  is also an associative with the names of the constraint fields as array
  520.      *                                  constraints. Each entry of this array is set to another type of associative
  521.      *                                  array that specifies properties of the constraint that are specific to
  522.      *                                  each field.
  523.      *
  524.      *                                  Example
  525.      *                                     array(
  526.      *                                         'fields' => array(
  527.      *                                             'user_name' => array(),
  528.      *                                             'last_login' => array()
  529.      *                                         )
  530.      *                                     )
  531.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  532.      * @access public
  533.      */
  534.     function createConstraint($table$name$definition)
  535.     {
  536.         $db =$this->getDBInstance();
  537.         if (PEAR::isError($db)) {
  538.             return $db;
  539.         }
  540.  
  541.         if (!empty($definition['primary'])) {
  542.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  543.                 'createConstraint: Creating Primary Constraints is not supported');
  544.         }
  545.  
  546.         $table $db->quoteIdentifier($tabletrue);
  547.         $name  $db->getIndexName($name);
  548.         $query = "CREATE UNIQUE INDEX $name ON $table";
  549.         $fields = array();
  550.         foreach ($definition['fields'as $field_name => $field{
  551.             $field_string $field_name;
  552.             if (!empty($field['sorting'])) {
  553.                 switch ($field['sorting']{
  554.                 case 'ascending':
  555.                     $field_string.= ' ASC';
  556.                     break;
  557.                 case 'descending':
  558.                     $field_string.= ' DESC';
  559.                     break;
  560.                 }
  561.             }
  562.             $fields[$field_string;
  563.         }
  564.         $query .= ' ('.implode(', '$fields')';
  565.         return $db->exec($query);
  566.     }
  567.  
  568.     // }}}
  569.     // {{{ dropConstraint()
  570.  
  571.     /**
  572.      * drop existing constraint
  573.      *
  574.      * @param string    $table        name of table that should be used in method
  575.      * @param string    $name         name of the constraint to be dropped
  576.      * @param string    $primary      hint if the constraint is primary
  577.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  578.      * @access public
  579.      */
  580.     function dropConstraint($table$name$primary = false)
  581.     {
  582.         $db =$this->getDBInstance();
  583.         if (PEAR::isError($db)) {
  584.             return $db;
  585.         }
  586.  
  587.         if ($primary || $name == 'PRIMARY'{
  588.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  589.                 'dropConstraints: Dropping Primary Constraints is not supported');
  590.         }
  591.  
  592.         $name $db->getIndexName($name);
  593.         return $db->exec("DROP INDEX $name");
  594.     }
  595.  
  596.     // }}}
  597.     // {{{ listTableConstraints()
  598.  
  599.     /**
  600.      * list all sonstraints in a table
  601.      *
  602.      * @param string    $table      name of table that should be used in method
  603.      * @return mixed data array on success, a MDB2 error on failure
  604.      * @access public
  605.      */
  606.     function listTableConstraints($table)
  607.     {
  608.         $db =$this->getDBInstance();
  609.         if (PEAR::isError($db)) {
  610.             return $db;
  611.         }
  612.  
  613.         $table $db->quote($table'text');
  614.         $query "SELECT sql FROM sqlite_master WHERE type='index' AND ";
  615.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  616.             $query.= 'LOWER(tbl_name)='.strtolower($table);
  617.         else {
  618.             $query.= "tbl_name=$table";
  619.         }
  620.         $query.= " AND sql NOT NULL ORDER BY name";
  621.         $indexes $db->queryCol($query'text');
  622.         if (PEAR::isError($indexes)) {
  623.             return $indexes;
  624.         }
  625.  
  626.         $result = array();
  627.         foreach ($indexes as $sql{
  628.             if (preg_match("/^create unique index ([^ ]*) on /i"$sql$tmp)) {
  629.                 $result[$this->_fixIndexName($tmp[1])= true;
  630.             }
  631.         }
  632.  
  633.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  634.             $result array_change_key_case($result$db->options['field_case']);
  635.         }
  636.         return array_keys($result);
  637.     }
  638.  
  639.     // }}}
  640.     // {{{ createSequence()
  641.  
  642.     /**
  643.      * create sequence
  644.      *
  645.      * @param string    $seq_name     name of the sequence to be created
  646.      * @param string    $start         start value of the sequence; default is 1
  647.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  648.      * @access public
  649.      */
  650.     function createSequence($seq_name$start = 1)
  651.     {
  652.         $db =$this->getDBInstance();
  653.         if (PEAR::isError($db)) {
  654.             return $db;
  655.         }
  656.  
  657.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  658.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  659.         $query = "CREATE TABLE $sequence_name ($seqcol_name INTEGER PRIMARY KEY DEFAULT 0 NOT NULL)";
  660.         $res $db->exec($query);
  661.         if (PEAR::isError($res)) {
  662.             return $res;
  663.         }
  664.         if ($start == 1{
  665.             return MDB2_OK;
  666.         }
  667.         $res $db->exec("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  668.         if (!PEAR::isError($res)) {
  669.             return MDB2_OK;
  670.         }
  671.         // Handle error
  672.         $result $db->exec("DROP TABLE $sequence_name");
  673.         if (PEAR::isError($result)) {
  674.             return $db->raiseError($resultnullnull,
  675.                 'createSequence: could not drop inconsistent sequence table');
  676.         }
  677.         return $db->raiseError($resnullnull,
  678.             'createSequence: could not create sequence table');
  679.     }
  680.  
  681.     // }}}
  682.     // {{{ dropSequence()
  683.  
  684.     /**
  685.      * drop existing sequence
  686.      *
  687.      * @param string    $seq_name     name of the sequence to be dropped
  688.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  689.      * @access public
  690.      */
  691.     function dropSequence($seq_name)
  692.     {
  693.         $db =$this->getDBInstance();
  694.         if (PEAR::isError($db)) {
  695.             return $db;
  696.         }
  697.  
  698.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  699.         return $db->exec("DROP TABLE $sequence_name");
  700.     }
  701.  
  702.     // }}}
  703.     // {{{ listSequences()
  704.  
  705.     /**
  706.      * list all sequences in the current database
  707.      *
  708.      * @return mixed data array on success, a MDB2 error on failure
  709.      * @access public
  710.      */
  711.     function listSequences()
  712.     {
  713.         $db =$this->getDBInstance();
  714.         if (PEAR::isError($db)) {
  715.             return $db;
  716.         }
  717.  
  718.         $query "SELECT name FROM sqlite_master WHERE type='table' AND sql NOT NULL ORDER BY name";
  719.         $table_names $db->queryCol($query);
  720.         if (PEAR::isError($table_names)) {
  721.             return $table_names;
  722.         }
  723.         $result = array();
  724.         foreach ($table_names as $table_name{
  725.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  726.                 $result[$sqn;
  727.             }
  728.         }
  729.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  730.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  731.         }
  732.         return $result;
  733.     }
  734.  
  735.     // }}}
  736. }
  737. ?>

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