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-2006 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.74 2006/05/31 14:38:06 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. // {{{ class MDB2_Driver_Manager_mssql
  51. /**
  52.  * MDB2 MSSQL driver for the management modules
  53.  *
  54.  * @package MDB2
  55.  * @category Database
  56.  * @author  Frank M. Kromann <frank@kromann.info>
  57.  * @author  David Coallier <davidc@php.net>
  58.  */
  59. class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
  60. {
  61.     // {{{ createDatabase()
  62.     /**
  63.      * create a new database
  64.      *
  65.      * @param string $name name of the database that should be created
  66.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  67.      * @access public
  68.      */
  69.     function createDatabase($name)
  70.     {
  71.         $db =$this->getDBInstance();
  72.         if (PEAR::isError($db)) {
  73.             return $db;
  74.         }
  75.  
  76.         $name $db->quoteIdentifier($nametrue);
  77.         $query = "CREATE DATABASE $name";
  78.         if ($db->options['database_device']{
  79.             $query.= ' ON '.$db->options['database_device'];
  80.             $query.= $db->options['database_size''=' .
  81.                      $db->options['database_size''';
  82.         }
  83.         return $db->standaloneQuery($querynulltrue);
  84.     }
  85.     // }}}
  86.     // {{{ dropDatabase()
  87.  
  88.     /**
  89.      * drop an existing database
  90.      *
  91.      * @param string $name name of the database that should be dropped
  92.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  93.      * @access public
  94.      */
  95.     function dropDatabase($name)
  96.     {
  97.         $db =$this->getDBInstance();
  98.         if (PEAR::isError($db)) {
  99.             return $db;
  100.         }
  101.  
  102.         $name $db->quoteIdentifier($nametrue);
  103.         return $db->standaloneQuery("DROP DATABASE $name"nulltrue);
  104.     }
  105.  
  106.     // }}}
  107.     // {{{ alterTable()
  108.  
  109.     /**
  110.      * alter an existing table
  111.      *
  112.      * @param string $name         name of the table that is intended to be changed.
  113.      * @param array $changes     associative array that contains the details of each type
  114.      *                              of change that is intended to be performed. The types of
  115.      *                              changes that are currently supported are defined as follows:
  116.      *
  117.      *                              name
  118.      *
  119.      *                                 New name for the table.
  120.      *
  121.      *                             add
  122.      *
  123.      *                                 Associative array with the names of fields to be added as
  124.      *                                  indexes of the array. The value of each entry of the array
  125.      *                                  should be set to another associative array with the properties
  126.      *                                  of the fields to be added. The properties of the fields should
  127.      *                                  be the same as defined by the Metabase parser.
  128.      *
  129.      *
  130.      *                             remove
  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.      *                             rename
  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.      *                             change
  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 change 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.      *                             Example
  159.      *                                 array(
  160.      *                                     'name' => 'userlist',
  161.      *                                     'add' => array(
  162.      *                                         'quota' => array(
  163.      *                                             'type' => 'integer',
  164.      *                                             'unsigned' => 1
  165.      *                                         )
  166.      *                                     ),
  167.      *                                     'remove' => array(
  168.      *                                         'file_limit' => array(),
  169.      *                                         'time_limit' => array()
  170.      *                                     ),
  171.      *                                     'change' => array(
  172.      *                                         'name' => array(
  173.      *                                             'length' => '20',
  174.      *                                             'definition' => array(
  175.      *                                                 'type' => 'text',
  176.      *                                                 'length' => 20,
  177.      *                                             ),
  178.      *                                         )
  179.      *                                     ),
  180.      *                                     'rename' => array(
  181.      *                                         'sex' => array(
  182.      *                                             'name' => 'gender',
  183.      *                                             'definition' => array(
  184.      *                                                 'type' => 'text',
  185.      *                                                 'length' => 1,
  186.      *                                                 'default' => 'M',
  187.      *                                             ),
  188.      *                                         )
  189.      *                                     )
  190.      *                                 )
  191.      *
  192.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  193.      *                              can perform the requested table alterations if the value is true or
  194.      *                              actually perform them otherwise.
  195.      * @access public
  196.      *
  197.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  198.      */
  199.     function alterTable($name$changes$check)
  200.     {
  201.         $db =$this->getDBInstance();
  202.         if (PEAR::isError($db)) {
  203.             return $db;
  204.         }
  205.  
  206.         foreach ($changes as $change_name => $change{
  207.             switch ($change_name{
  208.             case 'add':
  209.                 break;
  210.             case 'remove':
  211.                 break;
  212.             case 'name':
  213.             case 'rename':
  214.             case 'change':
  215.             default:
  216.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  217.                     'alterTable: change type "'.$change_name.'" not yet supported');
  218.             }
  219.         }
  220.  
  221.         if ($check{
  222.             return MDB2_OK;
  223.         }
  224.  
  225.         $query '';
  226.         if (!empty($changes['add']&& is_array($changes['add'])) {
  227.             foreach ($changes['add'as $field_name => $field{
  228.                 if ($query{
  229.                     $query.= ', ';
  230.                 }
  231.                 $query.= 'ADD ' $db->getDeclaration($field['type']$field_name$field);
  232.             }
  233.         }
  234.  
  235.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  236.             foreach ($changes['remove'as $field_name => $field{
  237.                 if ($query{
  238.                     $query.= ', ';
  239.                 }
  240.                 $field_name $db->quoteIdentifier($field_nametrue);
  241.                 $query.= 'DROP COLUMN ' $field_name;
  242.             }
  243.         }
  244.  
  245.         if (!$query{
  246.             return MDB2_OK;
  247.         }
  248.  
  249.         $name $db->quoteIdentifier($nametrue);
  250.         return $db->exec("ALTER TABLE $name $query");
  251.     }
  252.     // }}}
  253.     // {{{ listTables()
  254.     /**
  255.      * list all tables in the current database
  256.      *
  257.      * @return mixed data array on success, a MDB2 error on failure
  258.      * @access public
  259.      */
  260.     function listTables()
  261.     {
  262.         $db =$this->getDBInstance();
  263.  
  264.         if (PEAR::isError($db)) {
  265.             return $db;
  266.         }
  267.  
  268.         $query 'EXEC sp_tables @table_type = "\'TABLE\'"';
  269.         $table_names $db->queryCol($querynull2);
  270.         if (PEAR::isError($table_names)) {
  271.             return $table_names;
  272.         }
  273.         $result = array();
  274.         foreach ($table_names as $table_name{
  275.             if (!$this->_fixSequenceName($table_nametrue)) {
  276.                 $result[$table_name;
  277.             }
  278.         }
  279.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  280.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  281.                         'strtolower' 'strtoupper')$result);
  282.         }
  283.         return $result;
  284.     }
  285.     // }}}
  286.     // {{{ listTableFields()
  287.     /**
  288.      * list all fields in a tables in the current database
  289.      *
  290.      * @param string $table name of table that should be used in method
  291.      * @return mixed data array on success, a MDB2 error on failure
  292.      * @access public
  293.      */
  294.     function listTableFields($table)
  295.     {
  296.         $db =$this->getDBInstance();
  297.         if (PEAR::isError($db)) {
  298.             return $db;
  299.         }
  300.  
  301.         $table $db->quoteIdentifier($tabletrue);
  302.         $db->setLimit(1);
  303.         $result2 $db->query("SELECT * FROM $table");
  304.         if (PEAR::isError($result2)) {
  305.             return $result2;
  306.         }
  307.         $result $result2->getColumnNames();
  308.         $result2->free();
  309.         if (PEAR::isError($result)) {
  310.             return $result;
  311.         }
  312.         return array_flip($result);
  313.     }
  314.     // }}}
  315.     // {{{ listTableIndexes()
  316.  
  317.     /**
  318.      * list all indexes in a table
  319.      *
  320.      * @param string    $table     name of table that should be used in method
  321.      * @return mixed data array on success, a MDB2 error on failure
  322.      * @access public
  323.      */
  324.     function listTableIndexes($table)
  325.     {
  326.         $db =$this->getDBInstance();
  327.         if (PEAR::isError($db)) {
  328.             return $db;
  329.         }
  330.  
  331.         $key_name 'INDEX_NAME';
  332.         $pk_name 'PK_NAME';
  333.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  334.             if ($db->options['field_case'== CASE_LOWER{
  335.                 $key_name strtolower($key_name);
  336.                 $pk_name  strtolower($pk_name);
  337.             else {
  338.                 $key_name strtoupper($key_name);
  339.                 $pk_name  strtoupper($pk_name);
  340.             }
  341.         }
  342.         $table $db->quote($table'text');
  343.         $query = "EXEC sp_statistics @table_name=$table";
  344.         $indexes $db->queryCol($query'text'$key_name);
  345.         if (PEAR::isError($indexes)) {
  346.             return $indexes;
  347.         }
  348.         $query = "EXEC sp_pkeys @table_name=$table";
  349.         $pk_all $db->queryCol($query'text'$pk_name);
  350.         $result = array();
  351.         foreach ($indexes as $index{
  352.             if (!in_array($index$pk_all&& $index != null{
  353.                 $result[$this->_fixIndexName($index)= true;
  354.             }
  355.         }
  356.  
  357.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  358.             $result array_change_key_case($result$db->options['field_case']);
  359.         }
  360.         return array_keys($result);
  361.     }
  362.  
  363.     // }}}
  364.     // {{{ listTableTriggers()
  365.     /**
  366.      * This function will be called to
  367.      * display all the triggers from the current
  368.      * database ($db->getDatabase()).
  369.      *
  370.      * @access public
  371.      * @param  string $table      The name of the table from the
  372.      *                             previous database to query against.
  373.      * @return mixed Array of the triggers if the query
  374.      *                is successful, otherwise, false which
  375.      *                could be a db error if the db is not
  376.      *                instantiated or could be the results
  377.      *                of the error that occured during the
  378.      *                query'ing of the sysobject module.
  379.      */
  380.     function listTableTriggers($table = null)
  381.     {
  382.         $db =$this->getDBInstance();
  383.         if (PEAR::isError($db)) {
  384.             return $db;
  385.         }
  386.  
  387.         $table $db->quote($table'text');
  388.         $query "SELECT name FROM sysobjects WHERE xtype = 'TR'";
  389.         if (!is_null($table)) {
  390.             $query .= "AND object_name(parent_obj) = $table";
  391.         }
  392.  
  393.         $result $db->queryCol($query);
  394.         if (PEAR::isError($results)) {
  395.             return $result;
  396.         }
  397.  
  398.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE &&
  399.             $db->options['field_case'== CASE_LOWER)
  400.         {
  401.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  402.                 'strtolower' 'strtoupper')$result);
  403.         }
  404.         return $result;
  405.     }
  406.     // }}}
  407.     // {{{ listViews()
  408.     /**
  409.      * This function is a simple method that lists
  410.      * all the views that are set on a db instance
  411.      * (The db connected to it)
  412.      *
  413.      * @access public
  414.      * @return mixed Error on failure or array of views for a database.
  415.      */
  416.     function listViews()
  417.     {
  418.         $db =$this->getDBInstance();
  419.         if (PEAR::isError($db)) {
  420.             return $db;
  421.         }
  422.  
  423.         $query "SELECT name
  424.                    FROM sysobjects
  425.                     WHERE xtype = 'V'";
  426.  
  427.         $result $db->queryCol($query);
  428.         if (PEAR::isError($results)) {
  429.             return $result;
  430.         }
  431.  
  432.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE &&
  433.             $db->options['field_case'== CASE_LOWER)
  434.         {
  435.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  436.                           'strtolower' 'strtoupper')$result);
  437.         }
  438.         return $result;
  439.     }
  440.     // }}}
  441.     // {{{ createSequence()
  442.     /**
  443.      * create sequence
  444.      *
  445.      * @param string    $seq_name     name of the sequence to be created
  446.      * @param string    $start         start value of the sequence; default is 1
  447.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  448.      * @access public
  449.      */
  450.     function createSequence($seq_name$start = 1)
  451.     {
  452.         $db =$this->getDBInstance();
  453.         if (PEAR::isError($db)) {
  454.             return $db;
  455.         }
  456.  
  457.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  458.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  459.         $query = "CREATE TABLE $sequence_name ($seqcol_name " .
  460.                  "INT PRIMARY KEY CLUSTERED IDENTITY($start,1) NOT NULL)";
  461.  
  462.         $res $db->exec($query);
  463.         if (PEAR::isError($res)) {
  464.             return $res;
  465.         }
  466.  
  467.         if ($start == 1{
  468.             return MDB2_OK;
  469.         }
  470.  
  471.  
  472.         $query = "SET IDENTITY_INSERT $sequence_name ON ".
  473.                  "INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)";
  474.         $res $db->exec($query);
  475.  
  476.         if (!PEAR::isError($res)) {
  477.             return MDB2_OK;
  478.         }
  479.  
  480.         $result $db->exec("DROP TABLE $sequence_name");
  481.         if (PEAR::isError($result)) {
  482.             return $db->raiseError($resultnullnull,
  483.                 'createSequence: could not drop inconsistent sequence table');
  484.         }
  485.  
  486.         return $db->raiseError($resnullnull,
  487.             'createSequence: could not create sequence table');
  488.     }
  489.     // }}}
  490.     // {{{ dropSequence()
  491.     /**
  492.      * This function drops an existing sequence
  493.      *
  494.      * @param string $seq_name name of the sequence to be dropped
  495.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  496.      * @access public
  497.      */
  498.     function dropSequence($seq_name)
  499.     {
  500.         $db =$this->getDBInstance();
  501.         if (PEAR::isError($db)) {
  502.             return $db;
  503.         }
  504.  
  505.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  506.         return $db->exec("DROP TABLE $sequence_name");
  507.     }
  508.     // }}}
  509.     // {{{ listSequences()
  510.     /**
  511.      * list all sequences in the current database
  512.      *
  513.      * @return mixed data array on success, a MDB2 error on failure
  514.      * @access public
  515.      */
  516.     function listSequences()
  517.     {
  518.         $db =$this->getDBInstance();
  519.         if (PEAR::isError($db)) {
  520.             return $db;
  521.         }
  522.  
  523.         $query "SELECT name FROM sysobjects WHERE xtype = 'U'";
  524.         $table_names $db->queryCol($query);
  525.         if (PEAR::isError($table_names)) {
  526.             return $table_names;
  527.         }
  528.         $result = array();
  529.         foreach ($table_names as $table_name{
  530.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  531.                 $result[$sqn;
  532.             }
  533.         }
  534.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  535.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  536.                           'strtolower' 'strtoupper')$result);
  537.         }
  538.         return $result;
  539.     }
  540.     // }}}
  541. }
  542. // }}}
  543. ?>

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