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

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