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.82 2006/12/05 13:08:30 quipo Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Manager/Common.php';
  49.  
  50. // {{{ class MDB2_Driver_Manager_mssql
  51.  
  52. /**
  53.  * MDB2 MSSQL driver for the management modules
  54.  *
  55.  * @package MDB2
  56.  * @category Database
  57.  * @author  Frank M. Kromann <frank@kromann.info>
  58.  * @author  David Coallier <davidc@php.net>
  59.  */
  60. class MDB2_Driver_Manager_mssql extends MDB2_Driver_Manager_Common
  61. {
  62.     // {{{ createDatabase()
  63.     /**
  64.      * create a new database
  65.      *
  66.      * @param string $name name of the database that should be created
  67.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  68.      * @access public
  69.      */
  70.     function createDatabase($name)
  71.     {
  72.         $db =$this->getDBInstance();
  73.         if (PEAR::isError($db)) {
  74.             return $db;
  75.         }
  76.  
  77.         $name $db->quoteIdentifier($nametrue);
  78.         $query = "CREATE DATABASE $name";
  79.         if ($db->options['database_device']{
  80.             $query.= ' ON '.$db->options['database_device'];
  81.             $query.= $db->options['database_size''=' .
  82.                      $db->options['database_size''';
  83.         }
  84.         return $db->standaloneQuery($querynulltrue);
  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 =$this->getDBInstance();
  100.         if (PEAR::isError($db)) {
  101.             return $db;
  102.         }
  103.  
  104.         $name $db->quoteIdentifier($nametrue);
  105.         return $db->standaloneQuery("DROP DATABASE $name"nulltrue);
  106.     }
  107.  
  108.     // }}}
  109.     // {{{ alterTable()
  110.  
  111.     /**
  112.      * alter an existing table
  113.      *
  114.      * @param string $name         name of the table that is intended to be changed.
  115.      * @param array $changes     associative array that contains the details of each type
  116.      *                              of change that is intended to be performed. The types of
  117.      *                              changes that are currently supported are defined as follows:
  118.      *
  119.      *                              name
  120.      *
  121.      *                                 New name for the table.
  122.      *
  123.      *                             add
  124.      *
  125.      *                                 Associative array with the names of fields to be added as
  126.      *                                  indexes of the array. The value of each entry of the array
  127.      *                                  should be set to another associative array with the properties
  128.      *                                  of the fields to be added. The properties of the fields should
  129.      *                                  be the same as defined by the MDB2 parser.
  130.      *
  131.      *
  132.      *                             remove
  133.      *
  134.      *                                 Associative array with the names of fields to be removed as indexes
  135.      *                                  of the array. Currently the values assigned to each entry are ignored.
  136.      *                                  An empty array should be used for future compatibility.
  137.      *
  138.      *                             rename
  139.      *
  140.      *                                 Associative array with the names of fields to be renamed as indexes
  141.      *                                  of the array. The value of each entry of the array should be set to
  142.      *                                  another associative array with the entry named name with the new
  143.      *                                  field name and the entry named Declaration that is expected to contain
  144.      *                                  the portion of the field declaration already in DBMS specific SQL code
  145.      *                                  as it is used in the CREATE TABLE statement.
  146.      *
  147.      *                             change
  148.      *
  149.      *                                 Associative array with the names of the fields to be changed as indexes
  150.      *                                  of the array. Keep in mind that if it is intended to change either the
  151.      *                                  name of a field and any other properties, the change array entries
  152.      *                                  should have the new names of the fields as array indexes.
  153.      *
  154.      *                                 The value of each entry of the array should be set to another associative
  155.      *                                  array with the properties of the fields to that are meant to be changed as
  156.      *                                  array entries. These entries should be assigned to the new values of the
  157.      *                                  respective properties. The properties of the fields should be the same
  158.      *                                  as defined by the MDB2 parser.
  159.      *
  160.      *                             Example
  161.      *                                 array(
  162.      *                                     'name' => 'userlist',
  163.      *                                     'add' => array(
  164.      *                                         'quota' => array(
  165.      *                                             'type' => 'integer',
  166.      *                                             'unsigned' => 1
  167.      *                                         )
  168.      *                                     ),
  169.      *                                     'remove' => array(
  170.      *                                         'file_limit' => array(),
  171.      *                                         'time_limit' => array()
  172.      *                                     ),
  173.      *                                     'change' => array(
  174.      *                                         'name' => array(
  175.      *                                             'length' => '20',
  176.      *                                             'definition' => array(
  177.      *                                                 'type' => 'text',
  178.      *                                                 'length' => 20,
  179.      *                                             ),
  180.      *                                         )
  181.      *                                     ),
  182.      *                                     'rename' => array(
  183.      *                                         'sex' => array(
  184.      *                                             'name' => 'gender',
  185.      *                                             'definition' => array(
  186.      *                                                 'type' => 'text',
  187.      *                                                 'length' => 1,
  188.      *                                                 'default' => 'M',
  189.      *                                             ),
  190.      *                                         )
  191.      *                                     )
  192.      *                                 )
  193.      *
  194.      * @param boolean $check     indicates whether the function should just check if the DBMS driver
  195.      *                              can perform the requested table alterations if the value is true or
  196.      *                              actually perform them otherwise.
  197.      * @access public
  198.      *
  199.       * @return mixed MDB2_OK on success, a MDB2 error on failure
  200.      */
  201.     function alterTable($name$changes$check)
  202.     {
  203.         $db =$this->getDBInstance();
  204.         if (PEAR::isError($db)) {
  205.             return $db;
  206.         }
  207.  
  208.         foreach ($changes as $change_name => $change{
  209.             switch ($change_name{
  210.             case 'add':
  211.                 break;
  212.             case 'remove':
  213.                 break;
  214.             case 'name':
  215.             case 'rename':
  216.             case 'change':
  217.             default:
  218.                 return $db->raiseError(MDB2_ERROR_CANNOT_ALTERnullnull,
  219.                     'change type "'.$change_name.'" not yet supported'__FUNCTION__);
  220.             }
  221.         }
  222.  
  223.         if ($check{
  224.             return MDB2_OK;
  225.         }
  226.  
  227.         $query '';
  228.         if (!empty($changes['add']&& is_array($changes['add'])) {
  229.             foreach ($changes['add'as $field_name => $field{
  230.                 if ($query{
  231.                     $query.= ', ';
  232.                 else {
  233.                     $query.= 'ADD COLUMN ';
  234.                 }
  235.                 $query.= $db->getDeclaration($field['type']$field_name$field);
  236.             }
  237.         }
  238.  
  239.         if (!empty($changes['remove']&& is_array($changes['remove'])) {
  240.             foreach ($changes['remove'as $field_name => $field{
  241.                 if ($query{
  242.                     $query.= ', ';
  243.                 }
  244.                 $field_name $db->quoteIdentifier($field_nametrue);
  245.                 $query.= 'DROP COLUMN ' $field_name;
  246.             }
  247.         }
  248.  
  249.         if (!$query{
  250.             return MDB2_OK;
  251.         }
  252.  
  253.         $name $db->quoteIdentifier($nametrue);
  254.         return $db->exec("ALTER TABLE $name $query");
  255.     }
  256.  
  257.     // }}}
  258.     // {{{ listTables()
  259.  
  260.     /**
  261.      * list all tables in the current database
  262.      *
  263.      * @return mixed array of table names on success, a MDB2 error on failure
  264.      * @access public
  265.      */
  266.     function listTables()
  267.     {
  268.         $db =$this->getDBInstance();
  269.  
  270.         if (PEAR::isError($db)) {
  271.             return $db;
  272.         }
  273.  
  274.         $query 'EXEC sp_tables @table_type = "\'TABLE\'"';
  275.         $table_names $db->queryCol($querynull2);
  276.         if (PEAR::isError($table_names)) {
  277.             return $table_names;
  278.         }
  279.         $result = array();
  280.         foreach ($table_names as $table_name{
  281.             if (!$this->_fixSequenceName($table_nametrue)) {
  282.                 $result[$table_name;
  283.             }
  284.         }
  285.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  286.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  287.                         'strtolower' 'strtoupper')$result);
  288.         }
  289.         return $result;
  290.     }
  291.  
  292.     // }}}
  293.     // {{{ listTableFields()
  294.  
  295.     /**
  296.      * list all fields in a table in the current database
  297.      *
  298.      * @param string $table name of table that should be used in method
  299.      * @return mixed array of field names on success, a MDB2 error on failure
  300.      * @access public
  301.      */
  302.     function listTableFields($table)
  303.     {
  304.         $db =$this->getDBInstance();
  305.         if (PEAR::isError($db)) {
  306.             return $db;
  307.         }
  308.  
  309.         $table $db->quoteIdentifier($tabletrue);
  310.         $db->setLimit(1);
  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 array of index names 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.         $table $db->quote($table'text');
  352.         $query = "EXEC sp_statistics @table_name=$table";
  353.         $indexes $db->queryCol($query'text'$key_name);
  354.         if (PEAR::isError($indexes)) {
  355.             return $indexes;
  356.         }
  357.         $query = "EXEC sp_pkeys @table_name=$table";
  358.         $pk_all $db->queryCol($query'text'$pk_name);
  359.         $result = array();
  360.         foreach ($indexes as $index{
  361.             if (!in_array($index$pk_all&& ($index $this->_fixIndexName($index))) {
  362.                 $result[$index= true;
  363.             }
  364.         }
  365.  
  366.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  367.             $result array_change_key_case($result$db->options['field_case']);
  368.         }
  369.         return array_keys($result);
  370.     }
  371.  
  372.     // }}}
  373.     // {{{ listTableTriggers()
  374.  
  375.     /**
  376.      * list all triggers in the database that reference a given table
  377.      *
  378.      * @param string table for which all referenced triggers should be found
  379.      * @return mixed array of trigger names on success,  otherwise, false which
  380.      *                could be a db error if the db is not instantiated or could
  381.      *                be the results of the error that occured during the
  382.      *                querying of the sysobject module.
  383.      * @access public
  384.      */
  385.     function listTableTriggers($table = null)
  386.     {
  387.         $db =$this->getDBInstance();
  388.         if (PEAR::isError($db)) {
  389.             return $db;
  390.         }
  391.  
  392.         $table $db->quote($table'text');
  393.         $query "SELECT name FROM sysobjects WHERE xtype = 'TR'";
  394.         if (!is_null($table)) {
  395.             $query .= "AND object_name(parent_obj) = $table";
  396.         }
  397.  
  398.         $result $db->queryCol($query);
  399.         if (PEAR::isError($result)) {
  400.             return $result;
  401.         }
  402.  
  403.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE &&
  404.             $db->options['field_case'== CASE_LOWER)
  405.         {
  406.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  407.                 'strtolower' 'strtoupper')$result);
  408.         }
  409.         return $result;
  410.     }
  411.  
  412.     // }}}
  413.     // {{{ listViews()
  414.  
  415.     /**
  416.      * list all views in the current database
  417.      *
  418.      * @param string database, the current is default
  419.      * @return mixed array of view names on success, a MDB2 error on failure
  420.      * @access public
  421.      */
  422.     function listViews()
  423.     {
  424.         $db =$this->getDBInstance();
  425.         if (PEAR::isError($db)) {
  426.             return $db;
  427.         }
  428.  
  429.         $query "SELECT name
  430.                    FROM sysobjects
  431.                     WHERE xtype = 'V'";
  432.  
  433.         $result $db->queryCol($query);
  434.         if (PEAR::isError($result)) {
  435.             return $result;
  436.         }
  437.  
  438.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE &&
  439.             $db->options['field_case'== CASE_LOWER)
  440.         {
  441.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  442.                           'strtolower' 'strtoupper')$result);
  443.         }
  444.         return $result;
  445.     }
  446.  
  447.     // }}}
  448.     // {{{ dropIndex()
  449.  
  450.     /**
  451.      * drop existing index
  452.      *
  453.      * @param string    $table         name of table that should be used in method
  454.      * @param string    $name         name of the index to be dropped
  455.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  456.      * @access public
  457.      */
  458.     function dropIndex($table$name)
  459.     {
  460.         $db =$this->getDBInstance();
  461.         if (PEAR::isError($db)) {
  462.             return $db;
  463.         }
  464.  
  465.         $table $db->quoteIdentifier($tabletrue);
  466.         $name $db->quoteIdentifier($db->getIndexName($name)true);
  467.         return $db->exec("DROP INDEX $table.$name");
  468.     }
  469.  
  470.     // }}}
  471.     // {{{ createSequence()
  472.  
  473.     /**
  474.      * create sequence
  475.      *
  476.      * @param string    $seq_name     name of the sequence to be created
  477.      * @param string    $start         start value of the sequence; default is 1
  478.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  479.      * @access public
  480.      */
  481.     function createSequence($seq_name$start = 1)
  482.     {
  483.         $db =$this->getDBInstance();
  484.         if (PEAR::isError($db)) {
  485.             return $db;
  486.         }
  487.  
  488.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  489.         $seqcol_name $db->quoteIdentifier($db->options['seqcol_name']true);
  490.         $query = "CREATE TABLE $sequence_name ($seqcol_name " .
  491.                  "INT PRIMARY KEY CLUSTERED IDENTITY($start,1) NOT NULL)";
  492.  
  493.         $res $db->exec($query);
  494.         if (PEAR::isError($res)) {
  495.             return $res;
  496.         }
  497.  
  498.         if ($start == 1{
  499.             return MDB2_OK;
  500.         }
  501.  
  502.  
  503.         $query = "SET IDENTITY_INSERT $sequence_name ON ".
  504.                  "INSERT INTO $sequence_name ($seqcol_name) VALUES ($start)";
  505.         $res $db->exec($query);
  506.  
  507.         if (!PEAR::isError($res)) {
  508.             return MDB2_OK;
  509.         }
  510.  
  511.         $result $db->exec("DROP TABLE $sequence_name");
  512.         if (PEAR::isError($result)) {
  513.             return $db->raiseError($resultnullnull,
  514.                 'could not drop inconsistent sequence table'__FUNCTION__);
  515.         }
  516.  
  517.         return $db->raiseError($resnullnull,
  518.             'could not create sequence table'__FUNCTION__);
  519.     }
  520.  
  521.     // }}}
  522.     // {{{ dropSequence()
  523.  
  524.     /**
  525.      * This function drops an existing sequence
  526.      *
  527.      * @param string $seq_name name of the sequence to be dropped
  528.      * @return mixed MDB2_OK on success, a MDB2 error on failure
  529.      * @access public
  530.      */
  531.     function dropSequence($seq_name)
  532.     {
  533.         $db =$this->getDBInstance();
  534.         if (PEAR::isError($db)) {
  535.             return $db;
  536.         }
  537.  
  538.         $sequence_name $db->quoteIdentifier($db->getSequenceName($seq_name)true);
  539.         return $db->exec("DROP TABLE $sequence_name");
  540.     }
  541.  
  542.     // }}}
  543.     // {{{ listSequences()
  544.  
  545.     /**
  546.      * list all sequences in the current database
  547.      *
  548.      * @return mixed array of sequence names on success, a MDB2 error on failure
  549.      * @access public
  550.      */
  551.     function listSequences()
  552.     {
  553.         $db =$this->getDBInstance();
  554.         if (PEAR::isError($db)) {
  555.             return $db;
  556.         }
  557.  
  558.         $query "SELECT name FROM sysobjects WHERE xtype = 'U'";
  559.         $table_names $db->queryCol($query);
  560.         if (PEAR::isError($table_names)) {
  561.             return $table_names;
  562.         }
  563.         $result = array();
  564.         foreach ($table_names as $table_name{
  565.             if ($sqn $this->_fixSequenceName($table_nametrue)) {
  566.                 $result[$sqn;
  567.             }
  568.         }
  569.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  570.             $result array_map(($db->options['field_case'== CASE_LOWER ?
  571.                           'strtolower' 'strtoupper')$result);
  572.         }
  573.         return $result;
  574.     }
  575.  
  576.     // }}}
  577. }
  578.  
  579. // }}}
  580. ?>

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