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.13 2005/03/06 17:37:38 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.         if ($check{
  232.             foreach ($changes as $change_name => $change{
  233.                 switch ($change_name{
  234.                 case 'added_fields':
  235.                 case 'removed_fields':
  236.                 case 'renamed_fields':
  237.                     break;
  238.                 case 'changed_fields':
  239.                     $fields $changes['changed_fields'];
  240.                     foreach ($fields as $field{
  241.                         if (MDB2::isError($err $this->checkSupportedChanges($field))) {
  242.                             return $err;
  243.                         }
  244.                     }
  245.                     break;
  246.                 default:
  247.                     return $this->raiseError(MDB2_ERROR_MANAGER'''',
  248.                         'alterTable: change type ' $change_name ' not yet supported');
  249.                 }
  250.             }
  251.             return MDB2_OK;
  252.         else {
  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 (MDB2::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 (MDB2::isError($err $db->query("ALTER TABLE $name $query"))) {
  295.                 return $err;
  296.             }
  297.             return MDB2_OK;
  298.         }
  299.     }
  300.  
  301.     // }}}
  302.     // {{{ listTableFields()
  303.  
  304.     /**
  305.      * list all fields in a tables in the current database
  306.      *
  307.      * @param string $table name of table that should be used in method
  308.      * @return mixed data array on success, a MDB2 error on failure
  309.      * @access public
  310.      */
  311.     function listTableFields($table)
  312.     {
  313.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  314.         $query 'SELECT RDB$FIELD_SOURCE FROM RDB$RELATION_FIELDS WHERE RDB$RELATION_NAME=\'$table\'';
  315.         $result $db->query($querynullfalse);
  316.         if (MDB2::isError($result)) {
  317.             return $result;
  318.         }
  319.         $columns $result->getColumnNames();
  320.         $result->free();
  321.         if (MDB2::isError($columns)) {
  322.             return $columns;
  323.         }
  324.         return array_flip($columns);
  325.     }
  326.  
  327.     // }}}
  328.     // {{{ listViews()
  329.  
  330.     /**
  331.      * list the views in the database
  332.      *
  333.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  334.      * @access public
  335.      ***/
  336.     function listViews()
  337.     {
  338.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  339.         return $db->queryCol('SELECT RDB$VIEW_NAME');
  340.     }
  341.  
  342.     // }}}
  343.     // {{{ createIndex()
  344.  
  345.     /**
  346.      * get the stucture of a field into an array
  347.      *
  348.      * @param string    $table         name of the table on which the index is to be created
  349.      * @param string    $name         name of the index to be created
  350.      * @param array     $definition        associative array that defines properties of the index to be created.
  351.      *                                  Currently, only one property named FIELDS is supported. This property
  352.      *                                  is also an associative with the names of the index fields as array
  353.      *                                  indexes. Each entry of this array is set to another type of associative
  354.      *                                  array that specifies properties of the index that are specific to
  355.      *                                  each field.
  356.      *
  357.      *                                 Currently, only the sorting property is supported. It should be used
  358.      *                                  to define the sorting direction of the index. It may be set to either
  359.      *                                  ascending or descending.
  360.      *
  361.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  362.      *                                  drivers of those that do not support it ignore this property. Use the
  363.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  364.  
  365.      *                                  Example
  366.      *                                     array(
  367.      *                                         'fields' => array(
  368.      *                                             'user_name' => array(
  369.      *                                                 'sorting' => 'ascending'
  370.      *                                             ),
  371.      *                                             'last_login' => array()
  372.      *                                         )
  373.      *                                     )
  374.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  375.      * @access public
  376.      */
  377.     function createIndex($table$name$definition)
  378.     {
  379.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  380.         $query_sort $query_fields '';
  381.         foreach ($definition['fields'as $field_name => $field{
  382.             if ($query_fields{
  383.                 $query_fields .= ',';
  384.             }
  385.             $query_fields .= $field_name;
  386.             if ($query_sort && $db->support('index_sorting')
  387.                 && isset($definition['fields'][$field_name]['sorting'])
  388.             {
  389.                 switch ($definition['fields'][$field_name]['sorting']{
  390.                 case 'ascending':
  391.                     $query_sort ' ASC';
  392.                     break;
  393.                 case 'descending':
  394.                     $query_sort ' DESC';
  395.                     break;
  396.                 }
  397.             }
  398.         }
  399.         return $db->query('CREATE'.(isset($definition['unique']' UNIQUE' ''$query_sort.
  400.              " INDEX $name  ON $table ($query_fields)");
  401.     }
  402.  
  403.     // }}}
  404.     // {{{ createSequence()
  405.  
  406.     /**
  407.      * create sequence
  408.      *
  409.      * @param string $seq_name name of the sequence to be created
  410.      * @param string $start start value of the sequence; default is 1
  411.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  412.      * @access public
  413.      ***/
  414.     function createSequence($seq_name$start = 1)
  415.     {
  416.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  417.         $sequence_name $db->getSequenceName($seq_name);
  418.         if (MDB2::isError($result $db->query('CREATE GENERATOR '.strtoupper($sequence_name)))) {
  419.             return $result;
  420.         }
  421.         if (MDB2::isError($result $db->query('SET GENERATOR '.strtoupper($sequence_name).' TO '.($start-1)))) {
  422.             if (MDB2::isError($err $db->dropSequence($seq_name))) {
  423.                 return $this->raiseError(MDB2_ERROR_MANAGERnullnull,
  424.                     'createSequence: Could not setup sequence start value and then it was not possible to drop it: '.
  425.                     $err->getMessage().' - ' .$err->getUserInfo());
  426.             }
  427.         }
  428.         return $result;
  429.     }
  430.  
  431.     // }}}
  432.     // {{{ dropSequence()
  433.  
  434.     /**
  435.      * drop existing sequence
  436.      *
  437.      * @param string $seq_name name of the sequence to be dropped
  438.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  439.      * @access public
  440.      ***/
  441.     function dropSequence($seq_name)
  442.     {
  443.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  444.         $sequence_name $db->getSequenceName($seq_name);
  445.         return $db->query('DELETE FROM RDB$GENERATORS WHERE RDB$GENERATOR_NAME=\''.strtoupper($sequence_name).'\'');
  446.     }
  447.  
  448.     // }}}
  449.     // {{{ listSequences()
  450.  
  451.     /**
  452.      * list all sequences in the current database
  453.      *
  454.      * @return mixed data array on success, a MDB2 error on failure
  455.      * @access public
  456.      ***/
  457.     function listSequences()
  458.     {
  459.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  460.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS';
  461.         $table_names $db->queryCol($query);
  462.         if (MDB2::isError($table_names)) {
  463.             return $table_names;
  464.         }
  465.         $sequences = array();
  466.         for ($i = 0$j count($table_names)$i $j; ++$i{
  467.             if ($sqn $this->_isSequenceName($table_names[$i]))
  468.                 $sequences[$sqn;
  469.         }
  470.         return $sequences;
  471.     }
  472. }
  473. ?>

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