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

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