Skip to content

Commit 04c8bbd

Browse files
committed
Fix #66
Numeric should be returned as string. ext-pgsql now has parity with ext-pq for the numeric type.
1 parent 4fffbb2 commit 04c8bbd

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/Internal/PgSqlResultIterator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,9 @@ private function cast(int $oid, ?string $value): array|bool|int|float|string|nul
8989
),
9090
'B' => $value === 't', // Boolean
9191
'N' => match ($oid) { // Numeric
92-
700, 701, 1700 => (float) $value, // float4, float8, and numeric to float
93-
790 => $value, // money includes currency symbol as string
92+
700, 701 => (float) $value, // "float4" and "float8" to float
93+
1700 => $value, // Return "numeric" as string to retain precision
94+
790 => $value, // "money" includes currency symbol as string
9495
default => (int) $value, // All other numeric types cast to an integer
9596
},
9697
default => match ($oid) { // String

test/AbstractLinkTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ abstract class AbstractLinkTest extends AsyncTestCase
2222
tld VARCHAR(63) NOT NULL,
2323
keys INTEGER[] NOT NULL,
2424
enabled BOOLEAN NOT NULL,
25-
number DOUBLE PRECISION NOT NULL,
25+
number NUMERIC NOT NULL,
2626
nullable CHAR(1) DEFAULT NULL,
2727
bytea BYTEA DEFAULT NULL,
2828
json JSON DEFAULT NULL,
@@ -38,10 +38,10 @@ abstract class AbstractLinkTest extends AsyncTestCase
3838
protected function getParams(): array
3939
{
4040
return $this->data ??= [
41-
['amphp', 'org', [1], true, 3.14159, null, new PostgresByteA(\random_bytes(10)), \json_encode('string')],
42-
['github', 'com', [1, 2, 3, 4, 5], false, 2.71828, null, new PostgresByteA(\str_repeat("\0", 10)), \json_encode([1, 2, 3])],
43-
['google', 'com', [1, 2, 3, 4], true, 1.61803, null, new PostgresByteA(\random_bytes(42)), \json_encode(null)],
44-
['php', 'net', [1, 2], false, 0.0, null, null, \json_encode((object) ['value' => 1])],
41+
['amphp', 'org', [1], true, '3.14159', null, new PostgresByteA(\random_bytes(10)), \json_encode('string')],
42+
['github', 'com', [1, 2, 3, 4, 5], false, '2.71828', null, new PostgresByteA(\str_repeat("\0", 10)), \json_encode([1, 2, 3])],
43+
['google', 'com', [1, 2, 3, 4], true, '1.61803', null, new PostgresByteA(\random_bytes(42)), \json_encode(null)],
44+
['php', 'net', [1, 2], false, '0', null, null, \json_encode((object) ['value' => 1])],
4545
];
4646
}
4747

@@ -66,7 +66,7 @@ protected function verifyResult(SqlResult $result, array $data): void
6666
$this->assertSame($data[$i][1], $row['tld']);
6767
$this->assertSame($data[$i][2], $row['keys']);
6868
$this->assertSame($data[$i][3], $row['enabled']);
69-
$this->assertEqualsWithDelta($data[$i][4], $row['number'], 0.001);
69+
$this->assertSame($data[$i][4], $row['number']);
7070
$this->assertNull($row['nullable']);
7171
$this->assertEquals(\json_decode($data[$i][7]), \json_decode($row['json']));
7272
++$i;
@@ -159,7 +159,7 @@ public function testMultipleQueryWithCommandResultFirst()
159159
$this->assertInstanceOf(SqlResult::class, $result);
160160

161161
$data = $this->getData();
162-
$data[] = ['canon', 'jp', [1], true, 4.2, null, null, \json_encode(3.1415926)]; // Add inserted row to expected data.
162+
$data[] = ['canon', 'jp', [1], true, '4.2', null, null, \json_encode(3.1415926)]; // Add inserted row to expected data.
163163

164164
$this->verifyResult($result, $data);
165165

@@ -249,7 +249,7 @@ public function testPrepareWithCommandResult()
249249
'tld' => 'jp',
250250
'keys' => [1],
251251
'enabled' => true,
252-
'number' => 1,
252+
'number' => '1',
253253
'nullable' => null,
254254
'bytea' => null,
255255
'json' => '[1,2,3]',

0 commit comments

Comments
 (0)