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

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