Skip to content

Commit

Permalink
Allow setting index_type (e.g., 'HASH' or 'BTREE') (#949)
Browse files Browse the repository at this point in the history
* Allow setting index_type (e.g., 'HASH' or 'BTREE')

* review feedback

* fix test compilation
  • Loading branch information
ryanpbrewster authored Jun 15, 2020
1 parent 1dce52b commit 56863e0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ data class Index(
/** Whether the index in unique or not. */
val unique: Boolean,
/** Optional custom name for the index. */
val customName: String? = null
val customName: String? = null,
/** Optional custom index type (e.g, BTREE or HASH) */
val indexType: String? = null
) : DdlAware {
/** Table where the index is defined. */
val table: Table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -917,9 +917,10 @@ open class Table(name: String = "") : ColumnSet(), DdlAware {
* @param customIndexName Name of the index.
* @param columns Columns that compose the index.
* @param isUnique Whether the index is unique or not.
* @param indexType A custom index type (e.g., "BTREE" or "HASH").
*/
fun index(customIndexName: String? = null, isUnique: Boolean = false, vararg columns: Column<*>) {
_indices.add(Index(columns.toList(), isUnique, customIndexName))
fun index(customIndexName: String? = null, isUnique: Boolean = false, vararg columns: Column<*>, indexType: String? = null) {
_indices.add(Index(columns.toList(), isUnique, customIndexName, indexType = indexType))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,15 @@ abstract class VendorDialect(
val columnsList = index.columns.joinToString(prefix = "(", postfix = ")") { t.identity(it) }
return if (index.unique) {
"ALTER TABLE $quotedTableName ADD CONSTRAINT $quotedIndexName UNIQUE $columnsList"
} else if (index.indexType != null) {
return createIndexWithType(name = quotedIndexName, table = quotedTableName, columns = columnsList, type = index.indexType)
} else {
"CREATE INDEX $quotedIndexName ON $quotedTableName $columnsList"
}
}

protected open fun createIndexWithType(name: String, table: String, columns: String, type: String): String {
return "CREATE INDEX $name ON $table $columns USING $type"
}

override fun dropIndex(tableName: String, indexName: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ open class H2Dialect : VendorDialect(dialectName, H2DataTypeProvider, H2Function
exposedLogger.warn("Index on ${index.table.tableName} for ${index.columns.joinToString { it.name }} can't be created in H2")
return ""
}
if (index.indexType != null) {
exposedLogger.warn("Index of type ${index.indexType} on ${index.table.tableName} for ${index.columns.joinToString { it.name }} can't be created in H2")
return ""
}
return super.createIndex(index)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.jetbrains.exposed.sql.vendors

import org.jetbrains.exposed.sql.Expression
import org.jetbrains.exposed.sql.QueryBuilder
import org.jetbrains.exposed.sql.Sequence
import org.jetbrains.exposed.sql.append
import org.jetbrains.exposed.sql.*

internal object MariaDBFunctionProvider : MysqlFunctionProvider() {
override fun nextVal(seq: Sequence, builder: QueryBuilder) = builder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ open class PostgreSQLDialect : VendorDialect(dialectName, PostgreSQLDataTypeProv

override fun setSchema(schema: Schema): String = "SET search_path TO ${schema.identifier}"

override fun createIndexWithType(name: String, table: String, columns: String, type: String): String {
return "CREATE INDEX $name ON $table USING $type $columns"
}

companion object {
/** PostgreSQL dialect name */
const val dialectName: String = "postgresql"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ open class SQLiteDialect : VendorDialect(dialectName, SQLiteDataTypeProvider, SQ
override fun isAllowedAsColumnDefault(e: Expression<*>): Boolean = true

override fun createIndex(index: Index): String {
if (index.indexType != null) {
exposedLogger.warn("Index of type ${index.indexType} on ${index.table.tableName} for ${index.columns.joinToString { it.name }} can't be created in SQLite")
return ""
}
val originalCreateIndex = super.createIndex(index.copy(unique = false))
return if (index.unique) {
originalCreateIndex.replace("CREATE INDEX", "CREATE UNIQUE INDEX")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jetbrains.exposed.sql.tests.shared.ddl

import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.Table
import org.jetbrains.exposed.sql.exists
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.junit.Test

class CreateIndexTests : DatabaseTestsBase() {

@Test
fun createStandardIndex() {
val TestTable = object : Table("test_table") {
val id = integer("id")
val name = varchar("name", length = 42)

override val primaryKey = PrimaryKey(id)
val byName = index("test_table_by_name", false, name)
}

withTables(excludeSettings = listOf(TestDB.H2_MYSQL), tables = *arrayOf(TestTable)) {
SchemaUtils.createMissingTablesAndColumns(TestTable)
assertTrue(TestTable.exists())
SchemaUtils.drop(TestTable)
}
}

@Test
fun createHashIndex() {
val TestTable = object : Table("test_table") {
val id = integer("id")
val name = varchar("name", length = 42)

override val primaryKey = PrimaryKey(id)
val byNameHash = index("test_table_by_name", /* isUnique = */ false, name, indexType = "HASH")
}

withTables(excludeSettings = listOf(TestDB.H2_MYSQL), tables = *arrayOf(TestTable)) {
SchemaUtils.createMissingTablesAndColumns(TestTable)
assertTrue(TestTable.exists())
SchemaUtils.drop(TestTable)
}
}
}

0 comments on commit 56863e0

Please sign in to comment.