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

Source for file oci8.php

Documentation is available at oci8.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2004 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  7. // | All rights reserved.                                                 |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB  |
  10. // | API as well as database abstraction for PHP applications.            |
  11. // | This LICENSE is in the BSD license style.                            |
  12. // |                                                                      |
  13. // | Redistribution and use in source and binary forms, with or without   |
  14. // | modification, are permitted provided that the following conditions   |
  15. // | are met:                                                             |
  16. // |                                                                      |
  17. // | Redistributions of source code must retain the above copyright       |
  18. // | notice, this list of conditions and the following disclaimer.        |
  19. // |                                                                      |
  20. // | Redistributions in binary form must reproduce the above copyright    |
  21. // | notice, this list of conditions and the following disclaimer in the  |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // |                                                                      |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken,    |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission.                                                  |
  28. // |                                                                      |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT    |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS    |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE      |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,          |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // |  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED  |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT          |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE          |
  40. // | POSSIBILITY OF SUCH DAMAGE.                                          |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org>                           |
  43. // +----------------------------------------------------------------------+
  44.  
  45. // $Id: oci8.php,v 1.53 2005/12/15 16:54:53 lsmith Exp $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 oci8 driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author Lukas Smith <smith@pooteeweet.org>
  55.  */
  56. class MDB2_Driver_Manager_oci8 extends MDB2_Driver_Manager_Common
  57. {
  58.     // {{{ createDatabase()
  59.  
  60.     /**
  61.      * create a new database
  62.      *
  63.      * @param object $db database object that is extended by this class
  64.      * @param string $name name of the database that should be created
  65.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function createDatabase($name)
  69.     {
  70.         $db =$this->getDBInstance();
  71.         if (PEAR::isError($db)) {
  72.             return $db;
  73.         }
  74.  
  75.         $username $db->options['database_name_prefix'].$name;
  76.         $password $db->dsn['password'$db->dsn['password'$name;
  77.         $tablespace $db->options['default_tablespace']
  78.             ? ' DEFAULT TABLESPACE '.$db->options['default_tablespace''';
  79.  
  80.         $query 'CREATE USER '.$username.' IDENTIFIED BY '.$password.$tablespace;
  81.         $result $db->standaloneQuery($querynulltrue);
  82.         if (PEAR::isError($result)) {
  83.             return $result;
  84.         }
  85.         $query 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE TO '.$username;
  86.         $result $db->standaloneQuery($querynulltrue);
  87.         if (PEAR::isError($result)) {
  88.             $query 'DROP USER '.$username.' CASCADE';
  89.             $result2 $db->standaloneQuery($querynulltrue);
  90.             if (PEAR::isError($result2)) {
  91.                 return $db->raiseError(MDB2_ERRORnullnull,
  92.                     'createDatabase: could not setup the database user ('.$result->getUserinfo().
  93.                         ') and then could drop its records ('.$result2->getUserinfo().')');
  94.             }
  95.             return $result;
  96.         }
  97.         return MDB2_OK;
  98.     }
  99.  
  100.     // }}}
  101.     // {{{ dropDatabase()
  102.  
  103.     /**
  104.      * drop an existing database
  105.      *
  106.      * @param object $db database object that is extended by this class
  107.      * @param string $name name of the database that should be dropped
  108.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  109.      * @access public
  110.      */
  111.     function dropDatabase($name)
  112.     {
  113.         $db =$this->getDBInstance();
  114.         if (PEAR::isError($db)) {
  115.             return $db;
  116.         }
  117.  
  118.         $username $db->options['database_name_prefix'].$name;
  119.         return $db->standaloneQuery('DROP USER '.$username.' CASCADE'nulltrue);
  120.     }
  121.  
  122.  
  123.     // }}}
  124.     // {{{ _makeAutoincrement()
  125.  
  126.     /**
  127.      * add an autoincrement sequence + trigger
  128.      *
  129.      * @param string $name  name of the PK field
  130.      * @param string $table name of the table
  131.      * @param string $start start value for the sequence
  132.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  133.      * @access private
  134.      */
  135.     function _makeAutoincrement($name$table$start = 1)
  136.     {
  137.         $db =$this->getDBInstance();
  138.         if (PEAR::isError($db)) {
  139.             return $db;
  140.         }
  141.  
  142.         $index_name  $table '_AUTOINCREMENT_PK';
  143.         $definition = array(
  144.             'primary' => true,
  145.             'fields' => array($name),
  146.         );
  147.         $result $db->manager->createConstraint($table$index_name$definition);
  148.         if (PEAR::isError($result)) {
  149.             return $db->raiseError(MDB2_ERRORnullnull,
  150.                 '_makeAutoincrement: primary key for autoincrement PK could not be created');
  151.         }
  152.  
  153.         $table $db->quoteIdentifier($tabletrue);
  154.         $name $db->quoteIdentifier($nametrue);
  155.         if (is_null($start)) {
  156.             $db->beginTransaction();
  157.             $query = "SELECT MAX($name) FROM $table";
  158.             $start $this->db->queryOne($query'integer');
  159.             if (PEAR::isError($start)) {
  160.                 return $start;
  161.             }
  162.             ++$start;
  163.             $result $db->manager->createSequence($table$start);
  164.             $db->commit();
  165.         else {
  166.             $result $db->manager->createSequence($table$start);
  167.         }
  168.         if (PEAR::isError($result)) {
  169.             return $db->raiseError(MDB2_ERRORnullnull,
  170.                 '_makeAutoincrement: sequence for autoincrement PK could not be created');
  171.         }
  172.  
  173.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  174.         $trigger_name  $table '_AUTOINCREMENT_PK';
  175.         $trigger_name $db->quoteIdentifier($trigger_nametrue);
  176.         $trigger_sql = "CREATE TRIGGER $trigger_name BEFORE INSERT ON $table";
  177.         $trigger_sql.= " FOR EACH ROW BEGIN IF (:new.$name IS NULL) THEN SELECT ";
  178.         $trigger_sql.= "$sequence_name.NEXTVAL INTO :new.$name FROM DUAL; END IF; END;";
  179.  
  180.         return $db->exec($trigger_sql);
  181.     }
  182.  
  183.     // }}}
  184.     // {{{ _dropAutoincrement()
  185.  
  186.     /**
  187.      * drop an existing autoincrement sequence + trigger
  188.      *
  189.      * @param string $table name of the table
  190.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  191.      * @access private
  192.      */
  193.     function _dropAutoincrement($table)
  194.     {
  195.         $db =$this->getDBInstance();
  196.         if (PEAR::isError($db)) {
  197.             return $db;
  198.         }
  199.  
  200.         $trigger_name $table '_AUTOINCREMENT_PK';
  201.         $trigger_name $db->quoteIdentifier($trigger_nametrue);
  202.         $trigger $db->queryOne("SELECT trigger_name FROM user_triggers WHERE trigger_name = '$trigger_name'");
  203.         if (PEAR::isError($trigger)) {
  204.             return $trigger;
  205.         }
  206.  
  207.         if ($trigger{
  208.             $trigger_sql 'DROP TRIGGER ' $trigger_name;
  209.             $result $db->exec($trigger_sql);
  210.             if (PEAR::isError($result)) {
  211.                 return $db->raiseError(MDB2_ERRORnullnull,
  212.                     '_dropAutoincrement: trigger for autoincrement PK could not be dropped');
  213.             }
  214.  
  215.             $result $db->manager->dropSequence($table);
  216.             if (PEAR::isError($result)) {
  217.                 return $db->raiseError(MDB2_ERRORnullnull,
  218.                     '_dropAutoincrement: sequence for autoincrement PK could not be dropped');
  219.             }
  220.  
  221.             $index_name  $table '_AUTOINCREMENT_PK';
  222.             $result $db->manager->dropConstraint($table$index_name);
  223.             if (PEAR::isError($result)) {
  224.                 return $db->raiseError(MDB2_ERRORnullnull,
  225.                     '_dropAutoincrement: primary key for autoincrement PK could not be dropped');
  226.             }
  227.         }
  228.  
  229.         return MDB2_OK;
  230.     }
  231.  
  232.     // }}}
  233.     // {{{ createTable()
  234.  
  235.     /**
  236.      * create a new table
  237.      *
  238.      * @param string $name     Name of the database that should be created
  239.      * @param array $fields Associative array that contains the definition of each field of the new table
  240.      *                         The indexes of the array entries are the names of the fields of the table an
  241.      *                         the array entry values are associative arrays like those that are meant to be
  242.      *                          passed with the field definitions to get[Type]Declaration() functions.
  243.      *
  244.      *                         Example
  245.      *                         array(
  246.      *
  247.      *                             'id' => array(
  248.      *                                 'type' => 'integer',
  249.      *                                 'unsigned' => 1
  250.      *                                 'notnull' => 1
  251.      *                                 'default' => 0
  252.      *                             ),
  253.      *                             'name' => array(
  254.      *                                 'type' => 'text',
  255.      *                                 'length' => 12
  256.      *                             ),
  257.      *                             'password' => array(
  258.      *                                 'type' => 'text',
  259.      *                                 'length' => 12
  260.      *                             )
  261.      *                         );
  262.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  263.      * @access public
  264.      */
  265.     function createTable($name$fields)
  266.     {
  267.         $result = parent::createTable($name$fields);
  268.         if (PEAR::isError($result)) {
  269.             return $result;
  270.         }
  271.         foreach($fields as $field_name => $field{
  272.             if (array_key_exists('autoincrement'$field&& $field['autoincrement']{
  273.                 return $this->_makeAutoincrement($field_name$name);
  274.             }
  275.         }
  276.     }
  277.  
  278.     // }}}
  279.     // {{{ dropTable()
  280.  
  281.     /**
  282.      * drop an existing table
  283.      *
  284.      * @param string $name name of the table that should be dropped
  285.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  286.      * @access public
  287.      */
  288.     function dropTable($name)
  289.     {
  290.         $result $this->_dropAutoincrement($name);
  291.         if (PEAR::isError($result)) {
  292.             return $result;
  293.         }
  294.         return parent::dropTable($name);
  295.     }
  296.  
  297.     // }}}
  298.     // {{{ alterTable()
  299.  
  300.     /**
  301.      * alter an existing table
  302.      *
  303.      * @param string $name         name of the table that is intended to be changed.
  304.      * @param array $changes     associative array that contains the details of each type
  305.      *                              of change that is intended to be performed. The types of
  306.      *                              changes that are currently supported are defined as follows:
  307.      *
  308.      *                              name
  309.      *
  310.      *                                 New name for the table.
  311.      *
  312.      *                             add
  313.      *
  314.      *                                 Associative array with the names of fields to be added as
  315.      *                                  indexes of the array. The value of each entry of the array
  316.      *                                  should be set to another associative array with the properties
  317.      *                                  of the fields to be added. The properties of the fields should
  318.      *                                  be the same as defined by the Metabase parser.
  319.      *
  320.      *
  321.      *                             remove
  322.      *
  323.      *                                 Associative array with the names of fields to be removed as indexes
  324.      *                                  of the array. Currently the values assigned to each entry are ignored.
  325.      *                                  An empty array should be used for future compatibility.
  326.      *
  327.      *                             rename
  328.      *
  329.      *                                 Associative array with the names of fields to be renamed as indexes
  330.      *                                  of the array. The value of each entry of the array should be set to
  331.      *                                  another associative array with the entry named name with the new
  332.      *                                  field name and the entry named Declaration that is expected to contain
  333.      *                                  the portion of the field declaration already in DBMS specific SQL code
  334.      *                                  as it is used in the CREATE TABLE statement.
  335.      *
  336.      *                             change
  337.      *
  338.      *                                 Associative array with the names of the fields to be changed as indexes
  339.      *                                  of the array. Keep in mind that if it is intended to change either the
  340.      *                                  name of a field and any other properties, the change array entries
  341.      *                                  should have the new names of the fields as array indexes.
  342.      *
  343.      *                                 The value of each entry of the array should be set to another associative
  344.      *                                  array with the properties of the fields to that are meant to be changed as
  345.      *                                  array entries. These entries should be assigned to the new values of the
  346.      *                                  respective properties. The properties of the fields should be the same
  347.      *                                  as defined by the Metabase parser.
  348.      *
  349.      *                             Example
  350.      *                                 array(
  351.      *                                     'name' => 'userlist',
  352.      *                                     'add' => array(
  353.      *                                         'quota' => array(
  354.      *                                             'type' => 'integer',
  355.      *                                             'unsigned' => 1
  356.      *                                         )
  357.      *                                     ),
  358.      *                                     'remove' => array(
  359.      *                                         'file_limit' => array(),
  360.      *                                         'time_limit' => array()
  361.      *                                     ),
  362.      *                                     'change' => array(
  363.      *                                         'name' => array(
  364.      *                                             'length' => '20',
  365.      *                                             'definition' => array(
  366.      *                                                 'type' => 'text',
  367.      *                                                 'length' => 20,
  368.      *                                             ),
  369.      *                                         )
  370.      *                                     ),
  371.      *                                     'rename' => array(
  372.      *                                         'sex' => array(
  373.      *                                             'name' => 'gender',
  374.      *                                             'definition' => array(
  375.      *                                                 'type' => 'text',
  376.      *                                                 'length' => 1,
  377.      *                                                 'default' => 'M',
  378.      *                                             ),
  379.      *                                         )
  380.      *                                     )
  381.      *                                 )
  382.      *
  383.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  384.      *                              can perform the requested table alterations if the value is true or
  385.      *                              actually perform them otherwise.
  386.      * @access public
  387.      *
  388.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  389.      */
  390.     function alterTable($name$changes$check)
  391.     {
  392.         $db =$this->getDBInstance();
  393.         if (PEAR::isError($db)) {
  394.             return $db;
  395.         }
  396.  
  397.         foreach ($changes as $change_name => $change{
  398.             switch ($change_name{
  399.             case 'add':
  400.             case 'remove':
  401.             case 'change':
  402.             case 'name':
  403.                 break;
  404.             case 'rename':
  405.             default:
  406.                 return $db->raiseError(MDB2_ERRORnullnull,
  407.                     'alterTable: change type "'.$change_name.'" not yet supported');
  408.             }
  409.         }
  410.  
  411.         if ($check{
  412.             return MDB2_OK;
  413.         }
  414.  
  415.         if (array_key_exists('remove'$changes)) {
  416.             $query ' DROP (';
  417.             $fields = array();
  418.             foreach ($changes['remove'as $field{
  419.                 $fields[$db->quoteIdentifier($fieldtrue);
  420.             }
  421.             $query .= ' ('implode(', '$fields')';
  422.             $name $db->quoteIdentifier($nametrue);
  423.             if (PEAR::isError($result $db->exec("ALTER TABLE $name $query"))) {
  424.                 return $result;
  425.             }
  426.         }
  427.  
  428.         $query (array_key_exists('name'$changes'RENAME TO '.$db->quoteIdentifier($changes['name']true'');
  429.  
  430.         if (array_key_exists('add'$changes)) {
  431.             foreach ($changes['add'as $field_name => $field{
  432.                 if ($query{
  433.                     $query.= ', ';
  434.                 }
  435.                 $query.= ' ADD (' $db->getDeclaration($field['type']$field_name$field$name')';
  436.             }
  437.         }
  438.  
  439.         if (array_key_exists('change'$changes)) {
  440.             foreach ($changes['change'as $field_name => $field{
  441.                 $field_name $db->quoteIdentifier($field_nametrue);
  442.                 $query.= "MODIFY ($field_name " . $db->getDeclaration($field['type']$field_name$field).')';
  443.             }
  444.         }
  445.  
  446.         if (!$query{
  447.             return MDB2_OK;
  448.         }
  449.  
  450.         $name $db->quoteIdentifier($nametrue);
  451.         return $db->exec("ALTER TABLE $name $query");
  452.     }
  453.  
  454.     // }}}
  455.     // {{{ listDatabases()
  456.  
  457.     /**
  458.      * list all databases
  459.      *
  460.      * @return mixed data array on success, a MDB2 error on failure
  461.      * @access public
  462.      */
  463.     function listDatabases()
  464.     {
  465.         $db =$this->getDBInstance();
  466.         if (PEAR::isError($db)) {
  467.             return $db;
  468.         }
  469.  
  470.         if ($db->options['database_name_prefix']{
  471.             $query 'SELECT SUBSTR(username, '
  472.                 .(strlen($db->options['database_name_prefix'])+1)
  473.                 .") FROM sys.dba_users WHERE username LIKE '"
  474.                 .$db->options['database_name_prefix']."%'";
  475.         else {
  476.             $query 'SELECT username FROM sys.dba_users';
  477.         }
  478.         $result2 $db->standaloneQuery($queryarray('text')false);
  479.         if (PEAR::isError($result2)) {
  480.             return $result2;
  481.         }
  482.         $result $result2->fetchCol();
  483.         if (PEAR::isError($result)) {
  484.             return $result;
  485.         }
  486.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  487.             && $db->options['field_case'== CASE_LOWER
  488.         {
  489.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  490.         }
  491.         $result2->free();
  492.         return $result;
  493.     }
  494.  
  495.         // }}}
  496.     // {{{ listUsers()
  497.  
  498.     /**
  499.      * list all users in the current database
  500.      *
  501.      * @return mixed data array on success, a MDB2 error on failure
  502.      * @access public
  503.      */
  504.     function listUsers()
  505.     {
  506.         $db =$this->getDBInstance();
  507.         if (PEAR::isError($db)) {
  508.             return $db;
  509.         }
  510.  
  511.         $query 'SELECT username FROM sys.all_users';
  512.         return $db->queryCol($query);
  513.     }
  514.     // }}}
  515.     // {{{ listViews()
  516.  
  517.     /**
  518.      * list all views in the current database
  519.      *
  520.      * @return mixed data array on success, a MDB2 error on failure
  521.      * @access public
  522.      */
  523.     function listViews()
  524.     {
  525.         $db =$this->getDBInstance();
  526.         if (PEAR::isError($db)) {
  527.             return $db;
  528.         }
  529.  
  530.         $query 'SELECT view_name FROM sys.user_views';
  531.         $result $db->queryCol($query);
  532.         if (PEAR::isError($result)) {
  533.             return $result;
  534.         }
  535.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  536.             && $db->options['field_case'== CASE_LOWER
  537.         {
  538.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  539.         }
  540.         return $result;
  541.     }
  542.  
  543.     // }}}
  544.     // {{{ listFunctions()
  545.  
  546.     /**
  547.      * list all functions in the current database
  548.      *
  549.      * @return mixed data array on success, a MDB2 error on failure
  550.      * @access public
  551.      */
  552.     function listFunctions()
  553.     {
  554.         $db =$this->getDBInstance();
  555.         if (PEAR::isError($db)) {
  556.             return $db;
  557.         }
  558.  
  559.         $query "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
  560.         $result $db->queryCol($query);
  561.         if (PEAR::isError($result)) {
  562.             return $result;
  563.         }
  564.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  565.             && $db->options['field_case'== CASE_LOWER
  566.         {
  567.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  568.         }
  569.         return $result;
  570.     }
  571.  
  572.     // }}}
  573.     // {{{ listTables()
  574.  
  575.     /**
  576.      * list all tables in the current database
  577.      *
  578.      * @return mixed data array on success, a MDB error on failure
  579.      * @access public
  580.      ***/
  581.     function listTables()
  582.     {
  583.         $db =$this->getDBInstance();
  584.         if (PEAR::isError($db)) {
  585.             return $db;
  586.         }
  587.  
  588.         $query 'SELECT table_name FROM sys.user_tables';
  589.         $result $db->queryCol($query);
  590.         if (PEAR::isError($result)) {
  591.             return $result;
  592.         }
  593.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  594.             && $db->options['field_case'== CASE_LOWER
  595.         {
  596.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  597.         }
  598.         return $result;
  599.     }
  600.  
  601.     // }}}
  602.     // {{{ listTableFields()
  603.  
  604.     /**
  605.      * list all fields in a tables in the current database
  606.      *
  607.      * @param string $table name of table that should be used in method
  608.      * @return mixed data array on success, a MDB error on failure
  609.      * @access public
  610.      */
  611.     function listTableFields($table)
  612.     {
  613.         $db =$this->getDBInstance();
  614.         if (PEAR::isError($db)) {
  615.             return $db;
  616.         }
  617.  
  618.         $table strtoupper($table);
  619.         $query = "SELECT column_name FROM user_tab_columns WHERE table_name='$table' ORDER BY column_id";
  620.         $result $db->queryCol($query);
  621.         if (PEAR::isError($result)) {
  622.             return $result;
  623.         }
  624.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  625.             && $db->options['field_case'== CASE_LOWER
  626.         {
  627.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  628.         }
  629.         return $result;
  630.     }
  631.  
  632.     // }}}
  633.     // {{{ listTableIndexes()
  634.  
  635.     /**
  636.      * list all indexes in a table
  637.      *
  638.      * @param string $table name of table that should be used in method
  639.      * @return mixed data array on success, a MDB2 error on failure
  640.      * @access public
  641.      */
  642.     function listTableIndexes($table)
  643.     {
  644.         $db =$this->getDBInstance();
  645.         if (PEAR::isError($db)) {
  646.             return $db;
  647.         }
  648.         $table strtoupper($table);
  649.         $query = "SELECT index_name name FROM user_indexes WHERE table_name='$table'";
  650.         $indexes $db->queryCol($query'text');
  651.         if (PEAR::isError($indexes)) {
  652.             return $indexes;
  653.         }
  654.  
  655.         $result = array();
  656.         foreach ($indexes as $index{
  657.             if ($index $this->_isIndexName($index)) {
  658.                 $result[$index= true;
  659.             }
  660.         }
  661.  
  662.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  663.             && $db->options['field_case'== CASE_LOWER
  664.         {
  665.             $result array_change_key_case($result$db->options['field_case']);
  666.         }
  667.         return array_keys($result);
  668.     }
  669.  
  670.     // }}}
  671.     // {{{ listTableConstraints()
  672.  
  673.     /**
  674.      * list all sonstraints in a table
  675.      *
  676.      * @param string    $table      name of table that should be used in method
  677.      * @return mixed data array on success, a MDB2 error on failure
  678.      * @access public
  679.      */
  680.     function listTableConstraints($table)
  681.     {
  682.         $db =$this->getDBInstance();
  683.         if (PEAR::isError($db)) {
  684.             return $db;
  685.         }
  686.         $table strtoupper($table);
  687.         $query = "SELECT index_name name FROM user_constraints WHERE table_name='$table'";
  688.         $constraints $db->queryCol($query);
  689.         if (PEAR::isError($constraints)) {
  690.             return $constraints;
  691.         }
  692.  
  693.         $result = array();
  694.         foreach ($constraints as $constraint{
  695.             if ($constraint $this->_isIndexName($constraint)) {
  696.                 $result[$constraint= true;
  697.             }
  698.         }
  699.  
  700.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  701.             && $db->options['field_case'== CASE_LOWER
  702.         {
  703.             $result array_change_key_case($result$db->options['field_case']);
  704.         }
  705.         return array_keys($result);
  706.     }
  707.  
  708.     // }}}
  709.     // {{{ createSequence()
  710.  
  711.     /**
  712.      * create sequence
  713.      *
  714.      * @param object $db database object that is extended by this class
  715.      * @param string $seq_name name of the sequence to be created
  716.      * @param string $start start value of the sequence; default is 1
  717.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  718.      * @access public
  719.      */
  720.     function createSequence($seq_name$start = 1)
  721.     {
  722.         $db =$this->getDBInstance();
  723.         if (PEAR::isError($db)) {
  724.             return $db;
  725.         }
  726.  
  727.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  728.         return $db->exec("CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1".
  729.             ($start < 1 ? " MINVALUE $start" : ''));
  730.     }
  731.  
  732.     // }}}
  733.     // {{{ dropSequence()
  734.  
  735.     /**
  736.      * drop existing sequence
  737.      *
  738.      * @param object $db database object that is extended by this class
  739.      * @param string $seq_name name of the sequence to be dropped
  740.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  741.      * @access public
  742.      */
  743.     function dropSequence($seq_name)
  744.     {
  745.         $db =$this->getDBInstance();
  746.         if (PEAR::isError($db)) {
  747.             return $db;
  748.         }
  749.  
  750.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  751.         return $db->exec("DROP SEQUENCE $sequence_name");
  752.     }
  753.  
  754.     // }}}
  755.     // {{{ listSequences()
  756.  
  757.     /**
  758.      * list all sequences in the current database
  759.      *
  760.      * @return mixed data array on success, a MDB2 error on failure
  761.      * @access public
  762.      */
  763.     function listSequences()
  764.     {
  765.         $db =$this->getDBInstance();
  766.         if (PEAR::isError($db)) {
  767.             return $db;
  768.         }
  769.  
  770.         $query "SELECT sequence_name FROM sys.user_sequences";
  771.         $table_names $db->queryCol($query);
  772.         if (PEAR::isError($table_names)) {
  773.             return $table_names;
  774.         }
  775.         $result = array();
  776.         for ($i = 0$j count($table_names)$i $j; ++$i{
  777.             if ($sqn $this->_isSequenceName($table_names[$i])) {
  778.                 $result[$sqn;
  779.             }
  780.         }
  781.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  782.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  783.         }
  784.         return $result;
  785.     }
  786. }
  787. ?>

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