diff --git a/src/PgSqlHandle.php b/src/PgSqlHandle.php index 96b16d5..a596f37 100644 --- a/src/PgSqlHandle.php +++ b/src/PgSqlHandle.php @@ -366,7 +366,7 @@ final class PgSqlHandle implements Handle $modifiedSql = Internal\parseNamedParams($sql, $names); - $name = Handle::STATEMENT_NAME_PREFIX . \sha1($modifiedSql); + $name = Handle::STATEMENT_NAME_PREFIX . \sha1($sql); if (isset($this->statements[$name])) { $storage = $this->statements[$name]; diff --git a/src/Pool.php b/src/Pool.php index bf901af..e05ff66 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -54,9 +54,7 @@ final class Pool extends ConnectionPool implements Link $this->statements = $statements = new class($maxConnections) extends LRUCache implements \IteratorAggregate { public function getIterator(): \Iterator { - foreach ($this->data as $key => $data) { - yield $key => $data; - } + yield from $this->data; } }; @@ -147,8 +145,10 @@ final class Pool extends ConnectionPool implements Link } return call(function () use ($sql) { - if ($this->statements->containsKey($sql)) { - $statement = $this->statements->get($sql); + $name = Handle::STATEMENT_NAME_PREFIX . \sha1($sql); + + if ($this->statements->containsKey($name)) { + $statement = $this->statements->get($name); if ($statement instanceof Promise) { $statement = yield $statement; // Wait for prior request to resolve. @@ -162,17 +162,17 @@ final class Pool extends ConnectionPool implements Link } $promise = parent::prepare($sql); - $this->statements->put($sql, $promise); // Insert promise into queue so subsequent requests get promise. + $this->statements->put($name, $promise); // Insert promise into queue so subsequent requests get promise. try { $statement = yield $promise; \assert($statement instanceof StatementPool); } catch (\Throwable $exception) { - $this->statements->remove($sql); + $this->statements->remove($name); throw $exception; } - $this->statements->put($sql, $statement); // Replace promise in queue with statement object. + $this->statements->put($name, $statement); // Replace promise in queue with statement object. return $statement; }); diff --git a/src/PqHandle.php b/src/PqHandle.php index 64f8e92..e9fce5c 100644 --- a/src/PqHandle.php +++ b/src/PqHandle.php @@ -387,7 +387,7 @@ final class PqHandle implements Handle $modifiedSql = Internal\parseNamedParams($sql, $names); - $name = Handle::STATEMENT_NAME_PREFIX . \sha1($modifiedSql); + $name = Handle::STATEMENT_NAME_PREFIX . \sha1($sql); if (isset($this->statements[$name])) { $storage = $this->statements[$name]; diff --git a/test/AbstractLinkTest.php b/test/AbstractLinkTest.php index e83dc02..63b0687 100644 --- a/test/AbstractLinkTest.php +++ b/test/AbstractLinkTest.php @@ -346,6 +346,24 @@ abstract class AbstractLinkTest extends TestCase }); } + public function testPrepareSimilarQueryReturnsDifferentStatements() + { + Loop::run(function () { + /** @var Statement $statement1 */ + $statement1 = $this->connection->prepare("SELECT * FROM test WHERE domain=\$1"); + + /** @var Statement $statement2 */ + $statement2 = $this->connection->prepare("SELECT * FROM test WHERE domain=:domain"); + + list($statement1, $statement2) = yield [$statement1, $statement2]; + + $this->assertInstanceOf(Statement::class, $statement1); + $this->assertInstanceOf(Statement::class, $statement2); + + $this->assertNotSame($statement1, $statement2); + }); + } + public function testPrepareThenExecuteWithUnconsumedTupleResult() { Loop::run(function () {