-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #3155563 by mcdruid, mrinalini9, Hardik_Patel_12, mondrake, daf…
…fie: select query should quote aliases which are reserved words in MySQL
- Loading branch information
Showing
4 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
tests/Drupal/KernelTests/Core/Database/ReservedWordTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
<?php | ||
|
||
namespace Drupal\KernelTests\Core\Database; | ||
|
||
/** | ||
* Tests queries that include reserved words. | ||
* | ||
* @group Database | ||
*/ | ||
class ReservedWordTest extends DatabaseTestBase { | ||
|
||
/** | ||
* Tests SELECT count query from a table with a reserved name. | ||
*/ | ||
public function testSelectReservedWordTableCount() { | ||
$query = $this->connection->select('virtual'); | ||
$num_records = $query->countQuery()->execute()->fetchField(); | ||
|
||
$this->assertSame('1', $num_records); | ||
} | ||
|
||
/** | ||
* Tests SELECT query with a specific field from a table with a reserved name. | ||
*/ | ||
public function testSelectReservedWordTableSpecificField() { | ||
$query = $this->connection->select('virtual'); | ||
$query->addField('virtual', 'function'); | ||
$rows = $query->execute()->fetchCol(); | ||
|
||
$this->assertSame('Function value 1', $rows[0]); | ||
} | ||
|
||
/** | ||
* Tests SELECT query with all fields from a table with a reserved name. | ||
*/ | ||
public function testSelectReservedWordTableAllFields() { | ||
$query = $this->connection->select('virtual'); | ||
$query->fields('virtual'); | ||
$result = $query->execute()->fetchObject(); | ||
|
||
$this->assertSame('Function value 1', $result->function); | ||
} | ||
|
||
/** | ||
* Tests SELECT count query from a table with a reserved alias. | ||
*/ | ||
public function testSelectReservedWordAliasCount() { | ||
$query = $this->connection->select('test', 'character'); | ||
$num_records = $query->countQuery()->execute()->fetchField(); | ||
|
||
$this->assertSame('4', $num_records); | ||
} | ||
|
||
/** | ||
* Tests SELECT query with specific fields from a table with a reserved alias. | ||
*/ | ||
public function testSelectReservedWordAliasSpecificFields() { | ||
$query = $this->connection->select('test', 'high_priority'); | ||
$query->addField('high_priority', 'name'); | ||
$query->addField('high_priority', 'age', 'age'); | ||
$query->condition('age', 27); | ||
$record = $query->execute()->fetchObject(); | ||
|
||
// Ensure that we got the right record. | ||
$this->assertSame('George', $record->name); | ||
$this->assertSame('27', $record->age); | ||
} | ||
|
||
/** | ||
* Tests SELECT query with all fields from a table with a reserved alias. | ||
*/ | ||
public function testSelectReservedWordAliasAllFields() { | ||
$record = $this->connection->select('test', 'signal') | ||
->fields('signal') | ||
->condition('age', 27) | ||
->execute()->fetchObject(); | ||
|
||
// Ensure that we got the right record. | ||
$this->assertSame('George', $record->name); | ||
$this->assertSame('27', $record->age); | ||
} | ||
|
||
} |