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.56 2005/12/28 10:14:57 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.             case 'rename':
  404.                 break;
  405.             default:
  406.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  407.                     'alterTable: change type "'.$change_name.'" not yet supported');
  408.             }
  409.         }
  410.  
  411.         if ($check{
  412.             return MDB2_OK;
  413.         }
  414.  
  415.         $name $db->quoteIdentifier($nametrue);
  416.  
  417.         if (array_key_exists('add'$changes)) {
  418.             $fields = array();
  419.             foreach ($changes['add'as $field_name => $field{
  420.                 $fields[$db->getDeclaration($field['type']$field_name$field$name);
  421.             }
  422.             $result $db->exec("ALTER TABLE $name ADD (". implode(', '$fields).')');
  423.             if (PEAR::isError($result)) {
  424.                 return $result;
  425.             }
  426.         }
  427.  
  428.         if (array_key_exists('change'$changes)) {
  429.             $fields = array();
  430.             foreach ($changes['change'as $field_name => $field{
  431.                 $fields[$field_name' ' $db->getDeclaration($field['definition']['type']''$field['definition']);
  432.             }
  433.             $result $db->exec("ALTER TABLE $name MODIFY (". implode(', '$fields).')');
  434.             if (PEAR::isError($result)) {
  435.                 return $result;
  436.             }
  437.         }
  438.  
  439.         if (array_key_exists('rename'$changes)) {
  440.             foreach ($changes['rename'as $field_name => $field{
  441.                 $field_name $db->quoteIdentifier($field_nametrue);
  442.                 $result $db->exec("ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name']true));
  443.                 if (PEAR::isError($result)) {
  444.                     return $result;
  445.                 }
  446.             }
  447.         }
  448.  
  449.         if (array_key_exists('remove'$changes)) {
  450.             $fields = array();
  451.             foreach ($changes['remove'as $field_name => $field{
  452.                 $fields[$db->quoteIdentifier($field_nametrue);
  453.             }
  454.             $result $db->exec("ALTER TABLE $name DROP COLUMN ". implode(', '$fields));
  455.             if (PEAR::isError($result)) {
  456.                 return $result;
  457.             }
  458.         }
  459.  
  460.         if (array_key_exists('name'$changes)) {
  461.             $change_name $db->quoteIdentifier($changes['name']true);
  462.             $result $db->exec("ALTER TABLE $name RENAME TO ".$change_name);
  463.             if (PEAR::isError($result)) {
  464.                 return $result;
  465.             }
  466.         }
  467.  
  468.         return MDB2_OK;
  469.     }
  470.  
  471.     // }}}
  472.     // {{{ listDatabases()
  473.  
  474.     /**
  475.      * list all databases
  476.      *
  477.      * @return mixed data array on success, a MDB2 error on failure
  478.      * @access public
  479.      */
  480.     function listDatabases()
  481.     {
  482.         $db =$this->getDBInstance();
  483.         if (PEAR::isError($db)) {
  484.             return $db;
  485.         }
  486.  
  487.         if ($db->options['database_name_prefix']{
  488.             $query 'SELECT SUBSTR(username, '
  489.                 .(strlen($db->options['database_name_prefix'])+1)
  490.                 .") FROM sys.dba_users WHERE username LIKE '"
  491.                 .$db->options['database_name_prefix']."%'";
  492.         else {
  493.             $query 'SELECT username FROM sys.dba_users';
  494.         }
  495.         $result2 $db->standaloneQuery($queryarray('text')false);
  496.         if (PEAR::isError($result2)) {
  497.             return $result2;
  498.         }
  499.         $result $result2->fetchCol();
  500.         if (PEAR::isError($result)) {
  501.             return $result;
  502.         }
  503.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  504.             && $db->options['field_case'== CASE_LOWER
  505.         {
  506.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  507.         }
  508.         $result2->free();
  509.         return $result;
  510.     }
  511.  
  512.         // }}}
  513.     // {{{ listUsers()
  514.  
  515.     /**
  516.      * list all users in the current database
  517.      *
  518.      * @return mixed data array on success, a MDB2 error on failure
  519.      * @access public
  520.      */
  521.     function listUsers()
  522.     {
  523.         $db =$this->getDBInstance();
  524.         if (PEAR::isError($db)) {
  525.             return $db;
  526.         }
  527.  
  528.         $query 'SELECT username FROM sys.all_users';
  529.         return $db->queryCol($query);
  530.     }
  531.     // }}}
  532.     // {{{ listViews()
  533.  
  534.     /**
  535.      * list all views in the current database
  536.      *
  537.      * @return mixed data array on success, a MDB2 error on failure
  538.      * @access public
  539.      */
  540.     function listViews()
  541.     {
  542.         $db =$this->getDBInstance();
  543.         if (PEAR::isError($db)) {
  544.             return $db;
  545.         }
  546.  
  547.         $query 'SELECT view_name FROM sys.user_views';
  548.         $result $db->queryCol($query);
  549.         if (PEAR::isError($result)) {
  550.             return $result;
  551.         }
  552.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  553.             && $db->options['field_case'== CASE_LOWER
  554.         {
  555.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  556.         }
  557.         return $result;
  558.     }
  559.  
  560.     // }}}
  561.     // {{{ listFunctions()
  562.  
  563.     /**
  564.      * list all functions in the current database
  565.      *
  566.      * @return mixed data array on success, a MDB2 error on failure
  567.      * @access public
  568.      */
  569.     function listFunctions()
  570.     {
  571.         $db =$this->getDBInstance();
  572.         if (PEAR::isError($db)) {
  573.             return $db;
  574.         }
  575.  
  576.         $query "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
  577.         $result $db->queryCol($query);
  578.         if (PEAR::isError($result)) {
  579.             return $result;
  580.         }
  581.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  582.             && $db->options['field_case'== CASE_LOWER
  583.         {
  584.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  585.         }
  586.         return $result;
  587.     }
  588.  
  589.     // }}}
  590.     // {{{ listTables()
  591.  
  592.     /**
  593.      * list all tables in the current database
  594.      *
  595.      * @return mixed data array on success, a MDB error on failure
  596.      * @access public
  597.      ***/
  598.     function listTables()
  599.     {
  600.         $db =$this->getDBInstance();
  601.         if (PEAR::isError($db)) {
  602.             return $db;
  603.         }
  604.  
  605.         $query 'SELECT table_name FROM sys.user_tables';
  606.         $result $db->queryCol($query);
  607.         if (PEAR::isError($result)) {
  608.             return $result;
  609.         }
  610.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  611.             && $db->options['field_case'== CASE_LOWER
  612.         {
  613.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  614.         }
  615.         return $result;
  616.     }
  617.  
  618.     // }}}
  619.     // {{{ listTableFields()
  620.  
  621.     /**
  622.      * list all fields in a tables in the current database
  623.      *
  624.      * @param string $table name of table that should be used in method
  625.      * @return mixed data array on success, a MDB error on failure
  626.      * @access public
  627.      */
  628.     function listTableFields($table)
  629.     {
  630.         $db =$this->getDBInstance();
  631.         if (PEAR::isError($db)) {
  632.             return $db;
  633.         }
  634.  
  635.         $table strtoupper($table);
  636.         $query = "SELECT column_name FROM user_tab_columns WHERE table_name='$table' ORDER BY column_id";
  637.         $result $db->queryCol($query);
  638.         if (PEAR::isError($result)) {
  639.             return $result;
  640.         }
  641.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  642.             && $db->options['field_case'== CASE_LOWER
  643.         {
  644.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  645.         }
  646.         return $result;
  647.     }
  648.  
  649.     // }}}
  650.     // {{{ listTableIndexes()
  651.  
  652.     /**
  653.      * list all indexes in a table
  654.      *
  655.      * @param string $table name of table that should be used in method
  656.      * @return mixed data array on success, a MDB2 error on failure
  657.      * @access public
  658.      */
  659.     function listTableIndexes($table)
  660.     {
  661.         $db =$this->getDBInstance();
  662.         if (PEAR::isError($db)) {
  663.             return $db;
  664.         }
  665.         $table strtoupper($table);
  666.         $query = "SELECT index_name name FROM user_indexes WHERE table_name='$table'";
  667.         $indexes $db->queryCol($query'text');
  668.         if (PEAR::isError($indexes)) {
  669.             return $indexes;
  670.         }
  671.  
  672.         $result = array();
  673.         foreach ($indexes as $index{
  674.             if ($index $this->_isIndexName($index)) {
  675.                 $result[$index= true;
  676.             }
  677.         }
  678.  
  679.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  680.             && $db->options['field_case'== CASE_LOWER
  681.         {
  682.             $result array_change_key_case($result$db->options['field_case']);
  683.         }
  684.         return array_keys($result);
  685.     }
  686.  
  687.     // }}}
  688.     // {{{ listTableConstraints()
  689.  
  690.     /**
  691.      * list all sonstraints in a table
  692.      *
  693.      * @param string    $table      name of table that should be used in method
  694.      * @return mixed data array on success, a MDB2 error on failure
  695.      * @access public
  696.      */
  697.     function listTableConstraints($table)
  698.     {
  699.         $db =$this->getDBInstance();
  700.         if (PEAR::isError($db)) {
  701.             return $db;
  702.         }
  703.         $table strtoupper($table);
  704.         $query = "SELECT index_name name FROM user_constraints WHERE table_name='$table'";
  705.         $constraints $db->queryCol($query);
  706.         if (PEAR::isError($constraints)) {
  707.             return $constraints;
  708.         }
  709.  
  710.         $result = array();
  711.         foreach ($constraints as $constraint{
  712.             if ($constraint $this->_isIndexName($constraint)) {
  713.                 $result[$constraint= true;
  714.             }
  715.         }
  716.  
  717.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  718.             && $db->options['field_case'== CASE_LOWER
  719.         {
  720.             $result array_change_key_case($result$db->options['field_case']);
  721.         }
  722.         return array_keys($result);
  723.     }
  724.  
  725.     // }}}
  726.     // {{{ createSequence()
  727.  
  728.     /**
  729.      * create sequence
  730.      *
  731.      * @param object $db database object that is extended by this class
  732.      * @param string $seq_name name of the sequence to be created
  733.      * @param string $start start value of the sequence; default is 1
  734.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  735.      * @access public
  736.      */
  737.     function createSequence($seq_name$start = 1)
  738.     {
  739.         $db =$this->getDBInstance();
  740.         if (PEAR::isError($db)) {
  741.             return $db;
  742.         }
  743.  
  744.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  745.         return $db->exec("CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1".
  746.             ($start < 1 ? " MINVALUE $start" : ''));
  747.     }
  748.  
  749.     // }}}
  750.     // {{{ dropSequence()
  751.  
  752.     /**
  753.      * drop existing sequence
  754.      *
  755.      * @param object $db database object that is extended by this class
  756.      * @param string $seq_name name of the sequence to be dropped
  757.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  758.      * @access public
  759.      */
  760.     function dropSequence($seq_name)
  761.     {
  762.         $db =$this->getDBInstance();
  763.         if (PEAR::isError($db)) {
  764.             return $db;
  765.         }
  766.  
  767.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  768.         return $db->exec("DROP SEQUENCE $sequence_name");
  769.     }
  770.  
  771.     // }}}
  772.     // {{{ listSequences()
  773.  
  774.     /**
  775.      * list all sequences in the current database
  776.      *
  777.      * @return mixed data array on success, a MDB2 error on failure
  778.      * @access public
  779.      */
  780.     function listSequences()
  781.     {
  782.         $db =$this->getDBInstance();
  783.         if (PEAR::isError($db)) {
  784.             return $db;
  785.         }
  786.  
  787.         $query "SELECT sequence_name FROM sys.user_sequences";
  788.         $table_names $db->queryCol($query);
  789.         if (PEAR::isError($table_names)) {
  790.             return $table_names;
  791.         }
  792.         $result = array();
  793.         for ($i = 0$j count($table_names)$i $j; ++$i{
  794.             if ($sqn $this->_isSequenceName($table_names[$i])) {
  795.                 $result[$sqn;
  796.             }
  797.         }
  798.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  799.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  800.         }
  801.         return $result;
  802.     }
  803. }
  804. ?>

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