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, Frank M. Kromann                       |
  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: Lukas Smith <smith@backendmedia.com>                         |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id: mssql.php,v 1.7 2004/03/10 14:12:12 lsmith Exp $
  46. //
  47.  
  48. require_once 'MDB2/Driver/Reverse/Common.php';
  49.  
  50. /**
  51.  * MDB2 MSSQL driver for the schema reverse engineering module
  52.  *
  53.  * @package MDB2
  54.  * @category Database
  55.  * @author  Lukas Smith <smith@dybnet.de>
  56.  */
  57. {
  58.     // }}}
  59.     // {{{ constructor
  60.  
  61.     /**
  62.      * Constructor
  63.      */
  64.     function MDB2_Driver_Reverse_mssql($db_index)
  65.     {
  66.         $this->MDB2_Driver_Reverse_Common($db_index);
  67.     }
  68.  
  69.     // }}}
  70.     // {{{ tableInfo()
  71.  
  72.     /**
  73.      * Returns information about a table or a result set.
  74.      *
  75.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  76.      * is a table name.
  77.      *
  78.      * @param object|string $result  MDB2_result object from a query or a
  79.      *                                 string containing the name of a table
  80.      * @param int            $mode    a valid tableInfo mode
  81.      * @return array  an associative array with the information requested
  82.      *                 or an error object if something is wrong
  83.      * @access public
  84.      * @internal
  85.      * @see MDB2_Driver_Common::tableInfo()
  86.      */
  87.     function tableInfo($result$mode = null)
  88.     {
  89.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  90.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  91.             $case_func 'strtolower';
  92.         else {
  93.             $case_func 'strval';
  94.         }
  95.  
  96.         if (is_string($result)) {
  97.             /*
  98.              * Probably received a table name.
  99.              * Create a result resource identifier.
  100.              */
  101.             if (MDB2::isError($connect $db->connect())) {
  102.                 return $connect;
  103.             }
  104.             $id @mssql_query("SELECT * FROM $result WHERE 1=0",
  105.                 $db->connection);
  106.             $got_string = true;
  107.         else {
  108.             /*
  109.              * Probably received a result object.
  110.              * Extract the result resource identifier.
  111.              */
  112.             $id $result->getResource();
  113.             if (empty($id)) {
  114.                 return $db->raiseError();
  115.             }
  116.             $got_string = false;
  117.         }
  118.  
  119.         if (!is_resource($id)) {
  120.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  121.         }
  122.  
  123.         $count @mssql_num_fields($id);
  124.  
  125.         // made this IF due to performance (one if is faster than $count if's)
  126.         if (!$mode{
  127.             for ($i=0; $i<$count$i++{
  128.                 $res[$i]['table'$got_string $case_func($result'';
  129.                 $res[$i]['name']  $case_func(@mssql_field_name($id$i));
  130.                 $res[$i]['type']  @mssql_field_type($id$i);
  131.                 $res[$i]['len']   @mssql_field_length($id$i);
  132.                 // We only support flags for tables
  133.                 $res[$i]['flags'$got_string $this->_mssql_field_flags($result$res[$i]['name']'';
  134.             }
  135.  
  136.         else // full
  137.             $res['num_fields']$count;
  138.  
  139.             for ($i=0; $i<$count$i++{
  140.                 $res[$i]['table'$got_string $case_func($result'';
  141.                 $res[$i]['name']  $case_func(@mssql_field_name($id$i));
  142.                 $res[$i]['type']  @mssql_field_type($id$i);
  143.                 $res[$i]['len']   @mssql_field_length($id$i);
  144.                 // We only support flags for tables
  145.                 $res[$i]['flags'$got_string $this->_mssql_field_flags($result$res[$i]['name']'';
  146.  
  147.                 if ($mode MDB2_TABLEINFO_ORDER{
  148.                     $res['order'][$res[$i]['name']] $i;
  149.                 }
  150.                 if ($mode MDB2_TABLEINFO_ORDERTABLE{
  151.                     $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  152.                 }
  153.             }
  154.         }
  155.  
  156.         // free the result only if we were called on a table
  157.         if ($got_string{
  158.             @mssql_free_result($id);
  159.         }
  160.         return $res;
  161.     }
  162.  
  163.  
  164.     // }}}
  165.     // {{{ _mssql_field_flags()
  166.  
  167.     /**
  168.      * Get the flags for a field, currently supports "not_null", "primary_key",
  169.      * "auto_increment" (mssql identity), "timestamp" (mssql timestamp),
  170.      * "unique_key" (mssql unique index, unique check or primary_key) and
  171.      * "multiple_key" (multikey index)
  172.      *
  173.      * mssql timestamp is NOT similar to the mysql timestamp so this is maybe
  174.      * not useful at all - is the behaviour of mysql_field_flags that primary
  175.      * keys are alway unique? is the interpretation of multiple_key correct?
  176.      *
  177.      * @param string The table name
  178.      * @param string The field
  179.      * @author Joern Barthel <j_barthel@web.de>
  180.      * @access private
  181.      */
  182.     function _mssql_field_flags($table$column)
  183.     {
  184.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  185.  
  186.         static $tableName = null;
  187.         static $flags = array();
  188.  
  189.         if ($table != $tableName{
  190.  
  191.             $flags = array();
  192.             $tableName $table;
  193.  
  194.             // get unique and primary keys
  195.             $res $db->queryAll("EXEC SP_HELPINDEX[$table]"nullMDB2_FETCHMODE_ASSOC);
  196.  
  197.             foreach ($res as $val{
  198.                 $keys explode(', '$val['index_keys']);
  199.  
  200.                 if (sizeof($keys> 1{
  201.                     foreach ($keys as $key{
  202.                         $this->_add_flag($flags[$key]'multiple_key');
  203.                     }
  204.                 }
  205.  
  206.                 if (strpos($val['index_description']'primary key')) {
  207.                     foreach ($keys as $key{
  208.                         $this->_add_flag($flags[$key]'primary_key');
  209.                     }
  210.                 elseif (strpos($val['index_description']'unique')) {
  211.                     foreach ($keys as $key{
  212.                         $this->_add_flag($flags[$key]'unique_key');
  213.                     }
  214.                 }
  215.             }
  216.  
  217.             // get auto_increment, not_null and timestamp
  218.             $res $db->queryAll("EXEC SP_COLUMNS[$table]"nullMDB2_FETCHMODE_ASSOC);
  219.  
  220.             foreach ($res as $val{
  221.                 $val array_change_key_case($valCASE_LOWER);
  222.                 if ($val['nullable'== '0'{
  223.                     $this->_add_flag($flags[$val['column_name']]'not_null');
  224.                 }
  225.                 if (strpos($val['type_name']'identity')) {
  226.                     $this->_add_flag($flags[$val['column_name']]'auto_increment');
  227.                 }
  228.                 if (strpos($val['type_name']'timestamp')) {
  229.                     $this->_add_flag($flags[$val['column_name']]'timestamp');
  230.                 }
  231.             }
  232.         }
  233.  
  234.         if (array_key_exists($column$flags)) {
  235.             return implode(' '$flags[$column]);
  236.         }
  237.         return '';
  238.     }
  239.  
  240.     // }}}
  241.     // {{{ _add_flag()
  242.  
  243.     /**
  244.      * Adds a string to the flags array if the flag is not yet in there
  245.      * - if there is no flag present the array is created.
  246.      *
  247.      * @param reference  Reference to the flag-array
  248.      * @param value      The flag value
  249.      * @access private
  250.      * @author Joern Barthel <j_barthel@web.de>
  251.      */
  252.     function _add_flag(&$array$value)
  253.     {
  254.         if (!is_array($array)) {
  255.             $array = array($value);
  256.         elseif (!in_array($value$array)) {
  257.             array_push($array$value);
  258.         }
  259.     }
  260. }
  261. ?>

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