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

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