Skip to content

Commit

Permalink
Fix connection event order
Browse files Browse the repository at this point in the history
  • Loading branch information
castaneai authored and taka-oyama committed Jun 13, 2019
1 parent e488d2b commit dc17e44
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
31 changes: 20 additions & 11 deletions src/Colopl/Spanner/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,21 +306,30 @@ public function statement($query, $bindings = []): bool
*/
public function affectingStatement($query, $bindings = []): int
{
return $this->run($query, $bindings, function ($query, $bindings) {
if ($this->pretending()) {
return 0;
}
$body = function () use ($query, $bindings) {
return $this->run($query, $bindings, function ($query, $bindings) {
if ($this->pretending()) {
return 0;
}

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

$rowCount = $this->inTransaction() ? $queryCall() : $this->transaction($queryCall);
$rowCount = $queryCall();

$this->recordsHaveBeenModified($rowCount > 0);
$this->recordsHaveBeenModified($rowCount > 0);

return $rowCount;
});
return $rowCount;
});
};

if ($this->inTransaction()) {
return $body();
}

// Create a temporary transaction for single affecting statement
return $this->transaction($body);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/Colopl/Spanner/ConnectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,24 @@ public function testStaleReads()
$this->assertEquals($uuid, $row['userId']);
$this->assertEquals('first', $row['name']);
}

public function testEventListenOrder()
{
$receivedEventClasses = [];
$this->app['events']->listen(TransactionBeginning::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = TransactionBeginning::class; });
$this->app['events']->listen(QueryExecuted::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = QueryExecuted::class; });
$this->app['events']->listen(TransactionCommitted::class, function () use (&$receivedEventClasses) { $receivedEventClasses[] = TransactionCommitted::class; });

$conn = $this->getDefaultConnection();

$tableName = self::TABLE_NAME_USER;
$uuid = $this->generateUuid();
$name = 'test';
$conn->insert("INSERT INTO ${tableName} (`userId`, `name`) VALUES ('${uuid}', '${name}')");

$this->assertCount(3, $receivedEventClasses);
$this->assertEquals(TransactionBeginning::class, $receivedEventClasses[0]);
$this->assertEquals(QueryExecuted::class, $receivedEventClasses[1]);
$this->assertEquals(TransactionCommitted::class, $receivedEventClasses[2]);
}
}

0 comments on commit dc17e44

Please sign in to comment.