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

Documentation generated on Mon, 11 Mar 2019 15:01:29 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.