22
33namespace Amp \Postgres \Internal ;
44
5+ use Amp \Postgres \PostgresArray ;
56use Amp \Postgres \PostgresByteA ;
67use 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 ),
0 commit comments