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

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