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

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