Issue #3113403 by Beakerboy, daffie: Make Drupal\Core\Database\Query\Condition driver overridable
parent
449b333d5b
commit
7a943a3b5f
|
@ -942,6 +942,22 @@ abstract class Connection {
|
|||
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.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?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 @@ class Schema extends DatabaseSchema {
|
|||
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;
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
<?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 {}
|
|
@ -0,0 +1,10 @@
|
|||
<?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 @@ class Condition implements ConditionInterface, \Countable {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function conditionGroupFactory($conjunction = 'AND') {
|
||||
return new Condition($conjunction);
|
||||
return new static($conjunction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -36,7 +36,7 @@ class Delete extends Query implements ConditionInterface {
|
|||
parent::__construct($connection, $options);
|
||||
$this->table = $table;
|
||||
|
||||
$this->condition = new Condition('AND');
|
||||
$this->condition = $this->connection->condition('AND');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -138,7 +138,7 @@ class Merge extends Query implements ConditionInterface {
|
|||
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 @@ trait QueryConditionTrait {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function conditionGroupFactory($conjunction = 'AND') {
|
||||
return new Condition($conjunction);
|
||||
return $this->connection->condition($conjunction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -134,8 +134,8 @@ class Select extends Query implements SelectInterface {
|
|||
$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 @@ class SelectExtender implements SelectInterface {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function conditionGroupFactory($conjunction = 'AND') {
|
||||
return new Condition($conjunction);
|
||||
return $this->connection->condition($conjunction);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -65,7 +65,7 @@ class Update extends Query implements ConditionInterface {
|
|||
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 @@ abstract class Schema implements PlaceholderInterface {
|
|||
// 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 @@ class ConnectionTest extends DatabaseTestBase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @@ class DeleteTruncateTest extends DatabaseTestBase {
|
|||
$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 @@ class MergeTest extends DatabaseTestBase {
|
|||
$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 @@ class SelectTest extends DatabaseTestBase {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 @@ class UpdateTest extends DatabaseTestBase {
|
|||
$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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue