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 327310 2012-08-27 15:16:18Z danielc $
  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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::isError($db)) {
  139.             return $db;
  140.         }
  141.  
  142.         $table_quoted $db->quoteIdentifier($tabletrue);
  143.         if (null === $start{
  144.             $db->beginTransaction();
  145.             $query 'SELECT MAX(' $db->quoteIdentifier($nametrue') FROM ' $table_quoted;
  146.             $start $this->db->queryOne($query'integer');
  147.             if (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::isError($db)) {
  194.             return $db;
  195.         }
  196.         $result $db->manager->dropSequence($table);
  197.         if (MDB2::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 (MDB2::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 (MDB2::isError($db)) {
  259.             return $db;
  260.         }
  261.         $query $this->_getCreateTableQuery($name$fields$options);
  262.         if (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::isError($result)) {
  399.             return $result;
  400.         }
  401.         $result = parent::dropTable($name);
  402.         if (MDB2::isError($result)) {
  403.             return $result;
  404.         }
  405.         $this->_silentCommit();
  406.         return $result;
  407.     }
  408.  
  409.     // }}}
  410.     // {{{ vacuum()
  411.  
  412.     /**
  413.      * Optimize (vacuum) all the tables in the db (or only the specified table)
  414.      * and optionally run ANALYZE.
  415.      *
  416.      * @param string $table table name (all the tables if empty)
  417.      * @param array  $options an array with driver-specific options:
  418.      *                - timeout [int] (in seconds) [mssql-only]
  419.      *                - analyze [boolean] [pgsql and mysql]
  420.      *                - full [boolean] [pgsql-only]
  421.      *                - freeze [boolean] [pgsql-only]
  422.      *
  423.      * @return mixed MDB2_OK success, a MDB2 error on failure
  424.      * @access public
  425.      */
  426.     function vacuum($table = null$options = array())
  427.     {
  428.         // not needed in Interbase/Firebird
  429.         return MDB2_OK;
  430.     }
  431.  
  432.     // }}}
  433.     // {{{ alterTable()
  434.  
  435.     /**
  436.      * alter an existing table
  437.      *
  438.      * @param string  $name    name of the table that is intended to be changed.
  439.      * @param array   $changes associative array that contains the details of each type
  440.      *                          of change that is intended to be performed. The types of
  441.      *                          changes that are currently supported are defined as follows:
  442.      *
  443.      *                          name
  444.      *
  445.      *                              New name for the table.
  446.      *
  447.      *                          add
  448.      *
  449.      *                              Associative array with the names of fields to be added as
  450.      *                              indexes of the array. The value of each entry of the array
  451.      *                              should be set to another associative array with the properties
  452.      *                              of the fields to be added. The properties of the fields should
  453.      *                              be the same as defined by the MDB2 parser.
  454.      *
  455.      *
  456.      *                          remove
  457.      *
  458.      *                              Associative array with the names of fields to be removed as indexes
  459.      *                              of the array. Currently the values assigned to each entry are ignored.
  460.      *                              An empty array should be used for future compatibility.
  461.      *
  462.      *                          rename
  463.      *
  464.      *                              Associative array with the names of fields to be renamed as indexes
  465.      *                              of the array. The value of each entry of the array should be set to
  466.      *                              another associative array with the entry named name with the new
  467.      *                              field name and the entry named Declaration that is expected to contain
  468.      *                              the portion of the field declaration already in DBMS specific SQL code
  469.      *                              as it is used in the CREATE TABLE statement.
  470.      *
  471.      *                          change
  472.      *
  473.      *                              Associative array with the names of the fields to be changed as indexes
  474.      *                              of the array. Keep in mind that if it is intended to change either the
  475.      *                              name of a field and any other properties, the change array entries
  476.      *                              should have the new names of the fields as array indexes.
  477.      *
  478.      *                              The value of each entry of the array should be set to another associative
  479.      *                              array with the properties of the fields to that are meant to be changed as
  480.      *                              array entries. These entries should be assigned to the new values of the
  481.      *                              respective properties. The properties of the fields should be the same
  482.      *                              as defined by the MDB2 parser.
  483.      *
  484.      *                              Example
  485.      *                                 array(
  486.      *                                     'name' => 'userlist',
  487.      *                                     'add' => array(
  488.      *                                         'quota' => array(
  489.      *                                             'type' => 'integer',
  490.      *                                             'unsigned' => 1
  491.      *                                         )
  492.      *                                     ),
  493.      *                                     'remove' => array(
  494.      *                                         'file_limit' => array(),
  495.      *                                         'time_limit' => array()
  496.      *                                     ),
  497.      *                                     'change' => array(
  498.      *                                         'name' => array(
  499.      *                                             'length' => '20',
  500.      *                                             'definition' => array(
  501.      *                                                 'type' => 'text',
  502.      *                                                 'length' => 20,
  503.      *                                             ),
  504.      *                                         )
  505.      *                                     ),
  506.      *                                     'rename' => array(
  507.      *                                         'sex' => array(
  508.      *                                             'name' => 'gender',
  509.      *                                             'definition' => array(
  510.      *                                                 'type' => 'text',
  511.      *                                                 'length' => 1,
  512.      *                                                 'default' => 'M',
  513.      *                                             ),
  514.      *                                         )
  515.      *                                     )
  516.      *                                 )
  517.      *
  518.      * @param boolean $check   indicates whether the function should just check if the DBMS driver
  519.      *                          can perform the requested table alterations if the value is true or
  520.      *                          actually perform them otherwise.
  521.      *
  522.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  523.      * @access public
  524.      */
  525.     function alterTable($name$changes$check)
  526.     {
  527.         $db $this->getDBInstance();
  528.         if (MDB2::isError($db)) {
  529.             return $db;
  530.         }
  531.  
  532.         foreach ($changes as $change_name => $change{
  533.             switch ($change_name{
  534.             case 'add':
  535.             case 'remove':
  536.             case 'rename':
  537.                 break;
  538.             case 'change':
  539.                 foreach ($changes['change'as $field{
  540.                     if (MDB2::isError($err $this->checkSupportedChanges($field))) {
  541.                         return $err;
  542.                     }
  543.                 }
  544.                 break;
  545.             default:
  546.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  547.                     'change type "' $change_name '" not yet supported'__FUNCTION__);
  548.             }
  549.         }
  550.         if ($check{
  551.             return MDB2_OK;
  552.         }
  553.         $query '';
  554.         if (!empty($changes['add']&& is_array($changes['add'])) {
  555.             foreach ($changes['add'as $field_name => $field{
  556.                 if ($query{
  557.                     $query.= ', ';
  558.                 }
  559.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  560.             }
  561.         }
  562.  
  563.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  564.             foreach ($changes['remove'as $field_name => $field{
  565.                 if ($query{
  566.                     $query.= ', ';
  567.                 }
  568.                 $field_name $db->quoteIdentifier($field_nametrue);
  569.                 $query.= 'DROP ' $field_name;
  570.             }
  571.         }
  572.  
  573.         if (!empty($changes['rename']&& is_array($changes['rename'])) {
  574.             foreach ($changes['rename'as $field_name => $field{
  575.                 if ($query{
  576.                     $query.= ', ';
  577.                 }
  578.                 $field_name $db->quoteIdentifier($field_nametrue);
  579.                 $query.= 'ALTER ' $field_name ' TO ' $db->quoteIdentifier($field['name']true);
  580.             }
  581.         }
  582.  
  583.         if (!empty($changes['change']&& is_array($changes['change'])) {
  584.             // missing support to change DEFAULT and NULLability
  585.             foreach ($changes['change'as $field_name => $field{
  586.                 if (MDB2::isError($err $this->checkSupportedChanges($field))) {
  587.                     return $err;
  588.                 }
  589.                 if ($query{
  590.                     $query.= ', ';
  591.                 }
  592.                 $db->loadModule('Datatype'nulltrue);
  593.                 $field_name $db->quoteIdentifier($field_nametrue);
  594.                 $query.= 'ALTER ' $field_name.' TYPE ' $db->datatype->getTypeDeclaration($field['definition']);
  595.             }
  596.         }
  597.  
  598.         if (!strlen($query)) {
  599.             return MDB2_OK;
  600.         }
  601.  
  602.         $name $db->quoteIdentifier($nametrue);
  603.         $result $db->exec("ALTER TABLE $name $query");
  604.         if (MDB2::isError($result)) {
  605.             return $result;
  606.         }
  607.         $this->_silentCommit();
  608.         return MDB2_OK;
  609.     }
  610.  
  611.     // }}}
  612.     // {{{ listTables()
  613.  
  614.     /**
  615.      * list all tables in the current database
  616.      *
  617.      * @return mixed array of table names on success, a MDB2 error on failure
  618.      * @access public
  619.      */
  620.     function listTables()
  621.     {
  622.         $db $this->getDBInstance();
  623.         if (MDB2::isError($db)) {
  624.             return $db;
  625.         }
  626.         $query 'SELECT RDB$RELATION_NAME
  627.                     FROM RDB$RELATIONS
  628.                    WHERE (RDB$SYSTEM_FLAG=0 OR RDB$SYSTEM_FLAG IS NULL)
  629.                      AND RDB$VIEW_BLR IS NULL
  630.                 ORDER BY RDB$RELATION_NAME';
  631.         $result $db->queryCol($query);
  632.         if (MDB2::isError($result)) {
  633.             return $result;
  634.         }
  635.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  636.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  637.         }
  638.         return $result;
  639.     }
  640.  
  641.     // }}}
  642.     // {{{ listTableFields()
  643.  
  644.     /**
  645.      * list all fields in a table in the current database
  646.      *
  647.      * @param string $table name of table that should be used in method
  648.      *
  649.      * @return mixed array of field names on success, a MDB2 error on failure
  650.      * @access public
  651.      */
  652.     function listTableFields($table)
  653.     {
  654.         $db $this->getDBInstance();
  655.         if (MDB2::isError($db)) {
  656.             return $db;
  657.         }
  658.         $table $db->quote(strtoupper($table)'text');
  659.         $query = "SELECT RDB\$FIELD_NAME
  660.                     FROM RDB\$RELATION_FIELDS
  661.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  662.                      AND (RDB\$SYSTEM_FLAG=0 OR RDB\$SYSTEM_FLAG IS NULL)";
  663.         $result $db->queryCol($query);
  664.         if (MDB2::isError($result)) {
  665.             return $result;
  666.         }
  667.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  668.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  669.         }
  670.         return $result;
  671.     }
  672.  
  673.     // }}}
  674.     // {{{ listUsers()
  675.  
  676.     /**
  677.      * list all users
  678.      *
  679.      * @return mixed array of user names on success, a MDB2 error on failure
  680.      * @access public
  681.      */
  682.     function listUsers()
  683.     {
  684.         $db $this->getDBInstance();
  685.         if (MDB2::isError($db)) {
  686.             return $db;
  687.         }
  688.         return $db->queryCol('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES');
  689.     }
  690.  
  691.     // }}}
  692.     // {{{ listViews()
  693.  
  694.     /**
  695.      * list all views in the current database
  696.      *
  697.      * @return mixed array of view names on success, a MDB2 error on failure
  698.      * @access public
  699.      */
  700.     function listViews()
  701.     {
  702.         $db $this->getDBInstance();
  703.         if (MDB2::isError($db)) {
  704.             return $db;
  705.         }
  706.  
  707.         $result $db->queryCol('SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS');
  708.         if (MDB2::isError($result)) {
  709.             return $result;
  710.         }
  711.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  712.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  713.         }
  714.         return $result;
  715.     }
  716.  
  717.     // }}}
  718.     // {{{ listTableViews()
  719.  
  720.     /**
  721.      * list the views in the database that reference a given table
  722.      *
  723.      * @param string table for which all referenced views should be found
  724.      *
  725.      * @return mixed array of view names on success, a MDB2 error on failure
  726.      * @access public
  727.      */
  728.     function listTableViews($table)
  729.     {
  730.         $db $this->getDBInstance();
  731.         if (MDB2::isError($db)) {
  732.             return $db;
  733.         }
  734.  
  735.         $query 'SELECT DISTINCT RDB$VIEW_NAME FROM RDB$VIEW_RELATIONS';
  736.         $table $db->quote(strtoupper($table)'text');
  737.         $query .= " WHERE UPPER(RDB\$RELATION_NAME)=$table";
  738.         $result $db->queryCol($query);
  739.         if (MDB2::isError($result)) {
  740.             return $result;
  741.         }
  742.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  743.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  744.         }
  745.         return $result;
  746.     }
  747.  
  748.     // }}}
  749.     // {{{ listFunctions()
  750.  
  751.     /**
  752.      * list all functions (and stored procedures) in the current database
  753.      *
  754.      * @return mixed array of function names on success, a MDB2 error on failure
  755.      * @access public
  756.      */
  757.     function listFunctions()
  758.     {
  759.         $db $this->getDBInstance();
  760.         if (MDB2::isError($db)) {
  761.             return $db;
  762.         }
  763.  
  764.         $query 'SELECT RDB$FUNCTION_NAME FROM RDB$FUNCTIONS WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)
  765.                   UNION
  766.                   SELECT RDB$PROCEDURE_NAME FROM RDB$PROCEDURES';
  767.         $result $db->queryCol($query);
  768.         if (MDB2::isError($result)) {
  769.             return $result;
  770.         }
  771.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  772.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  773.         }
  774.         return $result;
  775.     }
  776.  
  777.     // }}}
  778.     // {{{ listTableTriggers()
  779.  
  780.     /**
  781.      * list all triggers in the database that reference a given table
  782.      *
  783.      * @param string table for which all referenced triggers should be found
  784.      *
  785.      * @return mixed array of trigger names on success, a MDB2 error on failure
  786.      * @access public
  787.      */
  788.     function listTableTriggers($table = null)
  789.     {
  790.         $db $this->getDBInstance();
  791.         if (MDB2::isError($db)) {
  792.             return $db;
  793.         }
  794.  
  795.         $query 'SELECT RDB$TRIGGER_NAME
  796.                     FROM RDB$TRIGGERS
  797.                    WHERE (RDB$SYSTEM_FLAG IS NULL
  798.                       OR RDB$SYSTEM_FLAG = 0)';
  799.         if (null !== $table{
  800.             $table $db->quote(strtoupper($table)'text');
  801.             $query .= " AND UPPER(RDB\$RELATION_NAME)=$table";
  802.         }
  803.         $result $db->queryCol($query);
  804.         if (MDB2::isError($result)) {
  805.             return $result;
  806.         }
  807.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  808.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  809.         }
  810.         return $result;
  811.     }
  812.  
  813.     // }}}
  814.     // {{{ createIndex()
  815.  
  816.     /**
  817.      * Get the stucture of a field into an array
  818.      *
  819.      * @param string $table      name of the table on which the index is to be created
  820.      * @param string $name       name of the index to be created
  821.      * @param array  $definition associative array that defines properties of the index to be created.
  822.      *                            Currently, only one property named FIELDS is supported. This property
  823.      *                            is also an associative with the names of the index fields as array
  824.      *                            indexes. Each entry of this array is set to another type of associative
  825.      *                            array that specifies properties of the index that are specific to
  826.      *                            each field.
  827.      *
  828.      *                            Currently, only the sorting property is supported. It should be used
  829.      *                            to define the sorting direction of the index. It may be set to either
  830.      *                            ascending or descending.
  831.      *
  832.      *                            Not all DBMS support index sorting direction configuration. The DBMS
  833.      *                            drivers of those that do not support it ignore this property. Use the
  834.      *                            function support() to determine whether the DBMS driver can manage indexes.
  835.  
  836.      *                            Example
  837.      *                            array(
  838.      *                                'fields' => array(
  839.      *                                    'user_name' => array(
  840.      *                                        'sorting' => 'ascending'
  841.      *                                     ),
  842.      *                                     'last_login' => array()
  843.      *                                 )
  844.      *                             )
  845.      *
  846.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  847.      * @access public
  848.      */
  849.     function createIndex($table$name$definition)
  850.     {
  851.         $db $this->getDBInstance();
  852.         if (MDB2::isError($db)) {
  853.             return $db;
  854.         }
  855.         $query 'CREATE';
  856.  
  857.         $query_sort '';
  858.         foreach ($definition['fields'as $field{
  859.             if (!strcmp($query_sort''&& isset($field['sorting'])) {
  860.                 switch ($field['sorting']{
  861.                 case 'ascending':
  862.                     $query_sort ' ASC';
  863.                     break;
  864.                 case 'descending':
  865.                     $query_sort ' DESC';
  866.                     break;
  867.                 }
  868.             }
  869.         }
  870.         $table $db->quoteIdentifier($tabletrue);
  871.         $name  $db->quoteIdentifier($db->getIndexName($name)true);
  872.         $query .= $query_sort. " INDEX $name ON $table";
  873.         $fields = array();
  874.         foreach (array_keys($definition['fields']as $field{
  875.             $fields[$db->quoteIdentifier($fieldtrue);
  876.         }
  877.         $query .= ' ('.implode(', '$fields')';
  878.         $result $db->exec($query);
  879.         if (MDB2::isError($result)) {
  880.             return $result;
  881.         }
  882.         $this->_silentCommit();
  883.         return MDB2_OK;
  884.     }
  885.  
  886.     // }}}
  887.     // {{{ listTableIndexes()
  888.  
  889.     /**
  890.      * list all indexes in a table
  891.      *
  892.      * @param string $table name of table that should be used in method
  893.      *
  894.      * @return mixed array of index names on success, a MDB2 error on failure
  895.      * @access public
  896.      */
  897.     function listTableIndexes($table)
  898.     {
  899.         $db $this->getDBInstance();
  900.         if (MDB2::isError($db)) {
  901.             return $db;
  902.         }
  903.         $table $db->quote(strtoupper($table)'text');
  904.         $query = "SELECT RDB\$INDEX_NAME
  905.                     FROM RDB\$INDICES
  906.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  907.                      AND (RDB\$SYSTEM_FLAG IS NULL OR RDB\$SYSTEM_FLAG = 0)
  908.                      AND (RDB\$UNIQUE_FLAG IS NULL OR RDB\$UNIQUE_FLAG = 0)
  909.                      AND RDB\$FOREIGN_KEY IS NULL";
  910.         $indexes $db->queryCol($query'text');
  911.         if (MDB2::isError($indexes)) {
  912.             return $indexes;
  913.         }
  914.  
  915.         $result = array();
  916.         foreach ($indexes as $index{
  917.             $index $this->_fixIndexName($index);
  918.             if (!empty($index)) {
  919.                 $result[$index= true;
  920.             }
  921.         }
  922.  
  923.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  924.             $result array_change_key_case($result$db->options['field_case']);
  925.         }
  926.         return array_keys($result);
  927.     }
  928.  
  929.     // }}}
  930.     // {{{ createConstraint()
  931.  
  932.     /**
  933.      * create a constraint on a table
  934.      *
  935.      * @param string $table      name of the table on which the constraint is to be created
  936.      * @param string $name       name of the constraint to be created
  937.      * @param array  $definition associative array that defines properties of the constraint to be created.
  938.      *                            Currently, only one property named FIELDS is supported. This property
  939.      *                            is also an associative with the names of the constraint fields as array
  940.      *                            constraints. Each entry of this array is set to another type of associative
  941.      *                            array that specifies properties of the constraint that are specific to
  942.      *                            each field.
  943.      *
  944.      *                            Example
  945.      *                                array(
  946.      *                                    'fields' => array(
  947.      *                                        'user_name' => array(),
  948.      *                                        'last_login' => array(),
  949.      *                                    )
  950.      *                                )
  951.      *
  952.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  953.      * @access public
  954.      */
  955.     function createConstraint($table$name$definition)
  956.     {
  957.         $db $this->getDBInstance();
  958.         if (MDB2::isError($db)) {
  959.             return $db;
  960.         }
  961.         $table $db->quoteIdentifier($tabletrue);
  962.         if (!empty($name)) {
  963.             $name $db->quoteIdentifier($db->getIndexName($name)true);
  964.         }
  965.         $query = "ALTER TABLE $table ADD";
  966.         if (!empty($definition['primary'])) {
  967.             if (!empty($name)) {
  968.                 $query.= ' CONSTRAINT '.$name;
  969.             }
  970.             $query.= ' PRIMARY KEY';
  971.         else {
  972.             $query.= ' CONSTRAINT '$name;
  973.             if (!empty($definition['unique'])) {
  974.                $query.= ' UNIQUE';
  975.             elseif (!empty($definition['foreign'])) {
  976.                 $query.= ' FOREIGN KEY';
  977.             }
  978.         }
  979.         $fields = array();
  980.         foreach (array_keys($definition['fields']as $field{
  981.             $fields[$db->quoteIdentifier($fieldtrue);
  982.         }
  983.         $query .= ' ('implode(', '$fields')';
  984.         if (!empty($definition['foreign'])) {
  985.             $query.= ' REFERENCES ' $db->quoteIdentifier($definition['references']['table']true);
  986.             $referenced_fields = array();
  987.             foreach (array_keys($definition['references']['fields']as $field{
  988.                 $referenced_fields[$db->quoteIdentifier($fieldtrue);
  989.             }
  990.             $query .= ' ('implode(', '$referenced_fields')';
  991.             $query .= $this->_getAdvancedFKOptions($definition);
  992.         }
  993.         $result $db->exec($query);
  994.         if (MDB2::isError($result)) {
  995.             return $result;
  996.         }
  997.         $this->_silentCommit();
  998.         return MDB2_OK;
  999.     }
  1000.  
  1001.     // }}}
  1002.     // {{{ listTableConstraints()
  1003.  
  1004.     /**
  1005.      * list all constraints in a table
  1006.      *
  1007.      * @param string $table name of table that should be used in method
  1008.      *
  1009.      * @return mixed array of constraint names on success, a MDB2 error on failure
  1010.      * @access public
  1011.      */
  1012.     function listTableConstraints($table)
  1013.     {
  1014.         $db $this->getDBInstance();
  1015.         if (MDB2::isError($db)) {
  1016.             return $db;
  1017.         }
  1018.         $table $db->quote(strtoupper($table)'text');
  1019.         $query = "SELECT RDB\$INDEX_NAME
  1020.                     FROM RDB\$INDICES
  1021.                    WHERE UPPER(RDB\$RELATION_NAME)=$table
  1022.                      AND (
  1023.                            (RDB\$UNIQUE_FLAG IS NOT NULL AND RDB\$UNIQUE_FLAG <> 0)
  1024.                         OR RDB\$FOREIGN_KEY IS NOT NULL
  1025.                      )";
  1026.         $constraints $db->queryCol($query);
  1027.         if (MDB2::isError($constraints)) {
  1028.             return $constraints;
  1029.         }
  1030.  
  1031.         $result = array();
  1032.         foreach ($constraints as $constraint{
  1033.             $constraint $this->_fixIndexName($constraint);
  1034.             if (!empty($constraint)) {
  1035.                 $result[$constraint= true;
  1036.             }
  1037.         }
  1038.  
  1039.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1040.             $result array_change_key_case($result$db->options['field_case']);
  1041.         }
  1042.         return array_keys($result);
  1043.     }
  1044.  
  1045.     // }}}
  1046.     // {{{ createSequence()
  1047.  
  1048.     /**
  1049.      * create sequence
  1050.      *
  1051.      * @param string $seq_name name of the sequence to be created
  1052.      * @param string $start start value of the sequence; default is 1
  1053.      *
  1054.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1055.      * @access public
  1056.      */
  1057.     function createSequence($seq_name$start = 1)
  1058.     {
  1059.         $db $this->getDBInstance();
  1060.         if (MDB2::isError($db)) {
  1061.             return $db;
  1062.         }
  1063.  
  1064.         $sequence_name $db->getSequenceName($seq_name);
  1065.         if (MDB2::isError($result $db->exec('CREATE GENERATOR '.$sequence_name))) {
  1066.             return $result;
  1067.         }
  1068.         if (MDB2::isError($result $db->exec('SET GENERATOR '.$sequence_name.' TO '.($start-1)))) {
  1069.             if (MDB2::isError($err $db->dropSequence($seq_name))) {
  1070.                 return $db->raiseError($resultnullnull,
  1071.                     'Could not setup sequence start value and then it was not possible to drop it'__FUNCTION__);
  1072.             }
  1073.         }
  1074.         return MDB2_OK;
  1075.     }
  1076.  
  1077.     // }}}
  1078.     // {{{ dropSequence()
  1079.  
  1080.     /**
  1081.      * drop existing sequence
  1082.      *
  1083.      * @param string $seq_name name of the sequence to be dropped
  1084.      *
  1085.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  1086.      * @access public
  1087.      */
  1088.     function dropSequence($seq_name)
  1089.     {
  1090.         $db $this->getDBInstance();
  1091.         if (MDB2::isError($db)) {
  1092.             return $db;
  1093.         }
  1094.  
  1095.         $sequence_name $db->getSequenceName($seq_name);
  1096.         $sequence_name $db->quote($sequence_name'text');
  1097.         $query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)=$sequence_name";
  1098.         $result $db->exec($query);
  1099.         if (MDB2::isError($result)) {
  1100.             return $result;
  1101.         }
  1102.         return MDB2_OK;
  1103.     }
  1104.  
  1105.     // }}}
  1106.     // {{{ listSequences()
  1107.  
  1108.     /**
  1109.      * list all sequences in the current database
  1110.      *
  1111.      * @return mixed array of sequence names on success, a MDB2 error on failure
  1112.      * @access public
  1113.      */
  1114.     function listSequences()
  1115.     {
  1116.         $db $this->getDBInstance();
  1117.         if (MDB2::isError($db)) {
  1118.             return $db;
  1119.         }
  1120.  
  1121.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE (RDB$SYSTEM_FLAG IS NULL OR RDB$SYSTEM_FLAG = 0)';
  1122.         $table_names $db->queryCol($query);
  1123.         if (MDB2::isError($table_names)) {
  1124.             return $table_names;
  1125.         }
  1126.         $result = array();
  1127.         foreach ($table_names as $table_name{
  1128.             $result[$this->_fixSequenceName($table_name);
  1129.         }
  1130.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  1131.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  1132.         }
  1133.         return $result;
  1134.     }
  1135. }
  1136. ?>

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