Convert IdentityTable to kotlin.

This commit is contained in:
Greyson Parrelli
2022-12-08 09:05:32 -05:00
committed by Alex Hart
parent 380b377ed8
commit 69003dfbe2
7 changed files with 301 additions and 289 deletions

View File

@@ -12,4 +12,8 @@ fun <E> Optional<E>.or(other: Optional<E>): Optional<E> {
fun <E> Optional<E>.isAbsent(): Boolean {
return !isPresent
}
fun <E : Any> E?.toOptional(): Optional<E> {
return Optional.ofNullable(this)
}

View File

@@ -38,10 +38,8 @@ fun SupportSQLiteDatabase.getTableRowCount(table: String): Int {
/**
* Checks if a row exists that matches the query.
*/
fun SupportSQLiteDatabase.exists(table: String, query: String, vararg args: Any): Boolean {
return this.query("SELECT EXISTS(SELECT 1 FROM $table WHERE $query)", SqlUtil.buildArgs(*args)).use { cursor ->
cursor.moveToFirst() && cursor.getInt(0) == 1
}
fun SupportSQLiteDatabase.exists(table: String): ExistsBuilderPart1 {
return ExistsBuilderPart1(this, table)
}
/**
@@ -259,3 +257,32 @@ class DeleteBuilderPart2(
return db.delete(tableName, where, whereArgs)
}
}
class ExistsBuilderPart1(
private val db: SupportSQLiteDatabase,
private val tableName: String
) {
fun where(@Language("sql") where: String, vararg whereArgs: Any): ExistsBuilderPart2 {
return ExistsBuilderPart2(db, tableName, where, SqlUtil.buildArgs(*whereArgs))
}
fun run(): Boolean {
return db.query("SELECT EXISTS(SELECT 1 FROM $tableName)", null).use { cursor ->
cursor.moveToFirst() && cursor.getInt(0) == 1
}
}
}
class ExistsBuilderPart2(
private val db: SupportSQLiteDatabase,
private val tableName: String,
private val where: String,
private val whereArgs: Array<String>
) {
fun run(): Boolean {
return db.query("SELECT EXISTS(SELECT 1 FROM $tableName WHERE $where)", SqlUtil.buildArgs(*whereArgs)).use { cursor ->
cursor.moveToFirst() && cursor.getInt(0) == 1
}
}
}