Unverified Commit 7a943a3b authored by alexpott's avatar alexpott

Issue #3113403 by Beakerboy, daffie: Make Drupal\Core\Database\Query\Condition driver overridable

parent 449b333d
......@@ -942,6 +942,22 @@ public function schema() {
return $this->schema;
}
/**
* Prepares and returns a CONDITION query object.
*
* @param string $conjunction
* The operator to use to combine conditions: 'AND' or 'OR'.
*
* @return \Drupal\Core\Database\Query\Condition
* A new Condition query object.
*
* @see \Drupal\Core\Database\Query\Condition
*/
public function condition($conjunction) {
$class = $this->getDriverClass('Condition');
return new $class($conjunction);
}
/**
* Escapes a database name string.
*
......
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Condition as QueryCondition;
/**
* MySQL implementation of \Drupal\Core\Database\Query\Condition.
*/
class Condition extends QueryCondition {}
......@@ -2,7 +2,6 @@
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\SchemaException;
use Drupal\Core\Database\SchemaObjectExistsException;
use Drupal\Core\Database\SchemaObjectDoesNotExistException;
......@@ -75,7 +74,7 @@ protected function getPrefixInfo($table = 'default', $add_prefix = TRUE) {
protected function buildTableNameCondition($table_name, $operator = '=', $add_prefix = TRUE) {
$table_info = $this->getPrefixInfo($table_name, $add_prefix);
$condition = new Condition('AND');
$condition = $this->connection->condition('AND');
$condition->condition('table_schema', $table_info['database']);
$condition->condition('table_name', $table_info['table'], $operator);
return $condition;
......
<?php
namespace Drupal\Core\Database\Driver\pgsql;
use Drupal\Core\Database\Query\Condition as QueryCondition;
/**
* PostgreSQL implementation of \Drupal\Core\Database\Query\Condition.
*/
class Condition extends QueryCondition {}
<?php
namespace Drupal\Core\Database\Driver\sqlite;
use Drupal\Core\Database\Query\Condition as QueryCondition;
/**
* SQLite implementation of \Drupal\Core\Database\Query\Condition.
*/
class Condition extends QueryCondition {}
......@@ -399,7 +399,7 @@ protected function mapConditionOperator($operator) {
* {@inheritdoc}
*/
public function conditionGroupFactory($conjunction = 'AND') {
return new Condition($conjunction);
return new static($conjunction);
}
/**
......
......@@ -36,7 +36,7 @@ public function __construct(Connection $connection, $table, array $options = [])
parent::__construct($connection, $options);
$this->table = $table;
$this->condition = new Condition('AND');
$this->condition = $this->connection->condition('AND');
}
/**
......
......@@ -138,7 +138,7 @@ public function __construct(Connection $connection, $table, array $options = [])
parent::__construct($connection, $options);
$this->table = $table;
$this->conditionTable = $table;
$this->condition = new Condition('AND');
$this->condition = $this->connection->condition('AND');
}
/**
......
......@@ -108,7 +108,7 @@ public function compiled() {
* {@inheritdoc}
*/
public function conditionGroupFactory($conjunction = 'AND') {
return new Condition($conjunction);
return $this->connection->condition($conjunction);
}
/**
......
......@@ -134,8 +134,8 @@ public function __construct(Connection $connection, $table, $alias = NULL, $opti
$options['return'] = Database::RETURN_STATEMENT;
parent::__construct($connection, $options);
$conjunction = isset($options['conjunction']) ? $options['conjunction'] : 'AND';
$this->condition = new Condition($conjunction);
$this->having = new Condition($conjunction);
$this->condition = $this->connection->condition($conjunction);
$this->having = $this->connection->condition($conjunction);
$this->addJoin(NULL, $table, $alias);
}
......
......@@ -521,7 +521,7 @@ public function __call($method, $args) {
* {@inheritdoc}
*/
public function conditionGroupFactory($conjunction = 'AND') {
return new Condition($conjunction);
return $this->connection->condition($conjunction);
}
/**
......
......@@ -65,7 +65,7 @@ public function __construct(Connection $connection, $table, array $options = [])
parent::__construct($connection, $options);
$this->table = $table;
$this->condition = new Condition('AND');
$this->condition = $this->connection->condition('AND');
}
/**
......
......@@ -2,7 +2,6 @@
namespace Drupal\Core\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\PlaceholderInterface;
/**
......@@ -150,7 +149,7 @@ protected function buildTableNameCondition($table_name, $operator = '=', $add_pr
// Retrieve the table name and schema
$table_info = $this->getPrefixInfo($table_name, $add_prefix);
$condition = new Condition('AND');
$condition = $this->connection->condition('AND');
$condition->condition('table_catalog', $info['database']);
$condition->condition('table_schema', $table_info['schema']);
$condition->condition('table_name', $table_info['table'], $operator);
......
......@@ -175,4 +175,20 @@ public function testPostgresqlReservedWords() {
}
}
/**
* Test that the method ::condition() returns an Condition object from the driver directory.
*/
public function testCondition() {
$db = Database::getConnection('default', 'default');
$namespace = (new \ReflectionObject($db))->getNamespaceName() . "\\Condition";
$condition = $db->condition('AND');
$this->assertIdentical($namespace, get_class($condition));
$nested_and_condition = $condition->andConditionGroup();
$this->assertIdentical($namespace, get_class($nested_and_condition));
$nested_or_condition = $condition->orConditionGroup();
$this->assertIdentical($namespace, get_class($nested_or_condition));
}
}
......@@ -159,4 +159,22 @@ public function testSpecialColumnDelete() {
$this->assertEqual($num_records_before, $num_records_after + $num_deleted, 'Deletion adds up.');
}
/**
* Tests namespace of the condition object.
*/
public function testNamespaceConditionObject() {
$namespace = (new \ReflectionObject($this->connection))->getNamespaceName() . "\\Condition";
$delete = $this->connection->delete('test');
$reflection = new \ReflectionObject($delete);
$condition_property = $reflection->getProperty('condition');
$condition_property->setAccessible(TRUE);
$this->assertIdentical($namespace, get_class($condition_property->getValue($delete)));
$nested_and_condition = $delete->andConditionGroup();
$this->assertIdentical($namespace, get_class($nested_and_condition));
$nested_or_condition = $delete->orConditionGroup();
$this->assertIdentical($namespace, get_class($nested_or_condition));
}
}
......@@ -230,4 +230,22 @@ public function testInvalidMerge() {
$this->fail('No InvalidMergeQueryException thrown');
}
/**
* Tests namespace of the condition object.
*/
public function testNamespaceConditionObject() {
$namespace = (new \ReflectionObject($this->connection))->getNamespaceName() . "\\Condition";
$merge = $this->connection->merge('test');
$reflection = new \ReflectionObject($merge);
$condition_property = $reflection->getProperty('condition');
$condition_property->setAccessible(TRUE);
$this->assertIdentical($namespace, get_class($condition_property->getValue($merge)));
$nested_and_condition = $merge->andConditionGroup();
$this->assertIdentical($namespace, get_class($nested_and_condition));
$nested_or_condition = $merge->orConditionGroup();
$this->assertIdentical($namespace, get_class($nested_or_condition));
}
}
......@@ -614,4 +614,26 @@ public function testEmptyInCondition() {
}
}
/**
* Tests namespace of the condition and having objects.
*/
public function testNamespaceConditionAndHavingObjects() {
$namespace = (new \ReflectionObject($this->connection))->getNamespaceName() . "\\Condition";
$select = $this->connection->select('test');
$reflection = new \ReflectionObject($select);
$condition_property = $reflection->getProperty('condition');
$condition_property->setAccessible(TRUE);
$this->assertIdentical($namespace, get_class($condition_property->getValue($select)));
$having_property = $reflection->getProperty('having');
$having_property->setAccessible(TRUE);
$this->assertIdentical($namespace, get_class($having_property->getValue($select)));
$nested_and_condition = $select->andConditionGroup();
$this->assertIdentical($namespace, get_class($nested_and_condition));
$nested_or_condition = $select->orConditionGroup();
$this->assertIdentical($namespace, get_class($nested_or_condition));
}
}
......@@ -141,4 +141,22 @@ public function testSpecialColumnUpdate() {
$this->assertIdentical($saved_value, 'New offset value', 'Updated special column name value successfully.');
}
/**
* Tests namespace of the condition object.
*/
public function testNamespaceConditionObject() {
$namespace = (new \ReflectionObject($this->connection))->getNamespaceName() . "\\Condition";
$update = $this->connection->update('test');
$reflection = new \ReflectionObject($update);
$condition_property = $reflection->getProperty('condition');
$condition_property->setAccessible(TRUE);
$this->assertIdentical($namespace, get_class($condition_property->getValue($update)));
$nested_and_condition = $update->andConditionGroup();
$this->assertIdentical($namespace, get_class($nested_and_condition));
$nested_or_condition = $update->orConditionGroup();
$this->assertIdentical($namespace, get_class($nested_or_condition));
}
}
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\Select;
use Drupal\Tests\UnitTestCase;
......@@ -25,7 +26,11 @@ class OrderByTest extends UnitTestCase {
protected function setUp() {
$connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->setMethods(['condition'])
->getMockForAbstractClass();
$connection->expects($this->any())
->method('condition')
->willReturn(new Condition('AND'));
$this->query = new Select($connection, 'test', NULL);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment