MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file oci8.php

Documentation is available at oci8.php

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

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