3 namespace Drupal\KernelTests\Core\Database;
6 * Tests SQL syntax interpretation.
8 * In order to ensure consistent SQL handling throughout Drupal
9 * across multiple kinds of database systems, we test that the
10 * database system interprets SQL syntax in an expected fashion.
14 class BasicSyntaxTest extends DatabaseTestBase {
16 * Tests string concatenation.
18 public function testConcatLiterals() {
19 $result = db_query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', [
26 $this->assertIdentical($result->fetchField(), 'This is a test.', 'Basic CONCAT works.');
30 * Tests string concatenation with field values.
32 public function testConcatFields() {
33 $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', [
34 ':a1' => 'The age of ',
39 $this->assertIdentical($result->fetchField(), 'The age of John is 25.', 'Field CONCAT works.');
43 * Tests string concatenation with separator.
45 public function testConcatWsLiterals() {
46 $result = db_query("SELECT CONCAT_WS(', ', :a1, NULL, :a2, :a3, :a4)", [
52 $this->assertIdentical($result->fetchField(), 'Hello, , world.');
56 * Tests string concatenation with separator, with field values.
58 public function testConcatWsFields() {
59 $result = db_query("SELECT CONCAT_WS('-', :a1, name, :a2, age) FROM {test} WHERE age = :age", [
64 $this->assertIdentical($result->fetchField(), 'name-John-age-25');
68 * Tests escaping of LIKE wildcards.
70 public function testLikeEscape() {
77 // Match both "Ringo" and "Ring_".
78 $num_matches = db_select('test', 't')
79 ->condition('name', 'Ring_', 'LIKE')
83 $this->assertIdentical($num_matches, '2', 'Found 2 records.');
84 // Match only "Ring_" using a LIKE expression with no wildcards.
85 $num_matches = db_select('test', 't')
86 ->condition('name', db_like('Ring_'), 'LIKE')
90 $this->assertIdentical($num_matches, '1', 'Found 1 record.');
94 * Tests a LIKE query containing a backslash.
96 public function testLikeBackslash() {
107 // Match both rows using a LIKE expression with two wildcards and a verbatim
109 $num_matches = db_select('test', 't')
110 ->condition('name', 'abc%\\\\_', 'LIKE')
114 $this->assertIdentical($num_matches, '2', 'Found 2 records.');
115 // Match only the former using a LIKE expression with no wildcards.
116 $num_matches = db_select('test', 't')
117 ->condition('name', db_like('abc%\_'), 'LIKE')
121 $this->assertIdentical($num_matches, '1', 'Found 1 record.');
125 * Tests \Drupal\Core\Database\Connection::getFullQualifiedTableName().
127 public function testGetFullQualifiedTableName() {
128 $database = \Drupal::database();
129 $num_matches = $database->select($database->getFullQualifiedTableName('test'), 't')
133 $this->assertIdentical($num_matches, '4', 'Found 4 records.');