Skip to content

Commit cf43bc0

Browse files
committed
Fix resuing hashed name for similar queries
1 parent ccce08f commit cf43bc0

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/PgSqlHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ public function prepare(string $sql): Promise
366366

367367
$modifiedSql = Internal\parseNamedParams($sql, $names);
368368

369-
$name = Handle::STATEMENT_NAME_PREFIX . \sha1($modifiedSql);
369+
$name = Handle::STATEMENT_NAME_PREFIX . \sha1($sql);
370370

371371
if (isset($this->statements[$name])) {
372372
$storage = $this->statements[$name];

src/Pool.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ public function __construct(
5454
$this->statements = $statements = new class($maxConnections) extends LRUCache implements \IteratorAggregate {
5555
public function getIterator(): \Iterator
5656
{
57-
foreach ($this->data as $key => $data) {
58-
yield $key => $data;
59-
}
57+
yield from $this->data;
6058
}
6159
};
6260

@@ -147,8 +145,10 @@ public function prepare(string $sql): Promise
147145
}
148146

149147
return call(function () use ($sql) {
150-
if ($this->statements->containsKey($sql)) {
151-
$statement = $this->statements->get($sql);
148+
$name = Handle::STATEMENT_NAME_PREFIX . \sha1($sql);
149+
150+
if ($this->statements->containsKey($name)) {
151+
$statement = $this->statements->get($name);
152152

153153
if ($statement instanceof Promise) {
154154
$statement = yield $statement; // Wait for prior request to resolve.
@@ -162,17 +162,17 @@ public function prepare(string $sql): Promise
162162
}
163163

164164
$promise = parent::prepare($sql);
165-
$this->statements->put($sql, $promise); // Insert promise into queue so subsequent requests get promise.
165+
$this->statements->put($name, $promise); // Insert promise into queue so subsequent requests get promise.
166166

167167
try {
168168
$statement = yield $promise;
169169
\assert($statement instanceof StatementPool);
170170
} catch (\Throwable $exception) {
171-
$this->statements->remove($sql);
171+
$this->statements->remove($name);
172172
throw $exception;
173173
}
174174

175-
$this->statements->put($sql, $statement); // Replace promise in queue with statement object.
175+
$this->statements->put($name, $statement); // Replace promise in queue with statement object.
176176

177177
return $statement;
178178
});

src/PqHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ public function prepare(string $sql): Promise
387387

388388
$modifiedSql = Internal\parseNamedParams($sql, $names);
389389

390-
$name = Handle::STATEMENT_NAME_PREFIX . \sha1($modifiedSql);
390+
$name = Handle::STATEMENT_NAME_PREFIX . \sha1($sql);
391391

392392
if (isset($this->statements[$name])) {
393393
$storage = $this->statements[$name];

test/AbstractLinkTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,24 @@ public function testSimultaneousPrepareSameQuery()
346346
});
347347
}
348348

349+
public function testPrepareSimilarQueryReturnsDifferentStatements()
350+
{
351+
Loop::run(function () {
352+
/** @var Statement $statement1 */
353+
$statement1 = $this->connection->prepare("SELECT * FROM test WHERE domain=\$1");
354+
355+
/** @var Statement $statement2 */
356+
$statement2 = $this->connection->prepare("SELECT * FROM test WHERE domain=:domain");
357+
358+
list($statement1, $statement2) = yield [$statement1, $statement2];
359+
360+
$this->assertInstanceOf(Statement::class, $statement1);
361+
$this->assertInstanceOf(Statement::class, $statement2);
362+
363+
$this->assertNotSame($statement1, $statement2);
364+
});
365+
}
366+
349367
public function testPrepareThenExecuteWithUnconsumedTupleResult()
350368
{
351369
Loop::run(function () {

0 commit comments

Comments
 (0)