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

Change subject: Added insert, update and delete methods to query interface
......................................................................


Added insert, update and delete methods to query interface

Change-Id: I539ad26b87e1db06a4546df21621d7ffefeec028
---
M repo/includes/Database/MediaWikiQueryInterface.php
M repo/includes/Database/ObservableQueryInterface.php
M repo/includes/Database/QueryInterface.php
M repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
4 files changed, 258 insertions(+), 1 deletion(-)

Approvals:
  John Erling Blad: Verified; Looks good to me, approved



diff --git a/repo/includes/Database/MediaWikiQueryInterface.php 
b/repo/includes/Database/MediaWikiQueryInterface.php
index 86313f0..29fa479 100644
--- a/repo/includes/Database/MediaWikiQueryInterface.php
+++ b/repo/includes/Database/MediaWikiQueryInterface.php
@@ -104,4 +104,60 @@
                return $this->getDB()->dropTable( $tableName, __METHOD__ ) !== 
false;
        }
 
+       /**
+        * @see QueryInterface::insert
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $values
+        *
+        * @return boolean Success indicator
+        */
+       public function insert( $tableName, array $values ) {
+               return $this->getDB()->insert(
+                       $tableName,
+                       $values,
+                       __METHOD__
+               ) !== false;
+       }
+
+       /**
+        * @see QueryInterface::update
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $values
+        * @param array $conditions
+        *
+        * @return boolean Success indicator
+        */
+       public function update( $tableName, array $values, array $conditions ) {
+               return $this->getDB()->update(
+                       $tableName,
+                       $values,
+                       $conditions,
+                       __METHOD__
+               ) !== false;
+       }
+
+       /**
+        * @see QueryInterface::delete
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $conditions
+        *
+        * @return boolean Success indicator
+        */
+       public function delete( $tableName, array $conditions ) {
+               return $this->getDB()->delete(
+                       $tableName,
+                       $conditions,
+                       __METHOD__
+               ) !== false;
+       }
+
 }
diff --git a/repo/includes/Database/ObservableQueryInterface.php 
b/repo/includes/Database/ObservableQueryInterface.php
index 48af945..8b01461 100644
--- a/repo/includes/Database/ObservableQueryInterface.php
+++ b/repo/includes/Database/ObservableQueryInterface.php
@@ -102,4 +102,47 @@
                $this->runCallbacks( __FUNCTION__, func_get_args() );
        }
 
+       /**
+        * @see QueryInterface::insert
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $values
+        *
+        * @return boolean Success indicator
+        */
+       public function insert( $tableName, array $values ) {
+               $this->runCallbacks( __FUNCTION__, func_get_args() );
+       }
+
+       /**
+        * @see QueryInterface::update
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $values
+        * @param array $conditions
+        *
+        * @return boolean Success indicator
+        */
+       public function update( $tableName, array $values, array $conditions ) {
+               $this->runCallbacks( __FUNCTION__, func_get_args() );
+       }
+
+       /**
+        * @see QueryInterface::delete
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $conditions
+        *
+        * @return boolean Success indicator
+        */
+       public function delete( $tableName, array $conditions ) {
+               $this->runCallbacks( __FUNCTION__, func_get_args() );
+       }
+
 }
diff --git a/repo/includes/Database/QueryInterface.php 
b/repo/includes/Database/QueryInterface.php
index 419fca4..85fe2c1 100644
--- a/repo/includes/Database/QueryInterface.php
+++ b/repo/includes/Database/QueryInterface.php
@@ -63,4 +63,47 @@
         */
        public function dropTable( $tableName );
 
+       /**
+        * Inserts the provided values into the specified table.
+        * The values are provided as an associative array in
+        * which the keys are the field names.
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $values
+        *
+        * @return boolean Success indicator
+        */
+       public function insert( $tableName, array $values );
+
+       /**
+        * Updates the rows that match the conditions with the provided values.
+        * The values and conditions are provided as an associative array in
+        * which the keys are the field names.
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $values
+        * @param array $conditions
+        *
+        * @return boolean Success indicator
+        */
+       public function update( $tableName, array $values, array $conditions );
+
+       /**
+        * Removes the rows matching the provided conditions from the specified 
table.
+        * The conditions are provided as an associative array in
+        * which the keys are the field names.
+        *
+        * @since wd.db
+        *
+        * @param string $tableName
+        * @param array $conditions
+        *
+        * @return boolean Success indicator
+        */
+       public function delete( $tableName, array $conditions );
+
 }
diff --git 
a/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php 
b/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
index c6557e4..4d18509 100644
--- a/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
+++ b/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Repo\Test\Database;
 
 use Wikibase\Repo\Database\MediaWikiQueryInterface;
+use Wikibase\Repo\Database\QueryInterface;
 use Wikibase\Repo\Database\TableDefinition;
 use Wikibase\Repo\Database\FieldDefinition;
 
@@ -56,7 +57,7 @@
        }
 
        /**
-        * @return MediaWikiQueryInterface
+        * @return QueryInterface
         */
        protected function newInstance() {
                $conn = new \Wikibase\Repo\LazyDBConnectionProvider( DB_MASTER 
);
@@ -147,4 +148,118 @@
                );
        }
 
+       public function testInsert() {
+               $queryInterface = $this->newInstance();
+
+               $table = new TableDefinition( 'testinsert', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, FieldDefinition::NULL ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT, FieldDefinition::NOT_NULL ),
+               ) );
+
+               $this->assertTrue( $queryInterface->createTable( $table ) );
+
+               $this->assertTrue( $queryInterface->insert(
+                       $table->getName(),
+                       array(
+                               'intfield' => 42,
+                               'textfield' => 'foobar baz',
+                       )
+               ) );
+
+               $this->assertTrue( $queryInterface->insert(
+                       $table->getName(),
+                       array(
+                               'textfield' => '~=[,,_,,]:3',
+                       )
+               ) );
+
+               // TODO: assert present
+
+               $this->assertTrue( $queryInterface->dropTable( 
$table->getName() ) );
+       }
+
+       public function testUpdate() {
+               $queryInterface = $this->newInstance();
+
+               $table = new TableDefinition( 'testupdate', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, FieldDefinition::NULL ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT, FieldDefinition::NOT_NULL ),
+               ) );
+
+               $this->assertTrue( $queryInterface->createTable( $table ) );
+
+               $this->assertTrue( $queryInterface->insert(
+                       $table->getName(),
+                       array(
+                               'intfield' => 42,
+                               'textfield' => 'foobar baz',
+                       )
+               ) );
+
+               $this->assertTrue( $queryInterface->update(
+                       $table->getName(),
+                       array(
+                               'textfield' => '~=[,,_,,]:3',
+                       ),
+                       array(
+                               'intfield' => 0
+                       )
+               ) );
+
+               // TODO: assert no change
+
+               $this->assertTrue( $queryInterface->update(
+                       $table->getName(),
+                       array(
+                               'textfield' => '~=[,,_,,]:3',
+                       ),
+                       array(
+                               'intfield' => 42
+                       )
+               ) );
+
+               // TODO: assert change
+
+               $this->assertTrue( $queryInterface->dropTable( 
$table->getName() ) );
+       }
+
+       public function testDelete() {
+               $queryInterface = $this->newInstance();
+
+               $table = new TableDefinition( 'testdelete', array(
+                       new FieldDefinition( 'intfield', 
FieldDefinition::TYPE_INTEGER, FieldDefinition::NULL ),
+                       new FieldDefinition( 'textfield', 
FieldDefinition::TYPE_TEXT, FieldDefinition::NOT_NULL ),
+               ) );
+
+               $this->assertTrue( $queryInterface->createTable( $table ) );
+
+               $this->assertTrue( $queryInterface->insert(
+                       $table->getName(),
+                       array(
+                               'intfield' => 42,
+                               'textfield' => 'foobar baz',
+                       )
+               ) );
+
+               $this->assertTrue( $queryInterface->delete(
+                       $table->getName(),
+                       array(
+                               'intfield' => 0
+                       )
+               ) );
+
+               // TODO: assert no change
+
+               $this->assertTrue( $queryInterface->delete(
+                       $table->getName(),
+                       array(
+                               'intfield' => 42
+                       )
+               ) );
+
+               // TODO: assert change
+
+               $this->assertTrue( $queryInterface->dropTable( 
$table->getName() ) );
+       }
+
 }

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I539ad26b87e1db06a4546df21621d7ffefeec028
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: John Erling Blad <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to