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.58 2006/01/13 14:53:06 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 => true),
  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.         if (is_null($start)) {
  154.             $db->beginTransaction();
  155.             $query 'SELECT MAX(' $db->quoteIdentifier($nametrue') FROM ' $db->quoteIdentifier($tabletrue);
  156.             $start $this->db->queryOne($query'integer');
  157.             if (PEAR::isError($start)) {
  158.                 return $start;
  159.             }
  160.             ++$start;
  161.             $result $db->manager->createSequence($table$start);
  162.             $db->commit();
  163.         else {
  164.             $result $db->manager->createSequence($table$start);
  165.         }
  166.         if (PEAR::isError($result)) {
  167.             return $db->raiseError(MDB2_ERRORnullnull,
  168.                 '_makeAutoincrement: sequence for autoincrement PK could not be created');
  169.         }
  170.         $sequence_name $db->getSequenceName($table);
  171.         $trigger_name  $db->quoteIdentifier($table '_AUTOINCREMENT_PK'true);
  172.         $table $db->quoteIdentifier($tabletrue);
  173.         $name  $db->quoteIdentifier($nametrue);
  174.         $trigger_sql = "CREATE TRIGGER $trigger_name BEFORE INSERT ON $table";
  175.         $trigger_sql.= " FOR EACH ROW BEGIN IF (:new.$name IS NULL) THEN SELECT ";
  176.         $trigger_sql.= "$sequence_name.NEXTVAL INTO :new.$name FROM DUAL; END IF; END;";
  177.         return $db->exec($trigger_sql);
  178.     }
  179.  
  180.     // }}}
  181.     // {{{ _dropAutoincrement()
  182.  
  183.     /**
  184.      * drop an existing autoincrement sequence + trigger
  185.      *
  186.      * @param string $table name of the table
  187.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  188.      * @access private
  189.      */
  190.     function _dropAutoincrement($table)
  191.     {
  192.         $db =$this->getDBInstance();
  193.         if (PEAR::isError($db)) {
  194.             return $db;
  195.         }
  196.  
  197.         $trigger_name $table '_AUTOINCREMENT_PK';
  198.         $trigger_name $db->quoteIdentifier($trigger_nametrue);
  199.         $trigger $db->queryOne("SELECT trigger_name FROM user_triggers WHERE trigger_name = '$trigger_name'");
  200.         if (PEAR::isError($trigger)) {
  201.             return $trigger;
  202.         }
  203.  
  204.         if ($trigger{
  205.             $trigger_sql 'DROP TRIGGER ' $trigger_name;
  206.             $result $db->exec($trigger_sql);
  207.             if (PEAR::isError($result)) {
  208.                 return $db->raiseError(MDB2_ERRORnullnull,
  209.                     '_dropAutoincrement: trigger for autoincrement PK could not be dropped');
  210.             }
  211.  
  212.             $result $db->manager->dropSequence($table);
  213.             if (PEAR::isError($result)) {
  214.                 return $db->raiseError(MDB2_ERRORnullnull,
  215.                     '_dropAutoincrement: sequence for autoincrement PK could not be dropped');
  216.             }
  217.  
  218.             $index_name  $table '_AUTOINCREMENT_PK';
  219.             $result $db->manager->dropConstraint($table$index_name);
  220.             if (PEAR::isError($result)) {
  221.                 return $db->raiseError(MDB2_ERRORnullnull,
  222.                     '_dropAutoincrement: primary key for autoincrement PK could not be dropped');
  223.             }
  224.         }
  225.  
  226.         return MDB2_OK;
  227.     }
  228.  
  229.     // }}}
  230.     // {{{ createTable()
  231.  
  232.     /**
  233.      * create a new table
  234.      *
  235.      * @param string $name     Name of the database that should be created
  236.      * @param array $fields Associative array that contains the definition of each field of the new table
  237.      *                         The indexes of the array entries are the names of the fields of the table an
  238.      *                         the array entry values are associative arrays like those that are meant to be
  239.      *                          passed with the field definitions to get[Type]Declaration() functions.
  240.      *
  241.      *                         Example
  242.      *                         array(
  243.      *
  244.      *                             'id' => array(
  245.      *                                 'type' => 'integer',
  246.      *                                 'unsigned' => 1
  247.      *                                 'notnull' => 1
  248.      *                                 'default' => 0
  249.      *                             ),
  250.      *                             'name' => array(
  251.      *                                 'type' => 'text',
  252.      *                                 'length' => 12
  253.      *                             ),
  254.      *                             'password' => array(
  255.      *                                 'type' => 'text',
  256.      *                                 'length' => 12
  257.      *                             )
  258.      *                         );
  259.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  260.      * @access public
  261.      */
  262.     function createTable($name$fields)
  263.     {
  264.         $result = parent::createTable($name$fields);
  265.         if (PEAR::isError($result)) {
  266.             return $result;
  267.         }
  268.         foreach ($fields as $field_name => $field{
  269.             if (array_key_exists('autoincrement'$field&& $field['autoincrement']{
  270.                 return $this->_makeAutoincrement($field_name$name);
  271.             }
  272.         }
  273.     }
  274.  
  275.     // }}}
  276.     // {{{ dropTable()
  277.  
  278.     /**
  279.      * drop an existing table
  280.      *
  281.      * @param string $name name of the table that should be dropped
  282.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  283.      * @access public
  284.      */
  285.     function dropTable($name)
  286.     {
  287.         $result $this->_dropAutoincrement($name);
  288.         if (PEAR::isError($result)) {
  289.             return $result;
  290.         }
  291.         return parent::dropTable($name);
  292.     }
  293.  
  294.     // }}}
  295.     // {{{ alterTable()
  296.  
  297.     /**
  298.      * alter an existing table
  299.      *
  300.      * @param string $name         name of the table that is intended to be changed.
  301.      * @param array $changes     associative array that contains the details of each type
  302.      *                              of change that is intended to be performed. The types of
  303.      *                              changes that are currently supported are defined as follows:
  304.      *
  305.      *                              name
  306.      *
  307.      *                                 New name for the table.
  308.      *
  309.      *                             add
  310.      *
  311.      *                                 Associative array with the names of fields to be added as
  312.      *                                  indexes of the array. The value of each entry of the array
  313.      *                                  should be set to another associative array with the properties
  314.      *                                  of the fields to be added. The properties of the fields should
  315.      *                                  be the same as defined by the Metabase parser.
  316.      *
  317.      *
  318.      *                             remove
  319.      *
  320.      *                                 Associative array with the names of fields to be removed as indexes
  321.      *                                  of the array. Currently the values assigned to each entry are ignored.
  322.      *                                  An empty array should be used for future compatibility.
  323.      *
  324.      *                             rename
  325.      *
  326.      *                                 Associative array with the names of fields to be renamed as indexes
  327.      *                                  of the array. The value of each entry of the array should be set to
  328.      *                                  another associative array with the entry named name with the new
  329.      *                                  field name and the entry named Declaration that is expected to contain
  330.      *                                  the portion of the field declaration already in DBMS specific SQL code
  331.      *                                  as it is used in the CREATE TABLE statement.
  332.      *
  333.      *                             change
  334.      *
  335.      *                                 Associative array with the names of the fields to be changed as indexes
  336.      *                                  of the array. Keep in mind that if it is intended to change either the
  337.      *                                  name of a field and any other properties, the change array entries
  338.      *                                  should have the new names of the fields as array indexes.
  339.      *
  340.      *                                 The value of each entry of the array should be set to another associative
  341.      *                                  array with the properties of the fields to that are meant to be changed as
  342.      *                                  array entries. These entries should be assigned to the new values of the
  343.      *                                  respective properties. The properties of the fields should be the same
  344.      *                                  as defined by the Metabase parser.
  345.      *
  346.      *                             Example
  347.      *                                 array(
  348.      *                                     'name' => 'userlist',
  349.      *                                     'add' => array(
  350.      *                                         'quota' => array(
  351.      *                                             'type' => 'integer',
  352.      *                                             'unsigned' => 1
  353.      *                                         )
  354.      *                                     ),
  355.      *                                     'remove' => array(
  356.      *                                         'file_limit' => array(),
  357.      *                                         'time_limit' => array()
  358.      *                                     ),
  359.      *                                     'change' => array(
  360.      *                                         'name' => array(
  361.      *                                             'length' => '20',
  362.      *                                             'definition' => array(
  363.      *                                                 'type' => 'text',
  364.      *                                                 'length' => 20,
  365.      *                                             ),
  366.      *                                         )
  367.      *                                     ),
  368.      *                                     'rename' => array(
  369.      *                                         'sex' => array(
  370.      *                                             'name' => 'gender',
  371.      *                                             'definition' => array(
  372.      *                                                 'type' => 'text',
  373.      *                                                 'length' => 1,
  374.      *                                                 'default' => 'M',
  375.      *                                             ),
  376.      *                                         )
  377.      *                                     )
  378.      *                                 )
  379.      *
  380.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  381.      *                              can perform the requested table alterations if the value is true or
  382.      *                              actually perform them otherwise.
  383.      * @access public
  384.      *
  385.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  386.      */
  387.     function alterTable($name$changes$check)
  388.     {
  389.         $db =$this->getDBInstance();
  390.         if (PEAR::isError($db)) {
  391.             return $db;
  392.         }
  393.  
  394.         foreach ($changes as $change_name => $change{
  395.             switch ($change_name{
  396.             case 'add':
  397.             case 'remove':
  398.             case 'change':
  399.             case 'name':
  400.             case 'rename':
  401.                 break;
  402.             default:
  403.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  404.                     'alterTable: change type "'.$change_name.'" not yet supported');
  405.             }
  406.         }
  407.  
  408.         if ($check{
  409.             return MDB2_OK;
  410.         }
  411.  
  412.         $name $db->quoteIdentifier($nametrue);
  413.  
  414.         if (array_key_exists('add'$changes)) {
  415.             $fields = array();
  416.             foreach ($changes['add'as $field_name => $field{
  417.                 $fields[$db->getDeclaration($field['type']$field_name$field$name);
  418.             }
  419.             $result $db->exec("ALTER TABLE $name ADD (". implode(', '$fields).')');
  420.             if (PEAR::isError($result)) {
  421.                 return $result;
  422.             }
  423.         }
  424.  
  425.         if (array_key_exists('change'$changes)) {
  426.             $fields = array();
  427.             foreach ($changes['change'as $field_name => $field{
  428.                 $fields[$field_name' ' $db->getDeclaration($field['definition']['type']''$field['definition']);
  429.             }
  430.             $result $db->exec("ALTER TABLE $name MODIFY (". implode(', '$fields).')');
  431.             if (PEAR::isError($result)) {
  432.                 return $result;
  433.             }
  434.         }
  435.  
  436.         if (array_key_exists('rename'$changes)) {
  437.             foreach ($changes['rename'as $field_name => $field{
  438.                 $field_name $db->quoteIdentifier($field_nametrue);
  439.                 $result $db->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name']true));
  440.                 if (PEAR::isError($result)) {
  441.                     return $result;
  442.                 }
  443.             }
  444.         }
  445.  
  446.         if (array_key_exists('remove'$changes)) {
  447.             $fields = array();
  448.             foreach ($changes['remove'as $field_name => $field{
  449.                 $fields[$db->quoteIdentifier($field_nametrue);
  450.             }
  451.             $result $db->exec("ALTER TABLE $name DROP COLUMN ". implode(', '$fields));
  452.             if (PEAR::isError($result)) {
  453.                 return $result;
  454.             }
  455.         }
  456.  
  457.         if (array_key_exists('name'$changes)) {
  458.             $change_name $db->quoteIdentifier($changes['name']true);
  459.             $result $db->exec("ALTER TABLE $name RENAME TO ".$change_name);
  460.             if (PEAR::isError($result)) {
  461.                 return $result;
  462.             }
  463.         }
  464.  
  465.         return MDB2_OK;
  466.     }
  467.  
  468.     // }}}
  469.     // {{{ listDatabases()
  470.  
  471.     /**
  472.      * list all databases
  473.      *
  474.      * @return mixed data array on success, a MDB2 error on failure
  475.      * @access public
  476.      */
  477.     function listDatabases()
  478.     {
  479.         $db =$this->getDBInstance();
  480.         if (PEAR::isError($db)) {
  481.             return $db;
  482.         }
  483.  
  484.         if ($db->options['database_name_prefix']{
  485.             $query 'SELECT SUBSTR(username, '
  486.                 .(strlen($db->options['database_name_prefix'])+1)
  487.                 .") FROM sys.dba_users WHERE username LIKE '"
  488.                 .$db->options['database_name_prefix']."%'";
  489.         else {
  490.             $query 'SELECT username FROM sys.dba_users';
  491.         }
  492.         $result2 $db->standaloneQuery($queryarray('text')false);
  493.         if (PEAR::isError($result2)) {
  494.             return $result2;
  495.         }
  496.         $result $result2->fetchCol();
  497.         if (PEAR::isError($result)) {
  498.             return $result;
  499.         }
  500.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  501.             && $db->options['field_case'== CASE_LOWER
  502.         {
  503.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  504.         }
  505.         $result2->free();
  506.         return $result;
  507.     }
  508.  
  509.         // }}}
  510.     // {{{ listUsers()
  511.  
  512.     /**
  513.      * list all users in the current database
  514.      *
  515.      * @return mixed data array on success, a MDB2 error on failure
  516.      * @access public
  517.      */
  518.     function listUsers()
  519.     {
  520.         $db =$this->getDBInstance();
  521.         if (PEAR::isError($db)) {
  522.             return $db;
  523.         }
  524.  
  525.         $query 'SELECT username FROM sys.all_users';
  526.         return $db->queryCol($query);
  527.     }
  528.     // }}}
  529.     // {{{ listViews()
  530.  
  531.     /**
  532.      * list all views in the current database
  533.      *
  534.      * @return mixed data array on success, a MDB2 error on failure
  535.      * @access public
  536.      */
  537.     function listViews()
  538.     {
  539.         $db =$this->getDBInstance();
  540.         if (PEAR::isError($db)) {
  541.             return $db;
  542.         }
  543.  
  544.         $query 'SELECT view_name FROM sys.user_views';
  545.         $result $db->queryCol($query);
  546.         if (PEAR::isError($result)) {
  547.             return $result;
  548.         }
  549.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  550.             && $db->options['field_case'== CASE_LOWER
  551.         {
  552.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  553.         }
  554.         return $result;
  555.     }
  556.  
  557.     // }}}
  558.     // {{{ listFunctions()
  559.  
  560.     /**
  561.      * list all functions in the current database
  562.      *
  563.      * @return mixed data array on success, a MDB2 error on failure
  564.      * @access public
  565.      */
  566.     function listFunctions()
  567.     {
  568.         $db =$this->getDBInstance();
  569.         if (PEAR::isError($db)) {
  570.             return $db;
  571.         }
  572.  
  573.         $query "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
  574.         $result $db->queryCol($query);
  575.         if (PEAR::isError($result)) {
  576.             return $result;
  577.         }
  578.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  579.             && $db->options['field_case'== CASE_LOWER
  580.         {
  581.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  582.         }
  583.         return $result;
  584.     }
  585.  
  586.     // }}}
  587.     // {{{ listTables()
  588.  
  589.     /**
  590.      * list all tables in the current database
  591.      *
  592.      * @return mixed data array on success, a MDB error on failure
  593.      * @access public
  594.      ***/
  595.     function listTables()
  596.     {
  597.         $db =$this->getDBInstance();
  598.         if (PEAR::isError($db)) {
  599.             return $db;
  600.         }
  601.  
  602.         $query 'SELECT table_name FROM sys.user_tables';
  603.         $result $db->queryCol($query);
  604.         if (PEAR::isError($result)) {
  605.             return $result;
  606.         }
  607.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  608.             && $db->options['field_case'== CASE_LOWER
  609.         {
  610.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  611.         }
  612.         return $result;
  613.     }
  614.  
  615.     // }}}
  616.     // {{{ listTableFields()
  617.  
  618.     /**
  619.      * list all fields in a tables in the current database
  620.      *
  621.      * @param string $table name of table that should be used in method
  622.      * @return mixed data array on success, a MDB error on failure
  623.      * @access public
  624.      */
  625.     function listTableFields($table)
  626.     {
  627.         $db =$this->getDBInstance();
  628.         if (PEAR::isError($db)) {
  629.             return $db;
  630.         }
  631.  
  632.         $table strtoupper($table);
  633.         $query = "SELECT column_name FROM user_tab_columns WHERE table_name='$table' ORDER BY column_id";
  634.         $result $db->queryCol($query);
  635.         if (PEAR::isError($result)) {
  636.             return $result;
  637.         }
  638.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  639.             && $db->options['field_case'== CASE_LOWER
  640.         {
  641.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  642.         }
  643.         return $result;
  644.     }
  645.  
  646.     // }}}
  647.     // {{{ listTableIndexes()
  648.  
  649.     /**
  650.      * list all indexes in a table
  651.      *
  652.      * @param string $table name of table that should be used in method
  653.      * @return mixed data array on success, a MDB2 error on failure
  654.      * @access public
  655.      */
  656.     function listTableIndexes($table)
  657.     {
  658.         $db =$this->getDBInstance();
  659.         if (PEAR::isError($db)) {
  660.             return $db;
  661.         }
  662.         $table strtoupper($table);
  663.         $query = "SELECT index_name name FROM user_indexes WHERE table_name='$table'";
  664.         $indexes $db->queryCol($query'text');
  665.         if (PEAR::isError($indexes)) {
  666.             return $indexes;
  667.         }
  668.  
  669.         $result = array();
  670.         foreach ($indexes as $index{
  671.             $result[$this->_fixIndexName($index)= true;
  672.         }
  673.  
  674.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  675.             && $db->options['field_case'== CASE_LOWER
  676.         {
  677.             $result array_change_key_case($result$db->options['field_case']);
  678.         }
  679.         return array_keys($result);
  680.     }
  681.  
  682.     // }}}
  683.     // {{{ listTableConstraints()
  684.  
  685.     /**
  686.      * list all sonstraints in a table
  687.      *
  688.      * @param string    $table      name of table that should be used in method
  689.      * @return mixed data array on success, a MDB2 error on failure
  690.      * @access public
  691.      */
  692.     function listTableConstraints($table)
  693.     {
  694.         $db =$this->getDBInstance();
  695.         if (PEAR::isError($db)) {
  696.             return $db;
  697.         }
  698.         $table strtoupper($table);
  699.         $query = "SELECT index_name name FROM user_constraints WHERE table_name='$table'";
  700.         $constraints $db->queryCol($query);
  701.         if (PEAR::isError($constraints)) {
  702.             return $constraints;
  703.         }
  704.  
  705.         $result = array();
  706.         foreach ($constraints as $constraint{
  707.             $result[$this->_fixIndexName($constraint)= true;
  708.         }
  709.  
  710.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  711.             && $db->options['field_case'== CASE_LOWER
  712.         {
  713.             $result array_change_key_case($result$db->options['field_case']);
  714.         }
  715.         return array_keys($result);
  716.     }
  717.  
  718.     // }}}
  719.     // {{{ createSequence()
  720.  
  721.     /**
  722.      * create sequence
  723.      *
  724.      * @param object $db database object that is extended by this class
  725.      * @param string $seq_name name of the sequence to be created
  726.      * @param string $start start value of the sequence; default is 1
  727.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  728.      * @access public
  729.      */
  730.     function createSequence($seq_name$start = 1)
  731.     {
  732.         $db =$this->getDBInstance();
  733.         if (PEAR::isError($db)) {
  734.             return $db;
  735.         }
  736.  
  737.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  738.         $query = "CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1";
  739.         $query.= ($start < 1 ? " MINVALUE $start" : '');
  740.         return $db->exec($query);
  741.     }
  742.  
  743.     // }}}
  744.     // {{{ dropSequence()
  745.  
  746.     /**
  747.      * drop existing sequence
  748.      *
  749.      * @param object $db database object that is extended by this class
  750.      * @param string $seq_name name of the sequence to be dropped
  751.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  752.      * @access public
  753.      */
  754.     function dropSequence($seq_name)
  755.     {
  756.         $db =$this->getDBInstance();
  757.         if (PEAR::isError($db)) {
  758.             return $db;
  759.         }
  760.  
  761.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  762.         return $db->exec("DROP SEQUENCE $sequence_name");
  763.     }
  764.  
  765.     // }}}
  766.     // {{{ listSequences()
  767.  
  768.     /**
  769.      * list all sequences in the current database
  770.      *
  771.      * @return mixed data array on success, a MDB2 error on failure
  772.      * @access public
  773.      */
  774.     function listSequences()
  775.     {
  776.         $db =$this->getDBInstance();
  777.         if (PEAR::isError($db)) {
  778.             return $db;
  779.         }
  780.  
  781.         $query "SELECT sequence_name FROM sys.user_sequences";
  782.         $table_names $db->queryCol($query);
  783.         if (PEAR::isError($table_names)) {
  784.             return $table_names;
  785.         }
  786.         $result = array();
  787.         foreach ($table_names as $table_name{
  788.             $result[$this->_fixSequenceName($table_name);
  789.         }
  790.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  791.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  792.         }
  793.         return $result;
  794.     }
  795. }
  796. ?>

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