Skip to content

Commit

Permalink
Unescape pgsql
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 25, 2020
1 parent 372f3f8 commit 81acca8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
7 changes: 4 additions & 3 deletions examples/bytea.php
Expand Up @@ -21,15 +21,16 @@
/** @var \Amp\Sql\Statement $statement */
$statement = yield $transaction->prepare('INSERT INTO test VALUES (?)');

yield $statement->execute([new ByteA($a = random_bytes(10))]);
yield $statement->execute([new ByteA($b = random_bytes(10))]);
yield $statement->execute([new ByteA($c = random_bytes(10))]);
yield $statement->execute([new ByteA($a = \random_bytes(10))]);
yield $statement->execute([new ByteA($b = \random_bytes(10))]);
yield $statement->execute([new ByteA($c = \random_bytes(10))]);

/** @var \Amp\Postgres\ResultSet $result */
$result = yield $transaction->execute('SELECT * FROM test WHERE val = :val', ['val' => new ByteA($a)]);

while (yield $result->advance()) {
$row = $result->getCurrent();
\assert($row['val'] === $a);
}

yield $transaction->rollback();
Expand Down
6 changes: 3 additions & 3 deletions src/PgSqlHandle.php
Expand Up @@ -152,10 +152,10 @@ public function __destruct()
}

/**
* Escape parameters (INTERNAL)
* Escape parameters (INTERNAL).
*
* @internal Only for internal use
*
*
* @param array $params
* @return array
*/
Expand All @@ -164,7 +164,7 @@ public function escapeParams(array $params): array
foreach ($params as $key => $param) {
if ($param instanceof ByteA) {
$params[$key] = \pg_escape_bytea($this->handle, $param->getString());
} elseif (is_array($param)) {
} elseif (\is_array($param)) {
$params[$key] = $this->escapeParams($param);
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/PgSqlResultSet.php
Expand Up @@ -149,13 +149,15 @@ private function cast(int $column, string $value)
case 1020: // box[] (semi-colon delimited)
return $this->parser->parse($value, null, ';');

case 1001: // bytea[]
return \pg_unescape_bytea($this->handle, $value);

case 199: // json[]
case 629: // line[]
case 651: // cidr[]
case 719: // circle[]
case 775: // macaddr8[]
case 791: // money[]
case 1001: // bytea[]
case 1002: // char[]
case 1003: // name[]
case 1006: // int2vector[]
Expand Down
6 changes: 3 additions & 3 deletions src/PqHandle.php
Expand Up @@ -296,10 +296,10 @@ private function release(): void
}

/**
* Escape parameters (INTERNAL)
* Escape parameters (INTERNAL).
*
* @internal Only for internal use
*
*
* @param array $params
* @return array
*/
Expand All @@ -308,7 +308,7 @@ public function escapeParams(array $params): array
foreach ($params as $key => $param) {
if ($param instanceof ByteA) {
$params[$key] = $this->handle->escapeByteA($param->getString());
} elseif (is_array($param)) {
} elseif (\is_array($param)) {
$params[$key] = $this->escapeParams($param);
}
}
Expand Down

0 comments on commit 81acca8

Please sign in to comment.