Skip to content
This repository has been archived by the owner on Jul 13, 2020. It is now read-only.

Added methods for creating and dropping a index. #472

Merged
merged 2 commits into from
Sep 28, 2017
Merged
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
20 changes: 20 additions & 0 deletions anko/library/static/sqlite/src/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,26 @@ fun SQLiteDatabase.dropTable(tableName: String, ifExists: Boolean = false) {
execSQL("DROP TABLE $ifExistsText `$escapedTableName`;")
}

fun SQLiteDatabase.createIndex(indexName: String, tableName: String, unique: Boolean = false, ifNotExists: Boolean = false, vararg columns: String) {
val escapedIndexName = indexName.replace("`", "``")
val escapedTableName = tableName.replace("`", "``")
val ifNotExistsText = if (ifNotExists) "IF NOT EXISTS" else ""
val uniqueText = if (unique) "UNIQUE" else ""
execSQL(
columns.joinToString(
separator = ",",
prefix = "CREATE $uniqueText INDEX $ifNotExistsText `$escapedIndexName` ON `$escapedTableName`(",
postfix = ");"
)
)
}

fun SQLiteDatabase.dropIndex(indexName: String, ifExists: Boolean = false) {
val escapedIndexName = indexName.replace("`", "``")
val ifExistsText = if (ifExists) "IF EXISTS" else ""
execSQL("DROP INDEX $ifExistsText `$escapedIndexName`;")
}

private val ARG_PATTERN: Pattern = Pattern.compile("([^\\\\])\\{([^{}]+)\\}")

internal fun applyArguments(whereClause: String, vararg args: Pair<String, Any>): String {
Expand Down