]> granicus.if.org Git - php/commitdiff
[DOC] Added PDO::FETCH_KEY_PAIR mode that will fetch a 2 column result set
authorIlia Alshanetsky <iliaa@php.net>
Wed, 16 May 2007 19:33:57 +0000 (19:33 +0000)
committerIlia Alshanetsky <iliaa@php.net>
Wed, 16 May 2007 19:33:57 +0000 (19:33 +0000)
into an associated array.

ext/pdo/pdo_dbh.c
ext/pdo/pdo_stmt.c
ext/pdo/php_pdo_driver.h
ext/pdo/tests/pdo_034.phpt [new file with mode: 0644]

index 6d4143e04c91c08b00a39cbb15b410fac2e66664..af0db42771c7a2394c4f69037e86b9fc94bac069 100755 (executable)
@@ -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);
index 13170a30342b7bfe3603b0e0dad83d771e704b33..758a09db2c4494b3194b6aa12f50079bc3876284 100755 (executable)
@@ -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:
index e645c58be7a09f810ecb4b73a60ccaf42edddd6f..77e2eaeb7790e5206125de10b45b38327e89ab15 100755 (executable)
@@ -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 (file)
index 0000000..ea6ff03
--- /dev/null
@@ -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)