Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting index_type (e.g., 'HASH' or 'BTREE') #949

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -877,9 +877,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 @@ -699,10 +699,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 @@ -145,6 +145,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 @@ -223,6 +223,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)
}
}
}