Attached is a patch for the MySQL and SQLite drivers of PDO that implements the feature request from #42589 by adding the name of the table where the column is from to the output of getColumnMeta().
The change for MySQL seems to be pretty straightforward, but for SQLite I had to add the SQLITE_ENABLE_COLUMN_METADATA preprocessor symbol to the build chain of the bundled SQLite in order to make it work [1]. This means that people who are using their own SQLite build for PDO will not be able to get the table name when their build didn't use the symbol. In this case the corresponding unit test will fail as well. (I haven't found a way to figure out from userland if PDO uses the bundled lib.) This is my first not-totally-trivial patch for PHP and the first time I wrote .phpt unit tests, so please bear with me. - Martin [1] http://www.sqlite.org/capi3ref.html#sqlite3_column_table_name
Index: pdo_mysql/mysql_statement.c =================================================================== RCS file: /repository/php-src/ext/pdo_mysql/mysql_statement.c,v retrieving revision 1.61 diff -u -u -r1.61 mysql_statement.c --- pdo_mysql/mysql_statement.c 28 May 2007 23:43:24 -0000 1.61 +++ pdo_mysql/mysql_statement.c 13 Oct 2007 09:36:57 -0000 @@ -532,6 +532,7 @@ add_assoc_string(return_value, "native_type", str, 1); } + add_assoc_string(return_value, "table", F->table, 1); add_assoc_zval(return_value, "flags", flags); return SUCCESS; } Index: pdo_mysql/tests/bug_42589.phpt =================================================================== RCS file: pdo_mysql/tests/bug_42589.phpt diff -N pdo_mysql/tests/bug_42589.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ pdo_mysql/tests/bug_42589.phpt 13 Oct 2007 09:36:57 -0000 @@ -0,0 +1,30 @@ +--TEST-- +PDO MySQL Feature Request #42589 (getColumnMeta() should also return table name) +--SKIPIF-- +<?php +if (!extension_loaded('pdo') || !extension_loaded('pdo_mysql')) die('skip not loaded'); +require 'ext/pdo_mysql/tests/config.inc'; +require 'ext/pdo/tests/pdo_test.inc'; +PDOTest::skip(); +?> +--INI-- +precision=14 +--FILE-- +<?php +require 'ext/pdo/tests/pdo_test.inc'; +$db = PDOTest::test_factory('ext/pdo_mysql/tests/common.phpt'); + +$db->exec('DROP TABLE IF EXISTS test'); +$db->exec('CREATE TABLE test (field1 VARCHAR(10))'); +$db->exec('INSERT INTO test VALUES("test")'); + +$result = $db->query('SELECT * FROM test t1 LEFT JOIN test t2 ON t1.field1 = t2.field1'); +$meta1 = $result->getColumnMeta(0); +$meta2 = $result->getColumnMeta(1); + +var_dump(!empty($meta1['table']) && $meta1['table'] == 't1'); +var_dump(!empty($meta2['table']) && $meta2['table'] == 't2'); +?> +--EXPECTF-- +bool(true) +bool(true) Index: pdo_sqlite/sqlite_statement.c =================================================================== RCS file: /repository/php-src/ext/pdo_sqlite/sqlite_statement.c,v retrieving revision 1.23 diff -u -u -r1.23 sqlite_statement.c --- pdo_sqlite/sqlite_statement.c 1 Jan 2007 09:29:28 -0000 1.23 +++ pdo_sqlite/sqlite_statement.c 13 Oct 2007 09:36:57 -0000 @@ -292,6 +292,9 @@ add_assoc_string(return_value, "sqlite:decl_type", str, 1); } +#ifdef SQLITE_ENABLE_COLUMN_METADATA + add_assoc_string(return_value, "table", sqlite3_column_table_name(S->stmt, colno), 1); +#endif add_assoc_zval(return_value, "flags", flags); return SUCCESS; Index: pdo_sqlite/config.m4 =================================================================== RCS file: /repository/php-src/ext/pdo_sqlite/config.m4,v retrieving revision 1.35 diff -u -u -r1.35 config.m4 --- pdo_sqlite/config.m4 3 Jul 2007 17:24:37 -0000 1.35 +++ pdo_sqlite/config.m4 13 Oct 2007 09:36:57 -0000 @@ -86,7 +86,7 @@ PHP_NEW_EXTENSION(pdo_sqlite, $php_pdo_sqlite_sources_core $pdo_sqlite_sources, - $ext_shared,,[EMAIL PROTECTED]@/sqlite/src -DPDO_SQLITE_BUNDLED=1 -DSQLITE_OMIT_CURSOR -I$pdo_inc_path) + $ext_shared,,[EMAIL PROTECTED]@/sqlite/src -DPDO_SQLITE_BUNDLED=1 -DSQLITE_OMIT_CURSOR -DSQLITE_ENABLE_COLUMN_METADATA -I$pdo_inc_path) PHP_SUBST(PDO_SQLITE_SHARED_LIBADD) PHP_ADD_BUILD_DIR($ext_builddir/sqlite/src, 1) Index: pdo_sqlite/tests/bug_42589.phpt =================================================================== RCS file: pdo_sqlite/tests/bug_42589.phpt diff -N pdo_sqlite/tests/bug_42589.phpt --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ pdo_sqlite/tests/bug_42589.phpt 13 Oct 2007 09:36:57 -0000 @@ -0,0 +1,23 @@ +--TEST-- +PDO SQLite Feature Request #42589 (getColumnMeta() should also return table name) +--SKIPIF-- +<?php +if (!extension_loaded('pdo') || !extension_loaded('pdo_sqlite')) die('skip not loaded'); +?> +--FILE-- +<?php +$db = new PDO("sqlite::memory:"); + +$db->exec('CREATE TABLE test (field1 VARCHAR(10))'); +$db->exec('INSERT INTO test VALUES("test")'); + +$result = $db->query('SELECT * FROM test t1 LEFT JOIN test t2 ON t1.field1 = t2.field1'); +$meta1 = $result->getColumnMeta(0); +$meta2 = $result->getColumnMeta(1); + +var_dump(!empty($meta1['table']) && $meta1['table'] == 'test'); +var_dump(!empty($meta2['table']) && $meta2['table'] == 'test'); +?> +--EXPECTF-- +bool(true) +bool(true)
-- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php