From 41bcffd6fb530c1f104658be12d0cdd6603f2e8f Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 3 Nov 2020 17:52:20 -0500 Subject: [PATCH] Correctly query PostgreSQL byte arrays This required different workarouynd for the native and PDO interfaces --- lib/Db/PostgreSQL/PDOResult.php | 26 +++++++++++++++++++++ lib/Db/PostgreSQL/PDOStatement.php | 14 +++++++++++ lib/Db/PostgreSQL/Result.php | 14 ++++++++++- tests/cases/Db/BaseResult.php | 13 +++++++++++ tests/cases/Db/PostgreSQL/TestResult.php | 13 +++++++++++ tests/cases/Db/PostgreSQLPDO/TestResult.php | 15 +++++++++++- tests/lib/DatabaseDrivers/PostgreSQLPDO.php | 2 +- 7 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 lib/Db/PostgreSQL/PDOResult.php diff --git a/lib/Db/PostgreSQL/PDOResult.php b/lib/Db/PostgreSQL/PDOResult.php new file mode 100644 index 00000000..91fe4c09 --- /dev/null +++ b/lib/Db/PostgreSQL/PDOResult.php @@ -0,0 +1,26 @@ +cur = $this->set->fetch(\PDO::FETCH_ASSOC); + if ($this->cur !== false) { + foreach($this->cur as $k => $v) { + if (is_resource($v)) { + $this->cur[$k] = stream_get_contents($v); + fclose($v); + } + } + return true; + } + return false; + } +} diff --git a/lib/Db/PostgreSQL/PDOStatement.php b/lib/Db/PostgreSQL/PDOStatement.php index c9b7b826..9929579a 100644 --- a/lib/Db/PostgreSQL/PDOStatement.php +++ b/lib/Db/PostgreSQL/PDOStatement.php @@ -6,6 +6,8 @@ declare(strict_types=1); namespace JKingWeb\Arsse\Db\PostgreSQL; +use JKingWeb\Arsse\Db\Result; + class PDOStatement extends \JKingWeb\Arsse\Db\PDOStatement { public static function mungeQuery(string $query, array $types, ...$extraData): string { return Statement::mungeQuery($query, $types, false); @@ -16,4 +18,16 @@ class PDOStatement extends \JKingWeb\Arsse\Db\PDOStatement { // PostgreSQL uses SQLSTATE exclusively, so this is not used return []; } + + public function runArray(array $values = []): Result { + $this->st->closeCursor(); + $this->bindValues($values); + try { + $this->st->execute(); + } catch (\PDOException $e) { + [$excClass, $excMsg, $excData] = $this->buildPDOException(true); + throw new $excClass($excMsg, $excData); + } + return new PDOResult($this->db, $this->st); + } } diff --git a/lib/Db/PostgreSQL/Result.php b/lib/Db/PostgreSQL/Result.php index 03dba17f..67a7352d 100644 --- a/lib/Db/PostgreSQL/Result.php +++ b/lib/Db/PostgreSQL/Result.php @@ -10,6 +10,7 @@ class Result extends \JKingWeb\Arsse\Db\AbstractResult { protected $db; protected $r; protected $cur; + protected $blobs = []; // actual public methods @@ -30,6 +31,11 @@ class Result extends \JKingWeb\Arsse\Db\AbstractResult { public function __construct($db, $result) { $this->db = $db; $this->r = $result; + for ($a = 0, $stop = pg_num_fields($result); $a < $stop; $a++) { + if (pg_field_type($result, $a) === "bytea") { + $this->blobs[$a] = pg_field_name($result, $a); + } + } } public function __destruct() { @@ -41,6 +47,12 @@ class Result extends \JKingWeb\Arsse\Db\AbstractResult { public function valid() { $this->cur = pg_fetch_row($this->r, null, \PGSQL_ASSOC); - return $this->cur !== false; + if ($this->cur !== false) { + foreach($this->blobs as $f) { + $this->cur[$f] = hex2bin(substr($this->cur[$f], 2)); + } + return true; + } + return false; } } diff --git a/tests/cases/Db/BaseResult.php b/tests/cases/Db/BaseResult.php index 4d3d2c49..7d63af2e 100644 --- a/tests/cases/Db/BaseResult.php +++ b/tests/cases/Db/BaseResult.php @@ -10,6 +10,7 @@ use JKingWeb\Arsse\Db\Result; abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest { protected static $insertDefault = "INSERT INTO arsse_test default values"; + protected static $selectBlob = "SELECT x'DEADBEEF' as \"blob\""; protected static $interface; protected $resultClass; @@ -129,4 +130,16 @@ abstract class BaseResult extends \JKingWeb\Arsse\Test\AbstractTest { $test = new $this->resultClass(...$this->makeResult("SELECT '2112' as album, '2112' as track union select 'Clockwork Angels' as album, 'The Wreckers' as track")); $this->assertEquals($exp, $test->getAll()); } + + public function testGetBlobRow(): void { + $exp = ['blob' => hex2bin("DEADBEEF")]; + $test = new $this->resultClass(...$this->makeResult(self::$selectBlob)); + $this->assertEquals($exp, $test->getRow()); + } + + public function testGetBlobValue(): void { + $exp = hex2bin("DEADBEEF"); + $test = new $this->resultClass(...$this->makeResult(self::$selectBlob)); + $this->assertEquals($exp, $test->getValue()); + } } diff --git a/tests/cases/Db/PostgreSQL/TestResult.php b/tests/cases/Db/PostgreSQL/TestResult.php index 0992962b..73dd8fb6 100644 --- a/tests/cases/Db/PostgreSQL/TestResult.php +++ b/tests/cases/Db/PostgreSQL/TestResult.php @@ -15,6 +15,7 @@ class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult { protected static $createMeta = "CREATE TABLE arsse_meta(key text primary key not null, value text)"; protected static $createTest = "CREATE TABLE arsse_test(id bigserial primary key)"; + protected static $selectBlob = "SELECT '\\xDEADBEEF'::bytea as blob"; protected function makeResult(string $q): array { $set = pg_query(static::$interface, $q); @@ -29,4 +30,16 @@ class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult { } parent::tearDownAfterClass(); } + + public function testGetBlobRow(): void { + $exp = ['blob' => hex2bin("DEADBEEF")]; + $test = new $this->resultClass(...$this->makeResult(self::$selectBlob)); + $this->assertEquals($exp, $test->getRow()); + } + + public function testGetBlobValue(): void { + $exp = hex2bin("DEADBEEF"); + $test = new $this->resultClass(...$this->makeResult(self::$selectBlob)); + $this->assertEquals($exp, $test->getValue()); + } } diff --git a/tests/cases/Db/PostgreSQLPDO/TestResult.php b/tests/cases/Db/PostgreSQLPDO/TestResult.php index aaf6bca2..c810b715 100644 --- a/tests/cases/Db/PostgreSQLPDO/TestResult.php +++ b/tests/cases/Db/PostgreSQLPDO/TestResult.php @@ -8,16 +8,29 @@ namespace JKingWeb\Arsse\TestCase\Db\PostgreSQLPDO; /** * @group slow - * @covers \JKingWeb\Arsse\Db\PDOResult + * @covers \JKingWeb\Arsse\Db\PostgreSQL\PDOResult */ class TestResult extends \JKingWeb\Arsse\TestCase\Db\BaseResult { use \JKingWeb\Arsse\Test\DatabaseDrivers\PostgreSQLPDO; protected static $createMeta = "CREATE TABLE arsse_meta(key text primary key not null, value text)"; protected static $createTest = "CREATE TABLE arsse_test(id bigserial primary key)"; + protected static $selectBlob = "SELECT '\\xDEADBEEF'::bytea as blob"; protected function makeResult(string $q): array { $set = static::$interface->query($q); return [static::$interface, $set]; } + + public function testGetBlobRow(): void { + $exp = ['blob' => hex2bin("DEADBEEF")]; + $test = new $this->resultClass(...$this->makeResult(self::$selectBlob)); + $this->assertEquals($exp, $test->getRow()); + } + + public function testGetBlobValue(): void { + $exp = hex2bin("DEADBEEF"); + $test = new $this->resultClass(...$this->makeResult(self::$selectBlob)); + $this->assertSame($exp, $test->getValue()); + } } diff --git a/tests/lib/DatabaseDrivers/PostgreSQLPDO.php b/tests/lib/DatabaseDrivers/PostgreSQLPDO.php index 58001b6f..116c3b23 100644 --- a/tests/lib/DatabaseDrivers/PostgreSQLPDO.php +++ b/tests/lib/DatabaseDrivers/PostgreSQLPDO.php @@ -11,7 +11,7 @@ use JKingWeb\Arsse\Arsse; trait PostgreSQLPDO { protected static $implementation = "PDO PostgreSQL"; protected static $backend = "PostgreSQL"; - protected static $dbResultClass = \JKingWeb\Arsse\Db\PDOResult::class; + protected static $dbResultClass = \JKingWeb\Arsse\Db\PostgreSQL\PDOResult::class; protected static $dbStatementClass = \JKingWeb\Arsse\Db\PostgreSQL\PDOStatement::class; protected static $dbDriverClass = \JKingWeb\Arsse\Db\PostgreSQL\PDODriver::class; protected static $stringOutput = false;