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

Source for file ibase.php

Documentation is available at ibase.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: Lorenzo Alberton <l.alberton@quipo.it>                       |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: ibase.php,v 1.18 2005/04/01 12:53:34 lsmith Exp $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 FireBird/InterBase driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  55.  */
  56. {
  57.     // {{{ createDatabase()
  58.  
  59.     /**
  60.      * create a new database
  61.      *
  62.      * @param string $name  name of the database that should be created
  63.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  64.      * @access public
  65.      ***/
  66.     function createDatabase($name)
  67.     {
  68.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  69.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull'Create database',
  70.                 'createDatabase: PHP Interbase API does not support direct queries. You have to '.
  71.                 'create the db manually by using isql command or a similar program');
  72.     }
  73.  
  74.     // }}}
  75.     // {{{ dropDatabase()
  76.  
  77.     /**
  78.      * drop an existing database
  79.      *
  80.      * @param string $name  name of the database that should be dropped
  81.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  82.      * @access public
  83.      ***/
  84.     function dropDatabase($name)
  85.     {
  86.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  87.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull'Drop database',
  88.                 'dropDatabase: PHP Interbase API does not support direct queries. You have '.
  89.                 'to drop the db manually by using isql command or a similar program');
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ checkSupportedChanges()
  94.  
  95.     /**
  96.      * check if planned changes are supported
  97.      *
  98.      * @param string $name name of the database that should be dropped
  99.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  100.      * @access public
  101.      ***/
  102.     function checkSupportedChanges(&$changes)
  103.     {
  104.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  105.         foreach ($changes as $change_name => $change{
  106.             switch ($change_name{
  107.             case 'changed_not_null':
  108.             case 'notnull':
  109.                 return $this->raiseError(MDB2_ERROR_MANAGER'''',
  110.                     'checkSupportedChanges: it is not supported changes to field not null constraint');
  111.             case 'ChangedDefault':
  112.             case 'default':
  113.                 return $this->raiseError(MDB2_ERROR_MANAGER'''',
  114.                     'checkSupportedChanges: it is not supported changes to field default value');
  115.             case 'length':
  116.                 return $this->raiseError(MDB2_ERROR_MANAGER'''',
  117.                     'checkSupportedChanges: it is not supported changes to field default length');
  118.             case 'unsigned':
  119.             case 'type':
  120.             case 'declaration':
  121.             case 'definition':
  122.                 break;
  123.             default:
  124.                 return $this->raiseError(MDB2_ERROR_MANAGER'''',
  125.                     'checkSupportedChanges: it is not supported change of type' $change_name);
  126.             }
  127.         }
  128.         return MDB2_OK;
  129.     }
  130.  
  131.     // }}}
  132.     // {{{ alterTable()
  133.  
  134.     /**
  135.      * alter an existing table
  136.      *
  137.      * @param string $name name of the table that is intended to be changed.
  138.      * @param array $changes associative array that contains the details of each type
  139.      *                               of change that is intended to be performed. The types of
  140.      *                               changes that are currently supported are defined as follows:
  141.      *
  142.      *                               name
  143.      *
  144.      *                                  New name for the table.
  145.      *
  146.      *                              added_fields
  147.      *
  148.      *                                  Associative array with the names of fields to be added as
  149.      *                                   indexes of the array. The value of each entry of the array
  150.      *                                   should be set to another associative array with the properties
  151.      *                                   of the fields to be added. The properties of the fields should
  152.      *                                   be the same as defined by the Metabase parser.
  153.      *
  154.      *                                  Additionally, there should be an entry named Declaration that
  155.      *                                   is expected to contain the portion of the field declaration already
  156.      *                                   in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  157.      *
  158.      *                              removed_fields
  159.      *
  160.      *                                  Associative array with the names of fields to be removed as indexes
  161.      *                                   of the array. Currently the values assigned to each entry are ignored.
  162.      *                                   An empty array should be used for future compatibility.
  163.      *
  164.      *                              renamed_fields
  165.      *
  166.      *                                  Associative array with the names of fields to be renamed as indexes
  167.      *                                   of the array. The value of each entry of the array should be set to
  168.      *                                   another associative array with the entry named name with the new
  169.      *                                   field name and the entry named Declaration that is expected to contain
  170.      *                                   the portion of the field declaration already in DBMS specific SQL code
  171.      *                                   as it is used in the CREATE TABLE statement.
  172.      *
  173.      *                              changed_fields
  174.      *
  175.      *                                  Associative array with the names of the fields to be changed as indexes
  176.      *                                   of the array. Keep in mind that if it is intended to change either the
  177.      *                                   name of a field and any other properties, the changed_fields array entries
  178.      *                                   should have the new names of the fields as array indexes.
  179.      *
  180.      *                                  The value of each entry of the array should be set to another associative
  181.      *                                   array with the properties of the fields to that are meant to be changed as
  182.      *                                   array entries. These entries should be assigned to the new values of the
  183.      *                                   respective properties. The properties of the fields should be the same
  184.      *                                   as defined by the Metabase parser.
  185.      *
  186.      *                                  If the default property is meant to be added, removed or changed, there
  187.      *                                   should also be an entry with index ChangedDefault assigned to 1. Similarly,
  188.      *                                   if the notnull constraint is to be added or removed, there should also be
  189.      *                                   an entry with index ChangedNotNull assigned to 1.
  190.      *
  191.      *                                  Additionally, there should be an entry named Declaration that is expected
  192.      *                                   to contain the portion of the field changed declaration already in DBMS
  193.      *                                   specific SQL code as it is used in the CREATE TABLE statement.
  194.      *                              Example
  195.      *                                  array(
  196.      *                                      'name' => 'userlist',
  197.      *                                      'added_fields' => array(
  198.      *                                          'quota' => array(
  199.      *                                              'type' => 'integer',
  200.      *                                              'unsigned' => 1
  201.      *                                              'declaration' => 'quota INT'
  202.      *                                          )
  203.      *                                      ),
  204.      *                                      'removed_fields' => array(
  205.      *                                          'file_limit' => array(),
  206.      *                                          'time_limit' => array()
  207.      *                                          ),
  208.      *                                      'changed_fields' => array(
  209.      *                                          'gender' => array(
  210.      *                                              'default' => 'M',
  211.      *                                              'change_default' => 1,
  212.      *                                              'declaration' => "gender CHAR(1) DEFAULT 'M'"
  213.      *                                          )
  214.      *                                      ),
  215.      *                                      'renamed_fields' => array(
  216.      *                                          'sex' => array(
  217.      *                                              'name' => 'gender',
  218.      *                                              'declaration' => "gender CHAR(1) DEFAULT 'M'"
  219.      *                                          )
  220.      *                                      )
  221.      *                                  )
  222.      * @param boolean $check indicates whether the function should just check if the DBMS driver
  223.      *                               can perform the requested table alterations if the value is true or
  224.      *                               actually perform them otherwise.
  225.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  226.      * @access public
  227.      ***/
  228.     function alterTable($name&$changes$check)
  229.     {
  230.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  231.         foreach ($changes as $change_name => $change{
  232.             switch ($change_name{
  233.             case 'added_fields':
  234.             case 'removed_fields':
  235.             case 'renamed_fields':
  236.                 break;
  237.             case 'changed_fields':
  238.                 $fields $changes['changed_fields'];
  239.                 foreach ($fields as $field{
  240.                     if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  241.                         return $err;
  242.                     }
  243.                 }
  244.                 break;
  245.             default:
  246.                 return $this->raiseError(MDB2_ERROR_MANAGER'''',
  247.                     'alterTable: change type ' $change_name ' not yet supported');
  248.             }
  249.         }
  250.         if ($check{
  251.             return MDB2_OK;
  252.         }
  253.         $query '';
  254.         if (isset($changes['added_fields'])) {
  255.             $fields $changes['added_fields'];
  256.             foreach ($fields as $field_name => $field{
  257.                 if ($query{
  258.                     $query .= ', ';
  259.                 }
  260.                 $query .= 'ADD ' $field['declaration'];
  261.             }
  262.         }
  263.         if (isset($changes['removed_fields'])) {
  264.             $fields $changes['removed_fields'];
  265.             foreach ($fields as $field_name => $field{
  266.                 if ($query{
  267.                     $query .= ', ';
  268.                 }
  269.                 $query .= 'DROP ' $field_name;
  270.             }
  271.         }
  272.         if (isset($changes['renamed_fields'])) {
  273.             $fields $changes['renamed_fields'];
  274.             foreach ($fields as $field_name => $field{
  275.                 if ($query{
  276.                     $query .= ', ';
  277.                 }
  278.                 $query .= 'ALTER ' $field_name ' TO ' $field['name'];
  279.             }
  280.         }
  281.         if (isset($changes['changed_fields'])) {
  282.             $fields $changes['changed_fields'];
  283.             foreach ($fields as $field_name => $field{
  284.                 if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  285.                     return $err;
  286.                 }
  287.                 if ($query{
  288.                     $query .= ', ';
  289.                 }
  290.                 $db->loadModule('Datatype');
  291.                 $query .= 'ALTER '.$field_name.' TYPE '.$db->datatype->getTypeDeclaration($field['definition']);
  292.             }
  293.         }
  294.         if (!$query{
  295.             return MDB2_OK;
  296.         }
  297.         return $db->query("ALTER TABLE $name $query");
  298.     }
  299.  
  300.     // }}}
  301.     // {{{ listTableFields()
  302.  
  303.     /**
  304.      * list all fields in a tables in the current database
  305.      *
  306.      * @param string $table name of table that should be used in method
  307.      * @return mixed data array on success, a MDB2 error on failure
  308.      * @access public
  309.      */
  310.     function listTableFields($table)
  311.     {
  312.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  313.         $query 'SELECT RDB$FIELD_SOURCE FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME=\'$table\'';
  314.         $result $db->query($query);
  315.         if (PEAR::isError($result)) {
  316.             return $result;
  317.         }
  318.         $columns $result->getColumnNames();
  319.         $result->free();
  320.         if (PEAR::isError($columns)) {
  321.             return $columns;
  322.         }
  323.         return array_flip($columns);
  324.     }
  325.  
  326.     // }}}
  327.     // {{{ listViews()
  328.  
  329.     /**
  330.      * list the views in the database
  331.      *
  332.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  333.      * @access public
  334.      ***/
  335.     function listViews()
  336.     {
  337.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  338.         return $db->queryCol('SELECT RDB$VIEW_NAME');
  339.     }
  340.  
  341.     // }}}
  342.     // {{{ createIndex()
  343.  
  344.     /**
  345.      * get the stucture of a field into an array
  346.      *
  347.      * @param string    $table         name of the table on which the index is to be created
  348.      * @param string    $name         name of the index to be created
  349.      * @param array     $definition        associative array that defines properties of the index to be created.
  350.      *                                  Currently, only one property named FIELDS is supported. This property
  351.      *                                  is also an associative with the names of the index fields as array
  352.      *                                  indexes. Each entry of this array is set to another type of associative
  353.      *                                  array that specifies properties of the index that are specific to
  354.      *                                  each field.
  355.      *
  356.      *                                 Currently, only the sorting property is supported. It should be used
  357.      *                                  to define the sorting direction of the index. It may be set to either
  358.      *                                  ascending or descending.
  359.      *
  360.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  361.      *                                  drivers of those that do not support it ignore this property. Use the
  362.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  363.  
  364.      *                                  Example
  365.      *                                     array(
  366.      *                                         'fields' => array(
  367.      *                                             'user_name' => array(
  368.      *                                                 'sorting' => 'ascending'
  369.      *                                             ),
  370.      *                                             'last_login' => array()
  371.      *                                         )
  372.      *                                     )
  373.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  374.      * @access public
  375.      */
  376.     function createIndex($table$name$definition)
  377.     {
  378.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  379.         $query_sort $query_fields '';
  380.         foreach ($definition['fields'as $field_name => $field{
  381.             if ($query_fields{
  382.                 $query_fields .= ',';
  383.             }
  384.             $query_fields .= $field_name;
  385.             if ($query_sort && $db->support('index_sorting')
  386.                 && isset($definition['fields'][$field_name]['sorting'])
  387.             {
  388.                 switch ($definition['fields'][$field_name]['sorting']{
  389.                 case 'ascending':
  390.                     $query_sort ' ASC';
  391.                     break;
  392.                 case 'descending':
  393.                     $query_sort ' DESC';
  394.                     break;
  395.                 }
  396.             }
  397.         }
  398.         return $db->query('CREATE'.(isset($definition['unique']' UNIQUE' ''$query_sort.
  399.              " INDEX $name  ON $table ($query_fields)");
  400.     }
  401.  
  402.     // }}}
  403.     // {{{ createSequence()
  404.  
  405.     /**
  406.      * create sequence
  407.      *
  408.      * @param string $seq_name name of the sequence to be created
  409.      * @param string $start start value of the sequence; default is 1
  410.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  411.      * @access public
  412.      ***/
  413.     function createSequence($seq_name$start = 1)
  414.     {
  415.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  416.         $sequence_name $db->getSequenceName($seq_name);
  417.         if (PEAR::isError($result $db->query('CREATE GENERATOR '.strtoupper($sequence_name)))) {
  418.             return $result;
  419.         }
  420.         if (PEAR::isError($result $db->query('SET GENERATOR '.strtoupper($sequence_name).' TO '.($start-1)))) {
  421.             if (PEAR::isError($err $db->dropSequence($seq_name))) {
  422.                 return $this->raiseError(MDB2_ERROR_MANAGERnullnull,
  423.                     'createSequence: Could not setup sequence start value and then it was not possible to drop it: '.
  424.                     $err->getMessage().' - ' .$err->getUserInfo());
  425.             }
  426.         }
  427.         return $result;
  428.     }
  429.  
  430.     // }}}
  431.     // {{{ dropSequence()
  432.  
  433.     /**
  434.      * drop existing sequence
  435.      *
  436.      * @param string $seq_name name of the sequence to be dropped
  437.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  438.      * @access public
  439.      ***/
  440.     function dropSequence($seq_name)
  441.     {
  442.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  443.         $sequence_name $db->getSequenceName($seq_name);
  444.         return $db->query('DELETE FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME=\''.strtoupper($sequence_name).'\'');
  445.     }
  446.  
  447.     // }}}
  448.     // {{{ listSequences()
  449.  
  450.     /**
  451.      * list all sequences in the current database
  452.      *
  453.      * @return mixed data array on success, a MDB2 error on failure
  454.      * @access public
  455.      ***/
  456.     function listSequences()
  457.     {
  458.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  459.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS';
  460.         $table_names $db->queryCol($query);
  461.         if (PEAR::isError($table_names)) {
  462.             return $table_names;
  463.         }
  464.         $sequences = array();
  465.         for ($i = 0$j count($table_names)$i $j; ++$i{
  466.             if ($sqn $this->_isSequenceName($table_names[$i]))
  467.                 $sequences[$sqn;
  468.         }
  469.         return $sequences;
  470.     }
  471. }
  472. ?>

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