View source
<?php
namespace Drupal\KernelTests\Core\Database;
class BasicSyntaxTest extends DatabaseTestBase {
public function testConcatLiterals() {
$result = $this->connection
->query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', [
':a1' => 'This',
':a2' => ' ',
':a3' => 'is',
':a4' => ' a ',
':a5' => 'test.',
]);
$this
->assertSame('This is a test.', $result
->fetchField(), 'Basic CONCAT works.');
}
public function testConcatFields() {
$result = $this->connection
->query('SELECT CONCAT(:a1, CONCAT([job], CONCAT(:a2, CONCAT([age], :a3)))) FROM {test} WHERE [age] = :age', [
':a1' => 'The age of ',
':a2' => ' is ',
':a3' => '.',
':age' => 25,
]);
$this
->assertSame('The age of Singer is 25.', $result
->fetchField(), 'Field CONCAT works.');
}
public function testConcatWsLiterals() {
$result = $this->connection
->query("SELECT CONCAT_WS(', ', :a1, NULL, :a2, :a3, :a4)", [
':a1' => 'Hello',
':a2' => NULL,
':a3' => '',
':a4' => 'world.',
]);
$this
->assertSame('Hello, , world.', $result
->fetchField());
}
public function testConcatWsFields() {
$result = $this->connection
->query("SELECT CONCAT_WS('-', :a1, [name], :a2, [age]) FROM {test} WHERE [age] = :age", [
':a1' => 'name',
':a2' => 'age',
':age' => 25,
]);
$this
->assertSame('name-John-age-25', $result
->fetchField());
}
public function testLikeEscape() {
$this->connection
->insert('test')
->fields([
'name' => 'Ring_',
])
->execute();
$num_matches = $this->connection
->select('test', 't')
->condition('name', 'Ring_', 'LIKE')
->countQuery()
->execute()
->fetchField();
$this
->assertSame('2', $num_matches, 'Found 2 records.');
$num_matches = $this->connection
->select('test', 't')
->condition('name', $this->connection
->escapeLike('Ring_'), 'LIKE')
->countQuery()
->execute()
->fetchField();
$this
->assertSame('1', $num_matches, 'Found 1 record.');
}
public function testLikeBackslash() {
$this->connection
->insert('test')
->fields([
'name',
])
->values([
'name' => 'abcde\\f',
])
->values([
'name' => 'abc%\\_',
])
->execute();
$num_matches = $this->connection
->select('test', 't')
->condition('name', 'abc%\\\\_', 'LIKE')
->countQuery()
->execute()
->fetchField();
$this
->assertSame('2', $num_matches, 'Found 2 records.');
$num_matches = $this->connection
->select('test', 't')
->condition('name', $this->connection
->escapeLike('abc%\\_'), 'LIKE')
->countQuery()
->execute()
->fetchField();
$this
->assertSame('1', $num_matches, 'Found 1 record.');
}
public function testGetFullQualifiedTableName() {
$database = \Drupal::database();
$num_matches = $database
->select($database
->getFullQualifiedTableName('test'), 't')
->countQuery()
->execute()
->fetchField();
$this
->assertSame('4', $num_matches, 'Found 4 records.');
}
public function testAllowSquareBrackets() {
$this->connection
->insert('test')
->fields([
'name',
])
->values([
'name' => '[square]',
])
->execute();
$result = $this->connection
->query("select name from {test} where name = '[square]'", [], [
'allow_square_brackets' => TRUE,
]);
$this
->assertSame('[square]', $result
->fetchField());
$result = $this->connection
->query("select [name] from {test} where [name] = :value", [
':value' => '[square]',
]);
$this
->assertSame('[square]', $result
->fetchField());
$result = $this->connection
->query("select name from {test} where name = :value", [
':value' => '[square]',
], [
'allow_square_brackets' => TRUE,
]);
$this
->assertSame('[square]', $result
->fetchField());
$result = $this->connection
->select('test')
->fields('test', [
'name',
])
->condition('name', '[square]')
->execute();
$this
->assertSame('[square]', $result
->fetchField());
}
}