MDB2
[ class tree: MDB2 ] [ index: MDB2 ] [ all elements ]

Source for file sqlsrv.php

Documentation is available at sqlsrv.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5                                                 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox,                 |
  6. // | Stig. S. Bakken, Lukas Smith, Frank M. Kromann, Lorenzo Alberton     |
  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. // | Authors: Lukas Smith <smith@pooteeweet.org>                          |
  43. // |          Lorenzo Alberton <l.alberton@quipo.it>                      |
  44. // +----------------------------------------------------------------------+
  45.  
  46. require_once 'MDB2/Driver/Reverse/Common.php';
  47.  
  48. /**
  49.  * MDB2 MSSQL driver for the schema reverse engineering module
  50.  *
  51.  * @package MDB2
  52.  * @category Database
  53.  * @author  Lukas Smith <smith@dybnet.de>
  54.  * @author  Lorenzo Alberton <l.alberton@quipo.it>
  55.  */
  56. class MDB2_Driver_Reverse_sqlsrv extends MDB2_Driver_Reverse_Common
  57. {
  58.     // {{{ getTableFieldDefinition()
  59.  
  60.     /**
  61.      * Get the structure of a field into an array
  62.      *
  63.      * @param string $table_name name of table that should be used in method
  64.      * @param string $field_name name of field that should be used in method
  65.      * @return mixed data array on success, a MDB2 error on failure
  66.      * @access public
  67.      */
  68.     function getTableFieldDefinition($table_name$field_name)
  69.     {
  70.         $db $this->getDBInstance();
  71.         if (MDB2::isError($db)) {
  72.             return $db;
  73.         }
  74.  
  75.         $result $db->loadModule('Datatype'nulltrue);
  76.         if (MDB2::isError($result)) {
  77.             return $result;
  78.         }
  79.  
  80.         list($schema$table$this->splitTableSchema($table_name);
  81.  
  82.         $table $db->quoteIdentifier($tabletrue);
  83.         $fldname $db->quoteIdentifier($field_nametrue);
  84.  
  85.         $query = "SELECT t.table_name,
  86.                          c.column_name 'name',
  87.                          c.data_type 'type',
  88.                          CASE c.is_nullable WHEN 'YES' THEN 1 ELSE 0 END AS 'is_nullable',
  89.                          c.column_default,
  90.                          c.character_maximum_length 'length',
  91.                          c.numeric_precision,
  92.                          c.numeric_scale,
  93.                          c.character_set_name,
  94.                          c.collation_name
  95.                     FROM INFORMATION_SCHEMA.TABLES t,
  96.                          INFORMATION_SCHEMA.COLUMNS c
  97.                    WHERE t.table_name = c.table_name
  98.                      AND t.table_name = '$table'
  99.                      AND c.column_name = '$fldname'";
  100.         if (!empty($schema)) {
  101.             $query .= " AND t.table_schema = '" .$db->quoteIdentifier($schematrue."'";
  102.         }
  103.         $query .= ' ORDER BY t.table_name';
  104.         $column $db->queryRow($querynullMDB2_FETCHMODE_ASSOC);
  105.         if (MDB2::isError($column)) {
  106.             return $column;
  107.         }
  108.         if (empty($column)) {
  109.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  110.                 'it was not specified an existing table column'__FUNCTION__);
  111.         }
  112.  
  113.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  114.             if ($db->options['field_case'== CASE_LOWER{
  115.                 $column['name'strtolower($column['name']);
  116.             else {
  117.                 $column['name'strtoupper($column['name']);
  118.             }
  119.         else {
  120.             $column array_change_key_case($column$db->options['field_case']);
  121.         }
  122.         $mapped_datatype $db->datatype->mapNativeDatatype($column);
  123.         if (MDB2::isError($mapped_datatype)) {
  124.             return $mapped_datatype;
  125.         }
  126.         list($types$length$unsigned$fixed$mapped_datatype;
  127.         $notnull = true;
  128.         if ($column['is_nullable']{
  129.             $notnull = false;
  130.         }
  131.         $default = false;
  132.         if (array_key_exists('column_default'$column)) {
  133.             $default $column['column_default'];
  134.             if ((null === $default&& $notnull{
  135.                 $default '';
  136.             elseif (strlen($default> 4
  137.                    && substr($default01== '('
  138.                    &&  substr($default-11== ')'
  139.             {
  140.                 //mssql wraps the default value in parentheses: "((1234))", "(NULL)"
  141.                 $default trim($default'()');
  142.                 if ($default == 'NULL'{
  143.                     $default = null;
  144.                 }
  145.             }
  146.         }
  147.         $definition[0= array(
  148.             'notnull' => $notnull,
  149.             'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i''\\1'$column['type'])
  150.         );
  151.         if (null !== $length{
  152.             $definition[0]['length'$length;
  153.         }
  154.         if (null !== $unsigned{
  155.             $definition[0]['unsigned'$unsigned;
  156.         }
  157.         if (null !== $fixed{
  158.             $definition[0]['fixed'$fixed;
  159.         }
  160.         if ($default !== false{
  161.             $definition[0]['default'$default;
  162.         }
  163.         foreach ($types as $key => $type{
  164.             $definition[$key$definition[0];
  165.             if ($type == 'clob' || $type == 'blob'{
  166.                 unset($definition[$key]['default']);
  167.             }
  168.             $definition[$key]['type'$type;
  169.             $definition[$key]['mdb2type'$type;
  170.         }
  171.         return $definition;
  172.     }
  173.  
  174.     // }}}
  175.     // {{{ getTableIndexDefinition()
  176.  
  177.     /**
  178.      * Get the structure of an index into an array
  179.      *
  180.      * @param string $table_name name of table that should be used in method
  181.      * @param string $index_name name of index that should be used in method
  182.      * @return mixed data array on success, a MDB2 error on failure
  183.      * @access public
  184.      */
  185.     function getTableIndexDefinition($table_name$index_name)
  186.     {
  187.         $db $this->getDBInstance();
  188.         if (MDB2::isError($db)) {
  189.             return $db;
  190.         }
  191.  
  192.         list($schema$table$this->splitTableSchema($table_name);
  193.  
  194.         $table $db->quoteIdentifier($tabletrue);
  195.         //$idxname = $db->quoteIdentifier($index_name, true);
  196.  
  197.         $query = "SELECT OBJECT_NAME(i.id) tablename,
  198.                          i.name indexname,
  199.                          c.name field_name,
  200.                          CASE INDEXKEY_PROPERTY(i.id, i.indid, ik.keyno, 'IsDescending')
  201.                            WHEN 1 THEN 'DESC' ELSE 'ASC'
  202.                          END 'collation',
  203.                          ik.keyno 'position'
  204.                     FROM sysindexes i
  205.                     JOIN sysindexkeys ik ON ik.id = i.id AND ik.indid = i.indid
  206.                     JOIN syscolumns c ON c.id = ik.id AND c.colid = ik.colid
  207.                    WHERE OBJECT_NAME(i.id) = '$table'
  208.                      AND i.name = '%s'
  209.                      AND NOT EXISTS (
  210.                             SELECT *
  211.                               FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
  212.                              WHERE k.table_name = OBJECT_NAME(i.id)
  213.                                AND k.constraint_name = i.name";
  214.         if (!empty($schema)) {
  215.             $query .= " AND k.table_schema = '" .$db->quoteIdentifier($schematrue."'";
  216.         }
  217.         $query .= ')
  218.                 ORDER BY tablename, indexname, ik.keyno';
  219.  
  220.         $index_name_mdb2 $db->getIndexName($index_name);
  221.         $result $db->queryRow(sprintf($query$index_name_mdb2));
  222.         if (!MDB2::isError($result&& (null !== $result)) {
  223.             // apply 'idxname_format' only if the query succeeded, otherwise
  224.             // fallback to the given $index_name, without transformation
  225.             $index_name $index_name_mdb2;
  226.         }
  227.         $result $db->query(sprintf($query$index_name));
  228.         if (MDB2::isError($result)) {
  229.             return $result;
  230.         }
  231.  
  232.         $definition = array();
  233.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  234.             $column_name $row['field_name'];
  235.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  236.                 if ($db->options['field_case'== CASE_LOWER{
  237.                     $column_name strtolower($column_name);
  238.                 else {
  239.                     $column_name strtoupper($column_name);
  240.                 }
  241.             }
  242.             $definition['fields'][$column_name= array(
  243.                 'position' => (int)$row['position'],
  244.             );
  245.             if (!empty($row['collation'])) {
  246.                 $definition['fields'][$column_name]['sorting'($row['collation'== 'ASC'
  247.                     ? 'ascending' 'descending');
  248.             }
  249.         }
  250.         $result->free();
  251.         if (empty($definition['fields'])) {
  252.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  253.                 'it was not specified an existing table index'__FUNCTION__);
  254.         }
  255.         return $definition;
  256.     }
  257.  
  258.     // }}}
  259.     // {{{ getTableConstraintDefinition()
  260.  
  261.     /**
  262.      * Get the structure of a constraint into an array
  263.      *
  264.      * @param string $table_name      name of table that should be used in method
  265.      * @param string $constraint_name name of constraint that should be used in method
  266.      * @return mixed data array on success, a MDB2 error on failure
  267.      * @access public
  268.      */
  269.     function getTableConstraintDefinition($table_name$constraint_name)
  270.     {
  271.         $db $this->getDBInstance();
  272.         if (MDB2::isError($db)) {
  273.             return $db;
  274.         }
  275.  
  276.         list($schema$table$this->splitTableSchema($table_name);
  277.  
  278.         $table $db->quoteIdentifier($tabletrue);
  279.         $query = "SELECT k.table_name,
  280.                          k.column_name field_name,
  281.                          CASE c.constraint_type WHEN 'PRIMARY KEY' THEN 1 ELSE 0 END 'primary',
  282.                          CASE c.constraint_type WHEN 'UNIQUE' THEN 1 ELSE 0 END 'unique',
  283.                          CASE c.constraint_type WHEN 'FOREIGN KEY' THEN 1 ELSE 0 END 'foreign',
  284.                          CASE c.constraint_type WHEN 'CHECK' THEN 1 ELSE 0 END 'check',
  285.                          CASE c.is_deferrable WHEN 'NO' THEN 0 ELSE 1 END 'deferrable',
  286.                          CASE c.initially_deferred WHEN 'NO' THEN 0 ELSE 1 END 'initiallydeferred',
  287.                          rc.match_option 'match',
  288.                          rc.update_rule 'onupdate',
  289.                          rc.delete_rule 'ondelete',
  290.                          kcu.table_name 'references_table',
  291.                          kcu.column_name 'references_field',
  292.                          k.ordinal_position 'field_position'
  293.                     FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
  294.                     LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS c
  295.                       ON k.table_name = c.table_name
  296.                      AND k.table_schema = c.table_schema
  297.                      AND k.table_catalog = c.table_catalog
  298.                      AND k.constraint_catalog = c.constraint_catalog
  299.                      AND k.constraint_name = c.constraint_name
  300.                LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
  301.                       ON rc.constraint_schema = c.constraint_schema
  302.                      AND rc.constraint_catalog = c.constraint_catalog
  303.                      AND rc.constraint_name = c.constraint_name
  304.                LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
  305.                       ON rc.unique_constraint_schema = kcu.constraint_schema
  306.                      AND rc.unique_constraint_catalog = kcu.constraint_catalog
  307.                      AND rc.unique_constraint_name = kcu.constraint_name
  308.                      AND k.ordinal_position = kcu.ordinal_position
  309.                    WHERE k.constraint_catalog = DB_NAME()
  310.                      AND k.table_name = '$table'
  311.                      AND k.constraint_name = '%s'";
  312.         if (!empty($schema)) {
  313.             $query .= " AND k.table_schema = '" .$db->quoteIdentifier($schematrue."'";
  314.         }
  315.         $query .= ' ORDER BY k.constraint_name,
  316.                              k.ordinal_position';
  317.  
  318.         $constraint_name_mdb2 $db->getIndexName($constraint_name);
  319.         $result $db->queryRow(sprintf($query$constraint_name_mdb2));
  320.         if (!MDB2::isError($result&& (null !== $result)) {
  321.             // apply 'idxname_format' only if the query succeeded, otherwise
  322.             // fallback to the given $index_name, without transformation
  323.             $constraint_name $constraint_name_mdb2;
  324.         }
  325.         $result $db->query(sprintf($query$constraint_name));
  326.         if (MDB2::isError($result)) {
  327.             return $result;
  328.         }
  329.  
  330.         $definition = array(
  331.             'fields' => array()
  332.         );
  333.         while (is_array($row $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  334.             $row array_change_key_case($rowCASE_LOWER);
  335.             $column_name $row['field_name'];
  336.             if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  337.                 if ($db->options['field_case'== CASE_LOWER{
  338.                     $column_name strtolower($column_name);
  339.                 else {
  340.                     $column_name strtoupper($column_name);
  341.                 }
  342.             }
  343.             $definition['fields'][$column_name= array(
  344.                 'position' => (int)$row['field_position']
  345.             );
  346.             if ($row['foreign']{
  347.                 $ref_column_name $row['references_field'];
  348.                 if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  349.                     if ($db->options['field_case'== CASE_LOWER{
  350.                         $ref_column_name strtolower($ref_column_name);
  351.                     else {
  352.                         $ref_column_name strtoupper($ref_column_name);
  353.                     }
  354.                 }
  355.                 $definition['references']['table'$row['references_table'];
  356.                 $definition['references']['fields'][$ref_column_name= array(
  357.                     'position' => (int)$row['field_position']
  358.                 );
  359.             }
  360.             //collation?!?
  361.             /*
  362.             if (!empty($row['collation'])) {
  363.                 $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'ASC'
  364.                     ? 'ascending' : 'descending');
  365.             }
  366.             */
  367.             $lastrow $row;
  368.             // otherwise $row is no longer usable on exit from loop
  369.         }
  370.         $result->free();
  371.         if (empty($definition['fields'])) {
  372.             return $db->raiseError(MDB2_ERROR_NOT_FOUNDnullnull,
  373.                 $constraint_name ' is not an existing table constraint'__FUNCTION__);
  374.         }
  375.  
  376.         $definition['primary'= (boolean)$lastrow['primary'];
  377.         $definition['unique']  = (boolean)$lastrow['unique'];
  378.         $definition['foreign'= (boolean)$lastrow['foreign'];
  379.         $definition['check']   = (boolean)$lastrow['check'];
  380.         $definition['deferrable'= (boolean)$lastrow['deferrable'];
  381.         $definition['initiallydeferred'= (boolean)$lastrow['initiallydeferred'];
  382.         $definition['onupdate'$lastrow['onupdate'];
  383.         $definition['ondelete'$lastrow['ondelete'];
  384.         $definition['match']    $lastrow['match'];
  385.  
  386.         return $definition;
  387.     }
  388.  
  389.     // }}}
  390.     // {{{ getTriggerDefinition()
  391.  
  392.     /**
  393.      * Get the structure of a trigger into an array
  394.      *
  395.      * EXPERIMENTAL
  396.      *
  397.      * WARNING: this function is experimental and may change the returned value
  398.      * at any time until labelled as non-experimental
  399.      *
  400.      * @param string    $trigger    name of trigger that should be used in method
  401.      * @return mixed data array on success, a MDB2 error on failure
  402.      * @access public
  403.      */
  404.     function getTriggerDefinition($trigger)
  405.     {
  406.         $db $this->getDBInstance();
  407.         if (MDB2::isError($db)) {
  408.             return $db;
  409.         }
  410.  
  411.         $query "SELECT sys1.name trigger_name,
  412.                          sys2.name table_name,
  413.                          c.text trigger_body,
  414.                          c.encrypted is_encripted,
  415.                          CASE
  416.                            WHEN OBJECTPROPERTY(sys1.id, 'ExecIsTriggerDisabled') = 1
  417.                            THEN 0 ELSE 1
  418.                          END trigger_enabled,
  419.                          CASE
  420.                            WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsertTrigger') = 1
  421.                            THEN 'INSERT'
  422.                            WHEN OBJECTPROPERTY(sys1.id, 'ExecIsUpdateTrigger') = 1
  423.                            THEN 'UPDATE'
  424.                            WHEN OBJECTPROPERTY(sys1.id, 'ExecIsDeleteTrigger') = 1
  425.                            THEN 'DELETE'
  426.                          END trigger_event,
  427.                          CASE WHEN OBJECTPROPERTY(sys1.id, 'ExecIsInsteadOfTrigger') = 1
  428.                            THEN 'INSTEAD OF' ELSE 'AFTER'
  429.                          END trigger_type,
  430.                          '' trigger_comment
  431.                     FROM sysobjects sys1
  432.                     JOIN sysobjects sys2 ON sys1.parent_obj = sys2.id
  433.                     JOIN syscomments c ON sys1.id = c.id
  434.                    WHERE sys1.xtype = 'TR'
  435.                      AND sys1.name = "$db->quote($trigger'text');
  436.  
  437.         $types = array(
  438.             'trigger_name'    => 'text',
  439.             'table_name'      => 'text',
  440.             'trigger_body'    => 'text',
  441.             'trigger_type'    => 'text',
  442.             'trigger_event'   => 'text',
  443.             'trigger_comment' => 'text',
  444.             'trigger_enabled' => 'boolean',
  445.             'is_encripted'    => 'boolean',
  446.         );
  447.  
  448.         $def $db->queryRow($query$typesMDB2_FETCHMODE_ASSOC);
  449.         if (MDB2::isError($def)) {
  450.             return $def;
  451.         }
  452.         $trg_body $db->queryCol('EXEC sp_helptext '$db->quote($trigger'text')'text');
  453.         if (!MDB2::isError($trg_body)) {
  454.             $def['trigger_body'implode(' '$trg_body);
  455.         }
  456.         return $def;
  457.     }
  458.  
  459.     // }}}
  460.     // {{{ tableInfo()
  461.  
  462.     /**
  463.      * Returns information about a table or a result set
  464.      *
  465.      * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  466.      * is a table name.
  467.      *
  468.      * @param object|string $result  MDB2_result object from a query or a
  469.      *                                  string containing the name of a table.
  470.      *                                  While this also accepts a query result
  471.      *                                  resource identifier, this behavior is
  472.      *                                  deprecated.
  473.      * @param int            $mode    a valid tableInfo mode
  474.      *
  475.      * @return array  an associative array with the information requested.
  476.      *                  A MDB2_Error object on failure.
  477.      *
  478.      * @see MDB2_Driver_Common::tableInfo()
  479.      */
  480.     function tableInfo($result$mode = null)
  481.     {
  482.         if (is_string($result)) {
  483.            return parent::tableInfo($result$mode);
  484.         }
  485.  
  486.         $db $this->getDBInstance();
  487.         if (MDB2::isError($db)) {
  488.             return $db;
  489.         }
  490.  
  491.         $resource = MDB2::isResultCommon($result$result->getResource($result;
  492.         if (!is_resource($resource)) {
  493.             return $db->raiseError(MDB2_ERROR_NEED_MORE_DATAnullnull,
  494.                 'Could not generate result resource'__FUNCTION__);
  495.         }
  496.  
  497.         if ($db->options['portability'MDB2_PORTABILITY_FIX_CASE{
  498.             if ($db->options['field_case'== CASE_LOWER{
  499.                 $case_func 'strtolower';
  500.             else {
  501.                 $case_func 'strtoupper';
  502.             }
  503.         else {
  504.             $case_func 'strval';
  505.         }
  506.  
  507.         $meta @sqlsrv_field_metadata($resource);
  508.         $count count($meta);
  509.         $res   = array();
  510.  
  511.         if ($mode{
  512.             $res['num_fields'$count;
  513.         }
  514.  
  515.         $db->loadModule('Datatype'nulltrue);
  516.         for ($i = 0; $i $count$i++{
  517.             $res[$i= array(
  518.                 'table' => '',
  519.                 'name'              => $case_func($meta[$i]['Name']),
  520.                 'type'              => $meta[$i]['Type'],
  521.                 'length'            => $meta[$i]['Size'],
  522.                 'numeric_precision' => $meta[$i]['Precision'],
  523.                 'numeric_scale'     => $meta[$i]['Scale'],
  524.                 'flags'             => ''
  525.             );
  526.             $mdb2type_info $db->datatype->mapNativeDatatype($res[$i]);
  527.             if (MDB2::isError($mdb2type_info)) {
  528.                return $mdb2type_info;
  529.             }
  530.             $res[$i]['mdb2type'$mdb2type_info[0][0];
  531.             if ($mode MDB2_TABLEINFO_ORDER{
  532.                 $res['order'][$res[$i]['name']] $i;
  533.             }
  534.             if ($mode MDB2_TABLEINFO_ORDERTABLE{
  535.                 $res['ordertable'][$res[$i]['table']][$res[$i]['name']] $i;
  536.             }
  537.         }
  538.  
  539.         return $res;
  540.     }
  541.  
  542.     // }}}
  543.     // {{{ _mssql_field_flags()
  544.  
  545.     /**
  546.      * Get a column's flags
  547.      *
  548.      * Supports "not_null", "primary_key",
  549.      * "auto_increment" (mssql identity), "timestamp" (mssql timestamp),
  550.      * "unique_key" (mssql unique index, unique check or primary_key) and
  551.      * "multiple_key" (multikey index)
  552.      *
  553.      * mssql timestamp is NOT similar to the mysql timestamp so this is maybe
  554.      * not useful at all - is the behaviour of mysql_field_flags that primary
  555.      * keys are alway unique? is the interpretation of multiple_key correct?
  556.      *
  557.      * @param string $table   the table name
  558.      * @param string $column  the field name
  559.      *
  560.      * @return string  the flags
  561.      *
  562.      * @access protected
  563.      * @author Joern Barthel <j_barthel@web.de>
  564.      */
  565.     function _mssql_field_flags($table$column)
  566.     {
  567.         $db $this->getDBInstance();
  568.         if (MDB2::isError($db)) {
  569.             return $db;
  570.         }
  571.  
  572.         static $tableName = null;
  573.         static $flags = array();
  574.  
  575.         if ($table != $tableName{
  576.  
  577.             $flags = array();
  578.             $tableName $table;
  579.  
  580.             // get unique and primary keys
  581.             $res $db->queryAll("EXEC SP_HELPINDEX[$table]"nullMDB2_FETCHMODE_ASSOC);
  582.  
  583.             foreach ($res as $val{
  584.                 $val array_change_key_case($valCASE_LOWER);
  585.                 $keys explode(', '$val['index_keys']);
  586.  
  587.                 if (sizeof($keys> 1{
  588.                     foreach ($keys as $key{
  589.                         $this->_add_flag($flags[$key]'multiple_key');
  590.                     }
  591.                 }
  592.  
  593.                 if (strpos($val['index_description']'primary key')) {
  594.                     foreach ($keys as $key{
  595.                         $this->_add_flag($flags[$key]'primary_key');
  596.                     }
  597.                 elseif (strpos($val['index_description']'unique')) {
  598.                     foreach ($keys as $key{
  599.                         $this->_add_flag($flags[$key]'unique_key');
  600.                     }
  601.                 }
  602.             }
  603.  
  604.             // get auto_increment, not_null and timestamp
  605.             $res $db->queryAll("EXEC SP_COLUMNS[$table]"nullMDB2_FETCHMODE_ASSOC);
  606.  
  607.             foreach ($res as $val{
  608.                 $val array_change_key_case($valCASE_LOWER);
  609.                 if ($val['nullable'== '0'{
  610.                     $this->_add_flag($flags[$val['column_name']]'not_null');
  611.                 }
  612.                 if (strpos($val['type_name']'identity')) {
  613.                     $this->_add_flag($flags[$val['column_name']]'auto_increment');
  614.                 }
  615.                 if (strpos($val['type_name']'timestamp')) {
  616.                     $this->_add_flag($flags[$val['column_name']]'timestamp');
  617.                 }
  618.             }
  619.         }
  620.  
  621.         if (!empty($flags[$column])) {
  622.             return(implode(' '$flags[$column]));
  623.         }
  624.         return '';
  625.     }
  626.  
  627.     // }}}
  628.     // {{{ _add_flag()
  629.  
  630.     /**
  631.      * Adds a string to the flags array if the flag is not yet in there
  632.      * - if there is no flag present the array is created
  633.      *
  634.      * @param array  &$array  the reference to the flag-array
  635.      * @param string $value   the flag value
  636.      *
  637.      * @return void 
  638.      *
  639.      * @access protected
  640.      * @author Joern Barthel <j_barthel@web.de>
  641.      */
  642.     function _add_flag(&$array$value)
  643.     {
  644.         if (!is_array($array)) {
  645.             $array = array($value);
  646.         elseif (!in_array($value$array)) {
  647.             array_push($array$value);
  648.         }
  649.     }
  650.  
  651.     // }}}
  652. }
  653. ?>

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