]> granicus.if.org Git - php/commitdiff
PDO: Honor ATTR_STRINGIFY_FETCHES for booleans
authorNikita Popov <nikita.ppv@gmail.com>
Mon, 21 Dec 2020 14:31:15 +0000 (15:31 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 21 Dec 2020 14:32:32 +0000 (15:32 +0100)
Of the important PDO drivers, this affects only PDO PgSQL, as
both MySQL and SQLite do not return native boolean types.

UPGRADING
ext/pdo/pdo_stmt.c
ext/pdo_pgsql/tests/bug62593.phpt
ext/pdo_pgsql/tests/bug71885.phpt
ext/pdo_pgsql/tests/bug71885_2.phpt
ext/pdo_pgsql/tests/bug75402.phpt

index c659515f51b2d4b8f6d24b1306ada7ea42a295b9..222b30b3e981d28977df01c3e354de79594ccabb 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -35,6 +35,10 @@ PHP 8.1 UPGRADE NOTES
   . The mysqlnd.fetch_copy_data ini setting has been removed. However, this
     should not result in user-visible behavior changes.
 
+- PDO:
+  . PDO::ATTR_STRINGIFY_FETCHES now also stringifies values of type bool to
+    "0" or "1". Previously booleans were not stringified.
+
 - PDO MySQL:
   . Integers and floats in result sets will now be returned using native PHP
     types instead of strings when using emulated prepared statements. This
index 7ea0f6df0fcc733ad3ec46aa7d0845e4fd7ca640..e1c51ef29a269c58959f253bfb3b03ea0f6f18bf 100644 (file)
@@ -616,6 +616,13 @@ static inline void fetch_value(pdo_stmt_t *stmt, zval *dest, int colno, int *typ
 
        if (stmt->dbh->stringify) {
                switch (Z_TYPE_P(dest)) {
+                       case IS_FALSE:
+                               /* Return "0" rather than "", because this is what database drivers that
+                                * don't have a dedicated boolean type would return. */
+                               zval_ptr_dtor_nogc(dest);
+                               ZVAL_INTERNED_STR(dest, ZSTR_CHAR('0'));
+                               break;
+                       case IS_TRUE:
                        case IS_LONG:
                        case IS_DOUBLE:
                                convert_to_string(dest);
index 598307a6521056d93622ef906641f38caba68404..a50bcdbc25b8ed1a23f48da53a3238f27cb4ac9b 100644 (file)
@@ -13,6 +13,7 @@ require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
 $db = PDOTest::test_factory(__DIR__ . '/common.phpt');
 $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
 $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
+$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
 $errors = array();
 
 $value = true;
index f47ffcb4441c218686216eaf133601dab4bf6989..a0a72b6796117764f73e3c189ee5a71189aa3362 100644 (file)
@@ -37,10 +37,10 @@ foreach ([false, true] as $emulate) {
 string(3) "ERR"
 array(1) {
   [0]=>
-  bool(true)
+  string(1) "1"
 }
 array(1) {
   [0]=>
-  bool(true)
+  string(1) "1"
 }
 ==OK==
index 2f9b9923b06fd11a2abee4083896fcf9f8052e27..334899db488969db4f4463d6b87d5490c6737bd0 100644 (file)
@@ -40,18 +40,18 @@ foreach ([false, true] as $emulate) {
 --EXPECT--
 array(1) {
   [0]=>
-  bool(false)
+  string(1) "0"
 }
 array(1) {
   [0]=>
-  bool(true)
+  string(1) "1"
 }
 array(1) {
   [0]=>
-  bool(false)
+  string(1) "0"
 }
 array(1) {
   [0]=>
-  bool(true)
+  string(1) "1"
 }
 ==OK==
index 3aa26660ef6adf03ea037c906e9c2147510e8bbb..5db01b16b57fe6d5487326e9ee18af0579aa5e2d 100644 (file)
@@ -105,7 +105,7 @@ object(stdClass)#2 (1) {
       ["sprogress"]=>
       string(3) "100"
       ["bhidden"]=>
-      bool(false)
+      string(1) "0"
       ["sdatetime"]=>
       string(19) "2017.10.16 08:36:45"
     }