mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-24 04:58:45 +00:00
Convert MessageTable to kotlin.
This commit is contained in:
@@ -162,4 +162,12 @@ inline fun <T> Cursor.firstOrNull(predicate: (T) -> Boolean = { true }, mapper:
|
||||
return null
|
||||
}
|
||||
|
||||
inline fun Cursor.forEach(operation: (Cursor) -> Unit) {
|
||||
use {
|
||||
while (moveToNext()) {
|
||||
operation(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun Boolean.toInt(): Int = if (this) 1 else 0
|
||||
|
||||
@@ -70,6 +70,10 @@ fun SupportSQLiteDatabase.delete(tableName: String): DeleteBuilderPart1 {
|
||||
return DeleteBuilderPart1(this, tableName)
|
||||
}
|
||||
|
||||
fun SupportSQLiteDatabase.insertInto(tableName: String): InsertBuilderPart1 {
|
||||
return InsertBuilderPart1(this, tableName)
|
||||
}
|
||||
|
||||
class SelectBuilderPart1(
|
||||
private val db: SupportSQLiteDatabase,
|
||||
private val columns: Array<String>
|
||||
@@ -117,6 +121,10 @@ class SelectBuilderPart3(
|
||||
return SelectBuilderPart4b(db, columns, tableName, where, whereArgs, limit.toString())
|
||||
}
|
||||
|
||||
fun limit(limit: String): SelectBuilderPart4b {
|
||||
return SelectBuilderPart4b(db, columns, tableName, where, whereArgs, limit)
|
||||
}
|
||||
|
||||
fun run(): Cursor {
|
||||
return db.query(
|
||||
SupportSQLiteQueryBuilder
|
||||
@@ -140,6 +148,10 @@ class SelectBuilderPart4a(
|
||||
return SelectBuilderPart5(db, columns, tableName, where, whereArgs, orderBy, limit.toString())
|
||||
}
|
||||
|
||||
fun limit(limit: String): SelectBuilderPart5 {
|
||||
return SelectBuilderPart5(db, columns, tableName, where, whereArgs, orderBy, limit)
|
||||
}
|
||||
|
||||
fun run(): Cursor {
|
||||
return db.query(
|
||||
SupportSQLiteQueryBuilder
|
||||
@@ -220,6 +232,10 @@ class UpdateBuilderPart2(
|
||||
return UpdateBuilderPart3(db, tableName, values, where, SqlUtil.buildArgs(*whereArgs))
|
||||
}
|
||||
|
||||
fun where(@Language("sql") where: String, whereArgs: Array<String>): UpdateBuilderPart3 {
|
||||
return UpdateBuilderPart3(db, tableName, values, where, whereArgs)
|
||||
}
|
||||
|
||||
fun run(conflictStrategy: Int = SQLiteDatabase.CONFLICT_NONE): Int {
|
||||
return db.update(tableName, conflictStrategy, values, null, null)
|
||||
}
|
||||
@@ -246,6 +262,10 @@ class DeleteBuilderPart1(
|
||||
return DeleteBuilderPart2(db, tableName, where, SqlUtil.buildArgs(*whereArgs))
|
||||
}
|
||||
|
||||
fun where(@Language("sql") where: String, whereArgs: Array<String>): DeleteBuilderPart2 {
|
||||
return DeleteBuilderPart2(db, tableName, where, whereArgs)
|
||||
}
|
||||
|
||||
fun run(): Int {
|
||||
return db.delete(tableName, null, null)
|
||||
}
|
||||
@@ -271,6 +291,10 @@ class ExistsBuilderPart1(
|
||||
return ExistsBuilderPart2(db, tableName, where, SqlUtil.buildArgs(*whereArgs))
|
||||
}
|
||||
|
||||
fun where(@Language("sql") where: String, whereArgs: Array<String>): ExistsBuilderPart2 {
|
||||
return ExistsBuilderPart2(db, tableName, where, whereArgs)
|
||||
}
|
||||
|
||||
fun run(): Boolean {
|
||||
return db.query("SELECT EXISTS(SELECT 1 FROM $tableName)", null).use { cursor ->
|
||||
cursor.moveToFirst() && cursor.getInt(0) == 1
|
||||
@@ -290,3 +314,26 @@ class ExistsBuilderPart2(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class InsertBuilderPart1(
|
||||
private val db: SupportSQLiteDatabase,
|
||||
private val tableName: String
|
||||
) {
|
||||
|
||||
fun values(values: ContentValues): InsertBuilderPart2 {
|
||||
return InsertBuilderPart2(db, tableName, values)
|
||||
}
|
||||
fun values(vararg values: Pair<String, Any?>): InsertBuilderPart2 {
|
||||
return InsertBuilderPart2(db, tableName, contentValuesOf(*values))
|
||||
}
|
||||
}
|
||||
|
||||
class InsertBuilderPart2(
|
||||
private val db: SupportSQLiteDatabase,
|
||||
private val tableName: String,
|
||||
private val values: ContentValues
|
||||
) {
|
||||
fun run(conflictStrategy: Int = SQLiteDatabase.CONFLICT_NONE): Long {
|
||||
return db.insert(tableName, conflictStrategy, values)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -244,11 +244,13 @@ object SqlUtil {
|
||||
@JvmOverloads
|
||||
@JvmStatic
|
||||
fun buildCollectionQuery(column: String, values: Collection<Any?>, prefix: String = "", maxSize: Int = MAX_QUERY_ARGS): List<Query> {
|
||||
require(!values.isEmpty()) { "Must have values!" }
|
||||
|
||||
return values
|
||||
.chunked(maxSize)
|
||||
.map { batch -> buildSingleCollectionQuery(column, batch, prefix) }
|
||||
return if (values.isEmpty()) {
|
||||
emptyList()
|
||||
} else {
|
||||
values
|
||||
.chunked(maxSize)
|
||||
.map { batch -> buildSingleCollectionQuery(column, batch, prefix) }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,3 +38,7 @@ fun String.asListContains(item: String): Boolean {
|
||||
fun String.toSingleLine(): String {
|
||||
return this.trimIndent().split("\n").joinToString(separator = " ")
|
||||
}
|
||||
|
||||
fun String?.emptyIfNull(): String {
|
||||
return this ?: ""
|
||||
}
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
package org.signal.core.util
|
||||
|
||||
import androidx.sqlite.db.SupportSQLiteProgram
|
||||
import androidx.sqlite.db.SupportSQLiteQuery
|
||||
|
||||
fun SupportSQLiteQuery.toAndroidQuery(): SqlUtil.Query {
|
||||
val program = CapturingSqliteProgram(this.argCount)
|
||||
this.bindTo(program)
|
||||
return SqlUtil.Query(this.sql, program.args())
|
||||
}
|
||||
|
||||
private class CapturingSqliteProgram(count: Int) : SupportSQLiteProgram {
|
||||
private val args: Array<String?> = arrayOfNulls(count)
|
||||
|
||||
fun args(): Array<String> {
|
||||
return args.filterNotNull().toTypedArray()
|
||||
}
|
||||
|
||||
override fun close() {
|
||||
}
|
||||
|
||||
override fun bindNull(index: Int) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun bindLong(index: Int, value: Long) {
|
||||
args[index - 1] = value.toString()
|
||||
}
|
||||
|
||||
override fun bindDouble(index: Int, value: Double) {
|
||||
args[index - 1] = value.toString()
|
||||
}
|
||||
|
||||
override fun bindString(index: Int, value: String?) {
|
||||
args[index - 1] = value
|
||||
}
|
||||
|
||||
override fun bindBlob(index: Int, value: ByteArray?) {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun clearBindings() {
|
||||
for (i in args.indices) {
|
||||
args[i] = null
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
@Config(manifest = Config.NONE, application = Application.class)
|
||||
@@ -164,9 +165,9 @@ public final class SqlUtilTest {
|
||||
assertArrayEquals(new String[] { "1", "2", "3" }, updateQuery.get(0).getWhereArgs());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void buildCollectionQuery_none() {
|
||||
SqlUtil.buildCollectionQuery("a", Collections.emptyList());
|
||||
List<SqlUtil.Query> results = SqlUtil.buildCollectionQuery("a", Collections.emptyList());
|
||||
assertTrue(results.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user