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

Source for file mssql.php

Documentation is available at mssql.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP Version 4                                                        |
  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.5 2004/04/09 10:41:21 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.     // {{{ constructor
  60.  
  61.     /**
  62.      * Constructor
  63.      */
  64.     function MDB2_Driver_Manager_mssql($db_index)
  65.     {
  66.         $this->MDB2_Driver_Manager_Common($db_index);
  67.     }
  68.  
  69.     // }}}
  70.     // {{{ createDatabase()
  71.  
  72.     /**
  73.      * create a new database
  74.      *
  75.      * @param string $name name of the database that should be created
  76.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  77.      * @access public
  78.      */
  79.     function createDatabase($name)
  80.     {
  81.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  82.         $DatabaseDevice ($db->options['database_device']$db->options['database_device''DEFAULT';
  83.         $DatabaseSize ($db->options['database_size']"=".$db->options['database_size''';
  84.         return $db->standaloneQuery("CREATE DATABASE $name ON ".$DatabaseDevice.$DatabaseSize);
  85.     }
  86.  
  87.     // }}}
  88.     // {{{ dropDatabase()
  89.  
  90.     /**
  91.      * drop an existing database
  92.      *
  93.      * @param string $name name of the database that should be dropped
  94.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  95.      * @access public
  96.      */
  97.     function dropDatabase($name)
  98.     {
  99.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  100.         return $db->standaloneQuery("DROP DATABASE $name");
  101.     }
  102.  
  103.     // }}}
  104.     // {{{ alterTable()
  105.  
  106.     /**
  107.      * alter an existing table
  108.      *
  109.      * @param string $name         name of the table that is intended to be changed.
  110.      * @param array $changes     associative array that contains the details of each type
  111.      *                              of change that is intended to be performed. The types of
  112.      *                              changes that are currently supported are defined as follows:
  113.      *
  114.      *                              name
  115.      *
  116.      *                                 New name for the table.
  117.      *
  118.      *                             added_fields
  119.      *
  120.      *                                 Associative array with the names of fields to be added as
  121.      *                                  indexes of the array. The value of each entry of the array
  122.      *                                  should be set to another associative array with the properties
  123.      *                                  of the fields to be added. The properties of the fields should
  124.      *                                  be the same as defined by the Metabase parser.
  125.      *
  126.      *                                 Additionally, there should be an entry named Declaration that
  127.      *                                  is expected to contain the portion of the field declaration already
  128.      *                                  in DBMS specific SQL code as it is used in the CREATE TABLE statement.
  129.      *
  130.      *                             removed_fields
  131.      *
  132.      *                                 Associative array with the names of fields to be removed as indexes
  133.      *                                  of the array. Currently the values assigned to each entry are ignored.
  134.      *                                  An empty array should be used for future compatibility.
  135.      *
  136.      *                             renamed_fields
  137.      *
  138.      *                                 Associative array with the names of fields to be renamed as indexes
  139.      *                                  of the array. The value of each entry of the array should be set to
  140.      *                                  another associative array with the entry named name with the new
  141.      *                                  field name and the entry named Declaration that is expected to contain
  142.      *                                  the portion of the field declaration already in DBMS specific SQL code
  143.      *                                  as it is used in the CREATE TABLE statement.
  144.      *
  145.      *                             changed_fields
  146.      *
  147.      *                                 Associative array with the names of the fields to be changed as indexes
  148.      *                                  of the array. Keep in mind that if it is intended to change either the
  149.      *                                  name of a field and any other properties, the changed_fields array entries
  150.      *                                  should have the new names of the fields as array indexes.
  151.      *
  152.      *                                 The value of each entry of the array should be set to another associative
  153.      *                                  array with the properties of the fields to that are meant to be changed as
  154.      *                                  array entries. These entries should be assigned to the new values of the
  155.      *                                  respective properties. The properties of the fields should be the same
  156.      *                                  as defined by the Metabase parser.
  157.      *
  158.      *                                 If the default property is meant to be added, removed or changed, there
  159.      *                                  should also be an entry with index ChangedDefault assigned to 1. Similarly,
  160.      *                                  if the notnull constraint is to be added or removed, there should also be
  161.      *                                  an entry with index ChangedNotNull assigned to 1.
  162.      *
  163.      *                                 Additionally, there should be an entry named Declaration that is expected
  164.      *                                  to contain the portion of the field changed declaration already in DBMS
  165.      *                                  specific SQL code as it is used in the CREATE TABLE statement.
  166.      *                             Example
  167.      *                                 array(
  168.      *                                     'name' => 'userlist',
  169.      *                                     'added_fields' => array(
  170.      *                                         'quota' => array(
  171.      *                                             'type' => 'integer',
  172.      *                                             'unsigned' => 1
  173.      *                                             'declaration' => 'quota INT'
  174.      *                                         )
  175.      *                                     ),
  176.      *                                     'removed_fields' => array(
  177.      *                                         'file_limit' => array(),
  178.      *                                         'time_limit' => array()
  179.      *                                         ),
  180.      *                                     'changed_fields' => array(
  181.      *                                         'gender' => array(
  182.      *                                             'default' => 'M',
  183.      *                                             'change_default' => 1,
  184.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  185.      *                                         )
  186.      *                                     ),
  187.      *                                     'renamed_fields' => array(
  188.      *                                         'sex' => array(
  189.      *                                             'name' => 'gender',
  190.      *                                             'declaration' => "gender CHAR(1) DEFAULT 'M'"
  191.      *                                         )
  192.      *                                     )
  193.      *                                 )
  194.      *
  195.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  196.      *                              can perform the requested table alterations if the value is true or
  197.      *                              actually perform them otherwise.
  198.      * @access public
  199.      *
  200.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  201.      */
  202.     function alterTable($name$changes$check)
  203.     {
  204.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  205.         if ($check{
  206.             for ($change = 0reset($changes);
  207.                 $change count($changes);
  208.                 next($changes)$change++)
  209.             {
  210.                 switch (key($changes)) {
  211.                     case 'added_fields':
  212.                         break;
  213.                     case 'removed_fields':
  214.                     case 'name':
  215.                     case 'renamed_fields':
  216.                     case 'changed_fields':
  217.                     default:
  218.                         return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  219.                             'alterTable: change type "'.key($changes).'" not yet supported');
  220.                 }
  221.             }
  222.             return MDB2_OK;
  223.         else {
  224.             if (isset($changes[$change 'removed_fields'])
  225.                 || isset($changes[$change 'name'])
  226.                 || isset($changes[$change 'renamed_fields'])
  227.                 || isset($changes[$change 'changed_fields']))
  228.             {
  229.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  230.                     'alterTable: change type "'.$change.'" is not supported by the server"');
  231.             }
  232.             $query='';
  233.             if (isset($changes['added_fields'])) {
  234.                 if (strcmp($query'')) {
  235.                     $query.= ', ';
  236.                 }
  237.                 $query.= 'ADD ';
  238.                 $fields $changes['added_fields'];
  239.                 for ($field = 0reset($fields);
  240.                     $field count($fields);
  241.                     next($fields)$field++)
  242.                 {
  243.                     if (strcmp($query'')) {
  244.                         $query.= ', ';
  245.                     }
  246.                     $query.= $fields[key($fields)]['declaration'];
  247.                 }
  248.             }
  249.             return strcmp($query''$db->query("ALTER TABLE $name $query"MDB2_OK;
  250.         }
  251.     }
  252.  
  253.     // }}}
  254.     // {{{ listTables()
  255.  
  256.     /**
  257.      * list all tables in the current database
  258.      *
  259.      * @return mixed data array on success, a MDB error on failure
  260.      * @access public
  261.      ***/
  262.     function listTables(&$db)
  263.     {
  264.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  265.         $query 'EXECUTE sp_tables @table_type = "\'TABLE\'"';
  266.         $table_names $db->queryCol($querynull2);
  267.         if (MDB::isError($table_names)) {
  268.             return($table_names);
  269.         }
  270.         $tables = array();
  271.         for ($i = 0$j count($table_names)$i <$j; ++$i{
  272.             if (!$this->_isSequenceName($db$table_names[$i])) {
  273.                 $tables[$table_names[$i];
  274.             }
  275.         }
  276.         return($tables);
  277.     }
  278.  
  279.     // }}}
  280.     // {{{ listTableFields()
  281.  
  282.     /**
  283.      * list all fields in a tables in the current database
  284.      *
  285.      * @param string $table name of table that should be used in method
  286.      * @return mixed data array on success, a MDB error on failure
  287.      * @access public
  288.      */
  289.     function listTableFields($table)
  290.     {
  291.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  292.         $result $db->query("SELECT * FROM $table");
  293.         if (MDB::isError($result)) {
  294.             return($result);
  295.         }
  296.         $columns $db->getColumnNames($result);
  297.         if (MDB::isError($columns)) {
  298.             $db->freeResult($columns);
  299.             return $columns;
  300.         }
  301.         return(array_flip($columns));
  302.     }
  303.  
  304.     // }}}
  305.     // {{{ createSequence()
  306.  
  307.     /**
  308.      * create sequence
  309.      *
  310.      * @param string    $seq_name     name of the sequence to be created
  311.      * @param string    $start         start value of the sequence; default is 1
  312.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  313.      * @access public
  314.      */
  315.     function createSequence($seq_name$start = 1)
  316.     {
  317.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  318.         $sequence_name $db->getSequenceName($seq_name);
  319.         $query = "CREATE TABLE $sequence_name (".$db->options['seqname_col_name']." INT NOT NULL IDENTITY($start,1) PRIMARY KEY CLUSTERED)";
  320.         return $db->query($query);
  321.     }
  322.  
  323.     // }}}
  324.     // {{{ dropSequence()
  325.  
  326.     /**
  327.      * drop existing sequence
  328.      *
  329.      * @param string    $seq_name     name of the sequence to be dropped
  330.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  331.      * @access public
  332.      */
  333.     function dropSequence($seq_name)
  334.     {
  335.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  336.         $sequence_name $db->getSequenceName($seq_name);
  337.         return $db->Query("DROP TABLE $sequence_name");
  338.     }
  339. }
  340. ?>

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