diff options
-rwxr-xr-x | ext/pdo/pdo_dbh.c | 1 | ||||
-rwxr-xr-x | ext/pdo/pdo_stmt.c | 28 | ||||
-rwxr-xr-x | ext/pdo/php_pdo_driver.h | 1 | ||||
-rw-r--r-- | ext/pdo/tests/pdo_034.phpt | 64 |
4 files changed, 94 insertions, 0 deletions
diff --git a/ext/pdo/pdo_dbh.c b/ext/pdo/pdo_dbh.c index 6d4143e04c..af0db42771 100755 --- a/ext/pdo/pdo_dbh.c +++ b/ext/pdo/pdo_dbh.c @@ -1313,6 +1313,7 @@ void pdo_dbh_init(TSRMLS_D) REGISTER_PDO_CLASS_CONST_LONG("FETCH_FUNC", (long)PDO_FETCH_FUNC); REGISTER_PDO_CLASS_CONST_LONG("FETCH_GROUP",(long)PDO_FETCH_GROUP); REGISTER_PDO_CLASS_CONST_LONG("FETCH_UNIQUE",(long)PDO_FETCH_UNIQUE); + REGISTER_PDO_CLASS_CONST_LONG("FETCH_KEY_PAIR",(long)PDO_FETCH_KEY_PAIR); REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASSTYPE",(long)PDO_FETCH_CLASSTYPE); #if PHP_MAJOR_VERSION > 5 || PHP_MINOR_VERSION >= 1 REGISTER_PDO_CLASS_CONST_LONG("FETCH_SERIALIZE",(long)PDO_FETCH_SERIALIZE); diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c index 13170a3034..758a09db2c 100755 --- a/ext/pdo/pdo_stmt.c +++ b/ext/pdo/pdo_stmt.c @@ -900,6 +900,15 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, } break; + case PDO_FETCH_KEY_PAIR: + if (stmt->column_count != 2) { + pdo_raise_impl_error(stmt->dbh, stmt, "HY000", "PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain extactly 2 columns." TSRMLS_CC); + return 0; + } + + array_init(return_value); + break; + case PDO_FETCH_COLUMN: if (stmt->fetch.column >= 0 && stmt->fetch.column < stmt->column_count) { fetch_value(stmt, return_value, stmt->fetch.column, NULL TSRMLS_CC); @@ -1023,6 +1032,25 @@ static int do_fetch(pdo_stmt_t *stmt, int do_bind, zval *return_value, case PDO_FETCH_ASSOC: add_assoc_zval(return_value, stmt->columns[i].name, val); break; + + case PDO_FETCH_KEY_PAIR: + { + zval *tmp; + MAKE_STD_ZVAL(tmp); + fetch_value(stmt, tmp, ++i, NULL TSRMLS_CC); + + if (Z_TYPE_P(val) == IS_STRING) { + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL_P(val), Z_STRLEN_P(val) + 1, &tmp, sizeof(zval *), NULL); + } else if (Z_TYPE_P(val) = IS_LONG) { + zend_hash_index_update(Z_ARRVAL_P(return_value), Z_LVAL_P(val), tmp, sizeof(tmp), NULL); + } else { + convert_to_string(val); + zend_symtable_update(Z_ARRVAL_P(return_value), Z_STRVAL_P(val), Z_STRLEN_P(val) + 1, &tmp, sizeof(zval *), NULL); + } + zval_dtor(val); + FREE_ZVAL(val); + } + break; case PDO_FETCH_USE_DEFAULT: case PDO_FETCH_BOTH: diff --git a/ext/pdo/php_pdo_driver.h b/ext/pdo/php_pdo_driver.h index e645c58be7..77e2eaeb77 100755 --- a/ext/pdo/php_pdo_driver.h +++ b/ext/pdo/php_pdo_driver.h @@ -90,6 +90,7 @@ enum pdo_fetch_type { PDO_FETCH_INTO, /* fetch row into an existing object */ PDO_FETCH_FUNC, /* fetch into function and return its result */ PDO_FETCH_NAMED, /* like PDO_FETCH_ASSOC, but can handle duplicate names */ + PDO_FETCH_KEY_PAIR, /* fetch into an array where the 1st column is a key and all subsequent columns are values */ PDO_FETCH__MAX /* must be last */ }; diff --git a/ext/pdo/tests/pdo_034.phpt b/ext/pdo/tests/pdo_034.phpt new file mode 100644 index 0000000000..ea6ff0388a --- /dev/null +++ b/ext/pdo/tests/pdo_034.phpt @@ -0,0 +1,64 @@ +--TEST-- +PDO Common: PDO::FETCH_KEY_PAIR fetch mode test +--SKIPIF-- +<?php # vim:ft=php +if (!extension_loaded('pdo')) die('skip'); +$dir = getenv('REDIR_TEST_DIR'); +if (false == $dir) die('skip no driver'); +require_once $dir . 'pdo_test.inc'; +PDOTest::skip(); +?> +--FILE-- +<?php +if (getenv('REDIR_TEST_DIR') === false) putenv('REDIR_TEST_DIR='.dirname(__FILE__) . '/../../pdo/tests/'); +require_once getenv('REDIR_TEST_DIR') . 'pdo_test.inc'; +$db = PDOTest::factory(); + +$db->exec("CREATE TABLE test (a char(100), b char(100), c char(100))"); + +for ($i = 0; $i < 5; $i++) { + $db->exec("INSERT INTO test (a,b,c) VALUES('test".$i."','".$i."','".$i."')"); +} + +var_dump($db->query("SELECT a,b FROM test")->fetch(PDO::FETCH_KEY_PAIR)); +var_dump($db->query("SELECT a,b FROM test")->fetchAll(PDO::FETCH_KEY_PAIR)); +var_dump($db->query("SELECT * FROM test")->fetch(PDO::FETCH_KEY_PAIR)); + +?> +--EXPECTF-- +array(1) { + ["test0"]=> + string(1) "0" +} +array(5) { + [0]=> + array(1) { + ["test0"]=> + string(1) "0" + } + [1]=> + array(1) { + ["test1"]=> + string(1) "1" + } + [2]=> + array(1) { + ["test2"]=> + string(1) "2" + } + [3]=> + array(1) { + ["test3"]=> + string(1) "3" + } + [4]=> + array(1) { + ["test4"]=> + string(1) "4" + } +} + +Warning: PDOStatement::fetch(): SQLSTATE[HY000]: General error: PDO::FETCH_KEY_PAIR fetch mode requires the result set to contain extactly 2 columns. in %s/tests/pdo_034.php on line %d + +Warning: PDOStatement::fetch(): SQLSTATE[HY000]: General error in %s/tests/pdo_034.php on line %d +bool(false) |