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.74 2006/07/22 08:39:34 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.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  294.      * @access public
  295.      */
  296.     function createTable($name$fields)
  297.     {
  298.         $db =$this->getDBInstance();
  299.         if (PEAR::isError($db)) {
  300.             return $db;
  301.         }
  302.         $db->beginNestedTransaction();
  303.         $result = parent::createTable($name$fields);
  304.         if (!PEAR::isError($result)) {
  305.             foreach ($fields as $field_name => $field{
  306.                 if (!empty($field['autoincrement'])) {
  307.                     $result $this->_makeAutoincrement($field_name$name);
  308.                 }
  309.             }
  310.         }
  311.         $db->completeNestedTransaction();
  312.         return $result;
  313.     }
  314.  
  315.     // }}}
  316.     // {{{ dropTable()
  317.  
  318.     /**
  319.      * drop an existing table
  320.      *
  321.      * @param string $name name of the table that should be dropped
  322.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  323.      * @access public
  324.      */
  325.     function dropTable($name)
  326.     {
  327.         $db =$this->getDBInstance();
  328.         if (PEAR::isError($db)) {
  329.             return $db;
  330.         }
  331.         $db->beginNestedTransaction();
  332.         $result $this->_dropAutoincrement($name);
  333.         if (!PEAR::isError($result)) {
  334.             $result = parent::dropTable($name);
  335.         }
  336.         $db->completeNestedTransaction();
  337.         return $result;
  338.     }
  339.  
  340.     // }}}
  341.     // {{{ alterTable()
  342.  
  343.     /**
  344.      * alter an existing table
  345.      *
  346.      * @param string $name         name of the table that is intended to be changed.
  347.      * @param array $changes     associative array that contains the details of each type
  348.      *                              of change that is intended to be performed. The types of
  349.      *                              changes that are currently supported are defined as follows:
  350.      *
  351.      *                              name
  352.      *
  353.      *                                 New name for the table.
  354.      *
  355.      *                             add
  356.      *
  357.      *                                 Associative array with the names of fields to be added as
  358.      *                                  indexes of the array. The value of each entry of the array
  359.      *                                  should be set to another associative array with the properties
  360.      *                                  of the fields to be added. The properties of the fields should
  361.      *                                  be the same as defined by the Metabase parser.
  362.      *
  363.      *
  364.      *                             remove
  365.      *
  366.      *                                 Associative array with the names of fields to be removed as indexes
  367.      *                                  of the array. Currently the values assigned to each entry are ignored.
  368.      *                                  An empty array should be used for future compatibility.
  369.      *
  370.      *                             rename
  371.      *
  372.      *                                 Associative array with the names of fields to be renamed as indexes
  373.      *                                  of the array. The value of each entry of the array should be set to
  374.      *                                  another associative array with the entry named name with the new
  375.      *                                  field name and the entry named Declaration that is expected to contain
  376.      *                                  the portion of the field declaration already in DBMS specific SQL code
  377.      *                                  as it is used in the CREATE TABLE statement.
  378.      *
  379.      *                             change
  380.      *
  381.      *                                 Associative array with the names of the fields to be changed as indexes
  382.      *                                  of the array. Keep in mind that if it is intended to change either the
  383.      *                                  name of a field and any other properties, the change array entries
  384.      *                                  should have the new names of the fields as array indexes.
  385.      *
  386.      *                                 The value of each entry of the array should be set to another associative
  387.      *                                  array with the properties of the fields to that are meant to be changed as
  388.      *                                  array entries. These entries should be assigned to the new values of the
  389.      *                                  respective properties. The properties of the fields should be the same
  390.      *                                  as defined by the Metabase parser.
  391.      *
  392.      *                             Example
  393.      *                                 array(
  394.      *                                     'name' => 'userlist',
  395.      *                                     'add' => array(
  396.      *                                         'quota' => array(
  397.      *                                             'type' => 'integer',
  398.      *                                             'unsigned' => 1
  399.      *                                         )
  400.      *                                     ),
  401.      *                                     'remove' => array(
  402.      *                                         'file_limit' => array(),
  403.      *                                         'time_limit' => array()
  404.      *                                     ),
  405.      *                                     'change' => array(
  406.      *                                         'name' => array(
  407.      *                                             'length' => '20',
  408.      *                                             'definition' => array(
  409.      *                                                 'type' => 'text',
  410.      *                                                 'length' => 20,
  411.      *                                             ),
  412.      *                                         )
  413.      *                                     ),
  414.      *                                     'rename' => array(
  415.      *                                         'sex' => array(
  416.      *                                             'name' => 'gender',
  417.      *                                             'definition' => array(
  418.      *                                                 'type' => 'text',
  419.      *                                                 'length' => 1,
  420.      *                                                 'default' => 'M',
  421.      *                                             ),
  422.      *                                         )
  423.      *                                     )
  424.      *                                 )
  425.      *
  426.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  427.      *                              can perform the requested table alterations if the value is true or
  428.      *                              actually perform them otherwise.
  429.      * @access public
  430.      *
  431.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  432.      */
  433.     function alterTable($name$changes$check)
  434.     {
  435.         $db =$this->getDBInstance();
  436.         if (PEAR::isError($db)) {
  437.             return $db;
  438.         }
  439.  
  440.         foreach ($changes as $change_name => $change{
  441.             switch ($change_name{
  442.             case 'add':
  443.             case 'remove':
  444.             case 'change':
  445.             case 'name':
  446.             case 'rename':
  447.                 break;
  448.             default:
  449.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  450.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  451.             }
  452.         }
  453.  
  454.         if ($check{
  455.             return MDB2_OK;
  456.         }
  457.  
  458.         $name $db->quoteIdentifier($nametrue);
  459.  
  460.         if (!empty($changes['add']&& is_array($changes['add'])) {
  461.             $fields = array();
  462.             foreach ($changes['add'as $field_name => $field{
  463.                 $fields[$db->getDeclaration($field['type']$field_name$field$name);
  464.             }
  465.             $result $db->exec("ALTER TABLE $name ADD (". implode(', '$fields).')');
  466.             if (PEAR::isError($result)) {
  467.                 return $result;
  468.             }
  469.         }
  470.  
  471.         if (!empty($changes['change']&& is_array($changes['change'])) {
  472.             $fields = array();
  473.             foreach ($changes['change'as $field_name => $field{
  474.                 $fields[$field_name' ' $db->getDeclaration($field['definition']['type']''$field['definition']);
  475.             }
  476.             $result $db->exec("ALTER TABLE $name MODIFY (". implode(', '$fields).')');
  477.             if (PEAR::isError($result)) {
  478.                 return $result;
  479.             }
  480.         }
  481.  
  482.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  483.             foreach ($changes['rename'as $field_name => $field{
  484.                 $field_name $db->quoteIdentifier($field_nametrue);
  485.                 $query = "ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name']);
  486.                 $result $db->exec($query);
  487.                 if (PEAR::isError($result)) {
  488.                     return $result;
  489.                 }
  490.             }
  491.         }
  492.  
  493.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  494.             $fields = array();
  495.             foreach ($changes['remove'as $field_name => $field{
  496.                 $fields[$db->quoteIdentifier($field_nametrue);
  497.             }
  498.             $result $db->exec("ALTER TABLE $name DROP COLUMN ". implode(', '$fields));
  499.             if (PEAR::isError($result)) {
  500.                 return $result;
  501.             }
  502.         }
  503.  
  504.         if (!empty($changes['name'])) {
  505.             $change_name $db->quoteIdentifier($changes['name']true);
  506.             $result $db->exec("ALTER TABLE $name RENAME TO ".$change_name);
  507.             if (PEAR::isError($result)) {
  508.                 return $result;
  509.             }
  510.         }
  511.  
  512.         return MDB2_OK;
  513.     }
  514.  
  515.     // }}}
  516.     // {{{ listDatabases()
  517.  
  518.     /**
  519.      * list all databases
  520.      *
  521.      * @return mixed data array on success, a MDB2 error on failure
  522.      * @access public
  523.      */
  524.     function listDatabases()
  525.     {
  526.         $db =$this->getDBInstance();
  527.         if (PEAR::isError($db)) {
  528.             return $db;
  529.         }
  530.  
  531.         if (!$db->options['emulate_database']{
  532.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  533.                 'database listing is only supported if the "emulate_database" option is enabled'__FUNCTION__);
  534.         }
  535.  
  536.         if ($db->options['database_name_prefix']{
  537.             $query 'SELECT SUBSTR(username, ';
  538.             $query.= (strlen($db->options['database_name_prefix'])+1);
  539.             $query.= ") FROM sys.dba_users WHERE username LIKE '";
  540.             $query.= $db->options['database_name_prefix']."%'";
  541.         else {
  542.             $query 'SELECT username FROM sys.dba_users';
  543.         }
  544.         $result2 $db->standaloneQuery($queryarray('text')false);
  545.         if (PEAR::isError($result2)) {
  546.             return $result2;
  547.         }
  548.         $result $result2->fetchCol();
  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.         $result2->free();
  558.         return $result;
  559.     }
  560.  
  561.         // }}}
  562.     // {{{ listUsers()
  563.  
  564.     /**
  565.      * list all users in the current database
  566.      *
  567.      * @return mixed data array on success, a MDB2 error on failure
  568.      * @access public
  569.      */
  570.     function listUsers()
  571.     {
  572.         $db =$this->getDBInstance();
  573.         if (PEAR::isError($db)) {
  574.             return $db;
  575.         }
  576.  
  577.         if ($db->options['emulate_database'&& $db->options['database_name_prefix']{
  578.             $query 'SELECT SUBSTR(username, ';
  579.             $query.= (strlen($db->options['database_name_prefix'])+1);
  580.             $query.= ") FROM sys.dba_users WHERE username NOT LIKE '";
  581.             $query.= $db->options['database_name_prefix']."%'";
  582.         else {
  583.             $query 'SELECT username FROM sys.dba_users';
  584.         }
  585.         return $db->queryCol($query);
  586.     }
  587.     // }}}
  588.     // {{{ listViews()
  589.  
  590.     /**
  591.      * list all views in the current database
  592.      *
  593.      * @return mixed data array on success, a MDB2 error on failure
  594.      * @access public
  595.      */
  596.     function listViews()
  597.     {
  598.         $db =$this->getDBInstance();
  599.         if (PEAR::isError($db)) {
  600.             return $db;
  601.         }
  602.  
  603.         $query 'SELECT view_name FROM sys.user_views';
  604.         $result $db->queryCol($query);
  605.         if (PEAR::isError($result)) {
  606.             return $result;
  607.         }
  608.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  609.             && $db->options['field_case'== CASE_LOWER
  610.         {
  611.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  612.         }
  613.         return $result;
  614.     }
  615.  
  616.     // }}}
  617.     // {{{ listFunctions()
  618.  
  619.     /**
  620.      * list all functions in the current database
  621.      *
  622.      * @return mixed data array on success, a MDB2 error on failure
  623.      * @access public
  624.      */
  625.     function listFunctions()
  626.     {
  627.         $db =$this->getDBInstance();
  628.         if (PEAR::isError($db)) {
  629.             return $db;
  630.         }
  631.  
  632.         $query "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
  633.         $result $db->queryCol($query);
  634.         if (PEAR::isError($result)) {
  635.             return $result;
  636.         }
  637.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  638.             && $db->options['field_case'== CASE_LOWER
  639.         {
  640.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  641.         }
  642.         return $result;
  643.     }
  644.  
  645.     // }}}
  646.     // {{{ listTables()
  647.  
  648.     /**
  649.      * list all tables in the current database
  650.      *
  651.      * @return mixed data array on success, a MDB2 error on failure
  652.      * @access public
  653.      ***/
  654.     function listTables()
  655.     {
  656.         $db =$this->getDBInstance();
  657.         if (PEAR::isError($db)) {
  658.             return $db;
  659.         }
  660.  
  661.         $query 'SELECT table_name FROM sys.user_tables';
  662.         $result $db->queryCol($query);
  663.         if (PEAR::isError($result)) {
  664.             return $result;
  665.         }
  666.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  667.             && $db->options['field_case'== CASE_LOWER
  668.         {
  669.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  670.         }
  671.         return $result;
  672.     }
  673.  
  674.     // }}}
  675.     // {{{ listTableFields()
  676.  
  677.     /**
  678.      * list all fields in a tables in the current database
  679.      *
  680.      * @param string $table name of table that should be used in method
  681.      * @return mixed data array on success, a MDB2 error on failure
  682.      * @access public
  683.      */
  684.     function listTableFields($table)
  685.     {
  686.         $db =$this->getDBInstance();
  687.         if (PEAR::isError($db)) {
  688.             return $db;
  689.         }
  690.  
  691.         $table $db->quote($table'text');
  692.         $query 'SELECT column_name FROM user_tab_columns';
  693.         $query.= ' WHERE table_name='.$table.' OR table_name='.strtoupper($table).' ORDER BY column_id';
  694.         $result $db->queryCol($query);
  695.         if (PEAR::isError($result)) {
  696.             return $result;
  697.         }
  698.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  699.             && $db->options['field_case'== CASE_LOWER
  700.         {
  701.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  702.         }
  703.         return $result;
  704.     }
  705.  
  706.     // }}}
  707.     // {{{ listTableIndexes()
  708.  
  709.     /**
  710.      * list all indexes in a table
  711.      *
  712.      * @param string $table name of table that should be used in method
  713.      * @return mixed data array on success, a MDB2 error on failure
  714.      * @access public
  715.      */
  716.     function listTableIndexes($table)
  717.     {
  718.         $db =$this->getDBInstance();
  719.         if (PEAR::isError($db)) {
  720.             return $db;
  721.         }
  722.  
  723.         $table $db->quote($table'text');
  724.         $query 'SELECT index_name name FROM user_indexes';
  725.         $query.= ' WHERE table_name='.$table.' OR table_name='.strtoupper($table);
  726.         $indexes $db->queryCol($query'text');
  727.         if (PEAR::isError($indexes)) {
  728.             return $indexes;
  729.         }
  730.  
  731.         $result = array();
  732.         foreach ($indexes as $index{
  733.             $result[$this->_fixIndexName($index)= true;
  734.         }
  735.  
  736.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  737.             && $db->options['field_case'== CASE_LOWER
  738.         {
  739.             $result array_change_key_case($result$db->options['field_case']);
  740.         }
  741.         return array_keys($result);
  742.     }
  743.  
  744.     // }}}
  745.     // {{{ listTableConstraints()
  746.  
  747.     /**
  748.      * list all sonstraints in a table
  749.      *
  750.      * @param string    $table      name of table that should be used in method
  751.      * @return mixed data array on success, a MDB2 error on failure
  752.      * @access public
  753.      */
  754.     function listTableConstraints($table)
  755.     {
  756.         $db =$this->getDBInstance();
  757.         if (PEAR::isError($db)) {
  758.             return $db;
  759.         }
  760.  
  761.         $table $db->quote($table'text');
  762.         $query 'SELECT index_name name FROM user_constraints';
  763.         $query.= ' WHERE table_name='.$table.' OR table_name='.strtoupper($table);
  764.         $constraints $db->queryCol($query);
  765.         if (PEAR::isError($constraints)) {
  766.             return $constraints;
  767.         }
  768.  
  769.         $result = array();
  770.         foreach ($constraints as $constraint{
  771.             $result[$this->_fixIndexName($constraint)= true;
  772.         }
  773.  
  774.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  775.             && $db->options['field_case'== CASE_LOWER
  776.         {
  777.             $result array_change_key_case($result$db->options['field_case']);
  778.         }
  779.         return array_keys($result);
  780.     }
  781.  
  782.     // }}}
  783.     // {{{ createSequence()
  784.  
  785.     /**
  786.      * create sequence
  787.      *
  788.      * @param object $db database object that is extended by this class
  789.      * @param string $seq_name name of the sequence to be created
  790.      * @param string $start start value of the sequence; default is 1
  791.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  792.      * @access public
  793.      */
  794.     function createSequence($seq_name$start = 1)
  795.     {
  796.         $db =$this->getDBInstance();
  797.         if (PEAR::isError($db)) {
  798.             return $db;
  799.         }
  800.  
  801.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  802.         $query = "CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1";
  803.         $query.= ($start < 1 ? " MINVALUE $start" : '');
  804.         return $db->exec($query);
  805.     }
  806.  
  807.     // }}}
  808.     // {{{ dropSequence()
  809.  
  810.     /**
  811.      * drop existing sequence
  812.      *
  813.      * @param object $db database object that is extended by this class
  814.      * @param string $seq_name name of the sequence to be dropped
  815.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  816.      * @access public
  817.      */
  818.     function dropSequence($seq_name)
  819.     {
  820.         $db =$this->getDBInstance();
  821.         if (PEAR::isError($db)) {
  822.             return $db;
  823.         }
  824.  
  825.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  826.         return $db->exec("DROP SEQUENCE $sequence_name");
  827.     }
  828.  
  829.     // }}}
  830.     // {{{ listSequences()
  831.  
  832.     /**
  833.      * list all sequences in the current database
  834.      *
  835.      * @return mixed data array on success, a MDB2 error on failure
  836.      * @access public
  837.      */
  838.     function listSequences()
  839.     {
  840.         $db =$this->getDBInstance();
  841.         if (PEAR::isError($db)) {
  842.             return $db;
  843.         }
  844.  
  845.         $query "SELECT sequence_name FROM sys.user_sequences";
  846.         $table_names $db->queryCol($query);
  847.         if (PEAR::isError($table_names)) {
  848.             return $table_names;
  849.         }
  850.         $result = array();
  851.         foreach ($table_names as $table_name{
  852.             $result[$this->_fixSequenceName($table_name);
  853.         }
  854.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  855.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  856.         }
  857.         return $result;
  858.     }
  859. }
  860. ?>

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