function MergeTest::testMergeExecutesSingleStatement
Tests that a merge executes as a single native MERGE statement.
File
-
core/
modules/ pgsql/ tests/ src/ Kernel/ pgsql/ MergeTest.php, line 59
Class
- MergeTest
- Tests the native MERGE implementation of the PostgreSQL driver.
Namespace
Drupal\Tests\pgsql\Kernel\pgsqlCode
public function testMergeExecutesSingleStatement() : void {
// Merge-insert: the key does not exist yet.
$result = NULL;
$queries = $this->getLoggedQueries(function () use (&$result) {
$result = $this->connection
->merge('test_people')
->key('job', 'Presenter')
->fields([
'age' => 31,
'name' => 'Tiffany',
])
->execute();
});
$this->assertSame(Merge::STATUS_INSERT, $result);
$this->assertCount(1, $queries);
$this->assertStringContainsString('MERGE INTO', $queries[0]);
// Merge-update: the same statement now matches the existing row.
$queries = $this->getLoggedQueries(function () use (&$result) {
$result = $this->connection
->merge('test_people')
->key('job', 'Presenter')
->fields([
'age' => 32,
'name' => 'Tiffany',
])
->execute();
});
$this->assertSame(Merge::STATUS_UPDATE, $result);
$this->assertCount(1, $queries);
$this->assertStringContainsString('MERGE INTO', $queries[0]);
$person = $this->connection
->query('SELECT * FROM {test_people} WHERE [job] = :job', [
':job' => 'Presenter',
])
->fetch();
$this->assertEquals('Tiffany', $person->name);
$this->assertEquals(32, $person->age);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.