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@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44.  
  45. // $Id: oci8.php,v 1.14 2005/03/06 17:37:12 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@backendmedia.com>
  55.  */
  56. {
  57.     // {{{ createDatabase()
  58.  
  59.     /**
  60.      * create a new database
  61.      *
  62.      * @param object $db database object that is extended by this class
  63.      * @param string $name name of the database that should be created
  64.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  65.      * @access public
  66.      */
  67.     function createDatabase($name)
  68.     {
  69.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  70.         $tablespace $db->options['default_tablespace'];
  71.         if (!MDB2::isError($tablespace&& $tablespace{
  72.             $tablespace ' DEFAULT TABLESPACE '.$tablespace;
  73.         else {
  74.             $tablespace '';
  75.         }
  76.         if (!($password $db->dsn['password'])) {
  77.             $password $name;
  78.         }
  79.         $username $db->options['database_name_prefix'].$name;
  80.         $query 'CREATE USER '.$username.' IDENTIFIED BY '.$password.$tablespace;
  81.         $result $db->standaloneQuery($query);
  82.         if (!MDB2::isError($result)) {
  83.             $query 'GRANT CREATE SESSION, CREATE TABLE, UNLIMITED TABLESPACE, CREATE SEQUENCE TO '.$username;
  84.             $result $db->standaloneQuery($query);
  85.             if (!MDB2::isError($result)) {
  86.                 return MDB2_OK;
  87.             else {
  88.                 $query 'DROP USER '.$username.' CASCADE';
  89.                 $result2 $db->standaloneQuery($query);
  90.                 if (MDB2::isError($result2)) {
  91.                     return $db->raiseError(MDB2_ERRORnullnull,
  92.                         'createDatabase: could not setup the database user ('.$result->getUserinfo().') and then could drop its records ('.$result2->getUserinfo().')');
  93.                 }
  94.                 return $db->raiseError(MDB2_ERRORnullnull,
  95.                     'createDatabase: could not setup the database user ('.$result->getUserinfo().')');
  96.             }
  97.         }
  98.         return $result;
  99.     }
  100.  
  101.     // }}}
  102.     // {{{ dropDatabase()
  103.  
  104.     /**
  105.      * drop an existing database
  106.      *
  107.      * @param object $db database object that is extended by this class
  108.      * @param string $name name of the database that should be dropped
  109.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  110.      * @access public
  111.      */
  112.     function dropDatabase($name)
  113.     {
  114.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  115.         $username $db->options['database_name_prefix'].$name;
  116.         return $db->standaloneQuery('DROP USER '.$username.' CASCADE');
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ alterTable()
  121.  
  122.     /**
  123.      * alter an existing table
  124.      *
  125.      * @param object $db database object that is extended by this class
  126.      * @param string $name name of the table that is intended to be changed.
  127.      * @param array $changes associative array that contains the details of each type
  128.      *                               of change that is intended to be performed. The types of
  129.      *                               changes that are currently supported are defined as follows:
  130.      *
  131.      *                               name
  132.      *
  133.      *                                  New name for the table.
  134.      *
  135.      *                              added_fields
  136.      *
  137.      *                                  Associative array with the names of fields to be added as
  138.      *                                   indexes of the array. The value of each entry of the array
  139.      *                                   should be set to another associative array with the properties
  140.      *                                   of the fields to be added. The properties of the fields should
  141.      *                                   be the same as defined by the Metabase parser.
  142.      *
  143.      *                                  Additionally, there should be an entry named Declaration that
  144.      *                                   is expected to contain the portion of the field declaration already
  145.      *                                   in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  146.      *
  147.      *                              removed_fields
  148.      *
  149.      *                                  Associative array with the names of fields to be removed as indexes
  150.      *                                   of the array. Currently the values assigned to each entry are ignored.
  151.      *                                   An empty array should be used for future compatibility.
  152.      *
  153.      *                              renamed_fields
  154.      *
  155.      *                                  Associative array with the names of fields to be renamed as indexes
  156.      *                                   of the array. The value of each entry of the array should be set to
  157.      *                                   another associative array with the entry named name with the new
  158.      *                                   field name and the entry named Declaration that is expected to contain
  159.      *                                   the portion of the field declaration already in DBMS specific SQL code
  160.      *                                   as it is used in the CREATE TABLE statement.
  161.      *
  162.      *                              changed_fields
  163.      *
  164.      *                                  Associative array with the names of the fields to be changed as indexes
  165.      *                                   of the array. Keep in mind that if it is intended to change either the
  166.      *                                   name of a field and any other properties, the changed_fields array entries
  167.      *                                   should have the new names of the fields as array indexes.
  168.      *
  169.      *                                  The value of each entry of the array should be set to another associative
  170.      *                                   array with the properties of the fields to that are meant to be changed as
  171.      *                                   array entries. These entries should be assigned to the new values of the
  172.      *                                   respective properties. The properties of the fields should be the same
  173.      *                                   as defined by the Metabase parser.
  174.      *
  175.      *                                  If the default property is meant to be added, removed or changed, there
  176.      *                                   should also be an entry with index ChangedDefault assigned to 1. Similarly,
  177.      *                                   if the notnull constraint is to be added or removed, there should also be
  178.      *                                   an entry with index ChangedNotNull assigned to 1.
  179.      *
  180.      *                                  Additionally, there should be an entry named Declaration that is expected
  181.      *                                   to contain the portion of the field changed declaration already in DBMS
  182.      *                                   specific SQL code as it is used in the CREATE TABLE statement.
  183.      *                              Example
  184.      *                                  array(
  185.      *                                      'name' => 'userlist',
  186.      *                                      'added_fields' => array(
  187.      *                                          'quota' => array(
  188.      *                                              'type' => 'integer',
  189.      *                                              'unsigned' => 1
  190.      *                                              'declaration' => 'quota INT'
  191.      *                                          )
  192.      *                                      ),
  193.      *                                      'removed_fields' => array(
  194.      *                                          'file_limit' => array(),
  195.      *                                          'time_limit' => array()
  196.      *                                          ),
  197.      *                                      'changed_fields' => array(
  198.      *                                          'gender' => array(
  199.      *                                              'default' => 'M',
  200.      *                                              'change_default' => 1,
  201.      *                                              'declaration' => "gender CHAR(1) DEFAULT 'M'"
  202.      *                                          )
  203.      *                                      ),
  204.      *                                      'renamed_fields' => array(
  205.      *                                          'sex' => array(
  206.      *                                              'name' => 'gender',
  207.      *                                              'declaration' => "gender CHAR(1) DEFAULT 'M'"
  208.      *                                          )
  209.      *                                      )
  210.      *                                  )
  211.      * @param boolean $check indicates whether the function should just check if the DBMS driver
  212.      *                               can perform the requested table alterations if the value is true or
  213.      *                               actually perform them otherwise.
  214.      * @access public
  215.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  216.      */
  217.     function alterTable($name$changes$check)
  218.     {
  219.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  220.         if ($check{
  221.             foreach ($changes as $change_name => $change{
  222.                 switch ($change_name{
  223.                 case 'added_fields':
  224.                 case 'removed_fields':
  225.                 case 'changed_fields':
  226.                 case 'name':
  227.                     break;
  228.                 case 'renamed_fields':
  229.                 default:
  230.                     return $db->raiseError(MDB2_ERRORnullnull,
  231.                         'alterTable: change type "'.$change_name.'" not yet supported');
  232.                 }
  233.             }
  234.             return MDB2_OK;
  235.         }
  236.         if (isset($changes['removed_fields'])) {
  237.             $query ' DROP (';
  238.             $fields $changes['removed_fields'];
  239.             $skipped_first = false;
  240.             foreach ($fields as $field_name => $field{
  241.                 if ($skipped_first{
  242.                     $query .= ', ';
  243.                 }
  244.                 $query .= $field_name;
  245.                 $skipped_first = true;
  246.             }
  247.             $query .= ')';
  248.             if (MDB2::isError($result $db->query("ALTER TABLE $name $query"))) {
  249.                 return $result;
  250.             }
  251.             $query '';
  252.         }
  253.         $query (isset($changes['name']'RENAME TO '.$changes['name''');
  254.         if (isset($changes['added_fields'])) {
  255.             $fields $changes['added_fields'];
  256.             foreach ($fields as $field{
  257.                 $query .= ' ADD ('.$field['declaration'].')';
  258.             }
  259.         }
  260.         if (isset($changes['changed_fields'])) {
  261.             $fields $changes['changed_fields'];
  262.             foreach ($fields as $field_name => $field{
  263.                 if (isset($renamed_fields[$field_name])) {
  264.                     $old_field_name $renamed_fields[$field_name];
  265.                     unset($renamed_fields[$field_name]);
  266.                 else {
  267.                     $old_field_name $field_name;
  268.                 }
  269.                 $change '';
  270.                 $change_type $change_default = false;
  271.                 if (isset($field['type'])) {
  272.                     $change_type $change_default = true;
  273.                 }
  274.                 if (isset($field['length'])) {
  275.                     $change_type = true;
  276.                 }
  277.                 if (isset($field['changed_default'])) {
  278.                     $change_default = true;
  279.                 }
  280.                 if ($change_type{
  281.                     $db->loadModule('Datatype');
  282.                     $change .= ' '.$db->datatype->getTypeDeclaration($field['definition']);
  283.                 }
  284.                 if ($change_default{
  285.                     $default (isset($field['definition']['default']$field['definition']['default': null);
  286.                     $change .= ' DEFAULT '.$db->quote($default$field['definition']['type']);
  287.                 }
  288.                 if (isset($field['changed_not_null'])) {
  289.                     $change .= (isset($field['notnull']' NOT' '').' NULL';
  290.                 }
  291.                 if ($change{
  292.                     $query .= " MODIFY ($old_field_name$change)";
  293.                 }
  294.             }
  295.         }
  296.         if ($query != '' &&
  297.             MDB2::isError($result $db->query("ALTER TABLE $name $query"))
  298.         {
  299.             return $result;
  300.         }
  301.         return MDB2_OK;
  302.     }
  303.  
  304.     // }}}
  305.     // {{{ listDatabases()
  306.  
  307.     /**
  308.      * list all databases
  309.      *
  310.      * @return mixed data array on success, a MDB2 error on failure
  311.      * @access public
  312.      */
  313.     function listDatabases()
  314.     {
  315.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  316.         if ($db->options['database_name_prefix']{
  317.             $query "SELECT SUBSTR(table_name, ".strlen($db->options['database_name_prefix'])
  318.                 .") FROM user_tables WHERE table_name LIKE '"
  319.                 .$db->options['database_name_prefix']."%'";
  320.         else {
  321.             $query "SELECT table_name FROM user_tables WHERE table_name LIKE '%'";
  322.         }
  323.         $result $db->standaloneQuery($query);
  324.         if (MDB2::isError($result)) {
  325.             return $result;
  326.         }
  327.         $databases $result->fetchCol();
  328.         $result->free();
  329.         return $databases;
  330.     }
  331.  
  332.     // }}}
  333.     // {{{ listTables()
  334.  
  335.     /**
  336.      * list all tables in the current database
  337.      *
  338.      * @return mixed data array on success, a MDB error on failure
  339.      * @access public
  340.      ***/
  341.     function listTables()
  342.     {
  343.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  344.         $query 'SELECT table_name FROM sys.user_tables';
  345.         return $db->queryCol($query);
  346.     }
  347.  
  348.     // }}}
  349.     // {{{ listTableFields()
  350.  
  351.     /**
  352.      * list all fields in a tables in the current database
  353.      *
  354.      * @param string $table name of table that should be used in method
  355.      * @return mixed data array on success, a MDB error on failure
  356.      * @access public
  357.      */
  358.     function listTableFields($table)
  359.     {
  360.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  361.         $table strtoupper($table);
  362.         $query = "SELECT column_name FROM user_tab_columns WHERE table_name='$table' ORDER BY column_id";
  363.         $fields $db->queryCol($query);
  364.         if (MDB2::isError($result)) {
  365.             return $result;
  366.         }
  367.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  368.             $fields array_flip(array_change_key_case(array_flip($fields)CASE_LOWER));
  369.         }
  370.         return $fields;
  371.     }
  372.  
  373.     // }}}
  374.     // {{{ createSequence()
  375.  
  376.     /**
  377.      * create sequence
  378.      *
  379.      * @param object $db database object that is extended by this class
  380.      * @param string $seq_name name of the sequence to be created
  381.      * @param string $start start value of the sequence; default is 1
  382.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  383.      * @access public
  384.      */
  385.     function createSequence($seq_name$start = 1)
  386.     {
  387.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  388.         $sequence_name $db->getSequenceName($seq_name);
  389.         return $db->query("CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1".
  390.             ($start < 1 ? " MINVALUE $start" : ''));
  391.     }
  392.  
  393.     // }}}
  394.     // {{{ dropSequence()
  395.  
  396.     /**
  397.      * drop existing sequence
  398.      *
  399.      * @param object $db database object that is extended by this class
  400.      * @param string $seq_name name of the sequence to be dropped
  401.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  402.      * @access public
  403.      */
  404.     function dropSequence($seq_name)
  405.     {
  406.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  407.         $sequence_name $db->getSequenceName($seq_name);
  408.         return $db->query("DROP SEQUENCE $sequence_name");
  409.     }
  410.  
  411.     // }}}
  412.     // {{{ listSequences()
  413.  
  414.     /**
  415.      * list all sequences in the current database
  416.      *
  417.      * @return mixed data array on success, a MDB2 error on failure
  418.      * @access public
  419.      */
  420.     function listSequences()
  421.     {
  422.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  423.         $query "SELECT sequence_name FROM sys.user_sequences";
  424.         $table_names $db->queryCol($query);
  425.         if (MDB2::isError($table_names)) {
  426.             return $table_names;
  427.         }
  428.         $sequences = array();
  429.         for ($i = 0$j count($table_names)$i $j; ++$i{
  430.             if ($sqn $this->_isSequenceName($table_names[$i]))
  431.                 $sequences[$sqn;
  432.         }
  433.         if ($db->options['optimize'== 'portability'{
  434.             $sequences array_flip($sequences);
  435.             $sequences array_change_key_case($sequencesCASE_LOWER);
  436.             $sequences array_flip($sequences);
  437.         }
  438.         return $sequences;
  439.     }}
  440. ?>

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