MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file mssql.php

Documentation is available at mssql.php

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

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