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, 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.12 2005/04/19 12:53:42 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.     // {{{ tableInfo()
  60.  
  61.     /**
  62.      * Returns information about a table or a result set
  63.      *
  64.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  65.      * is a table name.
  66.      *
  67.      * @param object|string $result  MDB2_result object from a query or a
  68.      *                                  string containing the name of a table.
  69.      *                                  While this also accepts a query result
  70.      *                                  resource identifier, this behavior is
  71.      *                                  deprecated.
  72.      * @param int            $mode    a valid tableInfo mode
  73.      *
  74.      * @return array  an associative array with the information requested.
  75.      *                  A MDB2_Error object on failure.
  76.      *
  77.      * @see MDB2_common::tableInfo()
  78.      */
  79.     function tableInfo($result$mode = null)
  80.     {
  81.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  82.         if (is_string($result)) {
  83.             /*
  84.              * Probably received a table name.
  85.              * Create a result resource identifier.
  86.              */
  87.             $id $db->_doQuery("SELECT * FROM $result LIMIT 0");
  88.             if (PEAR::isError($id)) {
  89.                 return $id;
  90.             }
  91.  
  92.             $got_string = true;
  93.         elseif (MDB2::isResultCommon($result)) {
  94.             /*
  95.              * Probably received a result object.
  96.              * Extract the result resource identifier.
  97.              */
  98.             $id $result->getResource();
  99.             $got_string = false;
  100.         else {
  101.             /*
  102.              * Probably received a result resource identifier.
  103.              * Copy it.
  104.              * Deprecated.  Here for compatibility only.
  105.              */
  106.             $id $result;
  107.             $got_string = false;
  108.         }
  109.  
  110.         if (!is_resource($id)) {
  111.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA);
  112.         }
  113.  
  114.         if ($db->options['portability'MDB2_PORTABILITY_LOWERCASE{
  115.             $case_func 'strtolower';
  116.         else {
  117.             $case_func 'strval';
  118.         }
  119.  
  120.         $count @mssql_num_fields($id);
  121.         $res   = array();
  122.  
  123.         if ($mode{
  124.             $res['num_fields'$count;
  125.         }
  126.  
  127.         for ($i = 0; $i $count$i++{
  128.             $res[$i= array(
  129.                 'table' => $got_string $case_func($result'',
  130.                 'name'  => $case_func(@mssql_field_name($id$i)),
  131.                 'type'  => @mssql_field_type($id$i),
  132.                 'len'   => @mssql_field_length($id$i),
  133.                 // We only support flags for table
  134.                 'flags' => $got_string
  135.                            ? $this->_mssql_field_flags($result@mssql_field_name($id$i)) '',
  136.             );
  137.             if ($mode MDB2_TABLEINFO_ORDER{
  138.                 $res['order'][$res[$i]['name']] $i;
  139.             }
  140.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  141.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  142.             }
  143.         }
  144.  
  145.         // free the result only if we were called on a table
  146.         if ($got_string{
  147.             @mssql_free_result($id);
  148.         }
  149.         return $res;
  150.     }
  151.  
  152.     // }}}
  153.     // {{{ _mssql_field_flags()
  154.  
  155.     /**
  156.      * Get a column's flags
  157.      *
  158.      * Supports "not_null", "primary_key",
  159.      * "auto_increment" (mssql identity), "timestamp" (mssql timestamp),
  160.      * "unique_key" (mssql unique index, unique check or primary_key) and
  161.      * "multiple_key" (multikey index)
  162.      *
  163.      * mssql timestamp is NOT similar to the mysql timestamp so this is maybe
  164.      * not useful at all - is the behaviour of mysql_field_flags that primary
  165.      * keys are alway unique? is the interpretation of multiple_key correct?
  166.      *
  167.      * @param string $table   the table name
  168.      * @param string $column  the field name
  169.      *
  170.      * @return string  the flags
  171.      *
  172.      * @access protected
  173.      * @author Joern Barthel <j_barthel@web.de>
  174.      */
  175.     function _mssql_field_flags($table$column)
  176.     {
  177.         $db =$GLOBALS['_MDB2_databases'][$this->db_index];
  178.  
  179.         static $tableName = null;
  180.         static $flags = array();
  181.  
  182.         if ($table != $tableName{
  183.  
  184.             $flags = array();
  185.             $tableName $table;
  186.  
  187.             // get unique and primary keys
  188.             $res $db->queryAll("EXEC SP_HELPINDEX[$table]"nullMDB2_FETCHMODE_ASSOC);
  189.  
  190.             foreach ($res as $val{
  191.                 $keys explode(', '$val['index_keys']);
  192.  
  193.                 if (sizeof($keys> 1{
  194.                     foreach ($keys as $key{
  195.                         $this->_add_flag($flags[$key]'multiple_key');
  196.                     }
  197.                 }
  198.  
  199.                 if (strpos($val['index_description']'primary key')) {
  200.                     foreach ($keys as $key{
  201.                         $this->_add_flag($flags[$key]'primary_key');
  202.                     }
  203.                 elseif (strpos($val['index_description']'unique')) {
  204.                     foreach ($keys as $key{
  205.                         $this->_add_flag($flags[$key]'unique_key');
  206.                     }
  207.                 }
  208.             }
  209.  
  210.             // get auto_increment, not_null and timestamp
  211.             $res $db->queryAll("EXEC SP_COLUMNS[$table]"nullMDB2_FETCHMODE_ASSOC);
  212.  
  213.             foreach ($res as $val{
  214.                 $val array_change_key_case($valCASE_LOWER);
  215.                 if ($val['nullable'== '0'{
  216.                     $this->_add_flag($flags[$val['column_name']]'not_null');
  217.                 }
  218.                 if (strpos($val['type_name']'identity')) {
  219.                     $this->_add_flag($flags[$val['column_name']]'auto_increment');
  220.                 }
  221.                 if (strpos($val['type_name']'timestamp')) {
  222.                     $this->_add_flag($flags[$val['column_name']]'timestamp');
  223.                 }
  224.             }
  225.         }
  226.  
  227.         if (array_key_exists($column$flags)) {
  228.             return(implode(' '$flags[$column]));
  229.         }
  230.         return '';
  231.     }
  232.  
  233.     // }}}
  234.     // {{{ _add_flag()
  235.  
  236.     /**
  237.      * Adds a string to the flags array if the flag is not yet in there
  238.      * - if there is no flag present the array is created
  239.      *
  240.      * @param array  &$array  the reference to the flag-array
  241.      * @param string $value   the flag value
  242.      *
  243.      * @return void 
  244.      *
  245.      * @access protected
  246.      * @author Joern Barthel <j_barthel@web.de>
  247.      */
  248.     function _add_flag(&$array$value)
  249.     {
  250.         if (!is_array($array)) {
  251.             $array = array($value);
  252.         elseif (!in_array($value$array)) {
  253.             array_push($array$value);
  254.         }
  255.     }
  256. }
  257. ?>

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