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

Source for file ibase.php

Documentation is available at ibase.php

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

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