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

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