diff --git a/core/lib/Drupal/Core/Database/Connection.php b/core/lib/Drupal/Core/Database/Connection.php index 94ba8bed4e..fd609caa0a 100644 --- a/core/lib/Drupal/Core/Database/Connection.php +++ b/core/lib/Drupal/Core/Database/Connection.php @@ -944,6 +944,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. * diff --git a/core/lib/Drupal/Core/Database/Driver/mysql/Condition.php b/core/lib/Drupal/Core/Database/Driver/mysql/Condition.php new file mode 100644 index 0000000000..5a14f7e3ec --- /dev/null +++ b/core/lib/Drupal/Core/Database/Driver/mysql/Condition.php @@ -0,0 +1,10 @@ +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; diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Condition.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Condition.php new file mode 100644 index 0000000000..d1f21d8e06 --- /dev/null +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Condition.php @@ -0,0 +1,10 @@ +table = $table; - $this->condition = new Condition('AND'); + $this->condition = $connection->condition('AND'); } /** diff --git a/core/lib/Drupal/Core/Database/Query/Merge.php b/core/lib/Drupal/Core/Database/Query/Merge.php index 6d89532472..7c7c5e8f62 100644 --- a/core/lib/Drupal/Core/Database/Query/Merge.php +++ b/core/lib/Drupal/Core/Database/Query/Merge.php @@ -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 = $connection->condition('AND'); } /** diff --git a/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php b/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php index 9053a17772..83be429fa5 100644 --- a/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php +++ b/core/lib/Drupal/Core/Database/Query/QueryConditionTrait.php @@ -108,7 +108,7 @@ public function compiled() { * {@inheritdoc} */ public function conditionGroupFactory($conjunction = 'AND') { - return new Condition($conjunction); + return $this->connection->condition($conjunction); } /** diff --git a/core/lib/Drupal/Core/Database/Query/Select.php b/core/lib/Drupal/Core/Database/Query/Select.php index fe88f84172..28a21d5e55 100644 --- a/core/lib/Drupal/Core/Database/Query/Select.php +++ b/core/lib/Drupal/Core/Database/Query/Select.php @@ -134,8 +134,8 @@ public function __construct($table, $alias, Connection $connection, $options = [ $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 = $connection->condition($conjunction); + $this->having = $connection->condition($conjunction); $this->addJoin(NULL, $table, $alias); } diff --git a/core/lib/Drupal/Core/Database/Query/SelectExtender.php b/core/lib/Drupal/Core/Database/Query/SelectExtender.php index bc15c19e70..47099edb63 100644 --- a/core/lib/Drupal/Core/Database/Query/SelectExtender.php +++ b/core/lib/Drupal/Core/Database/Query/SelectExtender.php @@ -528,7 +528,7 @@ public function __call($method, $args) { * {@inheritdoc} */ public function conditionGroupFactory($conjunction = 'AND') { - return new Condition($conjunction); + return $this->connection->condition($conjunction); } /** diff --git a/core/lib/Drupal/Core/Database/Query/Update.php b/core/lib/Drupal/Core/Database/Query/Update.php index 5de5d682ec..8d5b357c4f 100644 --- a/core/lib/Drupal/Core/Database/Query/Update.php +++ b/core/lib/Drupal/Core/Database/Query/Update.php @@ -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 = $connection->condition('AND'); } /** diff --git a/core/lib/Drupal/Core/Database/Schema.php b/core/lib/Drupal/Core/Database/Schema.php index 01ab753d92..8782b67dee 100644 --- a/core/lib/Drupal/Core/Database/Schema.php +++ b/core/lib/Drupal/Core/Database/Schema.php @@ -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); diff --git a/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php b/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php index c9ab16a551..fb42c0a746 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php @@ -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)); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DeleteTruncateTest.php b/core/tests/Drupal/KernelTests/Core/Database/DeleteTruncateTest.php index b4aa0a9b14..09ae7ddd01 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DeleteTruncateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DeleteTruncateTest.php @@ -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)); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php b/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php index b103b5fc6f..9236c8c7a7 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/MergeTest.php @@ -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)); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php index 3712ee8757..e1a0189149 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php @@ -615,4 +615,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)); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/UpdateTest.php b/core/tests/Drupal/KernelTests/Core/Database/UpdateTest.php index b5b28092e8..7697f530dc 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/UpdateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/UpdateTest.php @@ -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)); + } + } diff --git a/core/tests/Drupal/Tests/Core/Database/OrderByTest.php b/core/tests/Drupal/Tests/Core/Database/OrderByTest.php index bab257186b..34a37263ec 100644 --- a/core/tests/Drupal/Tests/Core/Database/OrderByTest.php +++ b/core/tests/Drupal/Tests/Core/Database/OrderByTest.php @@ -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; @@ -24,8 +25,13 @@ class OrderByTest extends UnitTestCase { */ protected function setUp() { $connection = $this->getMockBuilder('Drupal\Core\Database\Connection') + ->setMethods(['condition']) ->disableOriginalConstructor() + ->setMethods(['condition']) ->getMockForAbstractClass(); + $connection->expects($this->any()) + ->method('condition') + ->willReturn(new Condition('AND')); $this->query = new Select('test', NULL, $connection); }