Jeroen De Dauw has submitted this change and it was merged. Change subject: Add index support to createTable ......................................................................
Add index support to createTable Change-Id: If0a43b32151e648bce711352f1e34abc96e33274 --- M src/MySQL/MySqlTableSqlBuilder.php M tests/phpunit/MySQL/MySQLTableSqlBuilderTest.php 2 files changed, 107 insertions(+), 6 deletions(-) Approvals: Jeroen De Dauw: Looks good to me, approved jenkins-bot: Verified diff --git a/src/MySQL/MySqlTableSqlBuilder.php b/src/MySQL/MySqlTableSqlBuilder.php index 1008a00..da9f92e 100644 --- a/src/MySQL/MySqlTableSqlBuilder.php +++ b/src/MySQL/MySqlTableSqlBuilder.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 < [email protected] > + * @author Adam Shorland */ class MySqlTableSqlBuilder extends TableSqlBuilder { @@ -50,18 +52,20 @@ // TODO: get rid of global (DatabaseBase currently provides no access to its mTablePrefix field) $sql = 'CREATE TABLE `' . $this->dbName . '`.' . $this->tablePrefix . $table->getName() . ' ('; - $fields = array(); + $queryParts = array(); foreach ( $table->getFields() as $field ) { - $fields[] = $field->getName() . ' ' . $this->getFieldSQL( $field ); + $queryParts[] = $field->getName() . ' ' . $this->getFieldSQL( $field ); } - $sql .= implode( ', ', $fields ); + foreach ( $table->getIndexes() as $index ){ + $queryParts[] = $this->getIndexSQL( $index ); + } + + $sql .= implode( ', ', $queryParts ); // TODO: table options $sql .= ') ' . 'ENGINE=InnoDB, DEFAULT CHARSET=binary'; - - // TODO: indexes return $sql; } @@ -72,7 +76,6 @@ * @param FieldDefinition $field * * @return string - * @throws RuntimeException */ protected function getFieldSQL( FieldDefinition $field ) { $sql = $this->getFieldType( $field->getType() ); @@ -82,6 +85,30 @@ $sql .= $this->getNull( $field->allowsNull() ); // TODO: add all field stuff relevant here + + return $sql; + } + + /** + * @since 0.1 + * + * @param IndexDefinition $index + * + * @return string + */ + protected function getIndexSQL( IndexDefinition $index ) { + $sql = $this->getIndexType( $index->getType() ); + + if( $index->getType() !== IndexDefinition::TYPE_PRIMARY ){ + $sql .= ' `'.$index->getName().'`'; + } + + $columnNames = array(); + foreach( $index->getColumns() as $columnName => $intSize ){ + $columnNames[] = $columnName; + } + + $sql .= ' (`'.implode( '`,`', $columnNames ).'`)'; return $sql; } @@ -121,4 +148,29 @@ } } + /** + * Returns the MySQL field type for a given IndexDefinition type constant. + * + * @param string $indexType + * + * @return string + * @throws RuntimeException + */ + protected function getIndexType( $indexType ) { + switch ( $indexType ) { + case IndexDefinition::TYPE_PRIMARY: + return 'PRIMARY KEY'; + case IndexDefinition::TYPE_INDEX: + return 'INDEX'; + case IndexDefinition::TYPE_UNIQUE: + return 'UNIQUE INDEX'; + case IndexDefinition::TYPE_SPATIAL: + return 'SPATIAL INDEX'; + case IndexDefinition::TYPE_FULLTEXT: + return 'FULLTEXT INDEX'; + default: + throw new RuntimeException( __CLASS__ . ' does not support db indexes of type ' . $indexType ); + } + } + } diff --git a/tests/phpunit/MySQL/MySQLTableSqlBuilderTest.php b/tests/phpunit/MySQL/MySQLTableSqlBuilderTest.php index 56e1d61..c9461a5 100644 --- a/tests/phpunit/MySQL/MySQLTableSqlBuilderTest.php +++ b/tests/phpunit/MySQL/MySQLTableSqlBuilderTest.php @@ -3,6 +3,7 @@ namespace Wikibase\Database\Tests\MySQL; use Wikibase\Database\FieldDefinition; +use Wikibase\Database\IndexDefinition; use Wikibase\Database\MySQL\MySqlTableSqlBuilder; use Wikibase\Database\TableDefinition; @@ -20,6 +21,7 @@ * * @licence GNU GPL v2+ * @author Jeroen De Dauw < [email protected] > + * @author Adam Shorland */ class MySQLTableSqlBuilderTest extends \PHPUnit_Framework_TestCase { @@ -85,6 +87,53 @@ 'CREATE TABLE `dbName`.prefix_tableName (primaryField INT NOT NULL, textField BLOB NULL, intField INT DEFAULT 42 NOT NULL) ENGINE=InnoDB, DEFAULT CHARSET=binary' ); + + $argLists[] = array( + new TableDefinition( + 'tableName', + array( + new FieldDefinition( + 'textField', FieldDefinition::TYPE_TEXT + ), + new FieldDefinition( + 'intField', FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 42 + ), + ), + array( + new IndexDefinition( + 'indexName', array( 'textField' => 0, 'intField' => 0 ), IndexDefinition::TYPE_INDEX + ), + ) + ), + 'CREATE TABLE `dbName`.prefix_tableName (textField BLOB NULL, intField INT DEFAULT 42 NOT NULL, INDEX `indexName` (`textField`,`intField`)) ENGINE=InnoDB, DEFAULT CHARSET=binary' + ); + + $argLists[] = array( + new TableDefinition( + 'tableName', + array( + new FieldDefinition( + 'textField', FieldDefinition::TYPE_TEXT + ), + new FieldDefinition( + 'intField', FieldDefinition::TYPE_INTEGER, FieldDefinition::NOT_NULL, 42 + ), + new FieldDefinition( + 'textField2', FieldDefinition::TYPE_TEXT + ), + ), + array( + new IndexDefinition( + 'indexName', array( 'intField' => 0 ), IndexDefinition::TYPE_INDEX + ), + new IndexDefinition( + 'uniqueIndexName', array( 'textField2' => 0 ), IndexDefinition::TYPE_UNIQUE + ), + ) + ), + 'CREATE TABLE `dbName`.prefix_tableName (textField BLOB NULL, intField INT DEFAULT 42 NOT NULL, textField2 BLOB NULL, INDEX `indexName` (`intField`), UNIQUE INDEX `uniqueIndexName` (`textField2`)) ENGINE=InnoDB, DEFAULT CHARSET=binary' + ); + return $argLists; } -- To view, visit https://gerrit.wikimedia.org/r/80544 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: If0a43b32151e648bce711352f1e34abc96e33274 Gerrit-PatchSet: 4 Gerrit-Project: mediawiki/extensions/WikibaseDatabase Gerrit-Branch: master Gerrit-Owner: Addshore <[email protected]> Gerrit-Reviewer: Addshore <[email protected]> Gerrit-Reviewer: Denny Vrandecic <[email protected]> Gerrit-Reviewer: Jeroen De Dauw <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
