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

Source for file mysqli.php

Documentation is available at mysqli.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: mysqli.php,v 1.7 2005/04/19 12:53:42 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. /**
  51.  * MDB2 MySQL driver for the management modules
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@backendmedia.com>
  56.  */
  57. {
  58.     // {{{ properties
  59.     var $verified_table_types = array();#
  60.     // }}}
  61.  
  62.     // }}}
  63.     // {{{ _verifyTableType()
  64.  
  65.     /**
  66.      * verify that chosen transactional table hanlder is available in the database
  67.      *
  68.      * @param string $table_type name of the table handler
  69.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  70.      * @access protected
  71.      */
  72.     function _verifyTableType($table_type)
  73.     {
  74.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  75.         switch (strtoupper($table_type)) {
  76.         case 'BERKELEYDB':
  77.         case 'BDB':
  78.             $check = array('have_bdb');
  79.             break;
  80.         case 'INNODB':
  81.             $check = array('have_innobase''have_innodb');
  82.             break;
  83.         case 'GEMINI':
  84.             $check = array('have_gemini');
  85.             break;
  86.         case 'HEAP':
  87.         case 'ISAM':
  88.         case 'MERGE':
  89.         case 'MRG_MYISAM':
  90.         case 'MYISAM':
  91.         case '':
  92.             return MDB2_OK;
  93.         default:
  94.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  95.                 $table_type.' is not a supported table type');
  96.         }
  97.         if (isset($this->verified_table_types[$table_type])
  98.             && $this->verified_table_types[$table_type== $db->connection
  99.         {
  100.             return MDB2_OK;
  101.         }
  102.         $not_supported = false;
  103.         for ($i=0$j=count($check)$i<$j; ++$i{
  104.             $query 'SHOW VARIABLES LIKE '.$db->quote($check[$i]'text');
  105.             $has $db->queryRow($querynullMDB2_FETCHMODE_ORDERED);
  106.             if (PEAR::isError($has)) {
  107.                 return $has;
  108.             }
  109.             if (is_array($has)) {
  110.                 $not_supported = true;
  111.                 if ($has[1== 'YES'{
  112.                     $this->verified_table_types[$table_type$db->connection;
  113.                     return MDB2_OK;
  114.                 }
  115.             }
  116.         }
  117.         if ($not_supported{
  118.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  119.                 $table_type.' is not a supported table type by this MySQL database server');
  120.         }
  121.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  122.             'could not tell if '.$table_type.' is a supported table type');
  123.     }
  124.  
  125.     // }}}
  126.     // {{{ createDatabase()
  127.  
  128.     /**
  129.      * create a new database
  130.      *
  131.      * @param string $name name of the database that should be created
  132.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  133.      * @access public
  134.      */
  135.     function createDatabase($name)
  136.     {
  137.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  138.         $query 'CREATE DATABASE '.$name;
  139.         $result $db->query($query);
  140.         if (PEAR::isError($result)) {
  141.             return $result;
  142.         }
  143.         return MDB2_OK;
  144.     }
  145.  
  146.     // }}}
  147.     // {{{ dropDatabase()
  148.  
  149.     /**
  150.      * drop an existing database
  151.      *
  152.      * @param string $name name of the database that should be dropped
  153.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  154.      * @access public
  155.      */
  156.     function dropDatabase($name)
  157.     {
  158.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  159.         $query 'DROP DATABASE '.$name;
  160.         $result $db->query($query);
  161.         if (PEAR::isError($result)) {
  162.             return $result;
  163.         }
  164.         return MDB2_OK;
  165.     }
  166.  
  167.     // }}}
  168.     // {{{ createTable()
  169.  
  170.     /**
  171.      * create a new table
  172.      *
  173.      * @param string $name     Name of the database that should be created
  174.      * @param array $fields Associative array that contains the definition of each field of the new table
  175.      *                         The indexes of the array entries are the names of the fields of the table an
  176.      *                         the array entry values are associative arrays like those that are meant to be
  177.      *                          passed with the field definitions to get[Type]Declaration() functions.
  178.      *
  179.      *                         Example
  180.      *                         array(
  181.      *
  182.      *                             'id' => array(
  183.      *                                 'type' => 'integer',
  184.      *                                 'unsigned' => 1
  185.      *                                 'notnull' => 1
  186.      *                                 'default' => 0
  187.      *                             ),
  188.      *                             'name' => array(
  189.      *                                 'type' => 'text',
  190.      *                                 'length' => 12
  191.      *                             ),
  192.      *                             'password' => array(
  193.      *                                 'type' => 'text',
  194.      *                                 'length' => 12
  195.      *                             )
  196.      *                         );
  197.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  198.      * @access public
  199.      */
  200.     function createTable($name$fields)
  201.     {
  202.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  203.         if (!$name{
  204.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  205.                 'createTable: no valid table name specified');
  206.         }
  207.         if (!count($fields)) {
  208.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  209.                 'createTable: no fields specified for table "'.$name.'"');
  210.         }
  211.         if (PEAR::isError($verify $this->_verifyTableType($db->options['default_table_type']))) {
  212.             return $verify;
  213.         }
  214.         if (PEAR::isError($query_fields $this->getFieldDeclarationList($fields))) {
  215.             return $db->raiseError(MDB2_ERROR_CANNOT_CREATEnullnull,
  216.                 'createTable: '.$this->getUserinfo());
  217.         }
  218.         if (isset($db->supported['transactions'])
  219.             && ($db->options['default_table_type'== 'BDB' || $db->options['default_table_type'== 'BERKELEYDB')
  220.         {
  221.             $query_fields .= ', '.$db->dummy_primary_key.' INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ('.$db->dummy_primary_key.')';
  222.         }
  223.         $query = "CREATE TABLE $name ($query_fields)".(strlen($db->options['default_table_type'])
  224.             ? ' TYPE='.$db->options['default_table_type''');
  225.  
  226.         return $db->query($query);
  227.     }
  228.  
  229.     // }}}
  230.     // {{{ alterTable()
  231.  
  232.     /**
  233.      * alter an existing table
  234.      *
  235.      * @param string $name         name of the table that is intended to be changed.
  236.      * @param array $changes     associative array that contains the details of each type
  237.      *                              of change that is intended to be performed. The types of
  238.      *                              changes that are currently supported are defined as follows:
  239.      *
  240.      *                              name
  241.      *
  242.      *                                 New name for the table.
  243.      *
  244.      *                             added_fields
  245.      *
  246.      *                                 Associative array with the names of fields to be added as
  247.      *                                  indexes of the array. The value of each entry of the array
  248.      *                                  should be set to another associative array with the properties
  249.      *                                  of the fields to be added. The properties of the fields should
  250.      *                                  be the same as defined by the Metabase parser.
  251.      *
  252.      *                                 Additionally, there should be an entry named Declaration that
  253.      *                                  is expected to contain the portion of the field declaration already
  254.      *                                  in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  255.      *
  256.      *                             removed_fields
  257.      *
  258.      *                                 Associative array with the names of fields to be removed as indexes
  259.      *                                  of the array. Currently the values assigned to each entry are ignored.
  260.      *                                  An empty array should be used for future compatibility.
  261.      *
  262.      *                             renamed_fields
  263.      *
  264.      *                                 Associative array with the names of fields to be renamed as indexes
  265.      *                                  of the array. The value of each entry of the array should be set to
  266.      *                                  another associative array with the entry named name with the new
  267.      *                                  field name and the entry named Declaration that is expected to contain
  268.      *                                  the portion of the field declaration already in DBMS specific SQL code
  269.      *                                  as it is used in the CREATE TABLE statement.
  270.      *
  271.      *                             changed_fields
  272.      *
  273.      *                                 Associative array with the names of the fields to be changed as indexes
  274.      *                                  of the array. Keep in mind that if it is intended to change either the
  275.      *                                  name of a field and any other properties, the changed_fields array entries
  276.      *                                  should have the new names of the fields as array indexes.
  277.      *
  278.      *                                 The value of each entry of the array should be set to another associative
  279.      *                                  array with the properties of the fields to that are meant to be changed as
  280.      *                                  array entries. These entries should be assigned to the new values of the
  281.      *                                  respective properties. The properties of the fields should be the same
  282.      *                                  as defined by the Metabase parser.
  283.      *
  284.      *                                 If the default property is meant to be added, removed or changed, there
  285.      *                                  should also be an entry with index ChangedDefault assigned to 1. Similarly,
  286.      *                                  if the notnull constraint is to be added or removed, there should also be
  287.      *                                  an entry with index ChangedNotNull assigned to 1.
  288.      *
  289.      *                                 Additionally, there should be an entry named Declaration that is expected
  290.      *                                  to contain the portion of the field changed declaration already in DBMS
  291.      *                                  specific SQL code as it is used in the CREATE TABLE statement.
  292.      *                             Example
  293.      *                                 array(
  294.      *                                     'name' => 'userlist',
  295.      *                                     'added_fields' => array(
  296.      *                                         'quota' => array(
  297.      *                                             'type' => 'integer',
  298.      *                                             'unsigned' => 1
  299.      *                                             'declaration' => 'quota INT'
  300.      *                                         )
  301.      *                                     ),
  302.      *                                     'removed_fields' => array(
  303.      *                                         'file_limit' => array(),
  304.      *                                         'time_limit' => array()
  305.      *                                         ),
  306.      *                                     'changed_fields' => array(
  307.      *                                         'gender' => array(
  308.      *                                             'default' => 'M',
  309.      *                                             'change_default' => 1,
  310.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  311.      *                                         )
  312.      *                                     ),
  313.      *                                     'renamed_fields' => array(
  314.      *                                         'sex' => array(
  315.      *                                             'name' => 'gender',
  316.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  317.      *                                         )
  318.      *                                     )
  319.      *                                 )
  320.      *
  321.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  322.      *                              can perform the requested table alterations if the value is true or
  323.      *                              actually perform them otherwise.
  324.      * @access public
  325.      *
  326.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  327.      */
  328.     function alterTable($name$changes$check)
  329.     {
  330.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  331.         foreach ($changes as $change_name => $change{
  332.             switch ($change_name{
  333.             case 'added_fields':
  334.             case 'removed_fields':
  335.             case 'changed_fields':
  336.             case 'renamed_fields':
  337.             case 'name':
  338.                 break;
  339.             default:
  340.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  341.                     'alterTable: change type "'.$change_name.'" not yet supported');
  342.             }
  343.         }
  344.         if ($check{
  345.             return MDB2_OK;
  346.         }
  347.         $query (isset($changes['name']'RENAME AS '.$changes['name''');
  348.         if (isset($changes['added_fields'])) {
  349.             $fields $changes['added_fields'];
  350.             foreach ($fields as $field{
  351.                 if ($query{
  352.                     $query .= ',';
  353.                 }
  354.                 $query .= 'ADD '.$field['declaration'];
  355.             }
  356.         }
  357.         if (isset($changes['removed_fields'])) {
  358.             $fields $changes['removed_fields'];
  359.             foreach ($fields as $field_name => $field{
  360.                 if ($query{
  361.                     $query .= ',';
  362.                 }
  363.                 $query .= 'DROP '.$field_name;
  364.             }
  365.         }
  366.         $renamed_fields = array();
  367.         if (isset($changes['renamed_fields'])) {
  368.             $fields $changes['renamed_fields'];
  369.             foreach ($fields as $field_name => $field{
  370.                 $renamed_fields[$field['name']] $field_name;
  371.             }
  372.         }
  373.         if (isset($changes['changed_fields'])) {
  374.             $fields $changes['changed_fields'];
  375.             foreach ($fields as $field_name => $field{
  376.                 if ($query{
  377.                     $query .= ',';
  378.                 }
  379.                 if (isset($renamed_fields[$field_name])) {
  380.                     $old_field_name $renamed_fields[$field_name];
  381.                     unset($renamed_fields[$field_name]);
  382.                 else {
  383.                     $old_field_name $field_name;
  384.                 }
  385.                 $query .= "CHANGE $old_field_name ".$field['declaration'];
  386.             }
  387.         }
  388.         if (count($renamed_fields)) {
  389.             foreach ($renamed_fields as $renamed_fields_name => $renamed_field{
  390.                 if ($query{
  391.                     $query .= ',';
  392.                 }
  393.                 $old_field_name $renamed_field;
  394.                 $query .= "CHANGE $old_field_name ".$changes['renamed_fields'][$old_field_name]['declaration'];
  395.             }
  396.         }
  397.         if (!$query{
  398.             return MDB2_OK;
  399.         }
  400.         return $db->query("ALTER TABLE $name $query");
  401.     }
  402.  
  403.     // }}}
  404.     // {{{ listDatabases()
  405.  
  406.     /**
  407.      * list all databases
  408.      *
  409.      * @return mixed data array on success, a MDB2 error on failure
  410.      * @access public
  411.      */
  412.     function listDatabases()
  413.     {
  414.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  415.         $databases $db->queryCol('SHOW DATABASES');
  416.         return $databases;
  417.     }
  418.  
  419.     // }}}
  420.     // {{{ listUsers()
  421.  
  422.     /**
  423.      * list all users
  424.      *
  425.      * @return mixed data array on success, a MDB2 error on failure
  426.      * @access public
  427.      */
  428.     function listUsers()
  429.     {
  430.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  431.         $users $db->queryCol('SELECT DISTINCT USER FROM USER');
  432.         return $users;
  433.     }
  434.  
  435.     // }}}
  436.     // {{{ listTables()
  437.  
  438.     /**
  439.      * list all tables in the current database
  440.      *
  441.      * @return mixed data array on success, a MDB2 error on failure
  442.      * @access public
  443.      */
  444.     function listTables()
  445.     {
  446.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  447.         $table_names $db->queryCol('SHOW TABLES');
  448.         if (PEAR::isError($table_names)) {
  449.             return $table_names;
  450.         }
  451.         $tables = array();
  452.         for ($i = 0$j count($table_names)$i $j; ++$i{
  453.             if (!$this->_isSequenceName($table_names[$i]))
  454.                 $tables[$table_names[$i];
  455.         }
  456.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  457.             $tables array_flip(array_change_key_case(array_flip($tables)CASE_LOWER));
  458.         }
  459.         return $tables;
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ listTableFields()
  464.  
  465.     /**
  466.      * list all fields in a tables in the current database
  467.      *
  468.      * @param string $table name of table that should be used in method
  469.      * @return mixed data array on success, a MDB2 error on failure
  470.      * @access public
  471.      */
  472.     function listTableFields($table)
  473.     {
  474.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  475.         $fields $db->queryCol("SHOW COLUMNS FROM $table");
  476.         if (PEAR::isError($fields)) {
  477.             return $fields;
  478.         }
  479.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  480.             $fields array_flip(array_change_key_case(array_flip($fields)CASE_LOWER));
  481.         }
  482.         if (is_array($fields)) {
  483.             return array_diff($fieldsarray($db->dummy_primary_key));
  484.         }
  485.         return array();
  486.     }
  487.  
  488.     // }}}
  489.     // {{{ createIndex()
  490.  
  491.     /**
  492.      * get the stucture of a field into an array
  493.      *
  494.      * @param string    $table         name of the table on which the index is to be created
  495.      * @param string    $name         name of the index to be created
  496.      * @param array     $definition        associative array that defines properties of the index to be created.
  497.      *                                  Currently, only one property named FIELDS is supported. This property
  498.      *                                  is also an associative with the names of the index fields as array
  499.      *                                  indexes. Each entry of this array is set to another type of associative
  500.      *                                  array that specifies properties of the index that are specific to
  501.      *                                  each field.
  502.      *
  503.      *                                 Currently, only the sorting property is supported. It should be used
  504.      *                                  to define the sorting direction of the index. It may be set to either
  505.      *                                  ascending or descending.
  506.      *
  507.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  508.      *                                  drivers of those that do not support it ignore this property. Use the
  509.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  510.  
  511.      *                                  Example
  512.      *                                     array(
  513.      *                                         'fields' => array(
  514.      *                                             'user_name' => array(
  515.      *                                                 'sorting' => 'ascending'
  516.      *                                             ),
  517.      *                                             'last_login' => array()
  518.      *                                         )
  519.      *                                     )
  520.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  521.      * @access public
  522.      */
  523.     function createIndex($table$name$definition)
  524.     {
  525.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  526.         $query = "ALTER TABLE $table ADD ".(isset($definition['unique']'UNIQUE' 'INDEX')." $name (";
  527.         $skipped_first = false;
  528.         foreach ($definition['fields'as $field_name => $field{
  529.             if ($skipped_first{
  530.                 $query .= ',';
  531.             }
  532.             $query .= $field_name;
  533.             $skipped_first = true;
  534.         }
  535.         $query .= ')';
  536.         return $db->query($query);
  537.     }
  538.  
  539.     // }}}
  540.     // {{{ dropIndex()
  541.  
  542.     /**
  543.      * drop existing index
  544.      *
  545.      * @param string    $table         name of table that should be used in method
  546.      * @param string    $name         name of the index to be dropped
  547.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  548.      * @access public
  549.      */
  550.     function dropIndex($table$name)
  551.     {
  552.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  553.         return $db->query("ALTER TABLE $table DROP INDEX $name");
  554.     }
  555.  
  556.     // }}}
  557.     // {{{ listTableIndexes()
  558.  
  559.     /**
  560.      * list all indexes in a table
  561.      *
  562.      * @param string    $table      name of table that should be used in method
  563.      * @return mixed data array on success, a MDB2 error on failure
  564.      * @access public
  565.      */
  566.     function listTableIndexes($table)
  567.     {
  568.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  569.         $key_name 'Key_name';
  570.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  571.             $key_name strtolower($key_name);
  572.         }
  573.         $query = "SHOW INDEX FROM $table";
  574.         $indexes_all $db->queryCol($query'text'$key_name);
  575.         if (PEAR::isError($indexes_all)) {
  576.             return $indexes_all;
  577.         }
  578.         $found $indexes = array();
  579.         for ($index = 0$j count($indexes_all)$index $j; ++$index{
  580.             if ($indexes_all[$index!= 'PRIMARY'
  581.                 && !isset($found[$indexes_all[$index]])
  582.             {
  583.                 $indexes[$indexes_all[$index];
  584.                 $found[$indexes_all[$index]] = true;
  585.             }
  586.         }
  587.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  588.             $indexes array_flip(array_change_key_case(array_flip($indexes)CASE_LOWER));
  589.         }
  590.         return $indexes;
  591.     }
  592.  
  593.     // }}}
  594.     // {{{ createSequence()
  595.  
  596.     /**
  597.      * create sequence
  598.      *
  599.      * @param string    $seq_name     name of the sequence to be created
  600.      * @param string    $start         start value of the sequence; default is 1
  601.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  602.      * @access public
  603.      */
  604.     function createSequence($seq_name$start = 1)
  605.     {
  606.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  607.         $sequence_name $db->getSequenceName($seq_name);
  608.         $seqcol_name $db->options['seqcol_name'];
  609.         $result $this->_verifyTableType($db->options['default_table_type']);
  610.         if (PEAR::isError($result)) {
  611.             return $result;
  612.         }
  613.         $res $db->query("CREATE TABLE $sequence_name".
  614.             "($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))".
  615.             (strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''')
  616.         );
  617.         if (PEAR::isError($res)) {
  618.             return $res;
  619.         }
  620.         if ($start == 1{
  621.             return MDB2_OK;
  622.         }
  623.         $res $db->query("INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')');
  624.         if (!PEAR::isError($res)) {
  625.             return MDB2_OK;
  626.         }
  627.         // Handle error
  628.         $result $db->query("DROP TABLE $sequence_name");
  629.         if (PEAR::isError($result)) {
  630.             return $db->raiseError(MDB2_ERRORnullnull,
  631.                 'createSequence: could not drop inconsistent sequence table ('.
  632.                 $result->getMessage().' ('.$result->getUserinfo().'))');
  633.         }
  634.         return $db->raiseError(MDB2_ERRORnullnull,
  635.             'createSequence: could not create sequence table ('.
  636.             $res->getMessage().' ('.$res->getUserinfo().'))');
  637.     }
  638.  
  639.     // }}}
  640.     // {{{ dropSequence()
  641.  
  642.     /**
  643.      * drop existing sequence
  644.      *
  645.      * @param string    $seq_name     name of the sequence to be dropped
  646.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  647.      * @access public
  648.      */
  649.     function dropSequence($seq_name)
  650.     {
  651.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  652.         $sequence_name $db->getSequenceName($seq_name);
  653.         return $db->query("DROP TABLE $sequence_name");
  654.     }
  655.  
  656.     // }}}
  657.     // {{{ listSequences()
  658.  
  659.     /**
  660.      * list all sequences in the current database
  661.      *
  662.      * @return mixed data array on success, a MDB2 error on failure
  663.      * @access public
  664.      */
  665.     function listSequences()
  666.     {
  667.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  668.         $table_names $db->queryCol('SHOW TABLES');
  669.         if (PEAR::isError($table_names)) {
  670.             return $table_names;
  671.         }
  672.         $sequences = array();
  673.         for ($i = 0$j count($table_names)$i $j; ++$i{
  674.             if ($sqn $this->_isSequenceName($table_names[$i]))
  675.                 $sequences[$sqn;
  676.         }
  677.         return $sequences;
  678.     }
  679.  
  680.     // }}}
  681. }
  682. ?>

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