1
0
mirror of https://github.com/php/php-src.git synced 2026-03-24 00:02:20 +01:00

Fix the incorrect data type of float values in PDO query results

Close GH-12476
This commit is contained in:
Yurun
2023-10-20 16:54:05 +08:00
committed by David Carlier
parent 8372da71eb
commit 6d10a69898
3 changed files with 153 additions and 0 deletions

1
NEWS
View File

@@ -19,6 +19,7 @@ Opcache:
PDO_PGSQL:
. Fixed GH-12423, DSN credentials being prioritized over the user/password
PDO constructor arguments. (SakiTakamachi)
. Fixed native float support with pdo_pgsql query results. (Yurunsoft)
PGSQL:
. Added the possibility to have no conditions for pg_select. (OmarEmaraDev)

View File

@@ -51,6 +51,10 @@
#define TIMESTAMPOID 1114
#define VARCHARLABEL "varchar"
#define VARCHAROID 1043
#define FLOAT4LABEL "float4"
#define FLOAT4OID 700
#define FLOAT8LABEL "float8"
#define FLOAT8OID 701
@@ -513,6 +517,18 @@ static int pgsql_stmt_get_col(pdo_stmt_t *stmt, int colno, zval *result, enum pd
#endif
ZVAL_LONG(result, ZEND_ATOL(ptr));
break;
case FLOAT4OID:
case FLOAT8OID:
if (strncmp(ptr, "Infinity", len) == 0) {
ZVAL_DOUBLE(result, ZEND_INFINITY);
} else if (strncmp(ptr, "-Infinity", len) == 0) {
ZVAL_DOUBLE(result, -ZEND_INFINITY);
} else if (strncmp(ptr, "NaN", len) == 0) {
ZVAL_DOUBLE(result, ZEND_NAN);
} else {
ZVAL_DOUBLE(result, zend_strtod(ptr, NULL));
}
break;
case OIDOID: {
char *end_ptr;
@@ -632,6 +648,12 @@ static int pgsql_stmt_get_column_meta(pdo_stmt_t *stmt, zend_long colno, zval *r
case INT4OID:
add_assoc_string(return_value, "native_type", INT4LABEL);
break;
case FLOAT4OID:
add_assoc_string(return_value, "native_type", FLOAT4LABEL);
break;
case FLOAT8OID:
add_assoc_string(return_value, "native_type", FLOAT8LABEL);
break;
case TEXTOID:
add_assoc_string(return_value, "native_type", TEXTLABEL);
break;

View File

@@ -0,0 +1,130 @@
--TEST--
PDO PgSQL float value
--EXTENSIONS--
pdo
pdo_pgsql
--SKIPIF--
<?php
require __DIR__ . '/config.inc';
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
PDOTest::skip();
?>
--FILE--
<?php
require __DIR__ . '/../../../ext/pdo/tests/pdo_test.inc';
$db = PDOTest::test_factory(__DIR__ . '/common.phpt');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$db->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
$stmt = $db->query(<<<'SQL'
select cast(0.4 as float4) as value4,
cast(0.8 as float8) as value8,
cast('NaN' as float4) as valuenan,
cast('Infinity' as float4) as valueinfinity,
cast('-Infinity' as float4) as valueninfinity
SQL);
var_dump($stmt->fetchAll());
var_dump($stmt->getColumnMeta(0));
var_dump($stmt->getColumnMeta(1));
var_dump($stmt->getColumnMeta(2));
var_dump($stmt->getColumnMeta(3));
var_dump($stmt->getColumnMeta(4));
?>
--EXPECT--
array(1) {
[0]=>
array(5) {
["value4"]=>
float(0.4)
["value8"]=>
float(0.8)
["valuenan"]=>
float(NAN)
["valueinfinity"]=>
float(INF)
["valueninfinity"]=>
float(-INF)
}
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(6) "value4"
["len"]=>
int(4)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(701)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float8"
["pdo_type"]=>
int(2)
["name"]=>
string(6) "value8"
["len"]=>
int(8)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(8) "valuenan"
["len"]=>
int(4)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(13) "valueinfinity"
["len"]=>
int(4)
["precision"]=>
int(-1)
}
array(7) {
["pgsql:oid"]=>
int(700)
["pgsql:table_oid"]=>
int(0)
["native_type"]=>
string(6) "float4"
["pdo_type"]=>
int(2)
["name"]=>
string(14) "valueninfinity"
["len"]=>
int(4)
["precision"]=>
int(-1)
}