John Erling Blad has submitted this change and it was merged.

Change subject: Added NumberHandler
......................................................................


Added NumberHandler

Change-Id: I09f415b8f769f1fa17661fb311fcee041dd3e77f
---
M repo/config/Wikibase.experimental.php
A repo/includes/Query/SQLStore/DVHandler/NumberHandler.php
A repo/tests/phpunit/includes/Query/SQLStore/DVHandler/NumberHandlerTest.php
3 files changed, 227 insertions(+), 0 deletions(-)

Approvals:
  John Erling Blad: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/repo/config/Wikibase.experimental.php 
b/repo/config/Wikibase.experimental.php
index 2853eea..58760d6 100644
--- a/repo/config/Wikibase.experimental.php
+++ b/repo/config/Wikibase.experimental.php
@@ -64,6 +64,7 @@
 
        'Wikibase\Repo\Query\SQLStore\DVHandler\EntityIdHandler',
        'Wikibase\Repo\Query\SQLStore\DVHandler\GeoCoordinateHandler',
+       'Wikibase\Repo\Query\SQLStore\DVHandler\NumberHandler',
        'Wikibase\Repo\Query\SQLStore\DVHandler\StringHandler',
 
        'Wikibase\Repo\Query\SQLStore\DataValueHandler',
@@ -143,6 +144,7 @@
 
                'Query/SQLStore/DVHandler/EntityIdHandler',
                'Query/SQLStore/DVHandler/GeoCoordinateHandler',
+               'Query/SQLStore/DVHandler/NumberHandler',
                'Query/SQLStore/DVHandler/StringHandler',
 
                'Query/SQLStore/DataValueHandler',
diff --git a/repo/includes/Query/SQLStore/DVHandler/NumberHandler.php 
b/repo/includes/Query/SQLStore/DVHandler/NumberHandler.php
new file mode 100644
index 0000000..1e9af3a
--- /dev/null
+++ b/repo/includes/Query/SQLStore/DVHandler/NumberHandler.php
@@ -0,0 +1,146 @@
+<?php
+
+namespace Wikibase\Repo\Query\SQLStore\DVHandler;
+
+use Wikibase\Repo\Query\SQLStore\DataValueHandler;
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\FieldDefinition;
+use DataValues\DataValue;
+use DataValues\NumberValue;
+use InvalidArgumentException;
+
+/**
+ * Represents the mapping between Wikibase\NumberValue and
+ * the corresponding table in the store.
+ *
+ * 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 wd.qe
+ *
+ * @file
+ * @ingroup WikibaseSQLStore
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class NumberHandler extends DataValueHandler {
+
+       /**
+        * @see DataValueHandler::getTableDefinition
+        *
+        * @since wd.qe
+        *
+        * @return TableDefinition
+        */
+       public function getTableDefinition() {
+               $fields = array(
+                       new FieldDefinition( 'value', 
FieldDefinition::TYPE_FLOAT, false ),
+                       new FieldDefinition( 'json', 
FieldDefinition::TYPE_TEXT, false ),
+               );
+
+               return new TableDefinition( 'number', $fields );
+       }
+
+       /**
+        * @see DataValueHandler::getValueFieldName
+        *
+        * @since wd.qe
+        *
+        * @return string
+        */
+       public function getValueFieldName() {
+               return 'json';
+       }
+
+       /**
+        * @see DataValueHandler::getSortFieldName
+        *
+        * @since wd.qe
+        *
+        * @return string
+        */
+       public function getSortFieldName() {
+               return 'value';
+       }
+
+       /**
+        * @see DataValueHandler::newDataValueFromValueField
+        *
+        * @since wd.qe
+        *
+        * @param $valueFieldValue // TODO: mixed or string?
+        *
+        * @return DataValue
+        */
+       public function newDataValueFromValueField( $valueFieldValue ) {
+               return new NumberValue( $valueFieldValue );
+       }
+
+       /**
+        * @see DataValueHandler::getLabelFieldName
+        *
+        * @since wd.qe
+        *
+        * @return string|null
+        */
+       public function getLabelFieldName() {
+               return null;
+       }
+
+       /**
+        * @see DataValueHandler::getWhereConditions
+        *
+        * @since wd.qe
+        *
+        * @param DataValue $value
+        *
+        * @return array
+        * @throws InvalidArgumentException
+        */
+       public function getWhereConditions( DataValue $value ) {
+               if ( !( $value instanceof NumberValue ) ) {
+                       throw new InvalidArgumentException( 'Value is not a 
NumberValue' );
+               }
+
+               return array(
+                       'value' => $value->getValue(),
+               );
+       }
+
+       /**
+        * @see DataValueHandler::getInsertValues
+        *
+        * @since wd.qe
+        *
+        * @param DataValue $value
+        *
+        * @return array
+        * @throws InvalidArgumentException
+        */
+       public function getInsertValues( DataValue $value ) {
+               if ( !( $value instanceof NumberValue ) ) {
+                       throw new InvalidArgumentException( 'Value is not a 
NumberValue' );
+               }
+
+               $values = array(
+                       'json' => $value->getArrayValue(),
+                       'value' => $value->getValue(),
+               );
+
+               return $values;
+       }
+
+}
\ No newline at end of file
diff --git 
a/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/NumberHandlerTest.php 
b/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/NumberHandlerTest.php
new file mode 100644
index 0000000..bcf5ae9
--- /dev/null
+++ b/repo/tests/phpunit/includes/Query/SQLStore/DVHandler/NumberHandlerTest.php
@@ -0,0 +1,79 @@
+<?php
+
+namespace Wikibase\Repo\Test\Query\SQLStore\DVHandler;
+
+use Wikibase\Repo\Query\SQLStore\DataValueHandler;
+use Wikibase\Repo\Query\SQLStore\DVHandler\NumberHandler;
+use Wikibase\Repo\Test\Query\SQLStore\DataValueHandlerTest;
+use DataValues\NumberValue;
+
+/**
+ * Unit tests for the Wikibase\Repo\Query\SQLStore\DVHandler\NumberHandler 
class.
+ *
+ * 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 wd.qe
+ *
+ * @ingroup WikibaseRepoTest
+ *
+ * @group Wikibase
+ * @group WikibaseRepo
+ * @group WikibaseQuery
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class NumberHandlerTest extends DataValueHandlerTest {
+
+       /**
+        * @see DataValueHandlerTest::getInstances
+        *
+        * @since wd.qe
+        *
+        * @return DataValueHandler[]
+        */
+       protected function getInstances() {
+               $instances = array();
+
+               $instances[] = new NumberHandler();
+
+               return $instances;
+       }
+
+       /**
+        * @see DataValueHandlerTest::getValues
+        *
+        * @since wd.qe
+        *
+        * @return NumberValue[]
+        */
+       protected function getValues() {
+               $values = array();
+
+               $values[] = new NumberValue( 0 );
+               $values[] = new NumberValue( 1 );
+               $values[] = new NumberValue( 7101010 );
+               $values[] = new NumberValue( 9000.1 );
+               $values[] = new NumberValue( 0.000042 );
+               $values[] = new NumberValue( -0.000042 );
+               $values[] = new NumberValue( -123456 );
+               $values[] = new NumberValue( 71010.101010 );
+
+               return $values;
+       }
+
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/52207
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I09f415b8f769f1fa17661fb311fcee041dd3e77f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>
Gerrit-Reviewer: John Erling Blad <john.b...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to