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-2004 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. // | Author: Frank M. Kromann <frank@kromann.info>                        |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: mssql.php,v 1.23 2005/04/16 22:27:48 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. /**
  51.  * MDB2 MSSQL driver for the management modules
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Frank M. Kromann <frank@kromann.info>
  56.  */
  57. {
  58.     // }}}
  59.     // {{{ createDatabase()
  60.  
  61.     /**
  62.      * create a new database
  63.      *
  64.      * @param string $name name of the database that should be created
  65.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function createDatabase($name)
  69.     {
  70.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  71.         $database_device (isset($db->options['database_device'])) $db->options['database_device''DEFAULT';
  72.         $database_size (isset($db->options['database_size'])) '='.$db->options['database_size''';
  73.         return $db->standaloneQuery("CREATE DATABASE $name ON $database_device$database_size");
  74.     }
  75.  
  76.     // }}}
  77.     // {{{ dropDatabase()
  78.  
  79.     /**
  80.      * drop an existing database
  81.      *
  82.      * @param string $name name of the database that should be dropped
  83.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  84.      * @access public
  85.      */
  86.     function dropDatabase($name)
  87.     {
  88.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  89.         return $db->standaloneQuery("DROP DATABASE $name");
  90.     }
  91.  
  92.     // }}}
  93.     // {{{ alterTable()
  94.  
  95.     /**
  96.      * alter an existing table
  97.      *
  98.      * @param string $name         name of the table that is intended to be changed.
  99.      * @param array $changes     associative array that contains the details of each type
  100.      *                              of change that is intended to be performed. The types of
  101.      *                              changes that are currently supported are defined as follows:
  102.      *
  103.      *                              name
  104.      *
  105.      *                                 New name for the table.
  106.      *
  107.      *                             added_fields
  108.      *
  109.      *                                 Associative array with the names of fields to be added as
  110.      *                                  indexes of the array. The value of each entry of the array
  111.      *                                  should be set to another associative array with the properties
  112.      *                                  of the fields to be added. The properties of the fields should
  113.      *                                  be the same as defined by the Metabase parser.
  114.      *
  115.      *                                 Additionally, there should be an entry named Declaration that
  116.      *                                  is expected to contain the portion of the field declaration already
  117.      *                                  in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  118.      *
  119.      *                             removed_fields
  120.      *
  121.      *                                 Associative array with the names of fields to be removed as indexes
  122.      *                                  of the array. Currently the values assigned to each entry are ignored.
  123.      *                                  An empty array should be used for future compatibility.
  124.      *
  125.      *                             renamed_fields
  126.      *
  127.      *                                 Associative array with the names of fields to be renamed as indexes
  128.      *                                  of the array. The value of each entry of the array should be set to
  129.      *                                  another associative array with the entry named name with the new
  130.      *                                  field name and the entry named Declaration that is expected to contain
  131.      *                                  the portion of the field declaration already in DBMS specific SQL code
  132.      *                                  as it is used in the CREATE TABLE statement.
  133.      *
  134.      *                             changed_fields
  135.      *
  136.      *                                 Associative array with the names of the fields to be changed as indexes
  137.      *                                  of the array. Keep in mind that if it is intended to change either the
  138.      *                                  name of a field and any other properties, the changed_fields array entries
  139.      *                                  should have the new names of the fields as array indexes.
  140.      *
  141.      *                                 The value of each entry of the array should be set to another associative
  142.      *                                  array with the properties of the fields to that are meant to be changed as
  143.      *                                  array entries. These entries should be assigned to the new values of the
  144.      *                                  respective properties. The properties of the fields should be the same
  145.      *                                  as defined by the Metabase parser.
  146.      *
  147.      *                                 If the default property is meant to be added, removed or changed, there
  148.      *                                  should also be an entry with index ChangedDefault assigned to 1. Similarly,
  149.      *                                  if the notnull constraint is to be added or removed, there should also be
  150.      *                                  an entry with index ChangedNotNull assigned to 1.
  151.      *
  152.      *                                 Additionally, there should be an entry named Declaration that is expected
  153.      *                                  to contain the portion of the field changed declaration already in DBMS
  154.      *                                  specific SQL code as it is used in the CREATE TABLE statement.
  155.      *                             Example
  156.      *                                 array(
  157.      *                                     'name' => 'userlist',
  158.      *                                     'added_fields' => array(
  159.      *                                         'quota' => array(
  160.      *                                             'type' => 'integer',
  161.      *                                             'unsigned' => 1
  162.      *                                             'declaration' => 'quota INT'
  163.      *                                         )
  164.      *                                     ),
  165.      *                                     'removed_fields' => array(
  166.      *                                         'file_limit' => array(),
  167.      *                                         'time_limit' => array()
  168.      *                                         ),
  169.      *                                     'changed_fields' => array(
  170.      *                                         'gender' => array(
  171.      *                                             'default' => 'M',
  172.      *                                             'change_default' => 1,
  173.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  174.      *                                         )
  175.      *                                     ),
  176.      *                                     'renamed_fields' => array(
  177.      *                                         'sex' => array(
  178.      *                                             'name' => 'gender',
  179.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  180.      *                                         )
  181.      *                                     )
  182.      *                                 )
  183.      *
  184.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  185.      *                              can perform the requested table alterations if the value is true or
  186.      *                              actually perform them otherwise.
  187.      * @access public
  188.      *
  189.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  190.      */
  191.     function alterTable($name$changes$check)
  192.     {
  193.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  194.         foreach ($changes as $change_name => $change{
  195.             switch ($change_name{
  196.             case 'added_fields':
  197.                 break;
  198.             case 'removed_fields':
  199.             case 'name':
  200.             case 'renamed_fields':
  201.             case 'changed_fields':
  202.             default:
  203.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  204.                     'alterTable: change type "'.$change_name.'" not yet supported');
  205.             }
  206.         }
  207.         if ($check{
  208.             return MDB2_OK;
  209.         }
  210.         $query '';
  211.         if (isset($changes['added_fields'])) {
  212.             if ($query{
  213.                 $query.= ', ';
  214.             }
  215.             $query.= 'ADD ';
  216.             $fields $changes['added_fields'];
  217.             foreach ($fields as $field{
  218.                 if ($query{
  219.                     $query.= ', ';
  220.                 }
  221.                 $query.= $field['declaration'];
  222.             }
  223.         }
  224.         if (!$query{
  225.             return MDB2_OK;
  226.         }
  227.         return $db->query("ALTER TABLE $name $query");
  228.     }
  229.  
  230.     // }}}
  231.     // {{{ listTables()
  232.  
  233.     /**
  234.      * list all tables in the current database
  235.      *
  236.      * @return mixed data array on success, a MDB error on failure
  237.      * @access public
  238.      */
  239.     function listTables()
  240.     {
  241.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  242.         $query 'EXECUTE sp_tables @table_type = "\'TABLE\'"';
  243.         $table_names $db->queryCol($querynull2);
  244.         if (PEAR::isError($table_names)) {
  245.             return $table_names;
  246.         }
  247.         $tables = array();
  248.         for ($i = 0$j count($table_names)$i <$j; ++$i{
  249.             if (!$this->_isSequenceName($db$table_names[$i])) {
  250.                 $tables[$table_names[$i];
  251.             }
  252.         }
  253.         return $tables;
  254.     }
  255.  
  256.     // }}}
  257.     // {{{ listTableFields()
  258.  
  259.     /**
  260.      * list all fields in a tables in the current database
  261.      *
  262.      * @param string $table name of table that should be used in method
  263.      * @return mixed data array on success, a MDB error on failure
  264.      * @access public
  265.      */
  266.     function listTableFields($table)
  267.     {
  268.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  269.         $result $db->query("SELECT * FROM $table");
  270.         if (PEAR::isError($result)) {
  271.             return $result;
  272.         }
  273.         $columns $result->getColumnNames();
  274.         $result->free();
  275.         if (PEAR::isError($columns)) {
  276.             return $columns;
  277.         }
  278.         return array_flip($columns);
  279.     }
  280.  
  281.     // }}}
  282.     // {{{ listTableIndexes()
  283.  
  284.     /**
  285.      * list all indexes in a table
  286.      *
  287.      * @param string    $table     name of table that should be used in method
  288.      * @return mixed data array on success, a MDB error on failure
  289.      * @access public
  290.      */
  291.     function listTableIndexes($table)
  292.     {
  293.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  294.         $key_name 'INDEX_NAME';
  295.         $pk_name 'PK_NAME';
  296.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  297.             $key_name strtolower($key_name);
  298.             $pk_name strtolower($pk_name);
  299.         }
  300.         $query = "EXEC sp_statistics @table_name='$table'";
  301.         // third parameter is wrong! should this be a prepared query?
  302.         $indexes_all $db->query($query'text'$key_name);
  303.         if (PEAR::isError($indexes_all)) {
  304.             return $indexes_all;
  305.         }
  306.         $query = "EXEC sp_pkeys @table_name='$table'";
  307.         // third parameter is wrong! should this be a prepared query?
  308.         $pk_all $db->queryCol($query'text'$pk_name);
  309.         $found $indexes = array();
  310.         for ($index = 0$j count($indexes_all)$index $j; ++$index{
  311.             if (!in_array($indexes_all[$index]$pk_all)
  312.                 && $indexes_all[$index!= null
  313.                 && !isset($found[$indexes_all[$index]])
  314.             {
  315.                 $indexes[$indexes_all[$index];
  316.                 $found[$indexes_all[$index]] = 1;
  317.             }
  318.         }
  319.         return $indexes;
  320.     }
  321.  
  322.     // }}}
  323.     // {{{ createSequence()
  324.  
  325.     /**
  326.      * create sequence
  327.      *
  328.      * @param string    $seq_name     name of the sequence to be created
  329.      * @param string    $start         start value of the sequence; default is 1
  330.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  331.      * @access public
  332.      */
  333.     function createSequence($seq_name$start = 1)
  334.     {
  335.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  336.         $sequence_name $db->getSequenceName($seq_name);
  337.         $query = "CREATE TABLE $sequence_name (".$db->options['seqcol_name']
  338.             ." INT NOT NULL IDENTITY($start,1) PRIMARY KEY CLUSTERED)";
  339.         return $db->query($query);
  340.     }
  341.  
  342.     // }}}
  343.     // {{{ dropSequence()
  344.  
  345.     /**
  346.      * drop existing sequence
  347.      *
  348.      * @param string    $seq_name     name of the sequence to be dropped
  349.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  350.      * @access public
  351.      */
  352.     function dropSequence($seq_name)
  353.     {
  354.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  355.         $sequence_name $db->getSequenceName($seq_name);
  356.         return $db->Query("DROP TABLE $sequence_name");
  357.     }
  358.  
  359.     // }}}
  360.     // {{{ listSequences()
  361.  
  362.     /**
  363.      * list all sequences in the current database
  364.      *
  365.      * @return mixed data array on success, a MDB2 error on failure
  366.      * @access public
  367.      */
  368.     function listSequences()
  369.     {
  370.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  371.         $query "SELECT table_name FROM information_schema.tables WHERE table_type = 'BASE_TABLE'";
  372.         $table_names $db->queryCol($query);
  373.         if (PEAR::isError($table_names)) {
  374.             return $table_names;
  375.         }
  376.         $sequences = array();
  377.         for ($i = 0$j count($table_names)$i <$j; ++$i{
  378.             if ($this->_isSequenceName($db$table_names[$i])) {
  379.                 $sequences[$table_names[$i];
  380.             }
  381.         }
  382.         return $sequences;
  383.     }}
  384. ?>

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