Jeroen De Dauw has submitted this change and it was merged.
Change subject: Introducing IndexSqlBuilders
......................................................................
Introducing IndexSqlBuilders
Change-Id: I5e9b15462182617f92755debb4baf7178015cdbb
---
A src/MySQL/MySQLIndexSqlBuilder.php
M src/MySQL/MySQLSchemaSqlBuilder.php
A src/SQLite/SQLiteIndexSqlBuilder.php
M src/SQLite/SQLiteSchemaSqlBuilder.php
M src/SQLite/SQLiteTableSqlBuilder.php
A src/Schema/IndexSqlBuilder.php
M src/Schema/SchemaModificationSqlBuilder.php
7 files changed, 117 insertions(+), 55 deletions(-)
Approvals:
Jeroen De Dauw: Looks good to me, approved
diff --git a/src/MySQL/MySQLIndexSqlBuilder.php
b/src/MySQL/MySQLIndexSqlBuilder.php
new file mode 100644
index 0000000..74a1a51
--- /dev/null
+++ b/src/MySQL/MySQLIndexSqlBuilder.php
@@ -0,0 +1,20 @@
+<?php
+
+namespace Wikibase\Database\MySQL;
+
+use Wikibase\Database\Schema\Definitions\IndexDefinition;
+use Wikibase\Database\Schema\Definitions\TableDefinition;
+use Wikibase\Database\Schema\IndexSqlBuilder;
+
+/**
+ * @since 0.1
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ */
+class MySQLIndexSqlBuilder extends IndexSqlBuilder {
+
+ public function getIndexSQL( IndexDefinition $index, TableDefinition
$table ){
+ //TODO take logic from MySQLTableSqlBuilder
+ }
+
+}
\ No newline at end of file
diff --git a/src/MySQL/MySQLSchemaSqlBuilder.php
b/src/MySQL/MySQLSchemaSqlBuilder.php
index a4f0c94..5db7acc 100644
--- a/src/MySQL/MySQLSchemaSqlBuilder.php
+++ b/src/MySQL/MySQLSchemaSqlBuilder.php
@@ -61,11 +61,11 @@
/**
* @param string $tableName
- * @param IndexDefinition $field
+ * @param IndexDefinition $index
*
* @return string
*/
- public function getAddIndexSql( $tableName, IndexDefinition $field ){
+ public function getAddIndexSql( $tableName, IndexDefinition $index ){
//TODO
}
diff --git a/src/SQLite/SQLiteIndexSqlBuilder.php
b/src/SQLite/SQLiteIndexSqlBuilder.php
new file mode 100644
index 0000000..dbd342f
--- /dev/null
+++ b/src/SQLite/SQLiteIndexSqlBuilder.php
@@ -0,0 +1,62 @@
+<?php
+
+namespace Wikibase\Database\SQLite;
+
+use RuntimeException;
+use Wikibase\Database\Schema\Definitions\IndexDefinition;
+use Wikibase\Database\Schema\Definitions\TableDefinition;
+use Wikibase\Database\Schema\IndexSqlBuilder;
+use Wikibase\Database\TableNameFormatter;
+
+/**
+ * @since 0.1
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ */
+class SQLiteIndexSqlBuilder extends IndexSqlBuilder {
+
+ protected $tableNameFormatter;
+
+ /**
+ * @param TableNameFormatter $tableNameFormatter
+ */
+ public function __construct( TableNameFormatter $tableNameFormatter ) {
+ $this->tableNameFormatter = $tableNameFormatter;
+ }
+
+ public function getIndexSQL( IndexDefinition $index, TableDefinition
$table ){
+ $sql = 'CREATE ';
+ $sql .= $this->getIndexType( $index->getType() ) . ' ';
+ $sql .= $index->getName() . ' ';
+ $sql .= 'ON ' . $this->tableNameFormatter->formatTableName(
$table->getName() );
+
+ $columnNames = array();
+ foreach( $index->getColumns() as $columnName => $intSize ){
+ $columnNames[] = $columnName;
+ }
+
+ $sql .= ' ('.implode( ',', $columnNames ).');';
+
+ return $sql;
+ }
+
+ /**
+ * 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 );
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/SQLite/SQLiteSchemaSqlBuilder.php
b/src/SQLite/SQLiteSchemaSqlBuilder.php
index 003091a..4246d41 100644
--- a/src/SQLite/SQLiteSchemaSqlBuilder.php
+++ b/src/SQLite/SQLiteSchemaSqlBuilder.php
@@ -91,11 +91,11 @@
/**
* @param string $tableName
- * @param IndexDefinition $field
+ * @param IndexDefinition $index
*
* @return string
*/
- public function getAddIndexSql( $tableName, IndexDefinition $field ){
+ public function getAddIndexSql( $tableName, IndexDefinition $index ){
//TODO
}
diff --git a/src/SQLite/SQLiteTableSqlBuilder.php
b/src/SQLite/SQLiteTableSqlBuilder.php
index ab2fdf2..2dc10cd 100644
--- a/src/SQLite/SQLiteTableSqlBuilder.php
+++ b/src/SQLite/SQLiteTableSqlBuilder.php
@@ -23,6 +23,7 @@
protected $escaper;
protected $tableNameFormatter;
protected $fieldSqlBuilder;
+ protected $indexSqlBuilder;
/**
* @param Escaper $fieldValueEscaper
@@ -32,6 +33,7 @@
$this->escaper = $fieldValueEscaper;
$this->tableNameFormatter = $tableNameFormatter;
$this->fieldSqlBuilder = new SQLiteFieldSqlBuilder(
$this->escaper );
+ $this->indexSqlBuilder = new SQLiteIndexSqlBuilder(
$tableNameFormatter );
}
/**
@@ -45,7 +47,7 @@
*/
public function getCreateTableSql( TableDefinition $table ) {
$sql = 'CREATE TABLE ' .
- $this->formatTableName( $table->getName() ) . ' (';
+ $this->tableNameFormatter->formatTableName(
$table->getName() ) . ' (';
$fields = array();
@@ -58,57 +60,10 @@
$sql .= ');';
foreach ( $table->getIndexes() as $index ){
- $sql .= $this->getIndexSQL( $index, $table );
+ $sql .= $this->indexSqlBuilder->getIndexSQL( $index,
$table );
}
return $sql;
- }
-
- protected function formatTableName( $name ) {
- return $this->tableNameFormatter->formatTableName( $name );
- }
-
- /**
- * @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->formatTableName( $table->getName() );
-
- $columnNames = array();
- foreach( $index->getColumns() as $columnName => $intSize ){
- $columnNames[] = $columnName;
- }
-
- $sql .= ' ('.implode( ',', $columnNames ).');';
-
- return $sql;
- }
-
- /**
- * 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/src/Schema/IndexSqlBuilder.php b/src/Schema/IndexSqlBuilder.php
new file mode 100644
index 0000000..f9404b1
--- /dev/null
+++ b/src/Schema/IndexSqlBuilder.php
@@ -0,0 +1,25 @@
+<?php
+
+namespace Wikibase\Database\Schema;
+
+use Wikibase\Database\Schema\Definitions\IndexDefinition;
+use Wikibase\Database\Schema\Definitions\TableDefinition;
+
+/**
+ * @since 0.1
+ * @licence GNU GPL v2+
+ * @author Adam Shorland
+ */
+abstract class IndexSqlBuilder {
+
+ /**
+ * @since 0.1
+ *
+ * @param IndexDefinition $index
+ * @param TableDefinition $table
+ *
+ * @return string The SQL for creating the index
+ */
+ public abstract function getIndexSQL( IndexDefinition $index,
TableDefinition $table );
+
+}
\ No newline at end of file
diff --git a/src/Schema/SchemaModificationSqlBuilder.php
b/src/Schema/SchemaModificationSqlBuilder.php
index bcd23ad..c44e0df 100644
--- a/src/Schema/SchemaModificationSqlBuilder.php
+++ b/src/Schema/SchemaModificationSqlBuilder.php
@@ -38,10 +38,10 @@
/**
* @param string $tableName
- * @param IndexDefinition $field
+ * @param IndexDefinition $index
*
* @return string
*/
- public function getAddIndexSql( $tableName, IndexDefinition $field );
+ public function getAddIndexSql( $tableName, IndexDefinition $index );
}
--
To view, visit https://gerrit.wikimedia.org/r/88054
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I5e9b15462182617f92755debb4baf7178015cdbb
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/WikibaseDatabase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[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