Jeroen De Dauw has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/51811


Change subject: Implementing create table in the query interface [DO NOT MERGE]
......................................................................

Implementing create table in the query interface [DO NOT MERGE]

Change-Id: I763305af860c44867b7119769f93f5a4e3b62732
---
A repo/includes/Database/MWDB/ExtendedAbstraction.php
A repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php
M repo/includes/Database/MediaWikiQueryInterface.php
M repo/includes/Database/ObservableQueryInterface.php
M repo/includes/Database/QueryInterface.php
A repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php
A repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php
A repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
A repo/tests/phpunit/includes/Database/QueryInterfaceTest.php
9 files changed, 319 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/11/51811/1

diff --git a/repo/includes/Database/MWDB/ExtendedAbstraction.php 
b/repo/includes/Database/MWDB/ExtendedAbstraction.php
new file mode 100644
index 0000000..30bc9dd
--- /dev/null
+++ b/repo/includes/Database/MWDB/ExtendedAbstraction.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Wikibase\Repo\Database\MWDB;
+
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\DBConnectionProvider;
+use InvalidArgumentException;
+use DatabaseBase;
+
+/**
+ * Base database abstraction class to put stuff into that is not present
+ * in the MW core db abstraction layer.
+ *
+ * Like to core class DatabaseBase, each deriving class provides support
+ * for a specific type of database.
+ *
+ * Everything implemented in these classes could go into DatabaseBase and
+ * deriving classes, though this might take quite some time, hence 
implementation
+ * is first done here. If you feel like taking core CR crap and waiting a few
+ * months, by all means try to get the functionality into core.
+ *
+ * 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.db
+ *
+ * @file
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+abstract class ExtendedAbstraction {
+
+       /**
+        * @since wd.db
+        *
+        * @var DBConnectionProvider
+        */
+       private $connectionProvider;
+
+       /**
+        * @since wd.db
+        *
+        * @param DBConnectionProvider $connectionProvider
+        */
+       public function __construct( DBConnectionProvider $connectionProvider ) 
{
+               $this->connectionProvider = $connectionProvider;
+       }
+
+       /**
+        * @since wd.db
+        *
+        * @return DatabaseBase
+        * @throws InvalidArgumentException
+        */
+       protected function getDB() {
+               $db = $this->connectionProvider->getConnection();
+
+               if ( $db->getType() !== $this->getType() ) {
+                       throw new InvalidArgumentException();
+               }
+       }
+
+       /**
+        * Create the provided table.
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public abstract function createTable( TableDefinition $table );
+
+       /**
+        * Returns the type of the supported MW DB abstraction class.
+        *
+        * @since wd.db
+        *
+        * @return string
+        */
+       protected abstract function getType();
+
+}
\ No newline at end of file
diff --git a/repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php 
b/repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php
new file mode 100644
index 0000000..670da8e
--- /dev/null
+++ b/repo/includes/Database/MWDB/ExtendedMySQLAbstraction.php
@@ -0,0 +1,121 @@
+<?php
+
+namespace Wikibase\Repo\Database\MWDB;
+
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\FieldDefinition;
+use RuntimeException;
+
+/**
+ * MySQL implementation of ExtendedAbstraction.
+ *
+ * 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.db
+ *
+ * @file
+ * @ingroup WikibaseRepo
+ *
+ * @licence GNU GPL v2+
+ * @author Jeroen De Dauw < jeroended...@gmail.com >
+ */
+class ExtendedMySQLAbstraction extends ExtendedAbstraction {
+
+       /**
+        * @see ExtendedAbstraction::getType
+        *
+        * @since wd.db
+        *
+        * @return string
+        */
+       protected function getType() {
+               return 'mysql';
+       }
+
+       /**
+        * @see ExtendedAbstraction::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public function createTable( TableDefinition $table ) {
+               $db = $this->getDB();
+
+               $sql = 'CREATE TABLE `' . $db->getDBname() . '`.' . 
$db->addQuotes( $table->getName() ) . ' (';
+
+               $fields = array();
+
+               foreach ( $table->getFields() as $field ) {
+                       $fields[] = $field->getName() . ' ' . 
$this->getFieldSQL( $field );
+               }
+
+               $sql .= implode( ',', $fields );
+
+               // TODO: table options
+               $sql .= ') ' . 'ENGINE=InnoDB, DEFAULT CHARSET=binary';
+
+               $db->query( $sql, __METHOD__ );
+       }
+
+       /**
+        * @since wd.db
+        *
+        * @param FieldDefinition $field
+        *
+        * @return string
+        * @throws RuntimeException
+        */
+       protected function getFieldSQL( FieldDefinition $field ) {
+               $sql = $this->getFieldType( $field->getType() );
+
+               if ( $field->getDefault() !== null ) {
+                       $sql .= ' DEFAULT ' . $this->getDB()->addQuotes( 
$field->getDefault() );
+               }
+
+               // TODO: add all field stuff relevant here
+
+               $sql .= $field->allowsNull() ? ' NULL' : ' NOT NULL';
+
+               return $sql;
+       }
+
+       /**
+        * Returns the MySQL field type for a given FieldDefinition type 
constant.
+        *
+        * @param string $fieldType
+        *
+        * @return string
+        * @throws RuntimeException
+        */
+       protected function getFieldType( $fieldType ) {
+               switch ( $fieldType ) {
+                       case FieldDefinition::TYPE_INTEGER:
+                               return 'INT';
+                       case FieldDefinition::TYPE_FLOAT:
+                               return 'FLOAT';
+                       case FieldDefinition::TYPE_TEXT:
+                               return 'BLOB';
+                       case FieldDefinition::TYPE_BOOLEAN:
+                               return 'TINYINT';
+                       default:
+                               throw new RuntimeException( __CLASS__ . ' does 
not support db fields of type ' . $fieldType );
+               }
+       }
+
+}
diff --git a/repo/includes/Database/MediaWikiQueryInterface.php 
b/repo/includes/Database/MediaWikiQueryInterface.php
index a4a5e6d..029c1a4 100644
--- a/repo/includes/Database/MediaWikiQueryInterface.php
+++ b/repo/includes/Database/MediaWikiQueryInterface.php
@@ -3,6 +3,8 @@
 namespace Wikibase\Repo\Database;
 
 use Wikibase\Repo\DBConnectionProvider;
+use Wikibase\Repo\Database\TableDefinition;
+use Wikibase\Repo\Database\MWDB\ExtendedAbstraction;
 
 /**
  * Implementation of the QueryInterface interface using the MediaWiki
@@ -39,9 +41,19 @@
        private $connectionProvider;
 
        /**
-        * @param DBConnectionProvider $connectionProvider
+        * @var ExtendedAbstraction
         */
-       public function __construct( DBConnectionProvider $connectionProvider ) 
{
+       private $extendedAbstraction;
+
+       /**
+        * Constructor.
+        *
+        * @since wd.db
+        *
+        * @param DBConnectionProvider $connectionProvider
+        * @param ExtendedAbstraction $extendedAbstraction
+        */
+       public function __construct( DBConnectionProvider $connectionProvider, 
ExtendedAbstraction $extendedAbstraction ) {
                $this->connectionProvider = $connectionProvider;
        }
 
@@ -55,6 +67,8 @@
        /**
         * @see QueryInterface::tableExists
         *
+        * @since wd.db
+        *
         * @param string $tableName
         *
         * @return boolean
@@ -63,4 +77,17 @@
                return $this->getDB()->tableExists( $tableName, __METHOD__ );
        }
 
+       /**
+        * @see QueryInterface::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean Success indicator
+        */
+       public function createTable( TableDefinition $table ) {
+               $this->extendedAbstraction->createTable( $table );
+       }
+
 }
diff --git a/repo/includes/Database/ObservableQueryInterface.php 
b/repo/includes/Database/ObservableQueryInterface.php
index c4b9b56..158c620 100644
--- a/repo/includes/Database/ObservableQueryInterface.php
+++ b/repo/includes/Database/ObservableQueryInterface.php
@@ -39,6 +39,11 @@
        private $callbacks = array();
 
        /**
+        * Register a callback that should be called whenever the methods
+        * which name is provided is called with the arguments this method got.
+        *
+        * @since wd.db
+        *
         * @param string $method
         * @param callable $callback
         */
@@ -46,7 +51,13 @@
                $this->callbacks[$method] = $callback;
        }
 
-       private function runCallbacks( $method, $args ) {
+       /**
+        * @since wd.db
+        *
+        * @param string $method
+        * @param array $args
+        */
+       private function runCallbacks( $method, array $args ) {
                if ( array_key_exists( $method, $this->callbacks ) ) {
                        call_user_func_array( $this->callbacks[$method], $args 
);
                }
@@ -55,6 +66,8 @@
        /**
         * @see QueryInterface::tableExists
         *
+        * @since wd.db
+        *
         * @param string $tableName
         *
         * @return boolean
@@ -63,4 +76,17 @@
                $this->runCallbacks( __FUNCTION__, func_get_args() );
        }
 
+       /**
+        * @see QueryInterface::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean
+        */
+       public function createTable( TableDefinition $table ) {
+               $this->runCallbacks( __FUNCTION__, func_get_args() );
+       }
+
 }
diff --git a/repo/includes/Database/QueryInterface.php 
b/repo/includes/Database/QueryInterface.php
index 7619f9b..4b59269 100644
--- a/repo/includes/Database/QueryInterface.php
+++ b/repo/includes/Database/QueryInterface.php
@@ -33,10 +33,23 @@
        /**
         * Returns if the table exists in the database.
         *
+        * @since wd.db
+        *
         * @param string $tableName
         *
         * @return boolean
         */
        public function tableExists( $tableName );
 
+       /**
+        * @see QueryInterface::createTable
+        *
+        * @since wd.db
+        *
+        * @param TableDefinition $table
+        *
+        * @return boolean
+        */
+       public function createTable( TableDefinition $table );
+
 }
diff --git 
a/repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php 
b/repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php
new file mode 100644
index 0000000..e093c68
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/MWDB/ExtendedAbstractionTest.php
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: j
+ * Date: 02/03/13
+ * Time: 01:29
+ * To change this template use File | Settings | File Templates.
+ */
\ No newline at end of file
diff --git 
a/repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php 
b/repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php
new file mode 100644
index 0000000..e093c68
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/MWDB/ExtendedMySQLAbstractionTest.php
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: j
+ * Date: 02/03/13
+ * Time: 01:29
+ * To change this template use File | Settings | File Templates.
+ */
\ No newline at end of file
diff --git 
a/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php 
b/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
new file mode 100644
index 0000000..71ba6fc
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/MediaWikiQueryInterfaceTest.php
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: j
+ * Date: 02/03/13
+ * Time: 01:31
+ * To change this template use File | Settings | File Templates.
+ */
\ No newline at end of file
diff --git a/repo/tests/phpunit/includes/Database/QueryInterfaceTest.php 
b/repo/tests/phpunit/includes/Database/QueryInterfaceTest.php
new file mode 100644
index 0000000..71ba6fc
--- /dev/null
+++ b/repo/tests/phpunit/includes/Database/QueryInterfaceTest.php
@@ -0,0 +1,8 @@
+<?php
+/**
+ * Created by JetBrains PhpStorm.
+ * User: j
+ * Date: 02/03/13
+ * Time: 01:31
+ * To change this template use File | Settings | File Templates.
+ */
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I763305af860c44867b7119769f93f5a4e3b62732
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Jeroen De Dauw <jeroended...@gmail.com>

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

Reply via email to