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

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