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

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