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-2007 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.94 2007/09/23 18:46:05 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.         $result $db->exec($trigger_sql);
  208.         if (PEAR::isError($result)) {
  209.             return $result;
  210.         }
  211.         return MDB2_OK;
  212.     }
  213.  
  214.     // }}}
  215.     // {{{ _dropAutoincrement()
  216.  
  217.     /**
  218.      * drop an existing autoincrement sequence + trigger
  219.      *
  220.      * @param string $table name of the table
  221.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  222.      * @access private
  223.      */
  224.     function _dropAutoincrement($table)
  225.     {
  226.         $db =$this->getDBInstance();
  227.         if (PEAR::isError($db)) {
  228.             return $db;
  229.         }
  230.  
  231.         $table strtoupper($table);
  232.         $trigger_name $table '_AI_PK';
  233.         $trigger_name_quoted $db->quote($trigger_name'text');
  234.         $query 'SELECT trigger_name FROM user_triggers';
  235.         $query.= ' WHERE trigger_name='.$trigger_name_quoted.' OR trigger_name='.strtoupper($trigger_name_quoted);
  236.         $trigger $db->queryOne($query);
  237.         if (PEAR::isError($trigger)) {
  238.             return $trigger;
  239.         }
  240.  
  241.         if ($trigger{
  242.             $trigger_name  $db->quoteIdentifier($table '_AI_PK'true);
  243.             $trigger_sql 'DROP TRIGGER ' $trigger_name;
  244.             $result $db->exec($trigger_sql);
  245.             if (PEAR::isError($result)) {
  246.                 return $db->raiseError($resultnullnull,
  247.                     'trigger for autoincrement PK could not be dropped'__FUNCTION__);
  248.             }
  249.  
  250.             $result $this->dropSequence($table);
  251.             if (PEAR::isError($result)) {
  252.                 return $db->raiseError($resultnullnull,
  253.                     'sequence for autoincrement PK could not be dropped'__FUNCTION__);
  254.             }
  255.  
  256.             $index_name $table '_AI_PK';
  257.             $result $this->dropConstraint($table$index_name);
  258.             if (PEAR::isError($result)) {
  259.                 return $db->raiseError($resultnullnull,
  260.                     'primary key for autoincrement PK could not be dropped'__FUNCTION__);
  261.             }
  262.         }
  263.  
  264.         return MDB2_OK;
  265.     }
  266.  
  267.     // }}}
  268.     // {{{ _getTemporaryTableQuery()
  269.  
  270.     /**
  271.      * A method to return the required SQL string that fits between CREATE ... TABLE
  272.      * to create the table as a temporary table.
  273.      *
  274.      * @return string The string required to be placed between "CREATE" and "TABLE"
  275.      *                 to generate a temporary table, if possible.
  276.      */
  277.     function _getTemporaryTableQuery()
  278.     {
  279.         return 'GLOBAL TEMPORARY';
  280.     }
  281.  
  282.     // }}}
  283.     // {{{ _getAdvancedFKOptions()
  284.  
  285.     /**
  286.      * Return the FOREIGN KEY query section dealing with non-standard options
  287.      * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  288.      *
  289.      * @param array $definition 
  290.      * @return string 
  291.      * @access protected
  292.      */
  293.     function _getAdvancedFKOptions($definition)
  294.     {
  295.         $query '';
  296.         if (!empty($definition['ondelete']&& (strtoupper($definition['ondelete']!= 'NO ACTION')) {
  297.             $query .= ' ON DELETE '.$definition['ondelete'];
  298.         }
  299.         if (!empty($definition['deferrable'])) {
  300.             $query .= ' DEFERRABLE';
  301.         else {
  302.             $query .= ' NOT DEFERRABLE';
  303.         }
  304.         if (!empty($definition['initiallydeferred'])) {
  305.             $query .= ' INITIALLY DEFERRED';
  306.         else {
  307.             $query .= ' INITIALLY IMMEDIATE';
  308.         }
  309.         return $query;
  310.     }
  311.  
  312.     // }}}
  313.     // {{{ createTable()
  314.  
  315.     /**
  316.      * create a new table
  317.      *
  318.      * @param string $name     Name of the database that should be created
  319.      * @param array $fields Associative array that contains the definition of each field of the new table
  320.      *                         The indexes of the array entries are the names of the fields of the table an
  321.      *                         the array entry values are associative arrays like those that are meant to be
  322.      *                          passed with the field definitions to get[Type]Declaration() functions.
  323.      *
  324.      *                         Example
  325.      *                         array(
  326.      *
  327.      *                             'id' => array(
  328.      *                                 'type' => 'integer',
  329.      *                                 'unsigned' => 1
  330.      *                                 'notnull' => 1
  331.      *                                 'default' => 0
  332.      *                             ),
  333.      *                             'name' => array(
  334.      *                                 'type' => 'text',
  335.      *                                 'length' => 12
  336.      *                             ),
  337.      *                             'password' => array(
  338.      *                                 'type' => 'text',
  339.      *                                 'length' => 12
  340.      *                             )
  341.      *                         );
  342.      * @param array $options  An associative array of table options:
  343.      *                           array(
  344.      *                               'comment' => 'Foo',
  345.      *                               'temporary' => true|false,
  346.      *                           );
  347.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  348.      * @access public
  349.      */
  350.     function createTable($name$fields$options = array())
  351.     {
  352.         $db =$this->getDBInstance();
  353.         if (PEAR::isError($db)) {
  354.             return $db;
  355.         }
  356.         $db->beginNestedTransaction();
  357.         $result = parent::createTable($name$fields$options);
  358.         if (!PEAR::isError($result)) {
  359.             foreach ($fields as $field_name => $field{
  360.                 if (!empty($field['autoincrement'])) {
  361.                     $result $this->_makeAutoincrement($field_name$name);
  362.                 }
  363.             }
  364.         }
  365.         $db->completeNestedTransaction();
  366.         return $result;
  367.     }
  368.  
  369.     // }}}
  370.     // {{{ dropTable()
  371.  
  372.     /**
  373.      * drop an existing table
  374.      *
  375.      * @param string $name name of the table that should be dropped
  376.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  377.      * @access public
  378.      */
  379.     function dropTable($name)
  380.     {
  381.         $db =$this->getDBInstance();
  382.         if (PEAR::isError($db)) {
  383.             return $db;
  384.         }
  385.         $db->beginNestedTransaction();
  386.         $result $this->_dropAutoincrement($name);
  387.         if (!PEAR::isError($result)) {
  388.             $result = parent::dropTable($name);
  389.         }
  390.         $db->completeNestedTransaction();
  391.         return $result;
  392.     }
  393.  
  394.     // }}}
  395.     // {{{ alterTable()
  396.  
  397.     /**
  398.      * alter an existing table
  399.      *
  400.      * @param string $name         name of the table that is intended to be changed.
  401.      * @param array $changes     associative array that contains the details of each type
  402.      *                              of change that is intended to be performed. The types of
  403.      *                              changes that are currently supported are defined as follows:
  404.      *
  405.      *                              name
  406.      *
  407.      *                                 New name for the table.
  408.      *
  409.      *                             add
  410.      *
  411.      *                                 Associative array with the names of fields to be added as
  412.      *                                  indexes of the array. The value of each entry of the array
  413.      *                                  should be set to another associative array with the properties
  414.      *                                  of the fields to be added. The properties of the fields should
  415.      *                                  be the same as defined by the MDB2 parser.
  416.      *
  417.      *
  418.      *                             remove
  419.      *
  420.      *                                 Associative array with the names of fields to be removed as indexes
  421.      *                                  of the array. Currently the values assigned to each entry are ignored.
  422.      *                                  An empty array should be used for future compatibility.
  423.      *
  424.      *                             rename
  425.      *
  426.      *                                 Associative array with the names of fields to be renamed as indexes
  427.      *                                  of the array. The value of each entry of the array should be set to
  428.      *                                  another associative array with the entry named name with the new
  429.      *                                  field name and the entry named Declaration that is expected to contain
  430.      *                                  the portion of the field declaration already in DBMS specific SQL code
  431.      *                                  as it is used in the CREATE TABLE statement.
  432.      *
  433.      *                             change
  434.      *
  435.      *                                 Associative array with the names of the fields to be changed as indexes
  436.      *                                  of the array. Keep in mind that if it is intended to change either the
  437.      *                                  name of a field and any other properties, the change array entries
  438.      *                                  should have the new names of the fields as array indexes.
  439.      *
  440.      *                                 The value of each entry of the array should be set to another associative
  441.      *                                  array with the properties of the fields to that are meant to be changed as
  442.      *                                  array entries. These entries should be assigned to the new values of the
  443.      *                                  respective properties. The properties of the fields should be the same
  444.      *                                  as defined by the MDB2 parser.
  445.      *
  446.      *                             Example
  447.      *                                 array(
  448.      *                                     'name' => 'userlist',
  449.      *                                     'add' => array(
  450.      *                                         'quota' => array(
  451.      *                                             'type' => 'integer',
  452.      *                                             'unsigned' => 1
  453.      *                                         )
  454.      *                                     ),
  455.      *                                     'remove' => array(
  456.      *                                         'file_limit' => array(),
  457.      *                                         'time_limit' => array()
  458.      *                                     ),
  459.      *                                     'change' => array(
  460.      *                                         'name' => array(
  461.      *                                             'length' => '20',
  462.      *                                             'definition' => array(
  463.      *                                                 'type' => 'text',
  464.      *                                                 'length' => 20,
  465.      *                                             ),
  466.      *                                         )
  467.      *                                     ),
  468.      *                                     'rename' => array(
  469.      *                                         'sex' => array(
  470.      *                                             'name' => 'gender',
  471.      *                                             'definition' => array(
  472.      *                                                 'type' => 'text',
  473.      *                                                 'length' => 1,
  474.      *                                                 'default' => 'M',
  475.      *                                             ),
  476.      *                                         )
  477.      *                                     )
  478.      *                                 )
  479.      *
  480.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  481.      *                              can perform the requested table alterations if the value is true or
  482.      *                              actually perform them otherwise.
  483.      * @access public
  484.      *
  485.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  486.      */
  487.     function alterTable($name$changes$check)
  488.     {
  489.         $db =$this->getDBInstance();
  490.         if (PEAR::isError($db)) {
  491.             return $db;
  492.         }
  493.  
  494.         foreach ($changes as $change_name => $change{
  495.             switch ($change_name{
  496.             case 'add':
  497.             case 'remove':
  498.             case 'change':
  499.             case 'name':
  500.             case 'rename':
  501.                 break;
  502.             default:
  503.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  504.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  505.             }
  506.         }
  507.  
  508.         if ($check{
  509.             return MDB2_OK;
  510.         }
  511.  
  512.         $name $db->quoteIdentifier($nametrue);
  513.  
  514.         if (!empty($changes['add']&& is_array($changes['add'])) {
  515.             $fields = array();
  516.             foreach ($changes['add'as $field_name => $field{
  517.                 $fields[$db->getDeclaration($field['type']$field_name$field);
  518.             }
  519.             $result $db->exec("ALTER TABLE $name ADD (". implode(', '$fields).')');
  520.             if (PEAR::isError($result)) {
  521.                 return $result;
  522.             }
  523.         }
  524.  
  525.         if (!empty($changes['change']&& is_array($changes['change'])) {
  526.             $fields = array();
  527.             foreach ($changes['change'as $field_name => $field{
  528.                 $fields[$field_name' ' $db->getDeclaration($field['definition']['type']''$field['definition']);
  529.             }
  530.             $result $db->exec("ALTER TABLE $name MODIFY (". implode(', '$fields).')');
  531.             if (PEAR::isError($result)) {
  532.                 return $result;
  533.             }
  534.         }
  535.  
  536.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  537.             foreach ($changes['rename'as $field_name => $field{
  538.                 $field_name $db->quoteIdentifier($field_nametrue);
  539.                 $query = "ALTER TABLE $name RENAME COLUMN $field_name TO ".$db->quoteIdentifier($field['name']);
  540.                 $result $db->exec($query);
  541.                 if (PEAR::isError($result)) {
  542.                     return $result;
  543.                 }
  544.             }
  545.         }
  546.  
  547.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  548.             $fields = array();
  549.             foreach ($changes['remove'as $field_name => $field{
  550.                 $fields[$db->quoteIdentifier($field_nametrue);
  551.             }
  552.             $result $db->exec("ALTER TABLE $name DROP COLUMN ". implode(', '$fields));
  553.             if (PEAR::isError($result)) {
  554.                 return $result;
  555.             }
  556.         }
  557.  
  558.         if (!empty($changes['name'])) {
  559.             $change_name $db->quoteIdentifier($changes['name']true);
  560.             $result $db->exec("ALTER TABLE $name RENAME TO ".$change_name);
  561.             if (PEAR::isError($result)) {
  562.                 return $result;
  563.             }
  564.         }
  565.  
  566.         return MDB2_OK;
  567.     }
  568.  
  569.     // }}}
  570.     // {{{ _fetchCol()
  571.  
  572.     /**
  573.      * Utility method to fetch and format a column from a resultset
  574.      *
  575.      * @param resource $result 
  576.      * @param boolean $fixname (used when listing indices or constraints)
  577.      * @return mixed array of names on success, a MDB2 error on failure
  578.      * @access private
  579.      */
  580.     function _fetchCol($result$fixname = false)
  581.     {
  582.         if (PEAR::isError($result)) {
  583.             return $result;
  584.         }
  585.         $col $result->fetchCol();
  586.         if (PEAR::isError($col)) {
  587.             return $col;
  588.         }
  589.         $result->free();
  590.         
  591.         $db =$this->getDBInstance();
  592.         if (PEAR::isError($db)) {
  593.             return $db;
  594.         }
  595.         
  596.         if ($fixname{
  597.             foreach ($col as $k => $v{
  598.                 $col[$k$this->_fixIndexName($v);
  599.             }
  600.         }
  601.         
  602.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  603.             && $db->options['field_case'== CASE_LOWER
  604.         {
  605.             $col array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$col);
  606.         }
  607.         return $col;
  608.     }
  609.  
  610.     // }}}
  611.     // {{{ listDatabases()
  612.  
  613.     /**
  614.      * list all databases
  615.      *
  616.      * @return mixed array of database names on success, a MDB2 error on failure
  617.      * @access public
  618.      */
  619.     function listDatabases()
  620.     {
  621.         $db =$this->getDBInstance();
  622.         if (PEAR::isError($db)) {
  623.             return $db;
  624.         }
  625.  
  626.         if (!$db->options['emulate_database']{
  627.             return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  628.                 'database listing is only supported if the "emulate_database" option is enabled'__FUNCTION__);
  629.         }
  630.  
  631.         if ($db->options['database_name_prefix']{
  632.             $query 'SELECT SUBSTR(username, ';
  633.             $query.= (strlen($db->options['database_name_prefix'])+1);
  634.             $query.= ") FROM sys.dba_users WHERE username LIKE '";
  635.             $query.= $db->options['database_name_prefix']."%'";
  636.         else {
  637.             $query 'SELECT username FROM sys.dba_users';
  638.         }
  639.         $result $db->standaloneQuery($queryarray('text')false);
  640.         return $this->_fetchCol($result);
  641.     }
  642.  
  643.     // }}}
  644.     // {{{ listUsers()
  645.  
  646.     /**
  647.      * list all users
  648.      *
  649.      * @return mixed array of user names on success, a MDB2 error on failure
  650.      * @access public
  651.      */
  652.     function listUsers()
  653.     {
  654.         $db =$this->getDBInstance();
  655.         if (PEAR::isError($db)) {
  656.             return $db;
  657.         }
  658.  
  659.         if ($db->options['emulate_database'&& $db->options['database_name_prefix']{
  660.             $query 'SELECT SUBSTR(username, ';
  661.             $query.= (strlen($db->options['database_name_prefix'])+1);
  662.             $query.= ") FROM sys.dba_users WHERE username NOT LIKE '";
  663.             $query.= $db->options['database_name_prefix']."%'";
  664.         else {
  665.             $query 'SELECT username FROM sys.dba_users';
  666.         }
  667.         return $db->queryCol($query);
  668.     }
  669.  
  670.     // }}}
  671.     // {{{ listViews()
  672.  
  673.     /**
  674.      * list all views in the current database
  675.      *
  676.      * @param string owner, the current is default
  677.      * @return mixed array of view names on success, a MDB2 error on failure
  678.      * @access public
  679.      */
  680.     function listViews($owner = null)
  681.     {
  682.         $db =$this->getDBInstance();
  683.         if (PEAR::isError($db)) {
  684.             return $db;
  685.         }
  686.         
  687.         if (empty($owner)) {
  688.             $owner $db->dsn['username'];
  689.         }
  690.  
  691.         $query 'SELECT view_name
  692.                     FROM sys.all_views
  693.                    WHERE owner=? OR owner=?';
  694.         $stmt $db->prepare($query);
  695.         if (PEAR::isError($stmt)) {
  696.             return $stmt;
  697.         }
  698.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  699.         return $this->_fetchCol($result);
  700.     }
  701.  
  702.     // }}}
  703.     // {{{ listFunctions()
  704.  
  705.     /**
  706.      * list all functions in the current database
  707.      *
  708.      * @param string owner, the current is default
  709.      * @return mixed array of function names on success, a MDB2 error on failure
  710.      * @access public
  711.      */
  712.     function listFunctions($owner = null)
  713.     {
  714.         $db =$this->getDBInstance();
  715.         if (PEAR::isError($db)) {
  716.             return $db;
  717.         }
  718.  
  719.         if (empty($owner)) {
  720.             $owner $db->dsn['username'];
  721.         }
  722.  
  723.         $query "SELECT name
  724.                     FROM sys.all_source
  725.                    WHERE line = 1
  726.                      AND type = 'FUNCTION'
  727.                      AND (owner=? OR owner=?)";
  728.         $stmt $db->prepare($query);
  729.         if (PEAR::isError($stmt)) {
  730.             return $stmt;
  731.         }
  732.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  733.         return $this->_fetchCol($result);
  734.     }
  735.  
  736.     // }}}
  737.     // {{{ listTableTriggers()
  738.  
  739.     /**
  740.      * list all triggers in the database that reference a given table
  741.      *
  742.      * @param string table for which all referenced triggers should be found
  743.      * @return mixed array of trigger names on success, a MDB2 error on failure
  744.      * @access public
  745.      */
  746.     function listTableTriggers($table = null)
  747.     {
  748.         $db =$this->getDBInstance();
  749.         if (PEAR::isError($db)) {
  750.             return $db;
  751.         }
  752.  
  753.         if (empty($owner)) {
  754.             $owner $db->dsn['username'];
  755.         }
  756.  
  757.         $query "SELECT trigger_name
  758.                     FROM sys.all_triggers
  759.                    WHERE (table_name=? OR table_name=?)
  760.                      AND (owner=? OR owner=?)";
  761.         $stmt $db->prepare($query);
  762.         if (PEAR::isError($stmt)) {
  763.             return $stmt;
  764.         }
  765.         $args = array(
  766.             $table,
  767.             strtoupper($table),
  768.             $owner,
  769.             strtoupper($owner),
  770.         );
  771.         $result $stmt->execute($args);
  772.         return $this->_fetchCol($result);
  773.     }
  774.  
  775.     // }}}
  776.     // {{{ listTables()
  777.  
  778.     /**
  779.      * list all tables in the database
  780.      *
  781.      * @param string owner, the current is default
  782.      * @return mixed array of table names on success, a MDB2 error on failure
  783.      * @access public
  784.      */
  785.     function listTables($owner = null)
  786.     {
  787.         $db =$this->getDBInstance();
  788.         if (PEAR::isError($db)) {
  789.             return $db;
  790.         }
  791.         
  792.         if (empty($owner)) {
  793.             $owner $db->dsn['username'];
  794.         }
  795.  
  796.         $query 'SELECT table_name
  797.                     FROM sys.all_tables
  798.                    WHERE owner=? OR owner=?';
  799.         $stmt $db->prepare($query);
  800.         if (PEAR::isError($stmt)) {
  801.             return $stmt;
  802.         }
  803.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  804.         return $this->_fetchCol($result);
  805.     }
  806.  
  807.     // }}}
  808.     // {{{ listTableFields()
  809.  
  810.     /**
  811.      * list all fields in a table in the current database
  812.      *
  813.      * @param string $table name of table that should be used in method
  814.      * @return mixed array of field names on success, a MDB2 error on failure
  815.      * @access public
  816.      */
  817.     function listTableFields($table)
  818.     {
  819.         $db =$this->getDBInstance();
  820.         if (PEAR::isError($db)) {
  821.             return $db;
  822.         }
  823.         
  824.         list($owner$table$this->splitTableSchema($table);
  825.         if (empty($owner)) {
  826.             $owner $db->dsn['username'];
  827.         }
  828.  
  829.         $query 'SELECT column_name
  830.                     FROM all_tab_columns
  831.                    WHERE (table_name=? OR table_name=?)
  832.                      AND (owner=? OR owner=?)
  833.                 ORDER BY column_id';
  834.         $stmt $db->prepare($query);
  835.         if (PEAR::isError($stmt)) {
  836.             return $stmt;
  837.         }
  838.         $args = array(
  839.             $table,
  840.             strtoupper($table),
  841.             $owner,
  842.             strtoupper($owner),
  843.         );
  844.         $result $stmt->execute($args);
  845.         return $this->_fetchCol($result);
  846.     }
  847.  
  848.     // }}}
  849.     // {{{ listTableIndexes()
  850.  
  851.     /**
  852.      * list all indexes in a table
  853.      *
  854.      * @param string $table name of table that should be used in method
  855.      * @return mixed array of index names on success, a MDB2 error on failure
  856.      * @access public
  857.      */
  858.     function listTableIndexes($table)
  859.     {
  860.         $db =$this->getDBInstance();
  861.         if (PEAR::isError($db)) {
  862.             return $db;
  863.         }
  864.         
  865.         list($owner$table$this->splitTableSchema($table);
  866.         if (empty($owner)) {
  867.             $owner $db->dsn['username'];
  868.         }
  869.         
  870.         $query 'SELECT i.index_name name
  871.                     FROM all_indexes i
  872.                LEFT JOIN all_constraints c
  873.                       ON c.index_name = i.index_name
  874.                      AND c.owner = i.owner
  875.                      AND c.table_name = i.table_name
  876.                    WHERE (i.table_name=? OR i.table_name=?)
  877.                      AND (i.owner=? OR i.owner=?)
  878.                      AND c.index_name IS NULL
  879.                      AND i.generated=' .$db->quote('N''text');
  880.         $stmt $db->prepare($query);
  881.         if (PEAR::isError($stmt)) {
  882.             return $stmt;
  883.         }
  884.         $args = array(
  885.             $table,
  886.             strtoupper($table),
  887.             $owner,
  888.             strtoupper($owner),
  889.         );
  890.         $result $stmt->execute($args);
  891.         return $this->_fetchCol($resulttrue);
  892.     }
  893.  
  894.     // }}}
  895.     // {{{ listTableConstraints()
  896.  
  897.     /**
  898.      * list all constraints in a table
  899.      *
  900.      * @param string $table name of table that should be used in method
  901.      * @return mixed array of constraint names on success, a MDB2 error on failure
  902.      * @access public
  903.      */
  904.     function listTableConstraints($table)
  905.     {
  906.         $db =$this->getDBInstance();
  907.         if (PEAR::isError($db)) {
  908.             return $db;
  909.         }
  910.  
  911.         list($owner$table$this->splitTableSchema($table);
  912.         if (empty($owner)) {
  913.             $owner $db->dsn['username'];
  914.         }
  915.  
  916.         $query 'SELECT constraint_name
  917.                     FROM all_constraints
  918.                    WHERE (table_name=? OR table_name=?)
  919.                      AND (owner=? OR owner=?)';
  920.         $stmt $db->prepare($query);
  921.         if (PEAR::isError($stmt)) {
  922.             return $stmt;
  923.         }
  924.         $args = array(
  925.             $table,
  926.             strtoupper($table),
  927.             $owner,
  928.             strtoupper($owner),
  929.         );
  930.         $result $stmt->execute($args);
  931.         return $this->_fetchCol($resulttrue);
  932.     }
  933.  
  934.     // }}}
  935.     // {{{ createSequence()
  936.  
  937.     /**
  938.      * create sequence
  939.      *
  940.      * @param object $db database object that is extended by this class
  941.      * @param string $seq_name name of the sequence to be created
  942.      * @param string $start start value of the sequence; default is 1
  943.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  944.      * @access public
  945.      */
  946.     function createSequence($seq_name$start = 1)
  947.     {
  948.         $db =$this->getDBInstance();
  949.         if (PEAR::isError($db)) {
  950.             return $db;
  951.         }
  952.  
  953.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  954.         $query = "CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1 NOCACHE";
  955.         $query.= ($start < 1 ? " MINVALUE $start" : '');
  956.         return $db->exec($query);
  957.     }
  958.  
  959.     // }}}
  960.     // {{{ dropSequence()
  961.  
  962.     /**
  963.      * drop existing sequence
  964.      *
  965.      * @param object $db database object that is extended by this class
  966.      * @param string $seq_name name of the sequence to be dropped
  967.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  968.      * @access public
  969.      */
  970.     function dropSequence($seq_name)
  971.     {
  972.         $db =$this->getDBInstance();
  973.         if (PEAR::isError($db)) {
  974.             return $db;
  975.         }
  976.  
  977.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  978.         return $db->exec("DROP SEQUENCE $sequence_name");
  979.     }
  980.  
  981.     // }}}
  982.     // {{{ listSequences()
  983.  
  984.     /**
  985.      * list all sequences in the current database
  986.      *
  987.      * @param string owner, the current is default
  988.      * @return mixed array of sequence names on success, a MDB2 error on failure
  989.      * @access public
  990.      */
  991.     function listSequences($owner = null)
  992.     {
  993.         $db =$this->getDBInstance();
  994.         if (PEAR::isError($db)) {
  995.             return $db;
  996.         }
  997.  
  998.         if (empty($owner)) {
  999.             $owner $db->dsn['username'];
  1000.         }
  1001.  
  1002.         $query 'SELECT sequence_name
  1003.                     FROM sys.all_sequences
  1004.                    WHERE (sequence_owner=? OR sequence_owner=?)';
  1005.         $stmt $db->prepare($query);
  1006.         if (PEAR::isError($stmt)) {
  1007.             return $stmt;
  1008.         }
  1009.         $result $stmt->execute(array($ownerstrtoupper($owner)));
  1010.         if (PEAR::isError($result)) {
  1011.             return $result;
  1012.         }
  1013.         $col $result->fetchCol();
  1014.         if (PEAR::isError($col)) {
  1015.             return $col;
  1016.         }
  1017.         $result->free();
  1018.         
  1019.         foreach ($col as $k => $v{
  1020.             $col[$k$this->_fixSequenceName($v);
  1021.         }
  1022.  
  1023.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE
  1024.             && $db->options['field_case'== CASE_LOWER
  1025.         {
  1026.             $col array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$col);
  1027.         }
  1028.         return $col;
  1029.     }
  1030. }
  1031. ?>

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