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

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