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-2005 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.67 2005/12/22 17:50:18 quipo Exp $
  46.  
  47. require_once 'MDB2/Driver/Manager/Common.php';
  48.  
  49. /**
  50.  * MDB2 FireBird/InterBase driver for the management modules
  51.  *
  52.  * @package MDB2
  53.  * @category Database
  54.  * @author Lorenzo Alberton <l.alberton@quipo.it>
  55.  */
  56. class MDB2_Driver_Manager_ibase extends MDB2_Driver_Manager_Common
  57. {
  58.     // {{{ createDatabase()
  59.  
  60.     /**
  61.      * create a new database
  62.      *
  63.      * @param string $name  name of the database that should be created
  64.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  65.      * @access public
  66.      */
  67.     function createDatabase($name)
  68.     {
  69.         $db =$this->getDBInstance();
  70.         if (PEAR::isError($db)) {
  71.             return $db;
  72.         }
  73.  
  74.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull'Create database',
  75.                 'createDatabase: PHP Interbase API does not support direct queries. You have to '.
  76.                 'create the db manually by using isql command or a similar program');
  77.     }
  78.  
  79.     // }}}
  80.     // {{{ dropDatabase()
  81.  
  82.     /**
  83.      * drop an existing database
  84.      *
  85.      * @param string $name  name of the database that should be dropped
  86.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  87.      * @access public
  88.      */
  89.     function dropDatabase($name)
  90.     {
  91.         $db =$this->getDBInstance();
  92.         if (PEAR::isError($db)) {
  93.             return $db;
  94.         }
  95.  
  96.         return $db->raiseError(MDB2_ERROR_UNSUPPORTEDnullnull'Drop database',
  97.                 'dropDatabase: PHP Interbase API does not support direct queries. You have '.
  98.                 'to drop the db manually by using isql command or a similar program');
  99.     }
  100.  
  101.     // }}}
  102.     // {{{ _makeAutoincrement()
  103.  
  104.     /**
  105.      * add an autoincrement sequence + trigger
  106.      *
  107.      * @param string $name  name of the PK field
  108.      * @param string $table name of the table
  109.      * @param string $start start value for the sequence
  110.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  111.      * @access private
  112.      */
  113.     function _makeAutoincrement($name$table$start = null)
  114.     {
  115.         $db =$this->getDBInstance();
  116.         if (PEAR::isError($db)) {
  117.             return $db;
  118.         }
  119.  
  120.         if (is_null($start)) {
  121.             $db->beginTransaction();
  122.             $query 'SELECT MAX(' $db->quoteIdentifier($nametrue') FROM ' $db->quoteIdentifier($tabletrue);
  123.             $start $this->db->queryOne($query'integer');
  124.             if (PEAR::isError($start)) {
  125.                 return $start;
  126.             }
  127.             ++$start;
  128.             $result $db->manager->createSequence($table$start);
  129.             $db->commit();
  130.         else {
  131.             $result $db->manager->createSequence($table$start);
  132.         }
  133.         if (PEAR::isError($result)) {
  134.             return $db->raiseError(MDB2_ERRORnullnull,
  135.                 '_makeAutoincrement: sequence for autoincrement PK could not be created');
  136.         }
  137.  
  138.         $sequence_name $db->getSequenceName($table);
  139.         $trigger_name  $db->quoteIdentifier($table '_AUTOINCREMENT_PK'true);
  140.         $table $db->quoteIdentifier($tabletrue);
  141.         $name  $db->quoteIdentifier($nametrue);
  142.         $trigger_sql 'CREATE TRIGGER ' $trigger_name ' FOR ' $table '
  143.                         ACTIVE BEFORE INSERT POSITION 0
  144.                         AS
  145.                         BEGIN
  146.                         IF (NEW.' $name ' IS NULL OR NEW.' $name ' = 0) THEN
  147.                             NEW.' $name ' = GEN_ID('.$sequence_name.', 1);
  148.                         END';
  149.         return $db->exec($trigger_sql);
  150.     }
  151.  
  152.     // }}}
  153.     // {{{ _dropAutoincrement()
  154.  
  155.     /**
  156.      * drop an existing autoincrement sequence + trigger
  157.      *
  158.      * @param string $table name of the table
  159.      * @return mixed        MDB2_OK on success, a MDB2 error on failure
  160.      * @access private
  161.      */
  162.     function _dropAutoincrement($table)
  163.     {
  164.         $db =$this->getDBInstance();
  165.         if (PEAR::isError($db)) {
  166.             return $db;
  167.         }
  168.         $result $db->manager->dropSequence($table);
  169.         if (PEAR::isError($result)) {
  170.             return $db->raiseError(MDB2_ERRORnullnull,
  171.                 '_dropAutoincrement: sequence for autoincrement PK could not be dropped');
  172.         }
  173.         //remove autoincrement trigger associated with the table
  174.         $table strtoupper($table);
  175.         $trigger_name  $table '_AUTOINCREMENT_PK';
  176.         $result $db->exec("DELETE FROM RDB\$TRIGGERS WHERE UPPER(RDB\$RELATION_NAME)='$table' AND UPPER(RDB\$TRIGGER_NAME)='$trigger_name'");
  177.         if (PEAR::isError($result)) {
  178.             return $db->raiseError(MDB2_ERRORnullnull,
  179.                 '_dropAutoincrement: trigger for autoincrement PK could not be dropped');
  180.         }
  181.         return MDB2_OK;
  182.     }
  183.  
  184.     // }}}
  185.     // {{{ createTable()
  186.  
  187.     /**
  188.      * create a new table
  189.      *
  190.      * @param string $name     Name of the database that should be created
  191.      * @param array $fields Associative array that contains the definition of each field of the new table
  192.      *                         The indexes of the array entries are the names of the fields of the table an
  193.      *                         the array entry values are associative arrays like those that are meant to be
  194.      *                          passed with the field definitions to get[Type]Declaration() functions.
  195.      *
  196.      *                         Example
  197.      *                         array(
  198.      *
  199.      *                             'id' => array(
  200.      *                                 'type' => 'integer',
  201.      *                                 'unsigned' => 1,
  202.      *                                 'notnull' => 1,
  203.      *                                 'default' => 0,
  204.      *                             ),
  205.      *                             'name' => array(
  206.      *                                 'type' => 'text',
  207.      *                                 'length' => 12,
  208.      *                             ),
  209.      *                             'description' => array(
  210.      *                                 'type' => 'text',
  211.      *                                 'length' => 12,
  212.      *                             )
  213.      *                         );
  214.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  215.      * @access public
  216.      */
  217.     function createTable($name$fields)
  218.     {
  219.         $result = parent::createTable($name$fields);
  220.         if (PEAR::isError($result)) {
  221.             return $result;
  222.         }
  223.         foreach($fields as $field_name => $field{
  224.             if (array_key_exists('autoincrement'$field&& $field['autoincrement']{
  225.                 //create PK constraint
  226.                 $pk_definition = array(
  227.                     'fields' => array($field_name => array()),
  228.                     'primary' => true,
  229.                 );
  230.                 //$pk_name = $name.'_PK';
  231.                 $pk_name = null;
  232.                 $result $this->createConstraint($name$pk_name$pk_definition);
  233.                 if (PEAR::isError($result)) {
  234.                     return $result;
  235.                 }
  236.                 //create autoincrement sequence + trigger
  237.                 return $this->_makeAutoincrement($field_name$name1);
  238.             }
  239.         }
  240.     }
  241.  
  242.     // }}}
  243.     // {{{ checkSupportedChanges()
  244.  
  245.     /**
  246.      * check if planned changes are supported
  247.      *
  248.      * @param string $name name of the database that should be dropped
  249.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  250.      * @access public
  251.      */
  252.     function checkSupportedChanges(&$changes)
  253.     {
  254.         $db =$this->getDBInstance();
  255.         if (PEAR::isError($db)) {
  256.             return $db;
  257.         }
  258.  
  259.         foreach ($changes as $change_name => $change{
  260.             switch ($change_name{
  261.             case 'notnull':
  262.                 return $db->raiseError(MDB2_ERRORnullnull,
  263.                     'checkSupportedChanges: it is not supported changes to field not null constraint');
  264.             case 'default':
  265.                 return $db->raiseError(MDB2_ERRORnullnull,
  266.                     'checkSupportedChanges: it is not supported changes to field default value');
  267.             case 'length':
  268.                 /*
  269.                 return $db->raiseError(MDB2_ERROR, null, null,
  270.                     'checkSupportedChanges: it is not supported changes to field default length');
  271.                 */
  272.             case 'unsigned':
  273.             case 'type':
  274.             case 'declaration':
  275.             case 'definition':
  276.                 break;
  277.             default:
  278.                 return $db->raiseError(MDB2_ERRORnullnull,
  279.                     'checkSupportedChanges: it is not supported change of type' $change_name);
  280.             }
  281.         }
  282.         return MDB2_OK;
  283.     }
  284.  
  285.     // }}}
  286.     // {{{ dropTable()
  287.  
  288.     /**
  289.      * drop an existing table
  290.      *
  291.      * @param string $name name of the table that should be dropped
  292.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  293.      * @access public
  294.      */
  295.     function dropTable($name)
  296.     {
  297.         $result $this->_dropAutoincrement($name);
  298.         if (PEAR::isError($result)) {
  299.             return $result;
  300.         }
  301.         return parent::dropTable($name);
  302.     }
  303.  
  304.     // }}}
  305.     // {{{ alterTable()
  306.  
  307.     /**
  308.      * alter an existing table
  309.      *
  310.      * @param string $name         name of the table that is intended to be changed.
  311.      * @param array $changes     associative array that contains the details of each type
  312.      *                              of change that is intended to be performed. The types of
  313.      *                              changes that are currently supported are defined as follows:
  314.      *
  315.      *                              name
  316.      *
  317.      *                                 New name for the table.
  318.      *
  319.      *                             add
  320.      *
  321.      *                                 Associative array with the names of fields to be added as
  322.      *                                  indexes of the array. The value of each entry of the array
  323.      *                                  should be set to another associative array with the properties
  324.      *                                  of the fields to be added. The properties of the fields should
  325.      *                                  be the same as defined by the Metabase parser.
  326.      *
  327.      *
  328.      *                             remove
  329.      *
  330.      *                                 Associative array with the names of fields to be removed as indexes
  331.      *                                  of the array. Currently the values assigned to each entry are ignored.
  332.      *                                  An empty array should be used for future compatibility.
  333.      *
  334.      *                             rename
  335.      *
  336.      *                                 Associative array with the names of fields to be renamed as indexes
  337.      *                                  of the array. The value of each entry of the array should be set to
  338.      *                                  another associative array with the entry named name with the new
  339.      *                                  field name and the entry named Declaration that is expected to contain
  340.      *                                  the portion of the field declaration already in DBMS specific SQL code
  341.      *                                  as it is used in the CREATE TABLE statement.
  342.      *
  343.      *                             change
  344.      *
  345.      *                                 Associative array with the names of the fields to be changed as indexes
  346.      *                                  of the array. Keep in mind that if it is intended to change either the
  347.      *                                  name of a field and any other properties, the change array entries
  348.      *                                  should have the new names of the fields as array indexes.
  349.      *
  350.      *                                 The value of each entry of the array should be set to another associative
  351.      *                                  array with the properties of the fields to that are meant to be changed as
  352.      *                                  array entries. These entries should be assigned to the new values of the
  353.      *                                  respective properties. The properties of the fields should be the same
  354.      *                                  as defined by the Metabase parser.
  355.      *
  356.      *                             Example
  357.      *                                 array(
  358.      *                                     'name' => 'userlist',
  359.      *                                     'add' => array(
  360.      *                                         'quota' => array(
  361.      *                                             'type' => 'integer',
  362.      *                                             'unsigned' => 1
  363.      *                                         )
  364.      *                                     ),
  365.      *                                     'remove' => array(
  366.      *                                         'file_limit' => array(),
  367.      *                                         'time_limit' => array()
  368.      *                                     ),
  369.      *                                     'change' => array(
  370.      *                                         'name' => array(
  371.      *                                             'length' => '20',
  372.      *                                             'definition' => array(
  373.      *                                                 'type' => 'text',
  374.      *                                                 'length' => 20,
  375.      *                                             ),
  376.      *                                         )
  377.      *                                     ),
  378.      *                                     'rename' => array(
  379.      *                                         'sex' => array(
  380.      *                                             'name' => 'gender',
  381.      *                                             'definition' => array(
  382.      *                                                 'type' => 'text',
  383.      *                                                 'length' => 1,
  384.      *                                                 'default' => 'M',
  385.      *                                             ),
  386.      *                                         )
  387.      *                                     )
  388.      *                                 )
  389.      *
  390.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  391.      *                              can perform the requested table alterations if the value is true or
  392.      *                              actually perform them otherwise.
  393.      * @access public
  394.      *
  395.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  396.      */
  397.     function alterTable($name$changes$check)
  398.     {
  399.         $db =$this->getDBInstance();
  400.         if (PEAR::isError($db)) {
  401.             return $db;
  402.         }
  403.  
  404.         foreach ($changes as $change_name => $change{
  405.             switch ($change_name{
  406.             case 'add':
  407.             case 'remove':
  408.             case 'rename':
  409.                 break;
  410.             case 'change':
  411.                 foreach ($changes['change'as $field{
  412.                     if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  413.                         return $err;
  414.                     }
  415.                 }
  416.                 break;
  417.             default:
  418.                 return $db->raiseError(MDB2_ERRORnullnull,
  419.                     'alterTable: change type ' $change_name ' not yet supported');
  420.             }
  421.         }
  422.         if ($check{
  423.             return MDB2_OK;
  424.         }
  425.         $query '';
  426.         if (array_key_exists('add'$changes)) {
  427.             foreach ($changes['add'as $field_name => $field{
  428.                 if ($query{
  429.                     $query.= ', ';
  430.                 }
  431.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field$name);
  432.             }
  433.         }
  434.  
  435.         if (array_key_exists('remove'$changes)) {
  436.             foreach ($changes['remove'as $field_name => $field{
  437.                 if ($query{
  438.                     $query.= ', ';
  439.                 }
  440.                 $field_name $db->quoteIdentifier($field_nametrue);
  441.                 $query.= 'DROP ' $field_name;
  442.             }
  443.         }
  444.  
  445.         if (array_key_exists('rename'$changes)) {
  446.             foreach ($changes['rename'as $field_name => $field{
  447.                 if ($query{
  448.                     $query.= ', ';
  449.                 }
  450.                 $field_name $db->quoteIdentifier($field_nametrue);
  451.                 $query.= 'ALTER ' $field_name ' TO ' $db->quoteIdentifier($field['name']true);
  452.             }
  453.         }
  454.  
  455.         if (array_key_exists('change'$changes)) {
  456.             // missing support to change DEFAULT and NULLability
  457.             foreach ($changes['change'as $field_name => $field{
  458.                 if (PEAR::isError($err $this->checkSupportedChanges($field))) {
  459.                     return $err;
  460.                 }
  461.                 if ($query{
  462.                     $query.= ', ';
  463.                 }
  464.                 $db->loadModule('Datatype');
  465.                 $field_name $db->quoteIdentifier($field_nametrue);
  466.                 $query.= 'ALTER ' $field_name.' TYPE ' $db->datatype->getTypeDeclaration($field['definition']);
  467.             }
  468.         }
  469.  
  470.         if (!strlen($query)) {
  471.             return MDB2_OK;
  472.         }
  473.  
  474.         $name $db->quoteIdentifier($nametrue);
  475.         return $db->exec("ALTER TABLE $name $query");
  476.     }
  477.  
  478.     // }}}
  479.     // {{{ listTables()
  480.  
  481.     /**
  482.      * list all tables in the current database
  483.      *
  484.      * @return mixed data array on success, a MDB2 error on failure
  485.      * @access public
  486.      */
  487.     function listTables()
  488.     {
  489.         $db =$this->getDBInstance();
  490.         if (PEAR::isError($db)) {
  491.             return $db;
  492.         }
  493.         $query 'SELECT DISTINCT R.RDB$RELATION_NAME FROM RDB$RELATION_FIELDS R WHERE R.RDB$SYSTEM_FLAG=0';
  494.         $result $db->queryCol($query);
  495.         if (PEAR::isError($result)) {
  496.             return $result;
  497.         }
  498.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  499.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  500.         }
  501.         return $result;
  502.     }
  503.  
  504.     // }}}
  505.     // {{{ listTableFields()
  506.  
  507.     /**
  508.      * list all fields in a tables in the current database
  509.      *
  510.      * @param string $table name of table that should be used in method
  511.      * @return mixed data array on success, a MDB2 error on failure
  512.      * @access public
  513.      */
  514.     function listTableFields($table)
  515.     {
  516.         $db =$this->getDBInstance();
  517.         if (PEAR::isError($db)) {
  518.             return $db;
  519.         }
  520.         $table strtoupper($table);
  521.         $query = "SELECT RDB\$FIELD_NAME FROM RDB\$RELATION_FIELDS WHERE UPPER(RDB\$RELATION_NAME)='$table'";
  522.         $result $db->queryCol($query);
  523.         if (PEAR::isError($result)) {
  524.             return $result;
  525.         }
  526.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  527.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  528.         }
  529.         return $result;
  530.     }
  531.  
  532.     // }}}
  533.     // {{{ listUsers()
  534.  
  535.     /**
  536.      * list all users
  537.      *
  538.      * @return mixed data array on success, a MDB2 error on failure
  539.      * @access public
  540.      */
  541.     function listUsers()
  542.     {
  543.         $db =$this->getDBInstance();
  544.         if (PEAR::isError($db)) {
  545.             return $db;
  546.         }
  547.         return $db->queryCol('SELECT DISTINCT RDB$USER FROM RDB$USER_PRIVILEGES');
  548.     }
  549.  
  550.     // }}}
  551.     // {{{ listViews()
  552.  
  553.     /**
  554.      * list the views in the database
  555.      *
  556.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  557.      * @access public
  558.      */
  559.     function listViews()
  560.     {
  561.         $db =$this->getDBInstance();
  562.         if (PEAR::isError($db)) {
  563.             return $db;
  564.         }
  565.  
  566.         $result $db->queryCol('SELECT RDB$VIEW_NAME');
  567.         if (PEAR::isError($result)) {
  568.             return $result;
  569.         }
  570.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  571.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  572.         }
  573.         return $result;
  574.     }
  575.  
  576.     // }}}
  577.     // {{{ createIndex()
  578.  
  579.     /**
  580.      * get the stucture of a field into an array
  581.      *
  582.      * @param string    $table         name of the table on which the index is to be created
  583.      * @param string    $name         name of the index to be created
  584.      * @param array     $definition        associative array that defines properties of the index to be created.
  585.      *                                  Currently, only one property named FIELDS is supported. This property
  586.      *                                  is also an associative with the names of the index fields as array
  587.      *                                  indexes. Each entry of this array is set to another type of associative
  588.      *                                  array that specifies properties of the index that are specific to
  589.      *                                  each field.
  590.      *
  591.      *                                 Currently, only the sorting property is supported. It should be used
  592.      *                                  to define the sorting direction of the index. It may be set to either
  593.      *                                  ascending or descending.
  594.      *
  595.      *                                 Not all DBMS support index sorting direction configuration. The DBMS
  596.      *                                  drivers of those that do not support it ignore this property. Use the
  597.      *                                  function support() to determine whether the DBMS driver can manage indexes.
  598.  
  599.      *                                  Example
  600.      *                                     array(
  601.      *                                         'fields' => array(
  602.      *                                             'user_name' => array(
  603.      *                                                 'sorting' => 'ascending'
  604.      *                                             ),
  605.      *                                             'last_login' => array()
  606.      *                                         )
  607.      *                                     )
  608.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  609.      * @access public
  610.      */
  611.     function createIndex($table$name$definition)
  612.     {
  613.         $db =$this->getDBInstance();
  614.         if (PEAR::isError($db)) {
  615.             return $db;
  616.         }
  617.         $query 'CREATE';
  618.  
  619.         $query_sort '';
  620.         foreach ($definition['fields'as $field{
  621.             if (!strcmp($query_sort''&& isset($field['sorting'])) {
  622.                 switch ($field['sorting']{
  623.                 case 'ascending':
  624.                     $query_sort ' ASC';
  625.                     break;
  626.                 case 'descending':
  627.                     $query_sort ' DESC';
  628.                     break;
  629.                 }
  630.             }
  631.         }
  632.         $table $db->quoteIdentifier($tabletrue);
  633.         $name  $db->quoteIdentifier($db->getIndexName($name)true);
  634.         $query .= $query_sort. " INDEX $name ON $table";
  635.         $fields = array();
  636.         foreach (array_keys($definition['fields']as $field{
  637.             $fields[$db->quoteIdentifier($fieldtrue);
  638.         }
  639.         $query .= ' ('.implode(', '$fields')';
  640.         return $db->exec($query);
  641.     }
  642.  
  643.     // }}}
  644.     // {{{ listTableIndexes()
  645.  
  646.     /**
  647.      * list all indexes in a table
  648.      *
  649.      * @param string $table name of table that should be used in method
  650.      * @return mixed data array on success, a MDB2 error on failure
  651.      * @access public
  652.      */
  653.     function listTableIndexes($table)
  654.     {
  655.         $db =$this->getDBInstance();
  656.         if (PEAR::isError($db)) {
  657.             return $db;
  658.         }
  659.         $table strtoupper($table);
  660.         $query = "SELECT RDB\$INDEX_NAME FROM RDB\$INDICES WHERE RDB\$RELATION_NAME='$table'";
  661.         $indexes $db->queryCol($query'text');
  662.         if (PEAR::isError($indexes)) {
  663.             return $indexes;
  664.         }
  665.  
  666.         $result = array();
  667.         foreach ($indexes as $index{
  668.             if ($index $this->_isIndexName($index)) {
  669.                 $result[$index= true;
  670.             }
  671.         }
  672.  
  673.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  674.             $result array_change_key_case($result$db->options['field_case']);
  675.         }
  676.         return array_keys($result);
  677.     }
  678.  
  679.     // }}}
  680.     // {{{ createConstraint()
  681.  
  682.     /**
  683.      * create a constraint on a table
  684.      *
  685.      * @param string    $table      name of the table on which the constraint is to be created
  686.      * @param string    $name       name of the constraint to be created
  687.      * @param array     $definition associative array that defines properties of the constraint to be created.
  688.      *                               Currently, only one property named FIELDS is supported. This property
  689.      *                               is also an associative with the names of the constraint fields as array
  690.      *                               constraints. Each entry of this array is set to another type of associative
  691.      *                               array that specifies properties of the constraint that are specific to
  692.      *                               each field.
  693.      *
  694.      *                               Example
  695.      *                                   array(
  696.      *                                       'fields' => array(
  697.      *                                           'user_name' => array(),
  698.      *                                           'last_login' => array(),
  699.      *                                       )
  700.      *                                   )
  701.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  702.      * @access public
  703.      */
  704.     function createConstraint($table$name$definition)
  705.     {
  706.         $db =$this->getDBInstance();
  707.         if (PEAR::isError($db)) {
  708.             return $db;
  709.         }
  710.         $table $db->quoteIdentifier($tabletrue);
  711.         $name  $db->quoteIdentifier($db->getIndexName($name)true);
  712.         $query = "ALTER TABLE $table ADD";
  713.         if (array_key_exists('primary'$definition&& $definition['primary']{
  714.             if (!empty($name)) {
  715.                 $query.= ' CONSTRAINT '.$name;
  716.             }
  717.             $query.= ' PRIMARY KEY';
  718.         else {
  719.             $query.= ' CONSTRAINT '$name;
  720.             if (array_key_exists('unique'$definition&& $definition['unique']{
  721.                $query.= ' UNIQUE';
  722.             }
  723.         }
  724.         $fields = array();
  725.         foreach (array_keys($definition['fields']as $field{
  726.             $fields[$db->quoteIdentifier($fieldtrue);
  727.         }
  728.         $query .= ' ('implode(', '$fields')';
  729.         return $db->exec($query);
  730.     }
  731.  
  732.     // }}}
  733.     // {{{ listTableConstraints()
  734.  
  735.     /**
  736.      * list all sonstraints in a table
  737.      *
  738.      * @param string    $table      name of table that should be used in method
  739.      * @return mixed data array on success, a MDB2 error on failure
  740.      * @access public
  741.      */
  742.     function listTableConstraints($table)
  743.     {
  744.         $db =$this->getDBInstance();
  745.         if (PEAR::isError($db)) {
  746.             return $db;
  747.         }
  748.         $table strtoupper($table);
  749.         $query = "SELECT RDB\$CONSTRAINT_NAME AS constraint_name,
  750.                          RDB\$CONSTRAINT_TYPE AS constraint_type,
  751.                          RDB\$INDEX_NAME AS index_name
  752.                     FROM RDB\$RELATION_CONSTRAINTS
  753.                    WHERE RDB\$RELATION_NAME='$table'";
  754.         $constraints $db->queryCol($query);
  755.         if (PEAR::isError($constraints)) {
  756.             return $constraints;
  757.         }
  758.  
  759.         $result = array();
  760.         foreach ($constraints as $constraint{
  761.             if ($constraint $this->_isIndexName($constraint)) {
  762.                 $result[$constraint= true;
  763.             }
  764.         }
  765.  
  766.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  767.             $result array_change_key_case($result$db->options['field_case']);
  768.         }
  769.         return array_keys($result);
  770.     }
  771.  
  772.     // }}}
  773.     // {{{ createSequence()
  774.  
  775.     /**
  776.      * create sequence
  777.      *
  778.      * @param string $seq_name name of the sequence to be created
  779.      * @param string $start start value of the sequence; default is 1
  780.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  781.      * @access public
  782.      */
  783.     function createSequence($seq_name$start = 1)
  784.     {
  785.         $db =$this->getDBInstance();
  786.         if (PEAR::isError($db)) {
  787.             return $db;
  788.         }
  789.  
  790.         $sequence_name $db->getSequenceName($seq_name);
  791.         if (PEAR::isError($result $db->exec('CREATE GENERATOR '.$sequence_name))) {
  792.             return $result;
  793.         }
  794.         if (PEAR::isError($result $db->exec('SET GENERATOR '.$sequence_name.' TO '.($start-1)))) {
  795.             if (PEAR::isError($err $db->dropSequence($seq_name))) {
  796.                 return $db->raiseError(MDB2_ERRORnullnull,
  797.                     'createSequence: Could not setup sequence start value and then it was not possible to drop it: '.
  798.                     $err->getMessage().' - ' .$err->getUserInfo());
  799.             }
  800.         }
  801.         return $result;
  802.     }
  803.  
  804.     // }}}
  805.     // {{{ dropSequence()
  806.  
  807.     /**
  808.      * drop existing sequence
  809.      *
  810.      * @param string $seq_name name of the sequence to be dropped
  811.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  812.      * @access public
  813.      */
  814.     function dropSequence($seq_name)
  815.     {
  816.         $db =$this->getDBInstance();
  817.         if (PEAR::isError($db)) {
  818.             return $db;
  819.         }
  820.  
  821.         $sequence_name $db->getSequenceName($seq_name);
  822.         $query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)='$sequence_name'";
  823.         return $db->exec($query);
  824.     }
  825.  
  826.     // }}}
  827.     // {{{ listSequences()
  828.  
  829.     /**
  830.      * list all sequences in the current database
  831.      *
  832.      * @return mixed data array on success, a MDB2 error on failure
  833.      * @access public
  834.      */
  835.     function listSequences()
  836.     {
  837.         $db =$this->getDBInstance();
  838.         if (PEAR::isError($db)) {
  839.             return $db;
  840.         }
  841.  
  842.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$SYSTEM_FLAG IS NULL';
  843.         $table_names $db->queryCol($query);
  844.         if (PEAR::isError($table_names)) {
  845.             return $table_names;
  846.         }
  847.         $result = array();
  848.         foreach ($table_names as $table_name{
  849.             if ($sqn $this->_isSequenceName($table_name)) {
  850.                 $result[$sqn;
  851.             }
  852.         }
  853.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  854.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  855.         }
  856.         return $result;
  857.     }
  858. }
  859. ?>

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