Skip to content

Commit dc17e44

Browse files
castaneaitaka-oyama
authored andcommitted
Fix connection event order
1 parent e488d2b commit dc17e44

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/Colopl/Spanner/Connection.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -306,21 +306,30 @@ public function statement($query, $bindings = []): bool
306306
*/
307307
public function affectingStatement($query, $bindings = []): int
308308
{
309-
return $this->run($query, $bindings, function ($query, $bindings) {
310-
if ($this->pretending()) {
311-
return 0;
312-
}
309+
$body = function () use ($query, $bindings) {
310+
return $this->run($query, $bindings, function ($query, $bindings) {
311+
if ($this->pretending()) {
312+
return 0;
313+
}
313314

314-
$queryCall = function() use ($query, $bindings) {
315-
return $this->getCurrentTransaction()->executeUpdate($query, ['parameters' => $this->prepareBindings($bindings)]);
316-
};
315+
$queryCall = function () use ($query, $bindings) {
316+
return $this->getCurrentTransaction()->executeUpdate($query, ['parameters' => $this->prepareBindings($bindings)]);
317+
};
317318

318-
$rowCount = $this->inTransaction() ? $queryCall() : $this->transaction($queryCall);
319+
$rowCount = $queryCall();
319320

320-
$this->recordsHaveBeenModified($rowCount > 0);
321+
$this->recordsHaveBeenModified($rowCount > 0);
321322

322-
return $rowCount;
323-
});
323+
return $rowCount;
324+
});
325+
};
326+
327+
if ($this->inTransaction()) {
328+
return $body();
329+
}
330+
331+
// Create a temporary transaction for single affecting statement
332+
return $this->transaction($body);
324333
}
325334

326335
/**

tests/Colopl/Spanner/ConnectionTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,24 @@ public function testStaleReads()
342342
$this->assertEquals($uuid, $row['userId']);
343343
$this->assertEquals('first', $row['name']);
344344
}
345+
346+
public function testEventListenOrder()
347+
{
348+
$receivedEventClasses = [];
349+
$this->app['events']->listen(TransactionBeginning::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = TransactionBeginning::class; });
350+
$this->app['events']->listen(QueryExecuted::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = QueryExecuted::class; });
351+
$this->app['events']->listen(TransactionCommitted::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = TransactionCommitted::class; });
352+
353+
$conn = $this->getDefaultConnection();
354+
355+
$tableName = self::TABLE_NAME_USER;
356+
$uuid = $this->generateUuid();
357+
$name = 'test';
358+
$conn->insert("INSERT INTO ${tableName} (`userId`, `name`) VALUES ('${uuid}', '${name}')");
359+
360+
$this->assertCount(3, $receivedEventClasses);
361+
$this->assertEquals(TransactionBeginning::class, $receivedEventClasses[0]);
362+
$this->assertEquals(QueryExecuted::class, $receivedEventClasses[1]);
363+
$this->assertEquals(TransactionCommitted::class, $receivedEventClasses[2]);
364+
}
345365
}

0 commit comments

Comments
 (0)