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

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