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-2007 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith, Lorenzo Alberton                       |
  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.106 2007/09/23 18:46:05 quipo 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.     // {{{ createDatabase()
  59.  
  60.     /**
  61.      * create a new database
  62.      *
  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 =$this->getDBInstance();
  70.         if (PEAR::isError($db)) {
  71.             return $db;
  72.         }
  73.  
  74.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull'Create database',
  75.                 'PHP Interbase API does not support direct queries. You have to '.
  76.                 'create the db manually by using isql command or a similar program'__FUNCTION__);
  77.     }
  78.  
  79.     // }}}
  80.     // {{{ dropDatabase()
  81.  
  82.     /**
  83.      * drop an existing database
  84.      *
  85.      * @param string $name  name of the database that should be dropped
  86.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  87.      * @access public
  88.      */
  89.     function dropDatabase($name)
  90.     {
  91.         $db =$this->getDBInstance();
  92.         if (PEAR::isError($db)) {
  93.             return $db;
  94.         }
  95.  
  96.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull'Drop database',
  97.                 'PHP Interbase API does not support direct queries. You have '.
  98.                 'to drop the db manually by using isql command or a similar program'__FUNCTION__);
  99.     }
  100.  
  101.     // }}}
  102.     // {{{ _silentCommit()
  103.  
  104.     /**
  105.      * conditional COMMIT query to make changes permanent, when auto
  106.      * @access private
  107.      */
  108.     function _silentCommit()
  109.     {
  110.         $db =$this->getDBInstance();
  111.         if (PEAR::isError($db)) {
  112.             return $db;
  113.         }
  114.         if (!$db->in_transaction{
  115.             @$db->exec('COMMIT');
  116.         }
  117.     }
  118.  
  119.     // }}}
  120.     // {{{ _makeAutoincrement()
  121.  
  122.     /**
  123.      * add an autoincrement sequence + trigger
  124.      *
  125.      * @param string $name  name of the PK field
  126.      * @param string $table name of the table
  127.      * @param string $start start value for the sequence
  128.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  129.      * @access private
  130.      */
  131.     function _makeAutoincrement($name$table$start = null)
  132.     {
  133.         $db =$this->getDBInstance();
  134.         if (PEAR::isError($db)) {
  135.             return $db;
  136.         }
  137.  
  138.         if (is_null($start)) {
  139.             $db->beginTransaction();
  140.             $query 'SELECT MAX(' $db->quoteIdentifier($nametrue') FROM ' $db->quoteIdentifier($tabletrue);
  141.             $start $this->db->queryOne($query'integer');
  142.             if (PEAR::isError($start)) {
  143.                 return $start;
  144.             }
  145.             ++$start;
  146.             $result $db->manager->createSequence($table$start);
  147.             $db->commit();
  148.         else {
  149.             $result $db->manager->createSequence($table$start);
  150.         }
  151.         if (PEAR::isError($result)) {
  152.             return $db->raiseError(nullnullnull,
  153.                 'sequence for autoincrement PK could not be created'__FUNCTION__);
  154.         }
  155.  
  156.         $sequence_name $db->getSequenceName($table);
  157.         $trigger_name  $db->quoteIdentifier($table '_AUTOINCREMENT_PK'true);
  158.         $table $db->quoteIdentifier($tabletrue);
  159.         $name  $db->quoteIdentifier($nametrue);
  160.         $trigger_sql 'CREATE TRIGGER ' $trigger_name ' FOR ' $table '
  161.                         ACTIVE BEFORE INSERT POSITION 0
  162.                         AS
  163.                         BEGIN
  164.                         IF (NEW.' $name ' IS NULL OR NEW.' $name ' = 0) THEN
  165.                             NEW.' $name ' = GEN_ID('.$sequence_name.', 1);
  166.                         END';
  167.         $result $db->exec($trigger_sql);
  168.         if (PEAR::isError($result)) {
  169.             return $result;
  170.         }
  171.         $this->_silentCommit();
  172.         return MDB2_OK;
  173.     }
  174.  
  175.     // }}}
  176.     // {{{ _dropAutoincrement()
  177.  
  178.     /**
  179.      * drop an existing autoincrement sequence + trigger
  180.      *
  181.      * @param string $table name of the table
  182.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  183.      * @access private
  184.      */
  185.     function _dropAutoincrement($table)
  186.     {
  187.         $db =$this->getDBInstance();
  188.         if (PEAR::isError($db)) {
  189.             return $db;
  190.         }
  191.         $result $db->manager->dropSequence($table);
  192.         if (PEAR::isError($result)) {
  193.             return $db->raiseError(nullnullnull,
  194.                 'sequence for autoincrement PK could not be dropped'__FUNCTION__);
  195.         }
  196.         //remove autoincrement trigger associated with the table
  197.         $table $db->quote(strtoupper($table)'text');
  198.         $trigger_name $db->quote(strtoupper($table'_AUTOINCREMENT_PK''text');
  199.         $result $db->exec("DELETE FROM RDB\$TRIGGERS WHERE UPPER(RDB\$RELATION_NAME)=$table AND UPPER(RDB\$TRIGGER_NAME)=$trigger_name");
  200.         if (PEAR::isError($result)) {
  201.             return $db->raiseError(nullnullnull,
  202.                 'trigger for autoincrement PK could not be dropped'__FUNCTION__);
  203.         }
  204.         return MDB2_OK;
  205.     }
  206.  
  207.     // }}}
  208.     // {{{ createTable()
  209.  
  210.     /**
  211.      * create a new table
  212.      *
  213.      * @param string $name     Name of the database that should be created
  214.      * @param array $fields Associative array that contains the definition of each field of the new table
  215.      *                         The indexes of the array entries are the names of the fields of the table an
  216.      *                         the array entry values are associative arrays like those that are meant to be
  217.      *                          passed with the field definitions to get[Type]Declaration() functions.
  218.      *
  219.      *                         Example
  220.      *                         array(
  221.      *
  222.      *                             'id' => array(
  223.      *                                 'type' => 'integer',
  224.      *                                 'unsigned' => 1,
  225.      *                                 'notnull' => 1,
  226.      *                                 'default' => 0,
  227.      *                             ),
  228.      *                             'name' => array(
  229.      *                                 'type' => 'text',
  230.      *                                 'length' => 12,
  231.      *                             ),
  232.      *                             'description' => array(
  233.      *                                 'type' => 'text',
  234.      *                                 'length' => 12,
  235.      *                             )
  236.      *                         );
  237.      * @param array $options  An associative array of table options:
  238.      *                           array(
  239.      *                               'comment' => 'Foo',
  240.      *                               'temporary' => true|false,
  241.      *                           );
  242.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  243.      * @access public
  244.      */
  245.     function createTable($name$fields$options = array())
  246.     {
  247.         $db =$this->getDBInstance();
  248.         if (PEAR::isError($db)) {
  249.             return $db;
  250.         }
  251.         $query $this->_getCreateTableQuery($name$fields$options);
  252.         if (PEAR::isError($query)) {
  253.             return $query;
  254.         }
  255.  
  256.         $options_strings = array();
  257.  
  258.         if (!empty($options['comment'])) {
  259.             $options_strings['comment''/* '.$db->quote($options['comment']'text')' */';
  260.         }
  261.  
  262.         if (!empty($options_strings)) {
  263.             $query.= ' '.implode(' '$options_strings);
  264.         }
  265.         
  266.         $result $db->exec($query);
  267.         if (PEAR::isError($result)) {
  268.             return $result;
  269.         }
  270.         $this->_silentCommit();
  271.         foreach ($fields as $field_name => $field{
  272.             if (!empty($field['autoincrement'])) {
  273.                 //create PK constraint
  274.                 $pk_definition = array(
  275.                     'fields' => array($field_name => array()),
  276.                     'primary' => true,
  277.                 );
  278.                 //$pk_name = $name.'_PK';
  279.                 $pk_name = null;
  280.                 $result $this->createConstraint($name$pk_name$pk_definition);
  281.                 if (PEAR::isError($result)) {
  282.                     return $result;
  283.                 }
  284.                 //create autoincrement sequence + trigger
  285.                 return $this->_makeAutoincrement($field_name$name1);
  286.             }
  287.         }
  288.         return MDB2_OK;
  289.     }
  290.  
  291.     // }}}
  292.     // {{{ checkSupportedChanges()
  293.  
  294.     /**
  295.      * Check if planned changes are supported
  296.      *
  297.      * @param string $name name of the database that should be dropped
  298.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  299.      * @access public
  300.      */
  301.     function checkSupportedChanges(&$changes)
  302.     {
  303.         $db =$this->getDBInstance();
  304.         if (PEAR::isError($db)) {
  305.             return $db;
  306.         }
  307.  
  308.         foreach ($changes as $change_name => $change{
  309.             switch ($change_name{
  310.             case 'notnull':
  311.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  312.                     'it is not supported changes to field not null constraint'__FUNCTION__);
  313.             case 'default':
  314.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  315.                     'it is not supported changes to field default value'__FUNCTION__);
  316.             case 'length':
  317.                 /*
  318.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  319.                     'it is not supported changes to field default length', __FUNCTION__);
  320.                 */
  321.             case 'unsigned':
  322.             case 'type':
  323.             case 'declaration':
  324.             case 'definition':
  325.                 break;
  326.             default:
  327.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  328.                     'it is not supported change of type' $change_name__FUNCTION__);
  329.             }
  330.         }
  331.         return MDB2_OK;
  332.     }
  333.  
  334.     // }}}
  335.     // {{{ _getTemporaryTableQuery()
  336.  
  337.     /**
  338.      * A method to return the required SQL string that fits between CREATE ... TABLE
  339.      * to create the table as a temporary table.
  340.      *
  341.      * @return string The string required to be placed between "CREATE" and "TABLE"
  342.      *                 to generate a temporary table, if possible.
  343.      */
  344.     function _getTemporaryTableQuery()
  345.     {
  346.         return 'GLOBAL TEMPORARY';
  347.     }
  348.  
  349.     // }}}
  350.     // {{{ _getAdvancedFKOptions()
  351.  
  352.     /**
  353.      * Return the FOREIGN KEY query section dealing with non-standard options
  354.      * as MATCH, INITIALLY DEFERRED, ON UPDATE, ...
  355.      *
  356.      * @param array $definition 
  357.      * @return string 
  358.      * @access protected
  359.      */
  360.     function _getAdvancedFKOptions($definition)
  361.     {
  362.         $query '';
  363.         if (!empty($definition['onupdate'])) {
  364.             $query .= ' ON UPDATE '.$definition['onupdate'];
  365.         }
  366.         if (!empty($definition['ondelete'])) {
  367.             $query .= ' ON DELETE '.$definition['ondelete'];
  368.         }
  369.         return $query;
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ dropTable()
  374.  
  375.     /**
  376.      * drop an existing table
  377.      *
  378.      * @param string $name name of the table that should be dropped
  379.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  380.      * @access public
  381.      */
  382.     function dropTable($name)
  383.     {
  384.         $result $this->_dropAutoincrement($name);
  385.         if (PEAR::isError($result)) {
  386.             return $result;
  387.         }
  388.         $result = parent::dropTable($name);
  389.         $this->_silentCommit();
  390.         return $result;
  391.     }
  392.  
  393.     // }}}
  394.     // {{{ alterTable()
  395.  
  396.     /**
  397.      * alter an existing table
  398.      *
  399.      * @param string $name         name of the table that is intended to be changed.
  400.      * @param array $changes     associative array that contains the details of each type
  401.      *                              of change that is intended to be performed. The types of
  402.      *                              changes that are currently supported are defined as follows:
  403.      *
  404.      *                              name
  405.      *
  406.      *                                 New name for the table.
  407.      *
  408.      *                             add
  409.      *
  410.      *                                 Associative array with the names of fields to be added as
  411.      *                                  indexes of the array. The value of each entry of the array
  412.      *                                  should be set to another associative array with the properties
  413.      *                                  of the fields to be added. The properties of the fields should
  414.      *                                  be the same as defined by the MDB2 parser.
  415.      *
  416.      *
  417.      *                             remove
  418.      *
  419.      *                                 Associative array with the names of fields to be removed as indexes
  420.      *                                  of the array. Currently the values assigned to each entry are ignored.
  421.      *                                  An empty array should be used for future compatibility.
  422.      *
  423.      *                             rename
  424.      *
  425.      *                                 Associative array with the names of fields to be renamed as indexes
  426.      *                                  of the array. The value of each entry of the array should be set to
  427.      *                                  another associative array with the entry named name with the new
  428.      *                                  field name and the entry named Declaration that is expected to contain
  429.      *                                  the portion of the field declaration already in DBMS specific SQL code
  430.      *                                  as it is used in the CREATE TABLE statement.
  431.      *
  432.      *                             change
  433.      *
  434.      *                                 Associative array with the names of the fields to be changed as indexes
  435.      *                                  of the array. Keep in mind that if it is intended to change either the
  436.      *                                  name of a field and any other properties, the change array entries
  437.      *                                  should have the new names of the fields as array indexes.
  438.      *
  439.      *                                 The value of each entry of the array should be set to another associative
  440.      *                                  array with the properties of the fields to that are meant to be changed as
  441.      *                                  array entries. These entries should be assigned to the new values of the
  442.      *                                  respective properties. The properties of the fields should be the same
  443.      *                                  as defined by the MDB2 parser.
  444.      *
  445.      *                             Example
  446.      *                                 array(
  447.      *                                     'name' => 'userlist',
  448.      *                                     'add' => array(
  449.      *                                         'quota' => array(
  450.      *                                             'type' => 'integer',
  451.      *                                             'unsigned' => 1
  452.      *                                         )
  453.      *                                     ),
  454.      *                                     'remove' => array(
  455.      *                                         'file_limit' => array(),
  456.      *                                         'time_limit' => array()
  457.      *                                     ),
  458.      *                                     'change' => array(
  459.      *                                         'name' => array(
  460.      *                                             'length' => '20',
  461.      *                                             'definition' => array(
  462.      *                                                 'type' => 'text',
  463.      *                                                 'length' => 20,
  464.      *                                             ),
  465.      *                                         )
  466.      *                                     ),
  467.      *                                     'rename' => array(
  468.      *                                         'sex' => array(
  469.      *                                             'name' => 'gender',
  470.      *                                             'definition' => array(
  471.      *                                                 'type' => 'text',
  472.      *                                                 'length' => 1,
  473.      *                                                 'default' => 'M',
  474.      *                                             ),
  475.      *                                         )
  476.      *                                     )
  477.      *                                 )
  478.      *
  479.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  480.      *                              can perform the requested table alterations if the value is true or
  481.      *                              actually perform them otherwise.
  482.      * @access public
  483.      *
  484.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  485.      */
  486.     function alterTable($name$changes$check)
  487.     {
  488.         $db =$this->getDBInstance();
  489.         if (PEAR::isError($db)) {
  490.             return $db;
  491.         }
  492.  
  493.         foreach ($changes as $change_name => $change{
  494.             switch ($change_name{
  495.             case 'add':
  496.             case 'remove':
  497.             case 'rename':
  498.                 break;
  499.             case 'change':
  500.                 foreach ($changes['change'as $field{
  501.                     if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  502.                         return $err;
  503.                     }
  504.                 }
  505.                 break;
  506.             default:
  507.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  508.                     'change type ' $change_name ' not yet supported'__FUNCTION__);
  509.             }
  510.         }
  511.         if ($check{
  512.             return MDB2_OK;
  513.         }
  514.         $query '';
  515.         if (!empty($changes['add']&& is_array($changes['add'])) {
  516.             foreach ($changes['add'as $field_name => $field{
  517.                 if ($query{
  518.                     $query.= ', ';
  519.                 }
  520.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  521.             }
  522.         }
  523.  
  524.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  525.             foreach ($changes['remove'as $field_name => $field{
  526.                 if ($query{
  527.                     $query.= ', ';
  528.                 }
  529.                 $field_name $db->quoteIdentifier($field_nametrue);
  530.                 $query.= 'DROP ' $field_name;
  531.             }
  532.         }
  533.  
  534.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  535.             foreach ($changes['rename'as $field_name => $field{
  536.                 if ($query{
  537.                     $query.= ', ';
  538.                 }
  539.                 $field_name $db->quoteIdentifier($field_nametrue);
  540.                 $query.= 'ALTER ' $field_name ' TO ' $db->quoteIdentifier($field['name']true);
  541.             }
  542.         }
  543.  
  544.         if (!empty($changes['change']&& is_array($changes['change'])) {
  545.             // missing support to change DEFAULT and NULLability
  546.             foreach ($changes['change'as $field_name => $field{
  547.                 if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  548.                     return $err;
  549.                 }
  550.                 if ($query{
  551.                     $query.= ', ';
  552.                 }
  553.                 $db->loadModule('Datatype'nulltrue);
  554.                 $field_name $db->quoteIdentifier($field_nametrue);
  555.                 $query.= 'ALTER ' $field_name.' TYPE ' $db->datatype->getTypeDeclaration($field['definition']);
  556.             }
  557.         }
  558.  
  559.         if (!strlen($query)) {
  560.             return MDB2_OK;
  561.         }
  562.  
  563.         $name $db->quoteIdentifier($nametrue);
  564.         $result $db->exec("ALTER TABLE $name $query");
  565.         $this->_silentCommit();
  566.         return $result;
  567.     }
  568.  
  569.     // }}}
  570.     // {{{ listTables()
  571.  
  572.     /**
  573.      * list all tables in the current database
  574.      *
  575.      * @return mixed array of table names on success, a MDB2 error on failure
  576.      * @access public
  577.      */
  578.     function listTables()
  579.     {
  580.         $db =$this->getDBInstance();
  581.         if (PEAR::isError($db)) {
  582.             return $db;
  583.         }
  584.         $query 'SELECT RDB$RELATION_NAME
  585.                     FROM RDB$RELATIONS
  586.                    WHERE (RDB$SYSTEM_FLAG=0 OR RDB$SYSTEM_FLAG IS NULL)
  587.                      AND RDB$VIEW_BLR IS NULL
  588.                 ORDER BY RDB$RELATION_NAME';
  589.         $result $db->queryCol($query);
  590.         if (PEAR::isError($result)) {
  591.             return $result;
  592.         }
  593.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  594.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  595.         }
  596.         return $result;
  597.     }
  598.  
  599.     // }}}
  600.     // {{{ listTableFields()
  601.  
  602.     /**
  603.      * list all fields in a table in the current database
  604.      *
  605.      * @param string $table name of table that should be used in method
  606.      * @return mixed array of field names on success, a MDB2 error on failure
  607.      * @access public
  608.      */
  609.     function listTableFields($table)
  610.     {
  611.         $db =$this->getDBInstance();
  612.         if (PEAR::isError($db)) {
  613.             return $db;
  614.         }
  615.         $table $db->quote(strtoupper($table)'text');
  616.         $query = "SELECT RDB\$FIELD_NAME
  617.                     FROM RDB\$RELATION_FIELDS
  618.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  619.                      AND (RDB\$SYSTEM_FLAG=0 OR RDB\$SYSTEM_FLAG IS NULL)";
  620.         $result $db->queryCol($query);
  621.         if (PEAR::isError($result)) {
  622.             return $result;
  623.         }
  624.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  625.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  626.         }
  627.         return $result;
  628.     }
  629.  
  630.     // }}}
  631.     // {{{ listUsers()
  632.  
  633.     /**
  634.      * list all users
  635.      *
  636.      * @return mixed array of user names on success, a MDB2 error on failure
  637.      * @access public
  638.      */
  639.     function listUsers()
  640.     {
  641.         $db =$this->getDBInstance();
  642.         if (PEAR::isError($db)) {
  643.             return $db;
  644.         }
  645.         return $db->queryCol('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES');
  646.     }
  647.  
  648.     // }}}
  649.     // {{{ listViews()
  650.  
  651.     /**
  652.      * list all views in the current database
  653.      *
  654.      * @return mixed array of view names on success, a MDB2 error on failure
  655.      * @access public
  656.      */
  657.     function listViews()
  658.     {
  659.         $db =$this->getDBInstance();
  660.         if (PEAR::isError($db)) {
  661.             return $db;
  662.         }
  663.  
  664.         $result $db->queryCol('SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS');
  665.         if (PEAR::isError($result)) {
  666.             return $result;
  667.         }
  668.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  669.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  670.         }
  671.         return $result;
  672.     }
  673.  
  674.     // }}}
  675.     // {{{ listTableViews()
  676.  
  677.     /**
  678.      * list the views in the database that reference a given table
  679.      *
  680.      * @param string table for which all referenced views should be found
  681.      * @return mixed array of view names on success, a MDB2 error on failure
  682.      * @access public
  683.      */
  684.     function listTableViews($table)
  685.     {
  686.         $db =$this->getDBInstance();
  687.         if (PEAR::isError($db)) {
  688.             return $db;
  689.         }
  690.  
  691.         $query 'SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS';
  692.         $table $db->quote(strtoupper($table)'text');
  693.         $query .= " WHERE UPPER(RDB\$RELATION_NAME)=$table";
  694.         $result $db->queryCol($query);
  695.         if (PEAR::isError($result)) {
  696.             return $result;
  697.         }
  698.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  699.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  700.         }
  701.         return $result;
  702.     }
  703.  
  704.     // }}}
  705.     // {{{ listFunctions()
  706.  
  707.     /**
  708.      * list all functions (and stored procedures) in the current database
  709.      *
  710.      * @return mixed array of function names on success, a MDB2 error on failure
  711.      * @access public
  712.      */
  713.     function listFunctions()
  714.     {
  715.         $db =$this->getDBInstance();
  716.         if (PEAR::isError($db)) {
  717.             return $db;
  718.         }
  719.  
  720.         $query 'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE RDB$SYSTEM_FLAG IS NULL
  721.                   UNION
  722.                   SELECT RDB$PROCEDURE_NAME FROM RDB$PROCEDURES';
  723.         $result $db->queryCol($query);
  724.         if (PEAR::isError($result)) {
  725.             return $result;
  726.         }
  727.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  728.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  729.         }
  730.         return $result;
  731.     }
  732.  
  733.     // }}}
  734.     // {{{ listTableTriggers()
  735.  
  736.     /**
  737.      * list all triggers in the database that reference a given table
  738.      *
  739.      * @param string table for which all referenced triggers should be found
  740.      * @return mixed array of trigger names on success, a MDB2 error on failure
  741.      * @access public
  742.      */
  743.     function listTableTriggers($table = null)
  744.     {
  745.         $db =$this->getDBInstance();
  746.         if (PEAR::isError($db)) {
  747.             return $db;
  748.         }
  749.  
  750.         $query 'SELECT RDB$TRIGGER_NAME
  751.                     FROM RDB$TRIGGERS
  752.                    WHERE RDB$SYSTEM_FLAG IS NULL
  753.                       OR RDB$SYSTEM_FLAG = 0';
  754.         if (!is_null($table)) {
  755.             $table $db->quote(strtoupper($table)'text');
  756.             $query .= " AND UPPER(RDB\$RELATION_NAME)=$table";
  757.         }
  758.         $result $db->queryCol($query);
  759.         if (PEAR::isError($result)) {
  760.             return $result;
  761.         }
  762.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  763.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  764.         }
  765.         return $result;
  766.     }
  767.  
  768.     // }}}
  769.     // {{{ createIndex()
  770.  
  771.     /**
  772.      * Get the stucture of a field into an array
  773.      *
  774.      * @param string    $table         name of the table on which the index is to be created
  775.      * @param string    $name         name of the index to be created
  776.      * @param array     $definition        associative array that defines properties of the index to be created.
  777.      *                                  Currently, only one property named FIELDS is supported. This property
  778.      *                                  is also an associative with the names of the index fields as array
  779.      *                                  indexes. Each entry of this array is set to another type of associative
  780.      *                                  array that specifies properties of the index that are specific to
  781.      *                                  each field.
  782.      *
  783.      *                                 Currently, only the sorting property is supported. It should be used
  784.      *                                  to define the sorting direction of the index. It may be set to either
  785.      *                                  ascending or descending.
  786.      *
  787.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  788.      *                                  drivers of those that do not support it ignore this property. Use the
  789.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  790.  
  791.      *                                  Example
  792.      *                                     array(
  793.      *                                         'fields' => array(
  794.      *                                             'user_name' => array(
  795.      *                                                 'sorting' => 'ascending'
  796.      *                                             ),
  797.      *                                             'last_login' => array()
  798.      *                                         )
  799.      *                                     )
  800.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  801.      * @access public
  802.      */
  803.     function createIndex($table$name$definition)
  804.     {
  805.         $db =$this->getDBInstance();
  806.         if (PEAR::isError($db)) {
  807.             return $db;
  808.         }
  809.         $query 'CREATE';
  810.  
  811.         $query_sort '';
  812.         foreach ($definition['fields'as $field{
  813.             if (!strcmp($query_sort''&& isset($field['sorting'])) {
  814.                 switch ($field['sorting']{
  815.                 case 'ascending':
  816.                     $query_sort ' ASC';
  817.                     break;
  818.                 case 'descending':
  819.                     $query_sort ' DESC';
  820.                     break;
  821.                 }
  822.             }
  823.         }
  824.         $table $db->quoteIdentifier($tabletrue);
  825.         $name  $db->quoteIdentifier($db->getIndexName($name)true);
  826.         $query .= $query_sort. " INDEX $name ON $table";
  827.         $fields = array();
  828.         foreach (array_keys($definition['fields']as $field{
  829.             $fields[$db->quoteIdentifier($fieldtrue);
  830.         }
  831.         $query .= ' ('.implode(', '$fields')';
  832.         $result $db->exec($query);
  833.         $this->_silentCommit();
  834.         return $result;
  835.     }
  836.  
  837.     // }}}
  838.     // {{{ listTableIndexes()
  839.  
  840.     /**
  841.      * list all indexes in a table
  842.      *
  843.      * @param string $table name of table that should be used in method
  844.      * @return mixed array of index names on success, a MDB2 error on failure
  845.      * @access public
  846.      */
  847.     function listTableIndexes($table)
  848.     {
  849.         $db =$this->getDBInstance();
  850.         if (PEAR::isError($db)) {
  851.             return $db;
  852.         }
  853.         $table $db->quote(strtoupper($table)'text');
  854.         $query = "SELECT RDB\$INDEX_NAME
  855.                     FROM RDB\$INDICES
  856.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  857.                      AND (RDB\$SYSTEM_FLAG=0 OR RDB\$SYSTEM_FLAG IS NULL)
  858.                      AND RDB\$UNIQUE_FLAG IS NULL
  859.                      AND RDB\$FOREIGN_KEY IS NULL";
  860.         $indexes $db->queryCol($query'text');
  861.         if (PEAR::isError($indexes)) {
  862.             return $indexes;
  863.         }
  864.  
  865.         $result = array();
  866.         foreach ($indexes as $index{
  867.             $index $this->_fixIndexName($index);
  868.             if (!empty($index)) {
  869.                 $result[$index= true;
  870.             }
  871.         }
  872.  
  873.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  874.             $result array_change_key_case($result$db->options['field_case']);
  875.         }
  876.         return array_keys($result);
  877.     }
  878.  
  879.     // }}}
  880.     // {{{ createConstraint()
  881.  
  882.     /**
  883.      * create a constraint on a table
  884.      *
  885.      * @param string    $table      name of the table on which the constraint is to be created
  886.      * @param string    $name       name of the constraint to be created
  887.      * @param array     $definition associative array that defines properties of the constraint to be created.
  888.      *                               Currently, only one property named FIELDS is supported. This property
  889.      *                               is also an associative with the names of the constraint fields as array
  890.      *                               constraints. Each entry of this array is set to another type of associative
  891.      *                               array that specifies properties of the constraint that are specific to
  892.      *                               each field.
  893.      *
  894.      *                               Example
  895.      *                                   array(
  896.      *                                       'fields' => array(
  897.      *                                           'user_name' => array(),
  898.      *                                           'last_login' => array(),
  899.      *                                       )
  900.      *                                   )
  901.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  902.      * @access public
  903.      */
  904.     function createConstraint($table$name$definition)
  905.     {
  906.         $db =$this->getDBInstance();
  907.         if (PEAR::isError($db)) {
  908.             return $db;
  909.         }
  910.         $table $db->quoteIdentifier($tabletrue);
  911.         if (!empty($name)) {
  912.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  913.         }
  914.         $query = "ALTER TABLE $table ADD";
  915.         if (!empty($definition['primary'])) {
  916.             if (!empty($name)) {
  917.                 $query.= ' CONSTRAINT '.$name;
  918.             }
  919.             $query.= ' PRIMARY KEY';
  920.         else {
  921.             $query.= ' CONSTRAINT '$name;
  922.             if (!empty($definition['unique'])) {
  923.                $query.= ' UNIQUE';
  924.             elseif (!empty($definition['foreign'])) {
  925.                 $query.= ' FOREIGN KEY';
  926.             }
  927.         }
  928.         $fields = array();
  929.         foreach (array_keys($definition['fields']as $field{
  930.             $fields[$db->quoteIdentifier($fieldtrue);
  931.         }
  932.         $query .= ' ('implode(', '$fields')';
  933.         if (!empty($definition['foreign'])) {
  934.             $query.= ' REFERENCES ' $db->quoteIdentifier($definition['references']['table']true);
  935.             $referenced_fields = array();
  936.             foreach (array_keys($definition['references']['fields']as $field{
  937.                 $referenced_fields[$db->quoteIdentifier($fieldtrue);
  938.             }
  939.             $query .= ' ('implode(', '$referenced_fields')';
  940.             $query .= $this->_getAdvancedFKOptions($definition);
  941.         }
  942.         $result $db->exec($query);
  943.         $this->_silentCommit();
  944.         return $result;
  945.     }
  946.  
  947.     // }}}
  948.     // {{{ listTableConstraints()
  949.  
  950.     /**
  951.      * list all constraints in a table
  952.      *
  953.      * @param string $table name of table that should be used in method
  954.      * @return mixed array of constraint names on success, a MDB2 error on failure
  955.      * @access public
  956.      */
  957.     function listTableConstraints($table)
  958.     {
  959.         $db =$this->getDBInstance();
  960.         if (PEAR::isError($db)) {
  961.             return $db;
  962.         }
  963.         $table $db->quote(strtoupper($table)'text');
  964.         $query = "SELECT RDB\$INDEX_NAME
  965.                     FROM RDB\$INDICES
  966.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  967.                      AND (
  968.                            RDB\$UNIQUE_FLAG IS NOT NULL
  969.                         OR RDB\$FOREIGN_KEY IS NOT NULL
  970.                      )";
  971.         $constraints $db->queryCol($query);
  972.         if (PEAR::isError($constraints)) {
  973.             return $constraints;
  974.         }
  975.  
  976.         $result = array();
  977.         foreach ($constraints as $constraint{
  978.             $constraint $this->_fixIndexName($constraint);
  979.             if (!empty($constraint)) {
  980.                 $result[$constraint= true;
  981.             }
  982.         }
  983.  
  984.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  985.             $result array_change_key_case($result$db->options['field_case']);
  986.         }
  987.         return array_keys($result);
  988.     }
  989.  
  990.     // }}}
  991.     // {{{ createSequence()
  992.  
  993.     /**
  994.      * create sequence
  995.      *
  996.      * @param string $seq_name name of the sequence to be created
  997.      * @param string $start start value of the sequence; default is 1
  998.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  999.      * @access public
  1000.      */
  1001.     function createSequence($seq_name$start = 1)
  1002.     {
  1003.         $db =$this->getDBInstance();
  1004.         if (PEAR::isError($db)) {
  1005.             return $db;
  1006.         }
  1007.  
  1008.         $sequence_name $db->getSequenceName($seq_name);
  1009.         if (PEAR::isError($result $db->exec('CREATE GENERATOR '.$sequence_name))) {
  1010.             return $result;
  1011.         }
  1012.         if (PEAR::isError($result $db->exec('SET GENERATOR '.$sequence_name.' TO '.($start-1)))) {
  1013.             if (PEAR::isError($err $db->dropSequence($seq_name))) {
  1014.                 return $db->raiseError($resultnullnull,
  1015.                     'Could not setup sequence start value and then it was not possible to drop it'__FUNCTION__);
  1016.             }
  1017.         }
  1018.         return $result;
  1019.     }
  1020.  
  1021.     // }}}
  1022.     // {{{ dropSequence()
  1023.  
  1024.     /**
  1025.      * drop existing sequence
  1026.      *
  1027.      * @param string $seq_name name of the sequence to be dropped
  1028.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1029.      * @access public
  1030.      */
  1031.     function dropSequence($seq_name)
  1032.     {
  1033.         $db =$this->getDBInstance();
  1034.         if (PEAR::isError($db)) {
  1035.             return $db;
  1036.         }
  1037.  
  1038.         $sequence_name $db->getSequenceName($seq_name);
  1039.         $sequence_name $db->quote($sequence_name'text');
  1040.         $query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)=$sequence_name";
  1041.         return $db->exec($query);
  1042.     }
  1043.  
  1044.     // }}}
  1045.     // {{{ listSequences()
  1046.  
  1047.     /**
  1048.      * list all sequences in the current database
  1049.      *
  1050.      * @return mixed array of sequence names on success, a MDB2 error on failure
  1051.      * @access public
  1052.      */
  1053.     function listSequences()
  1054.     {
  1055.         $db =$this->getDBInstance();
  1056.         if (PEAR::isError($db)) {
  1057.             return $db;
  1058.         }
  1059.  
  1060.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$SYSTEM_FLAG IS NULL';
  1061.         $table_names $db->queryCol($query);
  1062.         if (PEAR::isError($table_names)) {
  1063.             return $table_names;
  1064.         }
  1065.         $result = array();
  1066.         foreach ($table_names as $table_name{
  1067.             $result[$this->_fixSequenceName($table_name);
  1068.         }
  1069.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1070.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  1071.         }
  1072.         return $result;
  1073.     }
  1074. }
  1075. ?>

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