ssb             Fri Mar 23 23:00:45 2001 EDT

  Added files:                 
    /php4/pear/DB/tests tableinfo.inc 
    /php4/pear/DB/tests/mysql   012.phpt 

  Modified files:              
    /php4/pear  DB.php 
    /php4/pear/DB       STATUS common.php mysql.php 
  Log:
  * added Christian Stocker's tableInfo() implementation
  
  
Index: php4/pear/DB.php
diff -u php4/pear/DB.php:1.52 php4/pear/DB.php:1.53
--- php4/pear/DB.php:1.52       Thu Mar 22 17:15:05 2001
+++ php4/pear/DB.php    Fri Mar 23 23:00:44 2001
@@ -17,7 +17,7 @@
 // |          Tomas V.V.Cox <[EMAIL PROTECTED]>                             |
 // +----------------------------------------------------------------------+
 //
-// $Id: DB.php,v 1.52 2001/03/23 01:15:05 ssb Exp $
+// $Id: DB.php,v 1.53 2001/03/24 07:00:44 ssb Exp $
 //
 // Database independent query interface.
 //
@@ -133,6 +133,17 @@
 define('DB_GETMODE_FLIPPED', DB_FETCHMODE_FLIPPED);
 
 /**
+ * these are constants for the tableInfo-function
+ * they are bitwised or'ed. so if there are more constants to be defined
+ * in the future, adjust DB_TABLEINFO_FULL accordingly 
+ */
+
+define('DB_TABLEINFO_ORDER', 1);
+define('DB_TABLEINFO_ORDERTABLE', 2);
+define('DB_TABLEINFO_FULL', 3);
+
+
+/**
  * The main "DB" class is simply a container class with some static
  * methods for creating DB objects as well as some utility functions
  * common to all parts of DB.
@@ -625,6 +636,11 @@
         }
         $this->result = false;
         return true;
+    }
+
+    function tableInfo($mode = null)
+    {
+        return $this->dbh->tableInfo($this->result, $mode);
     }
 }
 
Index: php4/pear/DB/STATUS
diff -u php4/pear/DB/STATUS:1.15 php4/pear/DB/STATUS:1.16
--- php4/pear/DB/STATUS:1.15    Thu Mar 22 17:15:06 2001
+++ php4/pear/DB/STATUS Fri Mar 23 23:00:45 2001
@@ -21,3 +21,4 @@
 transactions      x       n       n       n       x       x       x       n
 auto-commit       x       n       n       n       x       x       x       n
 error mapping     -       -       -       T       T       x       E       -
+tableInfo         n       n       n       T       n       n       n       n
Index: php4/pear/DB/common.php
diff -u php4/pear/DB/common.php:1.40 php4/pear/DB/common.php:1.41
--- php4/pear/DB/common.php:1.40        Thu Mar 22 17:15:06 2001
+++ php4/pear/DB/common.php     Fri Mar 23 23:00:45 2001
@@ -865,6 +865,14 @@
     }
 
     // }}}
+    // {{{ tableInfo()
+
+    function tableInfo($result, $mode = null)
+    {
+        return $this->raiseError(DB_ERROR_NOT_CAPABLE);
+    }
+
+    // }}}
 }
 
 ?>
Index: php4/pear/DB/mysql.php
diff -u php4/pear/DB/mysql.php:1.49 php4/pear/DB/mysql.php:1.50
--- php4/pear/DB/mysql.php:1.49 Mon Feb 19 04:22:26 2001
+++ php4/pear/DB/mysql.php      Fri Mar 23 23:00:45 2001
@@ -453,6 +453,102 @@
     }
 
     // }}}
+    // {{{ tableInfo()
+    
+    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 = @mysql_list_fields($this->dsn["database"],
+                                     $result, $this->connection);
+            if (empty($id)) {
+                return $this->mysqlRaiseError();
+            }
+        } else { // else we want information about a resultset
+            $id = $result;
+            if (empty($id)) {
+                return $this->mysqlRaiseError();
+            }
+        }
+
+        $count = @mysql_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"] = @mysql_field_table ($id, $i);
+                $res[$i]["name"]  = @mysql_field_name  ($id, $i);
+                $res[$i]["type"]  = @mysql_field_type  ($id, $i);
+                $res[$i]["len"]   = @mysql_field_len   ($id, $i);
+                $res[$i]["flags"] = @mysql_field_flags ($id, $i);
+            }
+        } else { // full
+            $res["num_fields"]= $count;
+
+            for ($i=0; $i<$count; $i++) {
+                $res[$i]["table"] = @mysql_field_table ($id, $i);
+                $res[$i]["name"]  = @mysql_field_name  ($id, $i);
+                $res[$i]["type"]  = @mysql_field_type  ($id, $i);
+                $res[$i]["len"]   = @mysql_field_len   ($id, $i);
+                $res[$i]["flags"] = @mysql_field_flags ($id, $i);
+                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 ($table) {
+            @mysql_free_result($id);
+        }
+        return $res;
+    }
+
+    // }}}
 
     // TODO/wishlist:
     // simpleFetch

Index: php4/pear/DB/tests/tableinfo.inc
+++ php4/pear/DB/tests/tableinfo.inc
<?php

$dbh->setErrorHandling(PEAR_ERROR_DIE);
$dbh->query("INSERT INTO phptest VALUES(1, 'one', 'One', '2001-02-16')");
$dbh->query("INSERT INTO phptest VALUES(2, 'two', 'Two', '2001-02-15')");
$dbh->query("INSERT INTO phptest VALUES(3, 'three', 'Three', '2001-02-14')");

$test_mktable_query2 = "CREATE TABLE phptest_fk (a INTEGER, fk INTEGER, c TEXT, d 
DATE)";
$dbh->query($test_mktable_query2);

$dbh->query("INSERT INTO phptest_fk VALUES(1,  1, 'One', '2001-02-16')");
$dbh->query("INSERT INTO phptest_fk VALUES(2,  1, ' Two', '2001-02-15')");
$dbh->query("INSERT INTO phptest_fk VALUES(3,  2, 'Three', '2001-02-14')");



print "testing tableInfo:\n";
$sth = $dbh->query("SELECT * FROM phptest left join phptest_fk on phptest.a = 
phptest_fk.fk");

$tableInfo = $sth->tableInfo();
print "\nfirst field:\n";
print_foreach($tableInfo[0]) ;
print "\neight (last) field:\n";
print_foreach($tableInfo[7]) ;

print "\ntesting tableInfo (DB_TABLEINFO_ORDER):\n";
$tableInfo =$sth->tableInfo(DB_TABLEINFO_ORDER);
print "\nfirst field:\n";
print_foreach($tableInfo[0]) ;
print "\neight field:\n";
print_foreach($tableInfo[3]) ;
print "\nnum_fields:\n";
print "$tableInfo[num_fields]\n" ;
print "\norder:\n";
print_foreach($tableInfo[order]) ;

print "\ntesting tableInfo (DB_TABLEINFO_ORDERTABLE):\n";
$tableInfo =$sth->tableInfo(DB_TABLEINFO_ORDERTABLE);
print "\nfirst field:\n";
print_foreach($tableInfo[0]) ;
print "\neight field:\n";
print_foreach($tableInfo[3]) ;
print "\nnum_fields:\n";
print "$tableInfo[num_fields]\n" ;
print "\nordertable:\n";
print_foreach($tableInfo[ordertable]) ;
print "\nordertable[phptest]:\n";
print_foreach($tableInfo[ordertable][phptest]) ;
print "\nordertable[phptest_fk]:\n";
print_foreach($tableInfo[ordertable][phptest_fk]) ;

print "\ntesting tableInfo (table without query-result):\n";

$tableInfo = $dbh->tableInfo("phptest");
print "\nfirst field:\n";
print_foreach($tableInfo[0]) ;
print "\nfourth (last) field:\n";
print_foreach($tableInfo[3]) ;

print "\ntesting tableInfo (table without query-result and DB_TABLEINFO_FULL):\n";

$tableInfo = $dbh->tableInfo("phptest",DB_TABLEINFO_FULL);
print "\nfirst field:\n";
print_foreach($tableInfo[0]) ;
print "\norder:\n";
print_foreach($tableInfo[order]) ;
print "\nordertable:\n";
print_foreach($tableInfo[ordertable]) ;
print "\nordertable[phptest]:\n";
print_foreach($tableInfo[ordertable][phptest]) ;



//print_foreach($tableInfo[0]) ;
$dbh->query("DELETE FROM phptest WHERE a <> 42");
$dbh->query("DROP TABLE phptest_fk");

function print_foreach ($array) {
    foreach ($array as $key => $value) {
        print "$key => $value \n";
    }

}
?>

Index: php4/pear/DB/tests/mysql/012.phpt
+++ php4/pear/DB/tests/mysql/012.phpt
--TEST--
DB_mysql tableInfo test
--SKIPIF--
<?php include("skipif.inc"); ?>
--FILE--
<?php
require_once "DB.php";
include("mktable.inc");
include("../tableinfo.inc");
?>
--EXPECT--
testing tableInfo:

first field:
table => phptest 
name => a 
type => int 
len => 11 
flags =>  

eight (last) field:
table => phptest_fk 
name => d 
type => date 
len => 10 
flags =>  

testing tableInfo (DB_TABLEINFO_ORDER):

first field:
table => phptest 
name => a 
type => int 
len => 11 
flags =>  

eight field:
table => phptest 
name => d 
type => date 
len => 10 
flags =>  

num_fields:
8

order:
a => 4 
b => 1 
c => 6 
d => 7 
fk => 5 

testing tableInfo (DB_TABLEINFO_ORDERTABLE):

first field:
table => phptest 
name => a 
type => int 
len => 11 
flags =>  

eight field:
table => phptest 
name => d 
type => date 
len => 10 
flags =>  

num_fields:
8

ordertable:
phptest => Array 
phptest_fk => Array 

ordertable[phptest]:
a => 0 
b => 1 
c => 2 
d => 3 

ordertable[phptest_fk]:
a => 4 
fk => 5 
c => 6 
d => 7 

testing tableInfo (table without query-result):

first field:
table => phptest 
name => a 
type => int 
len => 11 
flags =>  

fourth (last) field:
table => phptest 
name => d 
type => date 
len => 10 
flags =>  

testing tableInfo (table without query-result and DB_TABLEINFO_FULL):

first field:
table => phptest 
name => a 
type => int 
len => 11 
flags =>  

order:
a => 0 
b => 1 
c => 2 
d => 3 

ordertable:
phptest => Array 

ordertable[phptest]:
a => 0 
b => 1 
c => 2 
d => 3 

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to