From 26b14bc518aa58cfde914b6180a19f49dd6a44de Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Wed, 9 Mar 2016 08:48:16 +0900 Subject: [PATCH] Issue #664722 by amateescu: Make insert queries Countable --- core/lib/Drupal/Core/Database/Query/Insert.php | 2 +- .../Drupal/Core/Database/Query/InsertTrait.php | 7 +++++++ core/lib/Drupal/Core/Database/Query/Upsert.php | 2 +- .../KernelTests/Core/Database/InsertTest.php | 16 ++++++++++++++++ 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/core/lib/Drupal/Core/Database/Query/Insert.php b/core/lib/Drupal/Core/Database/Query/Insert.php index 045413af725c..4f1d4dd1b988 100644 --- a/core/lib/Drupal/Core/Database/Query/Insert.php +++ b/core/lib/Drupal/Core/Database/Query/Insert.php @@ -14,7 +14,7 @@ * * @ingroup database */ -class Insert extends Query { +class Insert extends Query implements \Countable { use InsertTrait; diff --git a/core/lib/Drupal/Core/Database/Query/InsertTrait.php b/core/lib/Drupal/Core/Database/Query/InsertTrait.php index 551e870f6d52..a8bab759ba83 100644 --- a/core/lib/Drupal/Core/Database/Query/InsertTrait.php +++ b/core/lib/Drupal/Core/Database/Query/InsertTrait.php @@ -181,4 +181,11 @@ protected function getInsertPlaceholderFragment(array $nested_insert_values, arr return $values; } + /** + * {@inheritdoc} + */ + public function count() { + return count($this->insertValues); + } + } diff --git a/core/lib/Drupal/Core/Database/Query/Upsert.php b/core/lib/Drupal/Core/Database/Query/Upsert.php index e9b5d0e3ac54..ca58c1104d61 100644 --- a/core/lib/Drupal/Core/Database/Query/Upsert.php +++ b/core/lib/Drupal/Core/Database/Query/Upsert.php @@ -18,7 +18,7 @@ * Insert except the rows will be set to the desired values even if the key * existed before. */ -abstract class Upsert extends Query { +abstract class Upsert extends Query implements \Countable { use InsertTrait; diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php index ca0347aa68f4..2f91c7f063cd 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php @@ -25,6 +25,9 @@ function testSimpleInsert() { 'name' => 'Yoko', 'age' => '29', )); + + // Check how many records are queued for insertion. + $this->assertIdentical($query->count(), 1, 'One record is queued for insertion.'); $query->execute(); $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField(); @@ -51,9 +54,15 @@ function testMultiInsert() { 'name' => 'Curly', )); + // Check how many records are queued for insertion. + $this->assertIdentical($query->count(), 2, 'Two records are queued for insertion.'); + // We should be able to say "use the field order". // This is not the recommended mechanism for most cases, but it should work. $query->values(array('Moe', '32')); + + // Check how many records are queued for insertion. + $this->assertIdentical($query->count(), 3, 'Three records are queued for insertion.'); $query->execute(); $num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField(); @@ -78,6 +87,8 @@ function testRepeatedInsert() { 'name' => 'Larry', 'age' => '30', )); + // Check how many records are queued for insertion. + $this->assertIdentical($query->count(), 1, 'One record is queued for insertion.'); $query->execute(); // This should run the insert, but leave the fields intact. // We should be able to specify values in any order if named. @@ -85,10 +96,15 @@ function testRepeatedInsert() { 'age' => '31', 'name' => 'Curly', )); + // Check how many records are queued for insertion. + $this->assertIdentical($query->count(), 1, 'One record is queued for insertion.'); $query->execute(); // We should be able to say "use the field order". $query->values(array('Moe', '32')); + + // Check how many records are queued for insertion. + $this->assertIdentical($query->count(), 1, 'One record is queued for insertion.'); $query->execute(); $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField(); -- GitLab