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

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