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.17 2005/06/06 09:01:38 lsmith Exp $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 oci8 driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author Lukas Smith <smith@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(table_name, ".strlen($db->options['database_name_prefix'])
  311.                 .") FROM user_tables WHERE table_name LIKE '"
  312.                 .$db->options['database_name_prefix']."%'";
  313.         else {
  314.             $query "SELECT table_name FROM user_tables WHERE table_name LIKE '%'";
  315.         }
  316.         $result $db->standaloneQuery($query);
  317.         if (PEAR::isError($result)) {
  318.             return $result;
  319.         }
  320.         $databases $result->fetchCol();
  321.         $result->free();
  322.         return $databases;
  323.     }
  324.  
  325.         // }}}
  326.     // {{{ listUsers()
  327.  
  328.     /**
  329.      * list all users in the current database
  330.      *
  331.      * @return mixed data array on success, a MDB2 error on failure
  332.      * @access public
  333.      */
  334.     function listUsers()
  335.     {
  336.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  337.         $query "SELECT username FROM sys.all_users";
  338.         $users $db->queryCol($query);
  339.         if (PEAR::isError($users)) {
  340.             return $users;
  341.         }
  342.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  343.             $users array_flip($users);
  344.             $users array_change_key_case($usersCASE_LOWER);
  345.             $users array_flip($users);
  346.         }
  347.         return $users;
  348.     }
  349.     // }}}
  350.     // {{{ listViews()
  351.  
  352.     /**
  353.      * list all views in the current database
  354.      *
  355.      * @return mixed data array on success, a MDB2 error on failure
  356.      * @access public
  357.      */
  358.     function listViews()
  359.     {
  360.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  361.         $query "SELECT view_name FROM sys.user_views";
  362.         $views $db->queryCol($query);
  363.         if (PEAR::isError($views)) {
  364.             return $views;
  365.         }
  366.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  367.             $views array_flip($views);
  368.             $views array_change_key_case($viewsCASE_LOWER);
  369.             $views array_flip($views);
  370.         }
  371.         return $views;
  372.     }
  373.  
  374.     // }}}
  375.     // {{{ listFunctions()
  376.  
  377.     /**
  378.      * list all functions in the current database
  379.      *
  380.      * @return mixed data array on success, a MDB2 error on failure
  381.      * @access public
  382.      */
  383.     function listFunctions()
  384.     {
  385.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  386.         $query "SELECT name FROM sys.user_source WHERE line = 1 AND type = 'FUNCTION'";
  387.         $functions $db->queryCol($query);
  388.         if (PEAR::isError($functions)) {
  389.             return $functions;
  390.         }
  391.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  392.             $functions array_flip($functions);
  393.             $functions array_change_key_case($functionsCASE_LOWER);
  394.             $functions array_flip($functions);
  395.         }
  396.         return $functions;
  397.     }
  398.  
  399.     // }}}
  400.     // {{{ listTables()
  401.  
  402.     /**
  403.      * list all tables in the current database
  404.      *
  405.      * @return mixed data array on success, a MDB error on failure
  406.      * @access public
  407.      ***/
  408.     function listTables()
  409.     {
  410.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  411.         $query 'SELECT table_name FROM sys.user_tables';
  412.         return $db->queryCol($query);
  413.     }
  414.  
  415.     // }}}
  416.     // {{{ listTableFields()
  417.  
  418.     /**
  419.      * list all fields in a tables in the current database
  420.      *
  421.      * @param string $table name of table that should be used in method
  422.      * @return mixed data array on success, a MDB error on failure
  423.      * @access public
  424.      */
  425.     function listTableFields($table)
  426.     {
  427.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  428.         $table strtoupper($table);
  429.         $query = "SELECT column_name FROM user_tab_columns WHERE table_name='$table' ORDER BY column_id";
  430.         $fields $db->queryCol($query);
  431.         if (PEAR::isError($result)) {
  432.             return $result;
  433.         }
  434.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  435.             $fields array_flip(array_change_key_case(array_flip($fields)CASE_LOWER));
  436.         }
  437.         return $fields;
  438.     }
  439.  
  440.     // }}}
  441.     // {{{ createSequence()
  442.  
  443.     /**
  444.      * create sequence
  445.      *
  446.      * @param object $db database object that is extended by this class
  447.      * @param string $seq_name name of the sequence to be created
  448.      * @param string $start start value of the sequence; default is 1
  449.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  450.      * @access public
  451.      */
  452.     function createSequence($seq_name$start = 1)
  453.     {
  454.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  455.         $sequence_name $db->getSequenceName($seq_name);
  456.         return $db->query("CREATE SEQUENCE $sequence_name START WITH $start INCREMENT BY 1".
  457.             ($start < 1 ? " MINVALUE $start" : ''));
  458.     }
  459.  
  460.     // }}}
  461.     // {{{ dropSequence()
  462.  
  463.     /**
  464.      * drop existing sequence
  465.      *
  466.      * @param object $db database object that is extended by this class
  467.      * @param string $seq_name name of the sequence to be dropped
  468.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  469.      * @access public
  470.      */
  471.     function dropSequence($seq_name)
  472.     {
  473.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  474.         $sequence_name $db->getSequenceName($seq_name);
  475.         return $db->query("DROP SEQUENCE $sequence_name");
  476.     }
  477.  
  478.     // }}}
  479.     // {{{ listSequences()
  480.  
  481.     /**
  482.      * list all sequences in the current database
  483.      *
  484.      * @return mixed data array on success, a MDB2 error on failure
  485.      * @access public
  486.      */
  487.     function listSequences()
  488.     {
  489.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  490.         $query "SELECT sequence_name FROM sys.user_sequences";
  491.         $table_names $db->queryCol($query);
  492.         if (PEAR::isError($table_names)) {
  493.             return $table_names;
  494.         }
  495.         $sequences = array();
  496.         for ($i = 0$j count($table_names)$i $j; ++$i{
  497.             if ($sqn $this->_isSequenceName($table_names[$i]))
  498.                 $sequences[$sqn;
  499.         }
  500.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  501.             $sequences array_flip($sequences);
  502.             $sequences array_change_key_case($sequencesCASE_LOWER);
  503.             $sequences array_flip($sequences);
  504.         }
  505.         return $sequences;
  506.     }}
  507. ?>

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