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-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: mysqli.php,v 1.73 2006/09/26 13:57:11 quipo Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. /**
  51.  * MDB2 MySQLi 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_mysqli extends MDB2_Driver_Manager_Common
  58. {
  59.  
  60.     // }}}
  61.     // {{{ createDatabase()
  62.  
  63.     /**
  64.      * create a new database
  65.      *
  66.      * @param string $name name of the database that should be created
  67.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  68.      * @access public
  69.      */
  70.     function createDatabase($name)
  71.     {
  72.         $db =$this->getDBInstance();
  73.         if (PEAR::isError($db)) {
  74.             return $db;
  75.         }
  76.  
  77.         $name $db->quoteIdentifier($nametrue);
  78.         $query = "CREATE DATABASE $name";
  79.         $result $db->exec($query);
  80.         if (PEAR::isError($result)) {
  81.             return $result;
  82.         }
  83.         return MDB2_OK;
  84.     }
  85.  
  86.     // }}}
  87.     // {{{ dropDatabase()
  88.  
  89.     /**
  90.      * drop an existing database
  91.      *
  92.      * @param string $name name of the database that should be dropped
  93.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  94.      * @access public
  95.      */
  96.     function dropDatabase($name)
  97.     {
  98.         $db =$this->getDBInstance();
  99.         if (PEAR::isError($db)) {
  100.             return $db;
  101.         }
  102.  
  103.         $name $db->quoteIdentifier($nametrue);
  104.         $query = "DROP DATABASE $name";
  105.         $result $db->exec($query);
  106.         if (PEAR::isError($result)) {
  107.             return $result;
  108.         }
  109.         return MDB2_OK;
  110.     }
  111.  
  112.     // }}}
  113.     // {{{ createTable()
  114.  
  115.     /**
  116.      * create a new table
  117.      *
  118.      * @param string $name   Name of the database that should be created
  119.      * @param array $fields  Associative array that contains the definition of each field of the new table
  120.      *                        The indexes of the array entries are the names of the fields of the table an
  121.      *                        the array entry values are associative arrays like those that are meant to be
  122.      *                        passed with the field definitions to get[Type]Declaration() functions.
  123.      *                           array(
  124.      *                               'id' => array(
  125.      *                                   'type' => 'integer',
  126.      *                                   'unsigned' => 1
  127.      *                                   'notnull' => 1
  128.      *                                   'default' => 0
  129.      *                               ),
  130.      *                               'name' => array(
  131.      *                                   'type' => 'text',
  132.      *                                   'length' => 12
  133.      *                               ),
  134.      *                               'password' => array(
  135.      *                                   'type' => 'text',
  136.      *                                   'length' => 12
  137.      *                               )
  138.      *                           );
  139.      * @param array $options  An associative array of table options:
  140.      *                           array(
  141.      *                               'comment' => 'Foo',
  142.      *                               'character_set' => 'utf8',
  143.      *                               'collate' => 'utf8_unicode_ci',
  144.      *                               'collate' => 'utf8_unicode_ci',
  145.      *                               'type'    => 'innodb',
  146.      *                           );
  147.      *
  148.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  149.      * @access public
  150.      */
  151.     function createTable($name$fields$options = array())
  152.     {
  153.         $db =$this->getDBInstance();
  154.         if (PEAR::isError($db)) {
  155.             return $db;
  156.         }
  157.  
  158.         $query $this->_getCreateTableQuery($name$fields$options);
  159.         if (PEAR::isError($query)) {
  160.             return $query;
  161.         }
  162.  
  163.         $options_strings = array();
  164.  
  165.         if (!empty($options['comment'])) {
  166.             $options_strings['comment''COMMENT = '.$db->quote($options['comment']'text');
  167.         }
  168.  
  169.         if (!empty($options['charset'])) {
  170.             $options_strings['charset''DEFAULT CHARACTER SET '.$options['charset'];
  171.             if (!empty($options['collate'])) {
  172.                 $options_strings['charset'].= ' COLLATE '.$options['collate'];
  173.             }
  174.         }
  175.  
  176.         $type = false;
  177.         if (!empty($options['type'])) {
  178.             $type $options['type'];
  179.         elseif ($db->options['default_table_type']{
  180.             $type $db->options['default_table_type'];
  181.         }
  182.         if ($type{
  183.             $options_strings[= "ENGINE = $type";
  184.         }
  185.  
  186.         if (!empty($options_strings)) {
  187.             $query.= ' '.implode(' '$options_strings);
  188.         }
  189.         return $db->exec($query);
  190.     }
  191.  
  192.     // }}}
  193.     // {{{ alterTable()
  194.  
  195.     /**
  196.      * alter an existing table
  197.      *
  198.      * @param string $name         name of the table that is intended to be changed.
  199.      * @param array $changes     associative array that contains the details of each type
  200.      *                              of change that is intended to be performed. The types of
  201.      *                              changes that are currently supported are defined as follows:
  202.      *
  203.      *                              name
  204.      *
  205.      *                                 New name for the table.
  206.      *
  207.      *                             add
  208.      *
  209.      *                                 Associative array with the names of fields to be added as
  210.      *                                  indexes of the array. The value of each entry of the array
  211.      *                                  should be set to another associative array with the properties
  212.      *                                  of the fields to be added. The properties of the fields should
  213.      *                                  be the same as defined by the MDB2 parser.
  214.      *
  215.      *
  216.      *                             remove
  217.      *
  218.      *                                 Associative array with the names of fields to be removed as indexes
  219.      *                                  of the array. Currently the values assigned to each entry are ignored.
  220.      *                                  An empty array should be used for future compatibility.
  221.      *
  222.      *                             rename
  223.      *
  224.      *                                 Associative array with the names of fields to be renamed as indexes
  225.      *                                  of the array. The value of each entry of the array should be set to
  226.      *                                  another associative array with the entry named name with the new
  227.      *                                  field name and the entry named Declaration that is expected to contain
  228.      *                                  the portion of the field declaration already in DBMS specific SQL code
  229.      *                                  as it is used in the CREATE TABLE statement.
  230.      *
  231.      *                             change
  232.      *
  233.      *                                 Associative array with the names of the fields to be changed as indexes
  234.      *                                  of the array. Keep in mind that if it is intended to change either the
  235.      *                                  name of a field and any other properties, the change array entries
  236.      *                                  should have the new names of the fields as array indexes.
  237.      *
  238.      *                                 The value of each entry of the array should be set to another associative
  239.      *                                  array with the properties of the fields to that are meant to be changed as
  240.      *                                  array entries. These entries should be assigned to the new values of the
  241.      *                                  respective properties. The properties of the fields should be the same
  242.      *                                  as defined by the MDB2 parser.
  243.      *
  244.      *                             Example
  245.      *                                 array(
  246.      *                                     'name' => 'userlist',
  247.      *                                     'add' => array(
  248.      *                                         'quota' => array(
  249.      *                                             'type' => 'integer',
  250.      *                                             'unsigned' => 1
  251.      *                                         )
  252.      *                                     ),
  253.      *                                     'remove' => array(
  254.      *                                         'file_limit' => array(),
  255.      *                                         'time_limit' => array()
  256.      *                                     ),
  257.      *                                     'change' => array(
  258.      *                                         'name' => array(
  259.      *                                             'length' => '20',
  260.      *                                             'definition' => array(
  261.      *                                                 'type' => 'text',
  262.      *                                                 'length' => 20,
  263.      *                                             ),
  264.      *                                         )
  265.      *                                     ),
  266.      *                                     'rename' => array(
  267.      *                                         'sex' => array(
  268.      *                                             'name' => 'gender',
  269.      *                                             'definition' => array(
  270.      *                                                 'type' => 'text',
  271.      *                                                 'length' => 1,
  272.      *                                                 'default' => 'M',
  273.      *                                             ),
  274.      *                                         )
  275.      *                                     )
  276.      *                                 )
  277.      *
  278.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  279.      *                              can perform the requested table alterations if the value is true or
  280.      *                              actually perform them otherwise.
  281.      * @access public
  282.      *
  283.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  284.      */
  285.     function alterTable($name$changes$check)
  286.     {
  287.         $db =$this->getDBInstance();
  288.         if (PEAR::isError($db)) {
  289.             return $db;
  290.         }
  291.  
  292.         foreach ($changes as $change_name => $change{
  293.             switch ($change_name{
  294.             case 'add':
  295.             case 'remove':
  296.             case 'change':
  297.             case 'rename':
  298.             case 'name':
  299.                 break;
  300.             default:
  301.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  302.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  303.             }
  304.         }
  305.  
  306.         if ($check{
  307.             return MDB2_OK;
  308.         }
  309.  
  310.         $query '';
  311.         if (!empty($changes['name'])) {
  312.             $change_name $db->quoteIdentifier($changes['name']true);
  313.             $query .= 'RENAME TO ' $change_name;
  314.         }
  315.  
  316.         if (!empty($changes['add']&& is_array($changes['add'])) {
  317.             foreach ($changes['add'as $field_name => $field{
  318.                 if ($query{
  319.                     $query.= ', ';
  320.                 }
  321.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  322.             }
  323.         }
  324.  
  325.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  326.             foreach ($changes['remove'as $field_name => $field{
  327.                 if ($query{
  328.                     $query.= ', ';
  329.                 }
  330.                 $field_name $db->quoteIdentifier($field_nametrue);
  331.                 $query.= 'DROP ' $field_name;
  332.             }
  333.         }
  334.  
  335.         $rename = array();
  336.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  337.             foreach ($changes['rename'as $field_name => $field{
  338.                 $rename[$field['name']] $field_name;
  339.             }
  340.         }
  341.  
  342.         if (!empty($changes['change']&& is_array($changes['change'])) {
  343.             foreach ($changes['change'as $field_name => $field{
  344.                 if ($query{
  345.                     $query.= ', ';
  346.                 }
  347.                 if (isset($rename[$field_name])) {
  348.                     $old_field_name $rename[$field_name];
  349.                     unset($rename[$field_name]);
  350.                 else {
  351.                     $old_field_name $field_name;
  352.                 }
  353.                 $old_field_name $db->quoteIdentifier($old_field_nametrue);
  354.                 $query.= "CHANGE $old_field_name " . $db->getDeclaration($field['definition']['type']$field_name$field['definition']);
  355.             }
  356.         }
  357.  
  358.         if (!empty($rename&& is_array($rename)) {
  359.             foreach ($rename as $rename_name => $renamed_field{
  360.                 if ($query{
  361.                     $query.= ', ';
  362.                 }
  363.                 $field $changes['rename'][$renamed_field];
  364.                 $renamed_field $db->quoteIdentifier($renamed_fieldtrue);
  365.                 $query.= 'CHANGE ' $renamed_field ' ' $db->getDeclaration($field['definition']['type']$field['name']$field['definition']);
  366.             }
  367.         }
  368.  
  369.         if (!$query{
  370.             return MDB2_OK;
  371.         }
  372.  
  373.         $name $db->quoteIdentifier($nametrue);
  374.         return $db->exec("ALTER TABLE $name $query");
  375.     }
  376.  
  377.     // }}}
  378.     // {{{ listDatabases()
  379.  
  380.     /**
  381.      * list all databases
  382.      *
  383.      * @return mixed data array on success, a MDB2 error on failure
  384.      * @access public
  385.      */
  386.     function listDatabases()
  387.     {
  388.         $db =$this->getDBInstance();
  389.         if (PEAR::isError($db)) {
  390.             return $db;
  391.         }
  392.  
  393.         $result $db->queryCol('SHOW DATABASES');
  394.         if (PEAR::isError($result)) {
  395.             return $result;
  396.         }
  397.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  398.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  399.         }
  400.         return $result;
  401.     }
  402.  
  403.     // }}}
  404.     // {{{ listUsers()
  405.  
  406.     /**
  407.      * list all users
  408.      *
  409.      * @return mixed data array on success, a MDB2 error on failure
  410.      * @access public
  411.      */
  412.     function listUsers()
  413.     {
  414.         $db =$this->getDBInstance();
  415.         if (PEAR::isError($db)) {
  416.             return $db;
  417.         }
  418.  
  419.         return $db->queryCol('SELECT DISTINCT USER FROM USER');
  420.     }
  421.  
  422.     // }}}
  423.     // {{{ listTables()
  424.  
  425.     /**
  426.      * list all tables in the current database
  427.      *
  428.      * @param string database, the current is default
  429.      * @return mixed data array on success, a MDB2 error on failure
  430.      * @access public
  431.      */
  432.     function listTables($database = null)
  433.     {
  434.         $db =$this->getDBInstance();
  435.         if (PEAR::isError($db)) {
  436.             return $db;
  437.         }
  438.  
  439.         $query "SHOW /*!50002 FULL*/ TABLES";
  440.         if (!is_null($database)) {
  441.             $query .= " FROM $database";
  442.         }
  443.         $query.= "/*!50002  WHERE Table_type = 'BASE TABLE'*/";
  444.  
  445.         $table_names $db->queryAll($querynullMDB2_FETCHMODE_ORDERED);
  446.         if (PEAR::isError($table_names)) {
  447.             return $table_names;
  448.         }
  449.  
  450.         $result = array();
  451.         foreach ($table_names as $table{
  452.             if (!$this->_fixSequenceName($table[0]true)) {
  453.                 $result[$table[0];
  454.             }
  455.         }
  456.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  457.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  458.         }
  459.         return $result;
  460.     }
  461.  
  462.     // }}}
  463.     // {{{ listViews()
  464.  
  465.     /**
  466.      * list the views in the database
  467.      *
  468.      * @param string database, the current is default
  469.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  470.      * @access public
  471.      ***/
  472.     function listViews($database = null)
  473.     {
  474.         $db =$this->getDBInstance();
  475.         if (PEAR::isError($db)) {
  476.             return $db;
  477.         }
  478.  
  479.         $query 'SHOW FULL TABLES';
  480.         if (!is_null($database)) {
  481.             $query.= " FROM $database";
  482.         }
  483.         $query.= " WHERE Table_type = 'VIEW'";
  484.  
  485.         $result $db->queryCol($query);
  486.         if (PEAR::isError($result)) {
  487.             return $result;
  488.         }
  489.  
  490.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  491.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  492.         }
  493.         return $result;
  494.     }
  495.  
  496.     // }}}
  497.     // {{{ listTableFields()
  498.  
  499.     /**
  500.      * list all fields in a tables in the current database
  501.      *
  502.      * @param string $table name of table that should be used in method
  503.      * @return mixed data array on success, a MDB2 error on failure
  504.      * @access public
  505.      */
  506.     function listTableFields($table)
  507.     {
  508.         $db =$this->getDBInstance();
  509.         if (PEAR::isError($db)) {
  510.             return $db;
  511.         }
  512.  
  513.         $table $db->quoteIdentifier($tabletrue);
  514.         $result $db->queryCol("SHOW COLUMNS FROM $table");
  515.         if (PEAR::isError($result)) {
  516.             return $result;
  517.         }
  518.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  519.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  520.         }
  521.         return $result;
  522.     }
  523.  
  524.     // }}}
  525.     // {{{ createIndex()
  526.  
  527.     /**
  528.      * Get the stucture of a field into an array
  529.      *
  530.      * @author Leoncx
  531.      * @param string    $table         name of the table on which the index is to be created
  532.      * @param string    $name         name of the index to be created
  533.      * @param array     $definition        associative array that defines properties of the index to be created.
  534.      *                                  Currently, only one property named FIELDS is supported. This property
  535.      *                                  is also an associative with the names of the index fields as array
  536.      *                                  indexes. Each entry of this array is set to another type of associative
  537.      *                                  array that specifies properties of the index that are specific to
  538.      *                                  each field.
  539.      *
  540.      *                                 Currently, only the sorting property is supported. It should be used
  541.      *                                  to define the sorting direction of the index. It may be set to either
  542.      *                                  ascending or descending.
  543.      *
  544.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  545.      *                                  drivers of those that do not support it ignore this property. Use the
  546.      *                                  function supports() to determine whether the DBMS driver can manage indexes.
  547.      *
  548.      *                                  Example
  549.      *                                     array(
  550.      *                                         'fields' => array(
  551.      *                                             'user_name' => array(
  552.      *                                                 'sorting' => 'ascending'
  553.      *                                                 'length' => 10
  554.      *                                             ),
  555.      *                                             'last_login' => array()
  556.      *                                         )
  557.      *                                     )
  558.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  559.      * @access public
  560.      */
  561.     function createIndex($table$name$definition)
  562.     {
  563.         $db =$this->getDBInstance();
  564.         if (PEAR::isError($db)) {
  565.             return $db;
  566.         }
  567.  
  568.         $table $db->quoteIdentifier($tabletrue);
  569.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  570.         $query = "CREATE INDEX $name ON $table";
  571.         $fields = array();
  572.         foreach ($definition['fields'as $field => $fieldinfo{
  573.             if (!empty($fieldinfo['length'])) {
  574.                 $fields[$db->quoteIdentifier($fieldtrue'(' $fieldinfo['length'')';
  575.             else {
  576.                 $fields[$db->quoteIdentifier($fieldtrue);
  577.             }
  578.         }
  579.         $query .= ' ('implode(', '$fields')';
  580.         return $db->exec($query);
  581.     }
  582.  
  583.     // }}}
  584.     // {{{ dropIndex()
  585.  
  586.     /**
  587.      * drop existing index
  588.      *
  589.      * @param string    $table         name of table that should be used in method
  590.      * @param string    $name         name of the index to be dropped
  591.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  592.      * @access public
  593.      */
  594.     function dropIndex($table$name)
  595.     {
  596.         $db =$this->getDBInstance();
  597.         if (PEAR::isError($db)) {
  598.             return $db;
  599.         }
  600.  
  601.         $table $db->quoteIdentifier($tabletrue);
  602.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  603.         return $db->exec("DROP INDEX $name ON $table");
  604.     }
  605.  
  606.     // }}}
  607.     // {{{ listTableIndexes()
  608.  
  609.     /**
  610.      * list all indexes in a table
  611.      *
  612.      * @param string    $table      name of table that should be used in method
  613.      * @return mixed data array on success, a MDB2 error on failure
  614.      * @access public
  615.      */
  616.     function listTableIndexes($table)
  617.     {
  618.         $db =$this->getDBInstance();
  619.         if (PEAR::isError($db)) {
  620.             return $db;
  621.         }
  622.  
  623.         $key_name 'Key_name';
  624.         $non_unique 'Non_unique';
  625.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  626.             if ($db->options['field_case'== CASE_LOWER{
  627.                 $key_name strtolower($key_name);
  628.                 $non_unique strtolower($non_unique);
  629.             else {
  630.                 $key_name strtoupper($key_name);
  631.                 $non_unique strtoupper($non_unique);
  632.             }
  633.         }
  634.  
  635.         $table $db->quoteIdentifier($tabletrue);
  636.         $query = "SHOW INDEX FROM $table";
  637.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  638.         if (PEAR::isError($indexes)) {
  639.             return $indexes;
  640.         }
  641.  
  642.         $result = array();
  643.         foreach ($indexes as $index_data{
  644.             if ($index_data[$non_unique&& ($index $this->_fixIndexName($index_data[$key_name]))) {
  645.                 $result[$index= true;
  646.             }
  647.         }
  648.  
  649.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  650.             $result array_change_key_case($result$db->options['field_case']);
  651.         }
  652.         return array_keys($result);
  653.     }
  654.  
  655.     // }}}
  656.     // {{{ createConstraint()
  657.  
  658.     /**
  659.      * create a constraint on a table
  660.      *
  661.      * @param string    $table         name of the table on which the constraint is to be created
  662.      * @param string    $name         name of the constraint to be created
  663.      * @param array     $definition        associative array that defines properties of the constraint to be created.
  664.      *                                  Currently, only one property named FIELDS is supported. This property
  665.      *                                  is also an associative with the names of the constraint fields as array
  666.      *                                  constraints. Each entry of this array is set to another type of associative
  667.      *                                  array that specifies properties of the constraint that are specific to
  668.      *                                  each field.
  669.      *
  670.      *                                  Example
  671.      *                                     array(
  672.      *                                         'fields' => array(
  673.      *                                             'user_name' => array(),
  674.      *                                             'last_login' => array()
  675.      *                                         )
  676.      *                                     )
  677.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  678.      * @access public
  679.      */
  680.     function createConstraint($table$name$definition)
  681.     {
  682.         $db =$this->getDBInstance();
  683.         if (PEAR::isError($db)) {
  684.             return $db;
  685.         }
  686.  
  687.         $type '';
  688.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  689.         if (!empty($definition['primary'])) {
  690.             $type 'PRIMARY';
  691.             $name 'KEY';
  692.         elseif (!empty($definition['unique'])) {
  693.             $type 'UNIQUE';
  694.         }
  695.  
  696.         $table $db->quoteIdentifier($tabletrue);
  697.         $query = "ALTER TABLE $table ADD $type $name";
  698.         $fields = array();
  699.         foreach (array_keys($definition['fields']as $field{
  700.             $fields[$db->quoteIdentifier($fieldtrue);
  701.         }
  702.         $query .= ' ('implode(', '$fields')';
  703.         return $db->exec($query);
  704.     }
  705.  
  706.     // }}}
  707.     // {{{ dropConstraint()
  708.  
  709.     /**
  710.      * drop existing constraint
  711.      *
  712.      * @param string    $table        name of table that should be used in method
  713.      * @param string    $name         name of the constraint to be dropped
  714.      * @param string    $primary      hint if the constraint is primary
  715.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  716.      * @access public
  717.      */
  718.     function dropConstraint($table$name$primary = false)
  719.     {
  720.         $db =$this->getDBInstance();
  721.         if (PEAR::isError($db)) {
  722.             return $db;
  723.         }
  724.  
  725.         $table $db->quoteIdentifier($tabletrue);
  726.         if ($primary || strtolower($name== 'primary'{
  727.             $query = "ALTER TABLE $table DROP PRIMARY KEY";
  728.         else {
  729.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  730.             $query = "ALTER TABLE $table DROP INDEX $name";
  731.         }
  732.         return $db->exec($query);
  733.     }
  734.  
  735.     // }}}
  736.     // {{{ listTableConstraints()
  737.  
  738.     /**
  739.      * list all constraints in a table
  740.      *
  741.      * @param string    $table      name of table that should be used in method
  742.      * @return mixed data array on success, a MDB2 error on failure
  743.      * @access public
  744.      */
  745.     function listTableConstraints($table)
  746.     {
  747.         $db =$this->getDBInstance();
  748.         if (PEAR::isError($db)) {
  749.             return $db;
  750.         }
  751.  
  752.         $key_name 'Key_name';
  753.         $non_unique 'Non_unique';
  754.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  755.             if ($db->options['field_case'== CASE_LOWER{
  756.                 $key_name strtolower($key_name);
  757.                 $non_unique strtolower($non_unique);
  758.             else {
  759.                 $key_name strtoupper($key_name);
  760.                 $non_unique strtoupper($non_unique);
  761.             }
  762.         }
  763.  
  764.         $table $db->quoteIdentifier($tabletrue);
  765.         $query = "SHOW INDEX FROM $table";
  766.         $indexes $db->queryAll($querynullMDB2_FETCHMODE_ASSOC);
  767.         if (PEAR::isError($indexes)) {
  768.             return $indexes;
  769.         }
  770.  
  771.         $result = array();
  772.         foreach ($indexes as $index_data{
  773.             if (!$index_data[$non_unique]{
  774.                 if ($index_data[$key_name!== 'PRIMARY'{
  775.                     $index $this->_fixIndexName($index_data[$key_name]);
  776.                 else {
  777.                     $index 'PRIMARY';
  778.                 }
  779.                 if (!empty($index)) {
  780.                     $result[$index= true;
  781.                 }
  782.             }
  783.         }
  784.  
  785.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  786.             $result array_change_key_case($result$db->options['field_case']);
  787.         }
  788.         return array_keys($result);
  789.     }
  790.  
  791.     // }}}
  792.     // {{{ createSequence()
  793.  
  794.     /**
  795.      * create sequence
  796.      *
  797.      * @param string    $seq_name     name of the sequence to be created
  798.      * @param string    $start         start value of the sequence; default is 1
  799.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  800.      * @access public
  801.      */
  802.     function createSequence($seq_name$start = 1)
  803.     {
  804.         $db =$this->getDBInstance();
  805.         if (PEAR::isError($db)) {
  806.             return $db;
  807.         }
  808.  
  809.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  810.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  811.  
  812.         $query = "CREATE TABLE $sequence_name ($seqcol_name INT NOT NULL AUTO_INCREMENT, PRIMARY KEY ($seqcol_name))";
  813.         $query.= strlen($db->options['default_table_type']' TYPE='.$db->options['default_table_type''';
  814.         $res $db->exec($query);
  815.  
  816.         if (PEAR::isError($res)) {
  817.             return $res;
  818.         }
  819.  
  820.         if ($start == 1{
  821.             return MDB2_OK;
  822.         }
  823.  
  824.         $query = "INSERT INTO $sequence_name ($seqcol_name) VALUES (".($start-1).')';
  825.         $res $db->exec($query);
  826.         if (!PEAR::isError($res)) {
  827.             return MDB2_OK;
  828.         }
  829.  
  830.         // Handle error
  831.         $result $db->exec("DROP TABLE $sequence_name");
  832.         if (PEAR::isError($result)) {
  833.             return $db->raiseError($resultnullnull,
  834.                 'could not drop inconsistent sequence table'__FUNCTION__);
  835.         }
  836.  
  837.         return $db->raiseError($resnullnull,
  838.             'could not create sequence table'__FUNCTION__);
  839.     }
  840.  
  841.     // }}}
  842.     // {{{ dropSequence()
  843.  
  844.     /**
  845.      * drop existing sequence
  846.      *
  847.      * @param string    $seq_name     name of the sequence to be dropped
  848.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  849.      * @access public
  850.      */
  851.     function dropSequence($seq_name)
  852.     {
  853.         $db =$this->getDBInstance();
  854.         if (PEAR::isError($db)) {
  855.             return $db;
  856.         }
  857.  
  858.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  859.         return $db->exec("DROP TABLE $sequence_name");
  860.     }
  861.  
  862.     // }}}
  863.     // {{{ listSequences()
  864.  
  865.     /**
  866.      * list all sequences in the current database
  867.      *
  868.      * @param string database, the current is default
  869.      * @return mixed data array on success, a MDB2 error on failure
  870.      * @access public
  871.      */
  872.     function listSequences($database = null)
  873.     {
  874.         $db =$this->getDBInstance();
  875.         if (PEAR::isError($db)) {
  876.             return $db;
  877.         }
  878.  
  879.         $query "SHOW TABLES";
  880.         if (!is_null($database)) {
  881.             $query .= " FROM $database";
  882.         }
  883.         $table_names $db->queryCol($query);
  884.         if (PEAR::isError($table_names)) {
  885.             return $table_names;
  886.         }
  887.  
  888.         $result = array();
  889.         foreach ($table_names as $table_name{
  890.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  891.                 $result[$sqn;
  892.             }
  893.         }
  894.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  895.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  896.         }
  897.         return $result;
  898.     }
  899.  
  900.     // }}}
  901. }
  902. ?>

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