Skip to content

Commit

Permalink
Fix #66
Browse files Browse the repository at this point in the history
Numeric should be returned as string. ext-pgsql now has parity with ext-pq for the numeric type.
  • Loading branch information
trowski committed Oct 11, 2024
1 parent 4fffbb2 commit 04c8bbd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/Internal/PgSqlResultIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ private function cast(int $oid, ?string $value): array|bool|int|float|string|nul
),
'B' => $value === 't', // Boolean
'N' => match ($oid) { // Numeric
700, 701, 1700 => (float) $value, // float4, float8, and numeric to float
790 => $value, // money includes currency symbol as string
700, 701 => (float) $value, // "float4" and "float8" to float
1700 => $value, // Return "numeric" as string to retain precision
790 => $value, // "money" includes currency symbol as string
default => (int) $value, // All other numeric types cast to an integer
},
default => match ($oid) { // String
Expand Down
16 changes: 8 additions & 8 deletions test/AbstractLinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class AbstractLinkTest extends AsyncTestCase
tld VARCHAR(63) NOT NULL,
keys INTEGER[] NOT NULL,
enabled BOOLEAN NOT NULL,
number DOUBLE PRECISION NOT NULL,
number NUMERIC NOT NULL,
nullable CHAR(1) DEFAULT NULL,
bytea BYTEA DEFAULT NULL,
json JSON DEFAULT NULL,
Expand All @@ -38,10 +38,10 @@ abstract class AbstractLinkTest extends AsyncTestCase
protected function getParams(): array
{
return $this->data ??= [
['amphp', 'org', [1], true, 3.14159, null, new PostgresByteA(\random_bytes(10)), \json_encode('string')],
['github', 'com', [1, 2, 3, 4, 5], false, 2.71828, null, new PostgresByteA(\str_repeat("\0", 10)), \json_encode([1, 2, 3])],
['google', 'com', [1, 2, 3, 4], true, 1.61803, null, new PostgresByteA(\random_bytes(42)), \json_encode(null)],
['php', 'net', [1, 2], false, 0.0, null, null, \json_encode((object) ['value' => 1])],
['amphp', 'org', [1], true, '3.14159', null, new PostgresByteA(\random_bytes(10)), \json_encode('string')],
['github', 'com', [1, 2, 3, 4, 5], false, '2.71828', null, new PostgresByteA(\str_repeat("\0", 10)), \json_encode([1, 2, 3])],
['google', 'com', [1, 2, 3, 4], true, '1.61803', null, new PostgresByteA(\random_bytes(42)), \json_encode(null)],
['php', 'net', [1, 2], false, '0', null, null, \json_encode((object) ['value' => 1])],
];
}

Expand All @@ -66,7 +66,7 @@ protected function verifyResult(SqlResult $result, array $data): void
$this->assertSame($data[$i][1], $row['tld']);
$this->assertSame($data[$i][2], $row['keys']);
$this->assertSame($data[$i][3], $row['enabled']);
$this->assertEqualsWithDelta($data[$i][4], $row['number'], 0.001);
$this->assertSame($data[$i][4], $row['number']);
$this->assertNull($row['nullable']);
$this->assertEquals(\json_decode($data[$i][7]), \json_decode($row['json']));
++$i;
Expand Down Expand Up @@ -159,7 +159,7 @@ public function testMultipleQueryWithCommandResultFirst()
$this->assertInstanceOf(SqlResult::class, $result);

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

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

Expand Down Expand Up @@ -249,7 +249,7 @@ public function testPrepareWithCommandResult()
'tld' => 'jp',
'keys' => [1],
'enabled' => true,
'number' => 1,
'number' => '1',
'nullable' => null,
'bytea' => null,
'json' => '[1,2,3]',
Expand Down

0 comments on commit 04c8bbd

Please sign in to comment.