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

Source for file mysql.php

Documentation is available at mysql.php

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

Documentation generated on Mon, 11 Mar 2019 10:15:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.