Skip to content

Commit abcfc45

Browse files
committed
Add PostgresArray
1 parent 0d1d0b3 commit abcfc45

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/Internal/functions.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Amp\Postgres\Internal;
44

5+
use Amp\Postgres\PostgresArray;
56
use Amp\Postgres\PostgresByteA;
67
use Amp\Postgres\PostgresExecutor;
78

@@ -105,8 +106,9 @@ function encodeParam(PostgresExecutor $executor, mixed $value): string|int|float
105106
return match (\gettype($value)) {
106107
"NULL", "integer", "double", "string" => $value,
107108
"boolean" => $value ? 't' : 'f',
108-
"array" => '{' . \implode(',', \array_map(fn ($i) => encodeArrayItem($executor, $i), $value)) . '}',
109+
"array" => encodeArray($executor, $value, ','),
109110
"object" => match (true) {
111+
$value instanceof PostgresArray => $value->encode($executor),
110112
$value instanceof PostgresByteA => $executor->escapeByteA($value->getData()),
111113
$value instanceof \BackedEnum => $value->value,
112114
$value instanceof \Stringable => (string) $value,
@@ -122,6 +124,14 @@ function encodeParam(PostgresExecutor $executor, mixed $value): string|int|float
122124
};
123125
}
124126

127+
/**
128+
* @internal
129+
*/
130+
function encodeArray(PostgresExecutor $executor, array $array, string $delimiter): string
131+
{
132+
return '{' . \implode($delimiter, \array_map(fn ($i) => encodeArrayItem($executor, $i), $array)) . '}';
133+
}
134+
125135
/**
126136
* @internal
127137
*
@@ -133,7 +143,10 @@ function encodeArrayItem(PostgresExecutor $executor, mixed $value): mixed
133143
"NULL" => "NULL",
134144
"string" => '"' . \str_replace(['\\', '"'], ['\\\\', '\\"'], $value) . '"',
135145
"array", "boolean", "integer", "double" => encodeParam($executor, $value),
136-
"object" => encodeArrayItem($executor, encodeParam($executor, $value)),
146+
"object" => match (true) {
147+
$value instanceof PostgresArray => encodeParam($executor, $value),
148+
default => encodeArrayItem($executor, encodeParam($executor, $value)),
149+
},
137150
default => throw new \TypeError(\sprintf(
138151
"Invalid value type '%s' in array",
139152
\get_debug_type($value),

src/PostgresArray.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Amp\Postgres;
4+
5+
final class PostgresArray
6+
{
7+
public function __construct(
8+
private readonly array $data,
9+
private readonly string $delimiter = ',',
10+
) {
11+
}
12+
13+
/**
14+
* Encodes the array for use in a query. Note that the value returned must still be quoted using
15+
* {@see PostgresExecutor::quoteLiteral()} if inserting directly into a query string.
16+
*/
17+
public function encode(PostgresExecutor $executor): string
18+
{
19+
return Internal\encodeArray($executor, $this->data, $this->delimiter);
20+
}
21+
}

0 commit comments

Comments
 (0)