home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / MDB2 / Driver / Manager / mssql.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  27.5 KB  |  776 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith                                         |
  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. // | Authors: Frank M. Kromann <frank@kromann.info>                       |
  43. // |          David Coallier <davidc@php.net>                             |
  44. // |          Lorenzo Alberton <l.alberton@quipo.it>                      |
  45. // +----------------------------------------------------------------------+
  46. //
  47. // $Id: mssql.php,v 1.89 2007/03/12 14:31:48 quipo Exp $
  48. //
  49.  
  50. require_once 'MDB2/Driver/Manager/Common.php';
  51.  
  52. // {{{ class MDB2_Driver_Manager_mssql
  53.  
  54. /**
  55.  * MDB2 MSSQL driver for the management modules
  56.  *
  57.  * @package MDB2
  58.  * @category Database
  59.  * @author  Frank M. Kromann <frank@kromann.info>
  60.  * @author  David Coallier <davidc@php.net>
  61.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  62.  */
  63. class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
  64. {
  65.     // {{{ createDatabase()
  66.     /**
  67.      * create a new database
  68.      *
  69.      * @param string $name name of the database that should be created
  70.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  71.      * @access public
  72.      */
  73.     function createDatabase($name)
  74.     {
  75.         $db =& $this->getDBInstance();
  76.         if (PEAR::isError($db)) {
  77.             return $db;
  78.         }
  79.  
  80.         $name = $db->quoteIdentifier($name, true);
  81.         $query = "CREATE DATABASE $name";
  82.         if ($db->options['database_device']) {
  83.             $query.= ' ON '.$db->options['database_device'];
  84.             $query.= $db->options['database_size'] ? '=' .
  85.                      $db->options['database_size'] : '';
  86.         }
  87.         return $db->standaloneQuery($query, null, true);
  88.     }
  89.  
  90.     // }}}
  91.     // {{{ dropDatabase()
  92.  
  93.     /**
  94.      * drop an existing database
  95.      *
  96.      * @param string $name name of the database that should be dropped
  97.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  98.      * @access public
  99.      */
  100.     function dropDatabase($name)
  101.     {
  102.         $db =& $this->getDBInstance();
  103.         if (PEAR::isError($db)) {
  104.             return $db;
  105.         }
  106.  
  107.         $name = $db->quoteIdentifier($name, true);
  108.         return $db->standaloneQuery("DROP DATABASE $name", null, true);
  109.     }
  110.  
  111.     // }}}
  112.     // {{{ _getTemporaryTableQuery()
  113.  
  114.     /**
  115.      * Override the parent method.
  116.      *
  117.      * @return string The string required to be placed between "CREATE" and "TABLE"
  118.      *                to generate a temporary table, if possible.
  119.      */
  120.     function _getTemporaryTableQuery()
  121.     {
  122.         return '';
  123.     }
  124.  
  125.     // }}}
  126.     // {{{ createTable()
  127.  
  128.     /**
  129.      * create a new table
  130.      *
  131.      * @param string $name     Name of the database that should be created
  132.      * @param array $fields Associative array that contains the definition of each field of the new table
  133.      *                        The indexes of the array entries are the names of the fields of the table an
  134.      *                        the array entry values are associative arrays like those that are meant to be
  135.      *                         passed with the field definitions to get[Type]Declaration() functions.
  136.      *
  137.      *                        Example
  138.      *                        array(
  139.      *
  140.      *                            'id' => array(
  141.      *                                'type' => 'integer',
  142.      *                                'unsigned' => 1,
  143.      *                                'notnull' => 1,
  144.      *                                'default' => 0,
  145.      *                            ),
  146.      *                            'name' => array(
  147.      *                                'type' => 'text',
  148.      *                                'length' => 12,
  149.      *                            ),
  150.      *                            'description' => array(
  151.      *                                'type' => 'text',
  152.      *                                'length' => 12,
  153.      *                            )
  154.      *                        );
  155.      * @param array $options  An associative array of table options:
  156.      *                          array(
  157.      *                              'comment' => 'Foo',
  158.      *                              'temporary' => true|false,
  159.      *                          );
  160.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  161.      * @access public
  162.      */
  163.     function createTable($name, $fields, $options = array())
  164.     {
  165.         if (!empty($options['temporary'])) {
  166.             $name = '#'.$name;
  167.         }
  168.         return parent::createTable($name, $fields, $options);
  169.     }
  170.  
  171.     // }}}
  172.     // {{{ alterTable()
  173.  
  174.     /**
  175.      * alter an existing table
  176.      *
  177.      * @param string $name         name of the table that is intended to be changed.
  178.      * @param array $changes     associative array that contains the details of each type
  179.      *                             of change that is intended to be performed. The types of
  180.      *                             changes that are currently supported are defined as follows:
  181.      *
  182.      *                             name
  183.      *
  184.      *                                New name for the table.
  185.      *
  186.      *                            add
  187.      *
  188.      *                                Associative array with the names of fields to be added as
  189.      *                                 indexes of the array. The value of each entry of the array
  190.      *                                 should be set to another associative array with the properties
  191.      *                                 of the fields to be added. The properties of the fields should
  192.      *                                 be the same as defined by the MDB2 parser.
  193.      *
  194.      *
  195.      *                            remove
  196.      *
  197.      *                                Associative array with the names of fields to be removed as indexes
  198.      *                                 of the array. Currently the values assigned to each entry are ignored.
  199.      *                                 An empty array should be used for future compatibility.
  200.      *
  201.      *                            rename
  202.      *
  203.      *                                Associative array with the names of fields to be renamed as indexes
  204.      *                                 of the array. The value of each entry of the array should be set to
  205.      *                                 another associative array with the entry named name with the new
  206.      *                                 field name and the entry named Declaration that is expected to contain
  207.      *                                 the portion of the field declaration already in DBMS specific SQL code
  208.      *                                 as it is used in the CREATE TABLE statement.
  209.      *
  210.      *                            change
  211.      *
  212.      *                                Associative array with the names of the fields to be changed as indexes
  213.      *                                 of the array. Keep in mind that if it is intended to change either the
  214.      *                                 name of a field and any other properties, the change array entries
  215.      *                                 should have the new names of the fields as array indexes.
  216.      *
  217.      *                                The value of each entry of the array should be set to another associative
  218.      *                                 array with the properties of the fields to that are meant to be changed as
  219.      *                                 array entries. These entries should be assigned to the new values of the
  220.      *                                 respective properties. The properties of the fields should be the same
  221.      *                                 as defined by the MDB2 parser.
  222.      *
  223.      *                            Example
  224.      *                                array(
  225.      *                                    'name' => 'userlist',
  226.      *                                    'add' => array(
  227.      *                                        'quota' => array(
  228.      *                                            'type' => 'integer',
  229.      *                                            'unsigned' => 1
  230.      *                                        )
  231.      *                                    ),
  232.      *                                    'remove' => array(
  233.      *                                        'file_limit' => array(),
  234.      *                                        'time_limit' => array()
  235.      *                                    ),
  236.      *                                    'change' => array(
  237.      *                                        'name' => array(
  238.      *                                            'length' => '20',
  239.      *                                            'definition' => array(
  240.      *                                                'type' => 'text',
  241.      *                                                'length' => 20,
  242.      *                                            ),
  243.      *                                        )
  244.      *                                    ),
  245.      *                                    'rename' => array(
  246.      *                                        'sex' => array(
  247.      *                                            'name' => 'gender',
  248.      *                                            'definition' => array(
  249.      *                                                'type' => 'text',
  250.      *                                                'length' => 1,
  251.      *                                                'default' => 'M',
  252.      *                                            ),
  253.      *                                        )
  254.      *                                    )
  255.      *                                )
  256.      *
  257.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  258.      *                             can perform the requested table alterations if the value is true or
  259.      *                             actually perform them otherwise.
  260.      * @access public
  261.      *
  262.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  263.      */
  264.     function alterTable($name, $changes, $check)
  265.     {
  266.         $db =& $this->getDBInstance();
  267.         if (PEAR::isError($db)) {
  268.             return $db;
  269.         }
  270.  
  271.         foreach ($changes as $change_name => $change) {
  272.             switch ($change_name) {
  273.             case 'add':
  274.                 break;
  275.             case 'remove':
  276.                 break;
  277.             case 'name':
  278.             case 'rename':
  279.             case 'change':
  280.             default:
  281.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTER, null, null,
  282.                     'change type "'.$change_name.'" not yet supported', __FUNCTION__);
  283.             }
  284.         }
  285.  
  286.         if ($check) {
  287.             return MDB2_OK;
  288.         }
  289.  
  290.         $query = '';
  291.         if (!empty($changes['add']) && is_array($changes['add'])) {
  292.             foreach ($changes['add'] as $field_name => $field) {
  293.                 if ($query) {
  294.                     $query.= ', ';
  295.                 } else {
  296.                     $query.= 'ADD COLUMN ';
  297.                 }
  298.                 $query.= $db->getDeclaration($field['type'], $field_name, $field);
  299.             }
  300.         }
  301.  
  302.         if (!empty($changes['remove']) && is_array($changes['remove'])) {
  303.             foreach ($changes['remove'] as $field_name => $field) {
  304.                 if ($query) {
  305.                     $query.= ', ';
  306.                 }
  307.                 $field_name = $db->quoteIdentifier($field_name, true);
  308.                 $query.= 'DROP COLUMN ' . $field_name;
  309.             }
  310.         }
  311.  
  312.         if (!$query) {
  313.             return MDB2_OK;
  314.         }
  315.  
  316.         $name = $db->quoteIdentifier($name, true);
  317.         return $db->exec("ALTER TABLE $name $query");
  318.     }
  319.  
  320.     // }}}
  321.     // {{{ listTables()
  322.  
  323.     /**
  324.      * list all tables in the current database
  325.      *
  326.      * @return mixed array of table names on success, a MDB2 error on failure
  327.      * @access public
  328.      */
  329.     function listTables()
  330.     {
  331.         $db =& $this->getDBInstance();
  332.  
  333.         if (PEAR::isError($db)) {
  334.             return $db;
  335.         }
  336.  
  337.         $query = 'EXEC sp_tables @table_type = "\'TABLE\'"';
  338.         $table_names = $db->queryCol($query, null, 2);
  339.         if (PEAR::isError($table_names)) {
  340.             return $table_names;
  341.         }
  342.         $result = array();
  343.         foreach ($table_names as $table_name) {
  344.             if (!$this->_fixSequenceName($table_name, true)) {
  345.                 $result[] = $table_name;
  346.             }
  347.         }
  348.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  349.             $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  350.                         'strtolower' : 'strtoupper'), $result);
  351.         }
  352.         return $result;
  353.     }
  354.  
  355.     // }}}
  356.     // {{{ listTableFields()
  357.  
  358.     /**
  359.      * list all fields in a table in the current database
  360.      *
  361.      * @param string $table name of table that should be used in method
  362.      * @return mixed array of field names on success, a MDB2 error on failure
  363.      * @access public
  364.      */
  365.     function listTableFields($table)
  366.     {
  367.         $db =& $this->getDBInstance();
  368.         if (PEAR::isError($db)) {
  369.             return $db;
  370.         }
  371.  
  372.         $table = $db->quoteIdentifier($table, true);
  373.         $db->setLimit(1);
  374.         $result2 = $db->query("SELECT * FROM $table");
  375.         if (PEAR::isError($result2)) {
  376.             return $result2;
  377.         }
  378.         $result = $result2->getColumnNames();
  379.         $result2->free();
  380.         if (PEAR::isError($result)) {
  381.             return $result;
  382.         }
  383.         return array_flip($result);
  384.     }
  385.  
  386.     // }}}
  387.     // {{{ listTableIndexes()
  388.  
  389.     /**
  390.      * list all indexes in a table
  391.      *
  392.      * @param string $table name of table that should be used in method
  393.      * @return mixed array of index names on success, a MDB2 error on failure
  394.      * @access public
  395.      */
  396.     function listTableIndexes($table)
  397.     {
  398.         $db =& $this->getDBInstance();
  399.         if (PEAR::isError($db)) {
  400.             return $db;
  401.         }
  402.  
  403.         $key_name = 'INDEX_NAME';
  404.         $pk_name = 'PK_NAME';
  405.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  406.             if ($db->options['field_case'] == CASE_LOWER) {
  407.                 $key_name = strtolower($key_name);
  408.                 $pk_name  = strtolower($pk_name);
  409.             } else {
  410.                 $key_name = strtoupper($key_name);
  411.                 $pk_name  = strtoupper($pk_name);
  412.             }
  413.         }
  414.         $table = $db->quote($table, 'text');
  415.         $query = "EXEC sp_statistics @table_name=$table";
  416.         $indexes = $db->queryCol($query, 'text', $key_name);
  417.         if (PEAR::isError($indexes)) {
  418.             return $indexes;
  419.         }
  420.         $query = "EXEC sp_pkeys @table_name=$table";
  421.         $pk_all = $db->queryCol($query, 'text', $pk_name);
  422.         $result = array();
  423.         foreach ($indexes as $index) {
  424.             if (!in_array($index, $pk_all) && ($index = $this->_fixIndexName($index))) {
  425.                 $result[$index] = true;
  426.             }
  427.         }
  428.  
  429.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  430.             $result = array_change_key_case($result, $db->options['field_case']);
  431.         }
  432.         return array_keys($result);
  433.     }
  434.  
  435.     // }}}
  436.     // {{{ listDatabases()
  437.  
  438.     /**
  439.      * list all databases
  440.      *
  441.      * @return mixed array of database names on success, a MDB2 error on failure
  442.      * @access public
  443.      */
  444.     function listDatabases()
  445.     {
  446.         $db =& $this->getDBInstance();
  447.         if (PEAR::isError($db)) {
  448.             return $db;
  449.         }
  450.  
  451.         $result = $db->queryCol('SELECT name FROM sys.databases');
  452.         if (PEAR::isError($result)) {
  453.             return $result;
  454.         }
  455.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  456.             $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  457.         }
  458.         return $result;
  459.     }
  460.  
  461.     // }}}
  462.     // {{{ listUsers()
  463.  
  464.     /**
  465.      * list all users
  466.      *
  467.      * @return mixed array of user names on success, a MDB2 error on failure
  468.      * @access public
  469.      */
  470.     function listUsers()
  471.     {
  472.         $db =& $this->getDBInstance();
  473.         if (PEAR::isError($db)) {
  474.             return $db;
  475.         }
  476.  
  477.         $result = $db->queryCol('SELECT DISTINCT loginame FROM master..sysprocesses');
  478.         if (PEAR::isError($result) || empty($result)) {
  479.             return $result;
  480.         }
  481.         foreach (array_keys($result) as $k) {
  482.             $result[$k] = trim($result[$k]);
  483.         }
  484.         return $result;
  485.     }
  486.  
  487.     // }}}
  488.     // {{{ listFunctions()
  489.  
  490.     /**
  491.      * list all functions in the current database
  492.      *
  493.      * @return mixed array of function names on success, a MDB2 error on failure
  494.      * @access public
  495.      */
  496.     function listFunctions()
  497.     {
  498.         $db =& $this->getDBInstance();
  499.         if (PEAR::isError($db)) {
  500.             return $db;
  501.         }
  502.  
  503.         $query = "SELECT name
  504.                     FROM sysobjects
  505.                    WHERE objectproperty(id, N'IsMSShipped') = 0
  506.                     AND (objectproperty(id, N'IsTableFunction') = 1
  507.                      OR objectproperty(id, N'IsScalarFunction') = 1)";
  508.         /*
  509.         SELECT ROUTINE_NAME
  510.           FROM INFORMATION_SCHEMA.ROUTINES
  511.          WHERE ROUTINE_TYPE = 'FUNCTION'
  512.         */
  513.         $result = $db->queryCol($query);
  514.         if (PEAR::isError($result)) {
  515.             return $result;
  516.         }
  517.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  518.             $result = array_map(($db->options['field_case'] == CASE_LOWER ? 'strtolower' : 'strtoupper'), $result);
  519.         }
  520.         return $result;
  521.     }
  522.  
  523.     // }}}
  524.     // {{{ listTableTriggers()
  525.  
  526.     /**
  527.      * list all triggers in the database that reference a given table
  528.      *
  529.      * @param string table for which all referenced triggers should be found
  530.      * @return mixed array of trigger names on success,  otherwise, false which
  531.      *               could be a db error if the db is not instantiated or could
  532.      *               be the results of the error that occured during the
  533.      *               querying of the sysobject module.
  534.      * @access public
  535.      */
  536.     function listTableTriggers($table = null)
  537.     {
  538.         $db =& $this->getDBInstance();
  539.         if (PEAR::isError($db)) {
  540.             return $db;
  541.         }
  542.  
  543.         $table = $db->quote($table, 'text');
  544.         $query = "SELECT o.name
  545.                     FROM sysobjects o
  546.                    WHERE xtype = 'TR'
  547.                      AND OBJECTPROPERTY(o.id, 'IsMSShipped') = 0";
  548.         if (!is_null($table)) {
  549.             $query .= " AND object_name(parent_obj) = $table";
  550.         }
  551.  
  552.         $result = $db->queryCol($query);
  553.         if (PEAR::isError($result)) {
  554.             return $result;
  555.         }
  556.  
  557.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE &&
  558.             $db->options['field_case'] == CASE_LOWER)
  559.         {
  560.             $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  561.                 'strtolower' : 'strtoupper'), $result);
  562.         }
  563.         return $result;
  564.     }
  565.  
  566.     // }}}
  567.     // {{{ listViews()
  568.  
  569.     /**
  570.      * list all views in the current database
  571.      *
  572.      * @param string database, the current is default
  573.      * @return mixed array of view names on success, a MDB2 error on failure
  574.      * @access public
  575.      */
  576.     function listViews()
  577.     {
  578.         $db =& $this->getDBInstance();
  579.         if (PEAR::isError($db)) {
  580.             return $db;
  581.         }
  582.  
  583.         $query = "SELECT name
  584.                    FROM sysobjects
  585.                     WHERE xtype = 'V'";
  586.         /*
  587.         SELECT *
  588.           FROM sysobjects
  589.          WHERE objectproperty(id, N'IsMSShipped') = 0
  590.            AND objectproperty(id, N'IsView') = 1
  591.         */
  592.  
  593.         $result = $db->queryCol($query);
  594.         if (PEAR::isError($result)) {
  595.             return $result;
  596.         }
  597.  
  598.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE &&
  599.             $db->options['field_case'] == CASE_LOWER)
  600.         {
  601.             $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  602.                           'strtolower' : 'strtoupper'), $result);
  603.         }
  604.         return $result;
  605.     }
  606.  
  607.     // }}}
  608.     // {{{ dropIndex()
  609.  
  610.     /**
  611.      * drop existing index
  612.      *
  613.      * @param string    $table         name of table that should be used in method
  614.      * @param string    $name         name of the index to be dropped
  615.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  616.      * @access public
  617.      */
  618.     function dropIndex($table, $name)
  619.     {
  620.         $db =& $this->getDBInstance();
  621.         if (PEAR::isError($db)) {
  622.             return $db;
  623.         }
  624.  
  625.         $table = $db->quoteIdentifier($table, true);
  626.         $name = $db->quoteIdentifier($db->getIndexName($name), true);
  627.         return $db->exec("DROP INDEX $table.$name");
  628.     }
  629.  
  630.     // }}}
  631.     // {{{ listTableConstraints()
  632.  
  633.     /**
  634.      * list all constraints in a table
  635.      *
  636.      * @param string $table name of table that should be used in method
  637.      * @return mixed array of constraint names on success, a MDB2 error on failure
  638.      * @access public
  639.      */
  640.     function listTableConstraints($table)
  641.     {
  642.         $db =& $this->getDBInstance();
  643.         if (PEAR::isError($db)) {
  644.             return $db;
  645.         }
  646.         $table = $db->quoteIdentifier($table, true);
  647.         
  648.         $query = "SELECT c.constraint_name
  649.                     FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
  650.                    WHERE c.constraint_catalog = DB_NAME()
  651.                     AND c.table_name = '$table'";
  652.         $constraints = $db->queryCol($query);
  653.         if (PEAR::isError($constraints)) {
  654.             return $constraints;
  655.         }
  656.  
  657.         $result = array();
  658.         foreach ($constraints as $constraint) {
  659.             $constraint = $this->_fixIndexName($constraint);
  660.             if (!empty($constraint)) {
  661.                 $result[$constraint] = true;
  662.             }
  663.         }
  664.  
  665.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  666.             $result = array_change_key_case($result, $db->options['field_case']);
  667.         }
  668.         return array_keys($result);
  669.     }
  670.  
  671.     // }}}
  672.     // {{{ createSequence()
  673.  
  674.     /**
  675.      * create sequence
  676.      *
  677.      * @param string    $seq_name     name of the sequence to be created
  678.      * @param string    $start         start value of the sequence; default is 1
  679.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  680.      * @access public
  681.      */
  682.     function createSequence($seq_name, $start = 1)
  683.     {
  684.         $db =& $this->getDBInstance();
  685.         if (PEAR::isError($db)) {
  686.             return $db;
  687.         }
  688.  
  689.         $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  690.         $seqcol_name = $db->quoteIdentifier($db->options['seqcol_name'], true);
  691.         $query = "CREATE TABLE $sequence_name ($seqcol_name " .
  692.                  "INT PRIMARY KEY CLUSTERED IDENTITY($start,1) NOT NULL)";
  693.  
  694.         $res = $db->exec($query);
  695.         if (PEAR::isError($res)) {
  696.             return $res;
  697.         }
  698.  
  699.         $query = "SET IDENTITY_INSERT $sequence_name ON ".
  700.                  "INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)";
  701.         $res = $db->exec($query);
  702.  
  703.         if (!PEAR::isError($res)) {
  704.             return MDB2_OK;
  705.         }
  706.  
  707.         $result = $db->exec("DROP TABLE $sequence_name");
  708.         if (PEAR::isError($result)) {
  709.             return $db->raiseError($result, null, null,
  710.                 'could not drop inconsistent sequence table', __FUNCTION__);
  711.         }
  712.  
  713.         return $db->raiseError($res, null, null,
  714.             'could not create sequence table', __FUNCTION__);
  715.     }
  716.  
  717.     // }}}
  718.     // {{{ dropSequence()
  719.  
  720.     /**
  721.      * This function drops an existing sequence
  722.      *
  723.      * @param string $seq_name name of the sequence to be dropped
  724.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  725.      * @access public
  726.      */
  727.     function dropSequence($seq_name)
  728.     {
  729.         $db =& $this->getDBInstance();
  730.         if (PEAR::isError($db)) {
  731.             return $db;
  732.         }
  733.  
  734.         $sequence_name = $db->quoteIdentifier($db->getSequenceName($seq_name), true);
  735.         return $db->exec("DROP TABLE $sequence_name");
  736.     }
  737.  
  738.     // }}}
  739.     // {{{ listSequences()
  740.  
  741.     /**
  742.      * list all sequences in the current database
  743.      *
  744.      * @return mixed array of sequence names on success, a MDB2 error on failure
  745.      * @access public
  746.      */
  747.     function listSequences()
  748.     {
  749.         $db =& $this->getDBInstance();
  750.         if (PEAR::isError($db)) {
  751.             return $db;
  752.         }
  753.  
  754.         $query = "SELECT name FROM sysobjects WHERE xtype = 'U'";
  755.         $table_names = $db->queryCol($query);
  756.         if (PEAR::isError($table_names)) {
  757.             return $table_names;
  758.         }
  759.         $result = array();
  760.         foreach ($table_names as $table_name) {
  761.             if ($sqn = $this->_fixSequenceName($table_name, true)) {
  762.                 $result[] = $sqn;
  763.             }
  764.         }
  765.         if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  766.             $result = array_map(($db->options['field_case'] == CASE_LOWER ?
  767.                           'strtolower' : 'strtoupper'), $result);
  768.         }
  769.         return $result;
  770.     }
  771.  
  772.     // }}}
  773. }
  774.  
  775. // }}}
  776. ?>