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

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