cvs: php4 /pear/DB STATUS mssql.php

From: Date: Tue, 04 Sep 2001 13:44:48 +0000
Subject: cvs: php4 /pear/DB STATUS mssql.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-789@lists.php.net to get a copy of this message
cox Tue Sep 4 09:44:48 2001 EDT Modified files: /php4/pear/DB mssql.php STATUS Log: added tableinfo to the MS SQL Server Pear DB extension (thanks Larry Blische <blische@kravis.org>!) Index: php4/pear/DB/mssql.php diff -u php4/pear/DB/mssql.php:1.26 php4/pear/DB/mssql.php:1.27 --- php4/pear/DB/mssql.php:1.26 Wed Aug 15 15:20:02 2001 +++ php4/pear/DB/mssql.php Tue Sep 4 09:44:47 2001 @@ -16,7 +16,7 @@ // | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mssql.php,v 1.26 2001/08/15 19:20:02 cox Exp $ +// $Id: mssql.php,v 1.27 2001/09/04 13:44:47 cox Exp $ // // Database independent query interface definition for PHP's Microsoft SQL Server // extension. @@ -332,6 +332,116 @@ } return $this->raiseError($code, null, null, null, mssql_get_last_message()); + } + + /** + + * Returns information about a table or a result set + * + * NOTE: doesn't support table name and flags if called from a db_result + * + * @param mixed $resource SQL Server result identifier or table name + * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or + * DB_TABLEINFO_ORDER) + * + * @return array An array with all the information + */ + + function tableInfo($result, $mode = null) + { + + $count = 0; + $id = 0; + $res = array(); + + /* + * depending on $mode, metadata returns the following values: + * + * - mode is false (default): + * $result[]: + * [0]["table"] table name + * [0]["name"] field name + * [0]["type"] field type + * [0]["len"] field length + * [0]["flags"] field flags + * + * - mode is DB_TABLEINFO_ORDER + * $result[]: + * ["num_fields"] number of metadata records + * [0]["table"] table name + * [0]["name"] field name + * [0]["type"] field type + * [0]["len"] field length + * [0]["flags"] field flags + * ["order"][field name] index of field named "field name" + * The last one is used, if you have a field name, but no index. + * Test: if (isset($result['meta']['myfield'])) { ... + * + * - mode is DB_TABLEINFO_ORDERTABLE + * the same as above. but additionally + * ["ordertable"][table name][field name] index of field + * named "field name" + * + * this is, because if you have fields from different + * tables with the same field name * they override each + * other with DB_TABLEINFO_ORDER + * + * you can combine DB_TABLEINFO_ORDER and + * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | + * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL + */ + + // if $result is a string, then we want information about a + // table without a resultset + + if (is_string($result)) { + $id = mssql_query("SELECT * FROM $result", $this->connection); + if (empty($id)) { + return $this->mssqlRaiseError(); + } + } else { // else we want information about a resultset + $id = $result; + if (empty($id)) { + return $this->mssqlRaiseError(); + } + } + + $count = @mssql_num_fields($id); + + // made this IF due to performance (one if is faster than $count if's) + if (empty($mode)) { + + for ($i=0; $i<$count; $i++) { + $res[$i]['table'] = (is_string($result)) ? $result : ''; + $res[$i]['name'] = @mssql_field_name($id, $i); + $res[$i]['type'] = @mssql_field_type($id, $i); + $res[$i]['len'] = @mssql_field_length($id, $i); + $res[$i]['flags'] = ''; + } + + } else { // full + $res['num_fields']= $count; + + for ($i=0; $i<$count; $i++) { + $res[$i]['table'] = (is_string($result)) ? $result : ''; + $res[$i]['name'] = @mssql_field_name($id, $i); + $res[$i]['type'] = @mssql_field_type($id, $i); + $res[$i]['len'] = @mssql_field_length($id, $i); + $res[$i]['flags'] = ''; + if ($mode & DB_TABLEINFO_ORDER) { + $res['order'][$res[$i]['name']] = $i; + } + if ($mode & DB_TABLEINFO_ORDERTABLE) { + $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; + } + } + } + + // free the result only if we were called on a table + if (is_string($result)) { + @mssql_free_result($id); + } + return $res; } } Index: php4/pear/DB/STATUS diff -u php4/pear/DB/STATUS:1.24 php4/pear/DB/STATUS:1.25 --- php4/pear/DB/STATUS:1.24 Tue Aug 7 07:58:34 2001 +++ php4/pear/DB/STATUS Tue Sep 4 09:44:47 2001 @@ -24,4 +24,4 @@ transactions x x n n x x x x x n auto-commit x x n n n n x x x n error mapping x - e - - T T x E - -tableInfo x n n n n T n n x n +tableInfo x n n n x T n n x n

« previous php.pear.cvs (#789) next »