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

Commit

Permalink
Added methods for creating and dropping a index. (#472)
Browse files Browse the repository at this point in the history
Added methods for creating and dropping a index
  • Loading branch information
ZhengKeli authored and 4u7 committed Sep 28, 2017
1 parent c685e07 commit e388295
Showing 1 changed file with 20 additions and 0 deletions.
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

0 comments on commit e388295

Please sign in to comment.