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-2006 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.95 2006/08/21 16:39:37 lsmith Exp $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 FireBird/InterBase driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author Lorenzo Alberton <l.alberton@quipo.it>
  55.  */
  56. class MDB2_Driver_Manager_ibase extends MDB2_Driver_Manager_Common
  57. {
  58.     // {{{ 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.         $this->_silentCommit();
  169.         return $result;
  170.     }
  171.  
  172.     // }}}
  173.     // {{{ _dropAutoincrement()
  174.  
  175.     /**
  176.      * drop an existing autoincrement sequence + trigger
  177.      *
  178.      * @param string $table name of the table
  179.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  180.      * @access private
  181.      */
  182.     function _dropAutoincrement($table)
  183.     {
  184.         $db =$this->getDBInstance();
  185.         if (PEAR::isError($db)) {
  186.             return $db;
  187.         }
  188.         $result $db->manager->dropSequence($table);
  189.         if (PEAR::isError($result)) {
  190.             return $db->raiseError(nullnullnull,
  191.                 'sequence for autoincrement PK could not be dropped'__FUNCTION__);
  192.         }
  193.         //remove autoincrement trigger associated with the table
  194.         $table $db->quote(strtoupper($table)'text');
  195.         $trigger_name $db->quote(strtoupper($table'_AUTOINCREMENT_PK''text');
  196.         $result $db->exec("DELETE FROM RDB\$TRIGGERS WHERE UPPER(RDB\$RELATION_NAME)=$table AND UPPER(RDB\$TRIGGER_NAME)=$trigger_name");
  197.         if (PEAR::isError($result)) {
  198.             return $db->raiseError(nullnullnull,
  199.                 'trigger for autoincrement PK could not be dropped'__FUNCTION__);
  200.         }
  201.         return MDB2_OK;
  202.     }
  203.  
  204.     // }}}
  205.     // {{{ createTable()
  206.  
  207.     /**
  208.      * create a new table
  209.      *
  210.      * @param string $name     Name of the database that should be created
  211.      * @param array $fields Associative array that contains the definition of each field of the new table
  212.      *                         The indexes of the array entries are the names of the fields of the table an
  213.      *                         the array entry values are associative arrays like those that are meant to be
  214.      *                          passed with the field definitions to get[Type]Declaration() functions.
  215.      *
  216.      *                         Example
  217.      *                         array(
  218.      *
  219.      *                             'id' => array(
  220.      *                                 'type' => 'integer',
  221.      *                                 'unsigned' => 1,
  222.      *                                 'notnull' => 1,
  223.      *                                 'default' => 0,
  224.      *                             ),
  225.      *                             'name' => array(
  226.      *                                 'type' => 'text',
  227.      *                                 'length' => 12,
  228.      *                             ),
  229.      *                             'description' => array(
  230.      *                                 'type' => 'text',
  231.      *                                 'length' => 12,
  232.      *                             )
  233.      *                         );
  234.      * @param array $options  An associative array of table options:
  235.      *
  236.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  237.      * @access public
  238.      */
  239.     function createTable($name$fields$options = array())
  240.     {
  241.         $result = parent::createTable($name$fields$options);
  242.         if (PEAR::isError($result)) {
  243.             return $result;
  244.         }
  245.         $this->_silentCommit();
  246.         foreach ($fields as $field_name => $field{
  247.             if (!empty($field['autoincrement'])) {
  248.                 //create PK constraint
  249.                 $pk_definition = array(
  250.                     'fields' => array($field_name => array()),
  251.                     'primary' => true,
  252.                 );
  253.                 //$pk_name = $name.'_PK';
  254.                 $pk_name = null;
  255.                 $result $this->createConstraint($name$pk_name$pk_definition);
  256.                 if (PEAR::isError($result)) {
  257.                     return $result;
  258.                 }
  259.                 //create autoincrement sequence + trigger
  260.                 return $this->_makeAutoincrement($field_name$name1);
  261.             }
  262.         }
  263.     }
  264.  
  265.     // }}}
  266.     // {{{ checkSupportedChanges()
  267.  
  268.     /**
  269.      * Check if planned changes are supported
  270.      *
  271.      * @param string $name name of the database that should be dropped
  272.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  273.      * @access public
  274.      */
  275.     function checkSupportedChanges(&$changes)
  276.     {
  277.         $db =$this->getDBInstance();
  278.         if (PEAR::isError($db)) {
  279.             return $db;
  280.         }
  281.  
  282.         foreach ($changes as $change_name => $change{
  283.             switch ($change_name{
  284.             case 'notnull':
  285.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  286.                     'it is not supported changes to field not null constraint'__FUNCTION__);
  287.             case 'default':
  288.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  289.                     'it is not supported changes to field default value'__FUNCTION__);
  290.             case 'length':
  291.                 /*
  292.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  293.                     'it is not supported changes to field default length', __FUNCTION__);
  294.                 */
  295.             case 'unsigned':
  296.             case 'type':
  297.             case 'declaration':
  298.             case 'definition':
  299.                 break;
  300.             default:
  301.                 return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull,
  302.                     'it is not supported change of type' $change_name__FUNCTION__);
  303.             }
  304.         }
  305.         return MDB2_OK;
  306.     }
  307.  
  308.     // }}}
  309.     // {{{ dropTable()
  310.  
  311.     /**
  312.      * drop an existing table
  313.      *
  314.      * @param string $name name of the table that should be dropped
  315.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  316.      * @access public
  317.      */
  318.     function dropTable($name)
  319.     {
  320.         $result $this->_dropAutoincrement($name);
  321.         if (PEAR::isError($result)) {
  322.             return $result;
  323.         }
  324.         $result = parent::dropTable($name);
  325.         $this->_silentCommit();
  326.         return $result;
  327.     }
  328.  
  329.     // }}}
  330.     // {{{ alterTable()
  331.  
  332.     /**
  333.      * alter an existing table
  334.      *
  335.      * @param string $name         name of the table that is intended to be changed.
  336.      * @param array $changes     associative array that contains the details of each type
  337.      *                              of change that is intended to be performed. The types of
  338.      *                              changes that are currently supported are defined as follows:
  339.      *
  340.      *                              name
  341.      *
  342.      *                                 New name for the table.
  343.      *
  344.      *                             add
  345.      *
  346.      *                                 Associative array with the names of fields to be added as
  347.      *                                  indexes of the array. The value of each entry of the array
  348.      *                                  should be set to another associative array with the properties
  349.      *                                  of the fields to be added. The properties of the fields should
  350.      *                                  be the same as defined by the Metabase parser.
  351.      *
  352.      *
  353.      *                             remove
  354.      *
  355.      *                                 Associative array with the names of fields to be removed as indexes
  356.      *                                  of the array. Currently the values assigned to each entry are ignored.
  357.      *                                  An empty array should be used for future compatibility.
  358.      *
  359.      *                             rename
  360.      *
  361.      *                                 Associative array with the names of fields to be renamed as indexes
  362.      *                                  of the array. The value of each entry of the array should be set to
  363.      *                                  another associative array with the entry named name with the new
  364.      *                                  field name and the entry named Declaration that is expected to contain
  365.      *                                  the portion of the field declaration already in DBMS specific SQL code
  366.      *                                  as it is used in the CREATE TABLE statement.
  367.      *
  368.      *                             change
  369.      *
  370.      *                                 Associative array with the names of the fields to be changed as indexes
  371.      *                                  of the array. Keep in mind that if it is intended to change either the
  372.      *                                  name of a field and any other properties, the change array entries
  373.      *                                  should have the new names of the fields as array indexes.
  374.      *
  375.      *                                 The value of each entry of the array should be set to another associative
  376.      *                                  array with the properties of the fields to that are meant to be changed as
  377.      *                                  array entries. These entries should be assigned to the new values of the
  378.      *                                  respective properties. The properties of the fields should be the same
  379.      *                                  as defined by the Metabase parser.
  380.      *
  381.      *                             Example
  382.      *                                 array(
  383.      *                                     'name' => 'userlist',
  384.      *                                     'add' => array(
  385.      *                                         'quota' => array(
  386.      *                                             'type' => 'integer',
  387.      *                                             'unsigned' => 1
  388.      *                                         )
  389.      *                                     ),
  390.      *                                     'remove' => array(
  391.      *                                         'file_limit' => array(),
  392.      *                                         'time_limit' => array()
  393.      *                                     ),
  394.      *                                     'change' => array(
  395.      *                                         'name' => array(
  396.      *                                             'length' => '20',
  397.      *                                             'definition' => array(
  398.      *                                                 'type' => 'text',
  399.      *                                                 'length' => 20,
  400.      *                                             ),
  401.      *                                         )
  402.      *                                     ),
  403.      *                                     'rename' => array(
  404.      *                                         'sex' => array(
  405.      *                                             'name' => 'gender',
  406.      *                                             'definition' => array(
  407.      *                                                 'type' => 'text',
  408.      *                                                 'length' => 1,
  409.      *                                                 'default' => 'M',
  410.      *                                             ),
  411.      *                                         )
  412.      *                                     )
  413.      *                                 )
  414.      *
  415.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  416.      *                              can perform the requested table alterations if the value is true or
  417.      *                              actually perform them otherwise.
  418.      * @access public
  419.      *
  420.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  421.      */
  422.     function alterTable($name$changes$check)
  423.     {
  424.         $db =$this->getDBInstance();
  425.         if (PEAR::isError($db)) {
  426.             return $db;
  427.         }
  428.  
  429.         foreach ($changes as $change_name => $change{
  430.             switch ($change_name{
  431.             case 'add':
  432.             case 'remove':
  433.             case 'rename':
  434.                 break;
  435.             case 'change':
  436.                 foreach ($changes['change'as $field{
  437.                     if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  438.                         return $err;
  439.                     }
  440.                 }
  441.                 break;
  442.             default:
  443.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  444.                     'change type ' $change_name ' not yet supported'__FUNCTION__);
  445.             }
  446.         }
  447.         if ($check{
  448.             return MDB2_OK;
  449.         }
  450.         $query '';
  451.         if (!empty($changes['add']&& is_array($changes['add'])) {
  452.             foreach ($changes['add'as $field_name => $field{
  453.                 if ($query{
  454.                     $query.= ', ';
  455.                 }
  456.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field$name);
  457.             }
  458.         }
  459.  
  460.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  461.             foreach ($changes['remove'as $field_name => $field{
  462.                 if ($query{
  463.                     $query.= ', ';
  464.                 }
  465.                 $field_name $db->quoteIdentifier($field_nametrue);
  466.                 $query.= 'DROP ' $field_name;
  467.             }
  468.         }
  469.  
  470.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  471.             foreach ($changes['rename'as $field_name => $field{
  472.                 if ($query{
  473.                     $query.= ', ';
  474.                 }
  475.                 $field_name $db->quoteIdentifier($field_nametrue);
  476.                 $query.= 'ALTER ' $field_name ' TO ' $db->quoteIdentifier($field['name']true);
  477.             }
  478.         }
  479.  
  480.         if (!empty($changes['change']&& is_array($changes['change'])) {
  481.             // missing support to change DEFAULT and NULLability
  482.             foreach ($changes['change'as $field_name => $field{
  483.                 if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  484.                     return $err;
  485.                 }
  486.                 if ($query{
  487.                     $query.= ', ';
  488.                 }
  489.                 $db->loadModule('Datatype'nulltrue);
  490.                 $field_name $db->quoteIdentifier($field_nametrue);
  491.                 $query.= 'ALTER ' $field_name.' TYPE ' $db->datatype->getTypeDeclaration($field['definition']);
  492.             }
  493.         }
  494.  
  495.         if (!strlen($query)) {
  496.             return MDB2_OK;
  497.         }
  498.  
  499.         $name $db->quoteIdentifier($nametrue);
  500.         $result $db->exec("ALTER TABLE $name $query");
  501.         $this->_silentCommit();
  502.         return $result;
  503.     }
  504.  
  505.     // }}}
  506.     // {{{ listTables()
  507.  
  508.     /**
  509.      * list all tables in the current database
  510.      *
  511.      * @return mixed data array on success, a MDB2 error on failure
  512.      * @access public
  513.      */
  514.     function listTables()
  515.     {
  516.         $db =$this->getDBInstance();
  517.         if (PEAR::isError($db)) {
  518.             return $db;
  519.         }
  520.         $query 'SELECT RDB$RELATION_NAME FROM RDB$RELATIONS WHERE RDB$SYSTEM_FLAG=0 AND RDB$VIEW_BLR IS NULL';
  521.         $result $db->queryCol($query);
  522.         if (PEAR::isError($result)) {
  523.             return $result;
  524.         }
  525.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  526.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  527.         }
  528.         return $result;
  529.     }
  530.  
  531.     // }}}
  532.     // {{{ listTableFields()
  533.  
  534.     /**
  535.      * list all fields in a tables in the current database
  536.      *
  537.      * @param string $table name of table that should be used in method
  538.      * @return mixed data array on success, a MDB2 error on failure
  539.      * @access public
  540.      */
  541.     function listTableFields($table)
  542.     {
  543.         $db =$this->getDBInstance();
  544.         if (PEAR::isError($db)) {
  545.             return $db;
  546.         }
  547.         $table $db->quote(strtoupper($table)'text');
  548.         $query = "SELECT RDB\$FIELD_NAME FROM RDB\$RELATION_FIELDS WHERE UPPER(RDB\$RELATION_NAME)=$table";
  549.         $result $db->queryCol($query);
  550.         if (PEAR::isError($result)) {
  551.             return $result;
  552.         }
  553.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  554.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  555.         }
  556.         return $result;
  557.     }
  558.  
  559.     // }}}
  560.     // {{{ listUsers()
  561.  
  562.     /**
  563.      * list all users
  564.      *
  565.      * @return mixed data array on success, a MDB2 error on failure
  566.      * @access public
  567.      */
  568.     function listUsers()
  569.     {
  570.         $db =$this->getDBInstance();
  571.         if (PEAR::isError($db)) {
  572.             return $db;
  573.         }
  574.         return $db->queryCol('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES');
  575.     }
  576.  
  577.     // }}}
  578.     // {{{ listViews()
  579.  
  580.     /**
  581.      * list the views in the database
  582.      *
  583.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  584.      * @access public
  585.      */
  586.     function listViews()
  587.     {
  588.         $db =$this->getDBInstance();
  589.         if (PEAR::isError($db)) {
  590.             return $db;
  591.         }
  592.  
  593.         $result $db->queryCol('SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS');
  594.         if (PEAR::isError($result)) {
  595.             return $result;
  596.         }
  597.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  598.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  599.         }
  600.         return $result;
  601.     }
  602.  
  603.     // }}}
  604.     // {{{ listTableViews()
  605.  
  606.     /**
  607.      * list the views in the database that reference a given table
  608.      *
  609.      * @param string table for which all references views should be found
  610.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  611.      * @access public
  612.      */
  613.     function listTableViews($table)
  614.     {
  615.         $db =$this->getDBInstance();
  616.         if (PEAR::isError($db)) {
  617.             return $db;
  618.         }
  619.  
  620.         $query 'SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS';
  621.         $table $db->quote(strtoupper($table)'text');
  622.         $query .= "WHERE UPPER(RDB\$RELATION_NAME)=$table";
  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.     // {{{ listFunctions()
  635.  
  636.     /**
  637.      * list all functions in the current database
  638.      *
  639.      * @return mixed data array on success, a MDB2 error on failure
  640.      * @access public
  641.      */
  642.     function listFunctions()
  643.     {
  644.         $db =$this->getDBInstance();
  645.         if (PEAR::isError($db)) {
  646.             return $db;
  647.         }
  648.  
  649.         $query 'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE RDB$SYSTEM_FLAG IS NULL';
  650.         $result $db->queryCol($query);
  651.         if (PEAR::isError($result)) {
  652.             return $result;
  653.         }
  654.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  655.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  656.         }
  657.         return $result;
  658.     }
  659.  
  660.     // }}}
  661.     // {{{ listTableTriggers()
  662.  
  663.     /**
  664.      * This function will be called to get all triggers of the
  665.      * current database ($db->getDatabase())
  666.      *
  667.      * @access public
  668.      * @param  string $table      The name of the table from the
  669.      *                             previous database to query against.
  670.      * @return mixed Array on success or MDB2 error on failure
  671.      */
  672.     function listTableTriggers($table = null)
  673.     {
  674.         $db =$this->getDBInstance();
  675.         if (PEAR::isError($db)) {
  676.             return $db;
  677.         }
  678.  
  679.         $query 'SELECT RDB$TRIGGER_NAME
  680.                     FROM RDB$TRIGGERS
  681.                    WHERE RDB$SYSTEM_FLAG IS NULL
  682.                       OR RDB$SYSTEM_FLAG = 0';
  683.         if (!is_null($table)) {
  684.             $table $db->quote(strtoupper($table)'text');
  685.             $query .= "WHERE UPPER(RDB\$RELATION_NAME)=$table";
  686.         }
  687.         $result $db->queryCol();
  688.         if (PEAR::isError($result)) {
  689.             return $result;
  690.         }
  691.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  692.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  693.         }
  694.         return $result;
  695.     }
  696.  
  697.     // }}}
  698.     // {{{ createIndex()
  699.  
  700.     /**
  701.      * Get the stucture of a field into an array
  702.      *
  703.      * @param string    $table         name of the table on which the index is to be created
  704.      * @param string    $name         name of the index to be created
  705.      * @param array     $definition        associative array that defines properties of the index to be created.
  706.      *                                  Currently, only one property named FIELDS is supported. This property
  707.      *                                  is also an associative with the names of the index fields as array
  708.      *                                  indexes. Each entry of this array is set to another type of associative
  709.      *                                  array that specifies properties of the index that are specific to
  710.      *                                  each field.
  711.      *
  712.      *                                 Currently, only the sorting property is supported. It should be used
  713.      *                                  to define the sorting direction of the index. It may be set to either
  714.      *                                  ascending or descending.
  715.      *
  716.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  717.      *                                  drivers of those that do not support it ignore this property. Use the
  718.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  719.  
  720.      *                                  Example
  721.      *                                     array(
  722.      *                                         'fields' => array(
  723.      *                                             'user_name' => array(
  724.      *                                                 'sorting' => 'ascending'
  725.      *                                             ),
  726.      *                                             'last_login' => array()
  727.      *                                         )
  728.      *                                     )
  729.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  730.      * @access public
  731.      */
  732.     function createIndex($table$name$definition)
  733.     {
  734.         $db =$this->getDBInstance();
  735.         if (PEAR::isError($db)) {
  736.             return $db;
  737.         }
  738.         $query 'CREATE';
  739.  
  740.         $query_sort '';
  741.         foreach ($definition['fields'as $field{
  742.             if (!strcmp($query_sort''&& isset($field['sorting'])) {
  743.                 switch ($field['sorting']{
  744.                 case 'ascending':
  745.                     $query_sort ' ASC';
  746.                     break;
  747.                 case 'descending':
  748.                     $query_sort ' DESC';
  749.                     break;
  750.                 }
  751.             }
  752.         }
  753.         $table $db->quoteIdentifier($tabletrue);
  754.         $name  $db->quoteIdentifier($db->getIndexName($name)true);
  755.         $query .= $query_sort. " INDEX $name ON $table";
  756.         $fields = array();
  757.         foreach (array_keys($definition['fields']as $field{
  758.             $fields[$db->quoteIdentifier($fieldtrue);
  759.         }
  760.         $query .= ' ('.implode(', '$fields')';
  761.         $result $db->exec($query);
  762.         $this->_silentCommit();
  763.         return $result;
  764.     }
  765.  
  766.     // }}}
  767.     // {{{ listTableIndexes()
  768.  
  769.     /**
  770.      * list all indexes in a table
  771.      *
  772.      * @param string $table name of table that should be used in method
  773.      * @return mixed data array on success, a MDB2 error on failure
  774.      * @access public
  775.      */
  776.     function listTableIndexes($table)
  777.     {
  778.         $db =$this->getDBInstance();
  779.         if (PEAR::isError($db)) {
  780.             return $db;
  781.         }
  782.         $table $db->quote(strtoupper($table)'text');
  783.         $query = "SELECT RDB\$INDEX_NAME
  784.                     FROM RDB\$INDICES
  785.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  786.                      AND RDB\$UNIQUE_FLAG IS NULL
  787.                      AND RDB\$FOREIGN_KEY IS NULL";
  788.         $indexes $db->queryCol($query'text');
  789.         if (PEAR::isError($indexes)) {
  790.             return $indexes;
  791.         }
  792.  
  793.         $result = array();
  794.         foreach ($indexes as $index{
  795.             $index $this->_fixIndexName($index);
  796.             if (!empty($index)) {
  797.                 $result[$index= true;
  798.             }
  799.         }
  800.  
  801.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  802.             $result array_change_key_case($result$db->options['field_case']);
  803.         }
  804.         return array_keys($result);
  805.     }
  806.  
  807.     // }}}
  808.     // {{{ createConstraint()
  809.  
  810.     /**
  811.      * create a constraint on a table
  812.      *
  813.      * @param string    $table      name of the table on which the constraint is to be created
  814.      * @param string    $name       name of the constraint to be created
  815.      * @param array     $definition associative array that defines properties of the constraint to be created.
  816.      *                               Currently, only one property named FIELDS is supported. This property
  817.      *                               is also an associative with the names of the constraint fields as array
  818.      *                               constraints. Each entry of this array is set to another type of associative
  819.      *                               array that specifies properties of the constraint that are specific to
  820.      *                               each field.
  821.      *
  822.      *                               Example
  823.      *                                   array(
  824.      *                                       'fields' => array(
  825.      *                                           'user_name' => array(),
  826.      *                                           'last_login' => array(),
  827.      *                                       )
  828.      *                                   )
  829.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  830.      * @access public
  831.      */
  832.     function createConstraint($table$name$definition)
  833.     {
  834.         $db =$this->getDBInstance();
  835.         if (PEAR::isError($db)) {
  836.             return $db;
  837.         }
  838.         $table $db->quoteIdentifier($tabletrue);
  839.         if (!empty($name)) {
  840.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  841.         }
  842.         $query = "ALTER TABLE $table ADD";
  843.         if (!empty($definition['primary'])) {
  844.             if (!empty($name)) {
  845.                 $query.= ' CONSTRAINT '.$name;
  846.             }
  847.             $query.= ' PRIMARY KEY';
  848.         else {
  849.             $query.= ' CONSTRAINT '$name;
  850.             if (!empty($definition['unique'])) {
  851.                $query.= ' UNIQUE';
  852.             }
  853.         }
  854.         $fields = array();
  855.         foreach (array_keys($definition['fields']as $field{
  856.             $fields[$db->quoteIdentifier($fieldtrue);
  857.         }
  858.         $query .= ' ('implode(', '$fields')';
  859.         $result $db->exec($query);
  860.         $this->_silentCommit();
  861.         return $result;
  862.     }
  863.  
  864.     // }}}
  865.     // {{{ listTableConstraints()
  866.  
  867.     /**
  868.      * list all constraints in a table
  869.      *
  870.      * @param string    $table      name of table that should be used in method
  871.      * @return mixed data array on success, a MDB2 error on failure
  872.      * @access public
  873.      */
  874.     function listTableConstraints($table)
  875.     {
  876.         $db =$this->getDBInstance();
  877.         if (PEAR::isError($db)) {
  878.             return $db;
  879.         }
  880.         $table $db->quote(strtoupper($table)'text');
  881.         $query = "SELECT RDB\$INDEX_NAME
  882.                     FROM RDB\$INDICES
  883.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  884.                      AND (
  885.                            RDB\$UNIQUE_FLAG IS NOT NULL
  886.                         OR RDB\$FOREIGN_KEY IS NOT NULL
  887.                      )";
  888.         $constraints $db->queryCol($query);
  889.         if (PEAR::isError($constraints)) {
  890.             return $constraints;
  891.         }
  892.  
  893.         $result = array();
  894.         foreach ($constraints as $constraint{
  895.             $constraint $this->_fixIndexName($constraint);
  896.             if (!empty($constraint)) {
  897.                 $result[$constraint= true;
  898.             }
  899.         }
  900.  
  901.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  902.             $result array_change_key_case($result$db->options['field_case']);
  903.         }
  904.         return array_keys($result);
  905.     }
  906.  
  907.     // }}}
  908.     // {{{ createSequence()
  909.  
  910.     /**
  911.      * create sequence
  912.      *
  913.      * @param string $seq_name name of the sequence to be created
  914.      * @param string $start start value of the sequence; default is 1
  915.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  916.      * @access public
  917.      */
  918.     function createSequence($seq_name$start = 1)
  919.     {
  920.         $db =$this->getDBInstance();
  921.         if (PEAR::isError($db)) {
  922.             return $db;
  923.         }
  924.  
  925.         $sequence_name $db->getSequenceName($seq_name);
  926.         if (PEAR::isError($result $db->exec('CREATE GENERATOR '.$sequence_name))) {
  927.             return $result;
  928.         }
  929.         if (PEAR::isError($result $db->exec('SET GENERATOR '.$sequence_name.' TO '.($start-1)))) {
  930.             if (PEAR::isError($err $db->dropSequence($seq_name))) {
  931.                 return $db->raiseError($resultnullnull,
  932.                     'Could not setup sequence start value and then it was not possible to drop it'__FUNCTION__);
  933.             }
  934.         }
  935.         return $result;
  936.     }
  937.  
  938.     // }}}
  939.     // {{{ dropSequence()
  940.  
  941.     /**
  942.      * drop existing sequence
  943.      *
  944.      * @param string $seq_name name of the sequence to be dropped
  945.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  946.      * @access public
  947.      */
  948.     function dropSequence($seq_name)
  949.     {
  950.         $db =$this->getDBInstance();
  951.         if (PEAR::isError($db)) {
  952.             return $db;
  953.         }
  954.  
  955.         $sequence_name $db->getSequenceName($seq_name);
  956.         $sequence_name $db->quote($sequence_name'text');
  957.         $query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)=$sequence_name";
  958.         return $db->exec($query);
  959.     }
  960.  
  961.     // }}}
  962.     // {{{ listSequences()
  963.  
  964.     /**
  965.      * list all sequences in the current database
  966.      *
  967.      * @return mixed data array on success, a MDB2 error on failure
  968.      * @access public
  969.      */
  970.     function listSequences()
  971.     {
  972.         $db =$this->getDBInstance();
  973.         if (PEAR::isError($db)) {
  974.             return $db;
  975.         }
  976.  
  977.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$SYSTEM_FLAG IS NULL';
  978.         $table_names $db->queryCol($query);
  979.         if (PEAR::isError($table_names)) {
  980.             return $table_names;
  981.         }
  982.         $result = array();
  983.         foreach ($table_names as $table_name{
  984.             $result[$this->_fixSequenceName($table_name);
  985.         }
  986.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  987.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  988.         }
  989.         return $result;
  990.     }
  991. }
  992. ?>

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