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

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