Jeroen De Dauw has submitted this change and it was merged.

Change subject: Add sqlite indexes support
......................................................................


Add sqlite indexes support

Change-Id: I6f1bf36e9e4772a0a6957debff4251167ed08ade
---
M src/SQLite/SQLiteTableSqlBuilder.php
M tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
2 files changed, 82 insertions(+), 1 deletion(-)

Approvals:
  Jeroen De Dauw: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/src/SQLite/SQLiteTableSqlBuilder.php 
b/src/SQLite/SQLiteTableSqlBuilder.php
index 3519a80..9f928cc 100644
--- a/src/SQLite/SQLiteTableSqlBuilder.php
+++ b/src/SQLite/SQLiteTableSqlBuilder.php
@@ -5,6 +5,7 @@
 use RuntimeException;
 use Wikibase\Database\Escaper;
 use Wikibase\Database\FieldDefinition;
+use Wikibase\Database\IndexDefinition;
 use Wikibase\Database\TableDefinition;
 use Wikibase\Database\TableSqlBuilder;
 
@@ -18,6 +19,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < jeroended...@gmail.com >
+ * @author Adam Shorland
  */
 class SQLiteTableSqlBuilder extends TableSqlBuilder {
 
@@ -58,7 +60,9 @@
                // TODO: table options
                $sql .= ');';
 
-               // TODO: indexes
+               foreach ( $table->getIndexes() as $index ){
+                       $sql .= $this->getIndexSQL( $index, $table );
+               }
 
                return $sql;
        }
@@ -77,6 +81,30 @@
                $sql .= $this->getDefault( $field->getDefault() );
 
                $sql .= $this->getNull( $field->allowsNull() );
+
+               return $sql;
+       }
+
+       /**
+        * @since 0.1
+        *
+        * @param IndexDefinition $index
+        * @param TableDefinition $table
+        *
+        * @return string
+        */
+       protected function getIndexSQL( IndexDefinition $index, TableDefinition 
$table ) {
+               $sql = 'CREATE ';
+               $sql .= $this->getIndexType( $index->getType() ) . ' ';
+               $sql .= $index->getName() . ' ';
+               $sql .= 'ON '.$this->tablePrefix . $table->getName();
+
+               $columnNames = array();
+               foreach( $index->getColumns() as $columnName => $intSize ){
+                       $columnNames[] = $columnName;
+               }
+
+               $sql .= ' ('.implode( ',', $columnNames ).');';
 
                return $sql;
        }
@@ -116,4 +144,23 @@
                }
        }
 
+       /**
+        * Returns the SQL field type for a given IndexDefinition type constant.
+        *
+        * @param string $indexType
+        *
+        * @return string
+        * @throws RuntimeException
+        */
+       protected function getIndexType( $indexType ) {
+               switch ( $indexType ) {
+                       case IndexDefinition::TYPE_INDEX:
+                               return 'INDEX';
+                       case IndexDefinition::TYPE_UNIQUE:
+                               return 'UNIQUE INDEX';
+                       default:
+                               throw new RuntimeException( __CLASS__ . ' does 
not support db indexes of type ' . $indexType );
+               }
+       }
+
 }
diff --git a/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php 
b/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
index 74b3dfc..7f0e558 100644
--- a/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
+++ b/tests/phpunit/SQLite/SQLiteTableSqlBuilderTest.php
@@ -3,6 +3,7 @@
 namespace Wikibase\Database\Tests\SQLite;
 
 use Wikibase\Database\FieldDefinition;
+use Wikibase\Database\IndexDefinition;
 use Wikibase\Database\SQLite\SQLiteTableSqlBuilder;
 use Wikibase\Database\TableDefinition;
 
@@ -20,6 +21,7 @@
  *
  * @licence GNU GPL v2+
  * @author Jeroen De Dauw < jeroended...@gmail.com >
+ * @author Adam Shorland
  */
 class SQLiteTableSqlBuilderTest extends \PHPUnit_Framework_TestCase {
 
@@ -93,6 +95,38 @@
                        'CREATE TABLE dbNametableName (primaryField INT NOT 
NULL, textField BLOB NULL, intField INT DEFAULT 42 NOT NULL);'
                );
 
+               $argLists[] = array(
+                       new TableDefinition(
+                               'tableName',
+                               array(
+                                       new FieldDefinition(
+                                               'primaryField',
+                                               FieldDefinition::TYPE_INTEGER,
+                                               FieldDefinition::NOT_NULL,
+                                               FieldDefinition::NO_DEFAULT,
+                                               FieldDefinition::NO_ATTRIB
+                                       ),
+                                       new FieldDefinition(
+                                               'textField',
+                                               FieldDefinition::TYPE_TEXT
+                                       ),
+                                       new FieldDefinition(
+                                               'intField',
+                                               FieldDefinition::TYPE_INTEGER,
+                                               FieldDefinition::NOT_NULL, 42
+                                       ),
+                               ),
+                               array(
+                                       new IndexDefinition(
+                                               'indexName',
+                                               array( 'intField' => 0, 
'textField' => 0 ),
+                                               IndexDefinition::TYPE_INDEX
+                                       ),
+                               )
+                       ),
+                       'CREATE TABLE dbNametableName (primaryField INT NOT 
NULL, textField BLOB NULL, intField INT DEFAULT 42 NOT NULL);CREATE INDEX 
indexName ON dbNametableName (intField,textField);'
+               );
+
                return $argLists;
        }
 

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I6f1bf36e9e4772a0a6957debff4251167ed08ade
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Denny Vrandecic <denny.vrande...@wikimedia.de>
Gerrit-Reviewer: Jeroen De Dauw <jeroended...@gmail.com>
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