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.57 2006/02/09 12:45:30 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49. // {{{ class MDB2_Driver_Manager_mssql
  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. class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
  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 =$this->getDBInstance();
  71.         if (PEAR::isError($db)) {
  72.             return $db;
  73.         }
  74.  
  75.         $name $db->quoteIdentifier($nametrue);
  76.         $query = "CREATE DATABASE $name";
  77.         if ($db->options['database_device']{
  78.             $query.= ' ON '.$db->options['database_device'];
  79.             $query.= $db->options['database_size''='.$db->options['database_size''';
  80.         }
  81.         return $db->standaloneQuery($querynulltrue);
  82.     }
  83.  
  84.     // }}}
  85.     // {{{ dropDatabase()
  86.  
  87.     /**
  88.      * drop an existing database
  89.      *
  90.      * @param string $name name of the database that should be dropped
  91.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  92.      * @access public
  93.      */
  94.     function dropDatabase($name)
  95.     {
  96.         $db =$this->getDBInstance();
  97.         if (PEAR::isError($db)) {
  98.             return $db;
  99.         }
  100.  
  101.         $name $db->quoteIdentifier($nametrue);
  102.         return $db->standaloneQuery("DROP DATABASE $name"nulltrue);
  103.     }
  104.  
  105.     // }}}
  106.     // {{{ alterTable()
  107.  
  108.     /**
  109.      * alter an existing table
  110.      *
  111.      * @param string $name         name of the table that is intended to be changed.
  112.      * @param array $changes     associative array that contains the details of each type
  113.      *                              of change that is intended to be performed. The types of
  114.      *                              changes that are currently supported are defined as follows:
  115.      *
  116.      *                              name
  117.      *
  118.      *                                 New name for the table.
  119.      *
  120.      *                             add
  121.      *
  122.      *                                 Associative array with the names of fields to be added as
  123.      *                                  indexes of the array. The value of each entry of the array
  124.      *                                  should be set to another associative array with the properties
  125.      *                                  of the fields to be added. The properties of the fields should
  126.      *                                  be the same as defined by the Metabase parser.
  127.      *
  128.      *
  129.      *                             remove
  130.      *
  131.      *                                 Associative array with the names of fields to be removed as indexes
  132.      *                                  of the array. Currently the values assigned to each entry are ignored.
  133.      *                                  An empty array should be used for future compatibility.
  134.      *
  135.      *                             rename
  136.      *
  137.      *                                 Associative array with the names of fields to be renamed as indexes
  138.      *                                  of the array. The value of each entry of the array should be set to
  139.      *                                  another associative array with the entry named name with the new
  140.      *                                  field name and the entry named Declaration that is expected to contain
  141.      *                                  the portion of the field declaration already in DBMS specific SQL code
  142.      *                                  as it is used in the CREATE TABLE statement.
  143.      *
  144.      *                             change
  145.      *
  146.      *                                 Associative array with the names of the fields to be changed as indexes
  147.      *                                  of the array. Keep in mind that if it is intended to change either the
  148.      *                                  name of a field and any other properties, the change array entries
  149.      *                                  should have the new names of the fields as array indexes.
  150.      *
  151.      *                                 The value of each entry of the array should be set to another associative
  152.      *                                  array with the properties of the fields to that are meant to be changed as
  153.      *                                  array entries. These entries should be assigned to the new values of the
  154.      *                                  respective properties. The properties of the fields should be the same
  155.      *                                  as defined by the Metabase parser.
  156.      *
  157.      *                             Example
  158.      *                                 array(
  159.      *                                     'name' => 'userlist',
  160.      *                                     'add' => array(
  161.      *                                         'quota' => array(
  162.      *                                             'type' => 'integer',
  163.      *                                             'unsigned' => 1
  164.      *                                         )
  165.      *                                     ),
  166.      *                                     'remove' => array(
  167.      *                                         'file_limit' => array(),
  168.      *                                         'time_limit' => array()
  169.      *                                     ),
  170.      *                                     'change' => array(
  171.      *                                         'name' => array(
  172.      *                                             'length' => '20',
  173.      *                                             'definition' => array(
  174.      *                                                 'type' => 'text',
  175.      *                                                 'length' => 20,
  176.      *                                             ),
  177.      *                                         )
  178.      *                                     ),
  179.      *                                     'rename' => array(
  180.      *                                         'sex' => array(
  181.      *                                             'name' => 'gender',
  182.      *                                             'definition' => array(
  183.      *                                                 'type' => 'text',
  184.      *                                                 'length' => 1,
  185.      *                                                 'default' => 'M',
  186.      *                                             ),
  187.      *                                         )
  188.      *                                     )
  189.      *                                 )
  190.      *
  191.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  192.      *                              can perform the requested table alterations if the value is true or
  193.      *                              actually perform them otherwise.
  194.      * @access public
  195.      *
  196.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  197.      */
  198.     function alterTable($name$changes$check)
  199.     {
  200.         $db =$this->getDBInstance();
  201.         if (PEAR::isError($db)) {
  202.             return $db;
  203.         }
  204.  
  205.         foreach ($changes as $change_name => $change{
  206.             switch ($change_name{
  207.             case 'add':
  208.                 break;
  209.             case 'remove':
  210.                 break;
  211.             case 'name':
  212.             case 'rename':
  213.             case 'change':
  214.             default:
  215.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  216.                     'alterTable: change type "'.$change_name.'" not yet supported');
  217.             }
  218.         }
  219.  
  220.         if ($check{
  221.             return MDB2_OK;
  222.         }
  223.  
  224.         $query '';
  225.         if (array_key_exists('add'$changes)) {
  226.             if ($query{
  227.                 $query.= ', ';
  228.             }
  229.             $query.= 'ADD ';
  230.             foreach ($changes['add'as $field_name => $field{
  231.                 if ($query{
  232.                     $query.= ', ';
  233.                 }
  234.                 $query.= $db->getDeclaration($field['type']$field_name$field);
  235.             }
  236.         }
  237.         if (array_key_exists('remove'$changes)) {
  238.             if ($query{
  239.             $query.= ', ';
  240.             }
  241.             $query.= 'DROP COLUMN';
  242.             foreach ($changes['remove'as $field_name => $field{
  243.                 if ($query{
  244.                     $query.= ', ';
  245.                 }
  246.                 // todo: this looks wrong ..
  247.                 $query.= $db->getDeclaration($field['type']$field_name$field);
  248.             }
  249.         }
  250.  
  251.         if (!$query{
  252.             return MDB2_OK;
  253.         }
  254.  
  255.         $name $db->quoteIdentifier($nametrue);
  256.         return $db->exec("ALTER TABLE $name $query");
  257.     }
  258.  
  259.     // }}}
  260.     // {{{ listTables()
  261.  
  262.     /**
  263.      * list all tables in the current database
  264.      *
  265.      * @return mixed data array on success, a MDB2 error on failure
  266.      * @access public
  267.      */
  268.     function listTables()
  269.     {
  270.         $db =$this->getDBInstance();
  271.  
  272.         if (PEAR::isError($db)) {
  273.             return $db;
  274.         }
  275.  
  276.         $query 'EXEC sp_tables @table_type = "\'TABLE\'"';
  277.         $table_names $db->queryCol($querynull2);
  278.         if (PEAR::isError($table_names)) {
  279.             return $table_names;
  280.         }
  281.         $result = array();
  282.         foreach ($table_names as $table_name{
  283.             if (!$this->_fixSequenceName($table_nametrue)) {
  284.                 $result[$table_name;
  285.             }
  286.         }
  287.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  288.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  289.         }
  290.         return $result;
  291.     }
  292.  
  293.     // }}}
  294.     // {{{ listTableFields()
  295.  
  296.     /**
  297.      * list all fields in a tables in the current database
  298.      *
  299.      * @param string $table name of table that should be used in method
  300.      * @return mixed data array on success, a MDB2 error on failure
  301.      * @access public
  302.      */
  303.     function listTableFields($table)
  304.     {
  305.         $db =$this->getDBInstance();
  306.         if (PEAR::isError($db)) {
  307.             return $db;
  308.         }
  309.  
  310.         $table $db->quoteIdentifier($tabletrue);
  311.         $result2 $db->query("SELECT * FROM $table");
  312.         if (PEAR::isError($result2)) {
  313.             return $result2;
  314.         }
  315.         $result $result2->getColumnNames();
  316.         $result2->free();
  317.         if (PEAR::isError($result)) {
  318.             return $result;
  319.         }
  320.         return array_flip($result);
  321.     }
  322.  
  323.     // }}}
  324.     // {{{ listTableIndexes()
  325.  
  326.     /**
  327.      * list all indexes in a table
  328.      *
  329.      * @param string    $table     name of table that should be used in method
  330.      * @return mixed data array on success, a MDB2 error on failure
  331.      * @access public
  332.      */
  333.     function listTableIndexes($table)
  334.     {
  335.         $db =$this->getDBInstance();
  336.         if (PEAR::isError($db)) {
  337.             return $db;
  338.         }
  339.  
  340.         $key_name 'INDEX_NAME';
  341.         $pk_name 'PK_NAME';
  342.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  343.             if ($db->options['field_case'== CASE_LOWER{
  344.                 $key_name strtolower($key_name);
  345.                 $pk_name  strtolower($pk_name);
  346.             else {
  347.                 $key_name strtoupper($key_name);
  348.                 $pk_name  strtoupper($pk_name);
  349.             }
  350.         }
  351.         $query = "EXEC sp_statistics @table_name='$table'";
  352.         $indexes $db->queryCol($query'text'$key_name);
  353.         if (PEAR::isError($indexes)) {
  354.             return $indexes;
  355.         }
  356.         $query = "EXEC sp_pkeys @table_name='$table'";
  357.         $pk_all $db->queryCol($query'text'$pk_name);
  358.         $result = array();
  359.         foreach ($indexes as $index{
  360.             if (!in_array($index$pk_all&& $index != null{
  361.                 $result[$this->_fixIndexName($index)= true;
  362.             }
  363.         }
  364.  
  365.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  366.             $result array_change_key_case($result$db->options['field_case']);
  367.         }
  368.         return array_keys($result);
  369.     }
  370.  
  371.     // }}}
  372.     // {{{ createSequence()
  373.  
  374.     /**
  375.      * create sequence
  376.      *
  377.      * @param string    $seq_name     name of the sequence to be created
  378.      * @param string    $start         start value of the sequence; default is 1
  379.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  380.      * @access public
  381.      */
  382.     function createSequence($seq_name$start = 1)
  383.     {
  384.         $db =$this->getDBInstance();
  385.         if (PEAR::isError($db)) {
  386.             return $db;
  387.         }
  388.  
  389.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  390.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  391.         $query = "CREATE TABLE $sequence_name ($seqcol_name " .
  392.                  "INT PRIMARY KEY CLUSTERED IDENTITY($start,1) NOT NULL)";
  393.  
  394.         $res $db->exec($query);
  395.         if (PEAR::isError($res)) {
  396.             return $res;
  397.         }
  398.  
  399.         if ($start == 1{
  400.             return MDB2_OK;
  401.         }
  402.  
  403.         $query = "SET IDENTITY_INSERT $sequence_name ON ".
  404.                  "INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)";
  405.         $res $db->exec($query);
  406.  
  407.         if (!PEAR::isError($res)) {
  408.             return MDB2_OK;
  409.         }
  410.  
  411.         $result $db->exec("DROP TABLE $sequence_name");
  412.         if (PEAR::isError($result)) {
  413.             return $db->raiseError(MDB2_ERRORnullnull,
  414.                    'createSequence: could not drop inconsistent sequence table ('.
  415.                    $result->getMessage().' ('.$result->getUserInfo()'))');
  416.         }
  417.  
  418.         return $db->raiseError(MDB2_ERRORnullnull,
  419.                'createSequence: could not create sequence table ('.
  420.                $res->getMessage()' ('.$res->getUserInfo()'))');
  421.     }
  422.  
  423.     // }}}
  424.     // {{{ dropSequence()
  425.  
  426.     /**
  427.      * drop existing sequence
  428.      *
  429.      * @param string    $seq_name     name of the sequence to be dropped
  430.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  431.      * @access public
  432.      */
  433.     function dropSequence($seq_name)
  434.     {
  435.         $db =$this->getDBInstance();
  436.         if (PEAR::isError($db)) {
  437.             return $db;
  438.         }
  439.  
  440.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  441.         return $db->exec("DROP TABLE $sequence_name");
  442.     }
  443.  
  444.     // }}}
  445.     // {{{ listSequences()
  446.  
  447.     /**
  448.      * list all sequences in the current database
  449.      *
  450.      * @return mixed data array on success, a MDB2 error on failure
  451.      * @access public
  452.      */
  453.     function listSequences()
  454.     {
  455.         $db =$this->getDBInstance();
  456.         if (PEAR::isError($db)) {
  457.             return $db;
  458.         }
  459.  
  460.         $query "SELECT name FROM sysobjects WHERE xtype = 'U'";
  461.         $table_names $db->queryCol($query);
  462.         if (PEAR::isError($table_names)) {
  463.             return $table_names;
  464.         }
  465.         $result = array();
  466.         foreach ($table_names as $table_name{
  467.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  468.                 $result[$sqn;
  469.             }
  470.         }
  471.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  472.             $result array_map(($db->options['field_case'== CASE_LOWER ? 'strtolower' 'strtoupper')$result);
  473.         }
  474.         return $result;
  475.     }
  476.     // }}}
  477. }
  478. // }}}
  479. ?>

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