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.74 2006/01/13 10:13:17 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_ERROR_CANNOT_ALTERnullnull,
  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'nulltrue);
  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
  661.                     FROM RDB\$INDICES
  662.                    WHERE RDB\$RELATION_NAME='$table'
  663.                      AND RDB\$UNIQUE_FLAG IS NULL
  664.                      AND RDB\$FOREIGN_KEY IS NULL";
  665.         $indexes $db->queryCol($query'text');
  666.         if (PEAR::isError($indexes)) {
  667.             return $indexes;
  668.         }
  669.  
  670.         $result = array();
  671.         foreach ($indexes as $index{
  672.             if ($index $this->_fixIndexName($index)) {
  673.                 $result[$index= true;
  674.             }
  675.         }
  676.  
  677.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  678.             $result array_change_key_case($result$db->options['field_case']);
  679.         }
  680.         return array_keys($result);
  681.     }
  682.  
  683.     // }}}
  684.     // {{{ createConstraint()
  685.  
  686.     /**
  687.      * create a constraint on a table
  688.      *
  689.      * @param string    $table      name of the table on which the constraint is to be created
  690.      * @param string    $name       name of the constraint to be created
  691.      * @param array     $definition associative array that defines properties of the constraint to be created.
  692.      *                               Currently, only one property named FIELDS is supported. This property
  693.      *                               is also an associative with the names of the constraint fields as array
  694.      *                               constraints. Each entry of this array is set to another type of associative
  695.      *                               array that specifies properties of the constraint that are specific to
  696.      *                               each field.
  697.      *
  698.      *                               Example
  699.      *                                   array(
  700.      *                                       'fields' => array(
  701.      *                                           'user_name' => array(),
  702.      *                                           'last_login' => array(),
  703.      *                                       )
  704.      *                                   )
  705.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  706.      * @access public
  707.      */
  708.     function createConstraint($table$name$definition)
  709.     {
  710.         $db =$this->getDBInstance();
  711.         if (PEAR::isError($db)) {
  712.             return $db;
  713.         }
  714.         $table $db->quoteIdentifier($tabletrue);
  715.         $name  $db->quoteIdentifier($db->getIndexName($name)true);
  716.         $query = "ALTER TABLE $table ADD";
  717.         if (array_key_exists('primary'$definition&& $definition['primary']{
  718.             if (!empty($name)) {
  719.                 $query.= ' CONSTRAINT '.$name;
  720.             }
  721.             $query.= ' PRIMARY KEY';
  722.         else {
  723.             $query.= ' CONSTRAINT '$name;
  724.             if (array_key_exists('unique'$definition&& $definition['unique']{
  725.                $query.= ' UNIQUE';
  726.             }
  727.         }
  728.         $fields = array();
  729.         foreach (array_keys($definition['fields']as $field{
  730.             $fields[$db->quoteIdentifier($fieldtrue);
  731.         }
  732.         $query .= ' ('implode(', '$fields')';
  733.         return $db->exec($query);
  734.     }
  735.  
  736.     // }}}
  737.     // {{{ listTableConstraints()
  738.  
  739.     /**
  740.      * list all sonstraints in a table
  741.      *
  742.      * @param string    $table      name of table that should be used in method
  743.      * @return mixed data array on success, a MDB2 error on failure
  744.      * @access public
  745.      */
  746.     function listTableConstraints($table)
  747.     {
  748.         $db =$this->getDBInstance();
  749.         if (PEAR::isError($db)) {
  750.             return $db;
  751.         }
  752.         $table strtoupper($table);
  753.         $query = "SELECT RDB\$INDEX_NAME
  754.                     FROM RDB\$INDICES
  755.                    WHERE UPPER(RDB\$RELATION_NAME)='$table'
  756.                      AND (
  757.                            RDB\$UNIQUE_FLAG IS NOT NULL
  758.                         OR RDB\$FOREIGN_KEY IS NOT NULL
  759.                      )";
  760.         $constraints $db->queryCol($query);
  761.         if (PEAR::isError($constraints)) {
  762.             return $constraints;
  763.         }
  764.  
  765.         $result = array();
  766.         foreach ($constraints as $constraint{
  767.             $result[$this->_fixIndexName($constraint)= true;
  768.         }
  769.  
  770.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  771.             $result array_change_key_case($result$db->options['field_case']);
  772.         }
  773.         return array_keys($result);
  774.     }
  775.  
  776.     // }}}
  777.     // {{{ createSequence()
  778.  
  779.     /**
  780.      * create sequence
  781.      *
  782.      * @param string $seq_name name of the sequence to be created
  783.      * @param string $start start value of the sequence; default is 1
  784.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  785.      * @access public
  786.      */
  787.     function createSequence($seq_name$start = 1)
  788.     {
  789.         $db =$this->getDBInstance();
  790.         if (PEAR::isError($db)) {
  791.             return $db;
  792.         }
  793.  
  794.         $sequence_name $db->getSequenceName($seq_name);
  795.         if (PEAR::isError($result $db->exec('CREATE GENERATOR '.$sequence_name))) {
  796.             return $result;
  797.         }
  798.         if (PEAR::isError($result $db->exec('SET GENERATOR '.$sequence_name.' TO '.($start-1)))) {
  799.             if (PEAR::isError($err $db->dropSequence($seq_name))) {
  800.                 return $db->raiseError(MDB2_ERRORnullnull,
  801.                     'createSequence: Could not setup sequence start value and then it was not possible to drop it: '.
  802.                     $err->getMessage().' - ' .$err->getUserInfo());
  803.             }
  804.         }
  805.         return $result;
  806.     }
  807.  
  808.     // }}}
  809.     // {{{ dropSequence()
  810.  
  811.     /**
  812.      * drop existing sequence
  813.      *
  814.      * @param string $seq_name name of the sequence to be dropped
  815.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  816.      * @access public
  817.      */
  818.     function dropSequence($seq_name)
  819.     {
  820.         $db =$this->getDBInstance();
  821.         if (PEAR::isError($db)) {
  822.             return $db;
  823.         }
  824.  
  825.         $sequence_name $db->getSequenceName($seq_name);
  826.         $query = "DELETE FROM RDB\$GENERATORS WHERE UPPER(RDB\$GENERATOR_NAME)='$sequence_name'";
  827.         return $db->exec($query);
  828.     }
  829.  
  830.     // }}}
  831.     // {{{ listSequences()
  832.  
  833.     /**
  834.      * list all sequences in the current database
  835.      *
  836.      * @return mixed data array on success, a MDB2 error on failure
  837.      * @access public
  838.      */
  839.     function listSequences()
  840.     {
  841.         $db =$this->getDBInstance();
  842.         if (PEAR::isError($db)) {
  843.             return $db;
  844.         }
  845.  
  846.         $query 'SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS WHERE RDB$SYSTEM_FLAG IS NULL';
  847.         $table_names $db->queryCol($query);
  848.         if (PEAR::isError($table_names)) {
  849.             return $table_names;
  850.         }
  851.         $result = array();
  852.         foreach ($table_names as $table_name{
  853.             $result[$this->_fixSequenceName($table_name);
  854.         }
  855.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  856.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  857.         }
  858.         return $result;
  859.     }
  860. }
  861. ?>

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