Daniel Werner has submitted this change and it was merged.
Change subject: Added select method to QueryInterface
......................................................................
Added select method to QueryInterface
Change-Id: Ia613af8f9fb734235059c1ef20161c2f744080de
---
M Database/Database.classes.php
M Database/Database.mw.php
M Database/Database/MediaWikiQueryInterface.php
M Database/Database/QueryInterface.php
A Database/Database/ResultIterator.php
M Database/tests/phpunit/MediaWikiQueryInterfaceTest.php
A Database/tests/phpunit/ResultIteratorTest.php
7 files changed, 283 insertions(+), 0 deletions(-)
Approvals:
Daniel Werner: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Database/Database.classes.php b/Database/Database.classes.php
index 9f199d4..2d25649 100644
--- a/Database/Database.classes.php
+++ b/Database/Database.classes.php
@@ -37,6 +37,7 @@
'Wikibase\Database\MessageReporter',
'Wikibase\Database\QueryInterface',
'Wikibase\Database\QueryInterfaceException',
+ 'Wikibase\Database\ResultIterator',
'Wikibase\Database\TableBuilder',
'Wikibase\Database\TableCreationFailedException',
'Wikibase\Database\TableDefinition',
diff --git a/Database/Database.mw.php b/Database/Database.mw.php
index 4857272..678d4d1 100644
--- a/Database/Database.mw.php
+++ b/Database/Database.mw.php
@@ -65,6 +65,7 @@
'FieldDefinition',
'MediaWikiQueryInterface',
+ 'ResultIterator',
'TableBuilder',
'TableDefinition',
'TableCreationFailedException',
diff --git a/Database/Database/MediaWikiQueryInterface.php
b/Database/Database/MediaWikiQueryInterface.php
index a86f60c..e575f30 100644
--- a/Database/Database/MediaWikiQueryInterface.php
+++ b/Database/Database/MediaWikiQueryInterface.php
@@ -169,4 +169,32 @@
return $this->getDB()->insertId();
}
+ /**
+ * @see QueryInterface::select
+ *
+ * @since 0.1
+ *
+ * @param string $tableName
+ * @param array $fields
+ * @param array $conditions
+ *
+ * @return ResultIterator
+ */
+ public function select( $tableName, array $fields, array $conditions ) {
+ $selectionResult = $this->getDB()->select(
+ $tableName,
+ $fields,
+ $conditions,
+ __METHOD__
+ );
+
+ if ( $selectionResult instanceof \ResultWrapper ) {
+ return new ResultIterator( iterator_to_array(
$selectionResult ) );
+ }
+
+ // TODO: throw
+ }
+
}
+
+
diff --git a/Database/Database/QueryInterface.php
b/Database/Database/QueryInterface.php
index 03b5d41..79ffc8e 100644
--- a/Database/Database/QueryInterface.php
+++ b/Database/Database/QueryInterface.php
@@ -117,4 +117,19 @@
*/
public function getInsertId();
+ /**
+ * Selects the specified fields from the rows that match the provided
conditions.
+ * The conditions are provided as an associative array in
+ * which the keys are the field names.
+ *
+ * @since 0.1
+ *
+ * @param string $tableName
+ * @param array $fields
+ * @param array $conditions
+ *
+ * @return ResultIterator
+ */
+ public function select( $tableName, array $fields, array $conditions );
+
}
diff --git a/Database/Database/ResultIterator.php
b/Database/Database/ResultIterator.php
new file mode 100644
index 0000000..a7cd243
--- /dev/null
+++ b/Database/Database/ResultIterator.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Wikibase\Database;
+
+/**
+ * Iterator for selection results.
+ * Each item is an object that has as properties the field names.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.1
+ *
+ * @file
+ * @ingroup WikibaseDatabase
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class ResultIterator implements \Iterator {
+
+ protected $iterator;
+
+ public function __construct( array $rows ) {
+ $this->iterator = new \ArrayIterator( $rows );
+ }
+
+ /**
+ * Return the current element
+ *
+ * @link http://php.net/manual/en/iterator.current.php
+ * @return mixed Can return any type.
+ */
+ public function current() {
+ return $this->iterator->current();
+ }
+
+ /**
+ * Move forward to next element
+ *
+ * @link http://php.net/manual/en/iterator.next.php
+ * @return void Any returned value is ignored.
+ */
+ public function next() {
+ $this->iterator->next();
+ }
+
+ /**
+ * Return the key of the current element
+ *
+ * @link http://php.net/manual/en/iterator.key.php
+ * @return mixed scalar on success, or null on failure.
+ */
+ public function key() {
+ return $this->iterator->key();
+ }
+
+ /**
+ * Checks if current position is valid
+ *
+ * @link http://php.net/manual/en/iterator.valid.php
+ * @return boolean The return value will be casted to boolean and then
evaluated.
+ * Returns true on success or false on failure.
+ */
+ public function valid() {
+ return $this->iterator->current();
+ }
+
+ /**
+ * Rewind the Iterator to the first element
+ *
+ * @link http://php.net/manual/en/iterator.rewind.php
+ * @return void Any returned value is ignored.
+ */
+ public function rewind() {
+ $this->iterator->rewind();
+ }
+
+}
diff --git a/Database/tests/phpunit/MediaWikiQueryInterfaceTest.php
b/Database/tests/phpunit/MediaWikiQueryInterfaceTest.php
index faf5a51..cff6ed7 100644
--- a/Database/tests/phpunit/MediaWikiQueryInterfaceTest.php
+++ b/Database/tests/phpunit/MediaWikiQueryInterfaceTest.php
@@ -327,6 +327,74 @@
$this->assertEquals( 42, $queryInterface->getInsertId() );
}
+ /**
+ * @dataProvider selectProvider
+ */
+ public function testSelect( $tableName, array $fields, array
$conditions ) {
+ $connection = $this->getMock( 'DatabaseMysql' );
+ $extendedAbstraction = $this->getMockBuilder(
'\Wikibase\Database\MWDB\ExtendedMySQLAbstraction' )
+ ->disableOriginalConstructor()->getMock();
+
+ $queryInterface = new MediaWikiQueryInterface(
+ new DirectConnectionProvider( $connection ),
+ $extendedAbstraction
+ );
+
+ $connection->expects( $this->once() )
+ ->method( 'select' )
+ ->with(
+ $this->equalTo( $tableName ),
+ $this->equalTo( $fields ),
+ $this->equalTo( $conditions )
+ );
+
+ $queryInterface->select( $tableName, $fields, $conditions );
+
+ // Ideally we would have the select method result a mock
ResultWrapper
+ // and would assert if the data was present in the selection
result.
+ // It however seems somewhat impossible to create a mock of
ResultWrapper.
+ }
+
+ public function selectProvider() {
+ $argLists = array();
+
+ $argLists[] = array(
+ 'table',
+ array(
+ 'foo',
+ 'bar',
+ 'baz',
+ ),
+ array(
+ 'intfield' => 42,
+ 'strfield' => 'nyan',
+ )
+ );
+
+ $argLists[] = array(
+ 'table',
+ array(
+ 'foo',
+ 'bar',
+ 'baz',
+ ),
+ array(
+ )
+ );
+
+ $argLists[] = array(
+ 'onoez',
+ array(
+ 'foo',
+ ),
+ array(
+ 'intfield' => 42,
+ )
+ );
+
+ return $argLists;
+ }
+
}
class DirectConnectionProvider implements DBConnectionProvider {
diff --git a/Database/tests/phpunit/ResultIteratorTest.php
b/Database/tests/phpunit/ResultIteratorTest.php
new file mode 100644
index 0000000..86d2d3f
--- /dev/null
+++ b/Database/tests/phpunit/ResultIteratorTest.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace Wikibase\Test\Database;
+
+use Wikibase\Database\ResultIterator;
+
+/**
+ * @covers Wikibase\Database\TableBuilder
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @since 0.1
+ *
+ * @ingroup WikibaseDatabaseTest
+ *
+ * @group Wikibase
+ * @group WikibaseDatabase
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < [email protected] >
+ */
+class ResultIteratorTest extends \PHPUnit_Framework_TestCase {
+
+ public function testCanConstruct() {
+ new ResultIterator( array() );
+ $this->assertTrue( true );
+ }
+
+ /**
+ * @dataProvider rowProvider
+ */
+ public function testRetainsInputData( array $inputRows ) {
+ $iterator = new ResultIterator( $inputRows );
+
+ $this->assertEquals(
+ $inputRows,
+ iterator_to_array( $iterator )
+ );
+ }
+
+ public function rowProvider() {
+ $argLists = array();
+
+ $argLists[] = array( array(
+ ) );
+
+ $argLists[] = array( array(
+ (object)array( 'foo' => 4, 'bar' => 2 ),
+ ) );
+
+ $argLists[] = array( array(
+ (object)array( 'foo' => 4, 'bar' => 2 ),
+ (object)array( 'foo' => 1, 'bar' => 3 ),
+ ) );
+
+ $argLists[] = array( array(
+ (object)array( 'foo' => 4, 'bar' => 2 ),
+ (object)array( 'foo' => 1, 'bar' => 3 ),
+ (object)array( 'baz' => 'nyan', 'bah' => 'cat' ),
+ ) );
+
+ return $argLists;
+ }
+
+}
--
To view, visit https://gerrit.wikimedia.org/r/64080
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Ia613af8f9fb734235059c1ef20161c2f744080de
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Anja Jentzsch <[email protected]>
Gerrit-Reviewer: Ataherivand <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Daniel Werner <[email protected]>
Gerrit-Reviewer: Denny Vrandecic <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: Jens Ohlig <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: Lydia Pintscher <[email protected]>
Gerrit-Reviewer: Markus Kroetzsch <[email protected]>
Gerrit-Reviewer: Nikola Smolenski <[email protected]>
Gerrit-Reviewer: Silke Meyer <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits