mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-23 04:28:35 +00:00
Improve safety of update and delete database methods.
This commit is contained in:
@@ -92,18 +92,31 @@ fun SupportSQLiteDatabase.count(): SelectBuilderPart1 {
|
||||
|
||||
/**
|
||||
* Begins an UPDATE statement with a helpful builder pattern.
|
||||
* Requires a WHERE clause as a way of mitigating mistakes. If you'd like to update all items in the table, use [updateAll].
|
||||
*/
|
||||
fun SupportSQLiteDatabase.update(tableName: String): UpdateBuilderPart1 {
|
||||
return UpdateBuilderPart1(this, tableName)
|
||||
}
|
||||
|
||||
fun SupportSQLiteDatabase.updateAll(tableName: String): UpdateAllBuilderPart1 {
|
||||
return UpdateAllBuilderPart1(this, tableName)
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins a DELETE statement with a helpful builder pattern.
|
||||
* Requires a WHERE clause as a way of mitigating mistakes. If you'd like to delete all items in the table, use [deleteAll].
|
||||
*/
|
||||
fun SupportSQLiteDatabase.delete(tableName: String): DeleteBuilderPart1 {
|
||||
return DeleteBuilderPart1(this, tableName)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes all data in the table.
|
||||
*/
|
||||
fun SupportSQLiteDatabase.deleteAll(tableName: String): Int {
|
||||
return this.delete(tableName, null, null)
|
||||
}
|
||||
|
||||
fun SupportSQLiteDatabase.insertInto(tableName: String): InsertBuilderPart1 {
|
||||
return InsertBuilderPart1(this, tableName)
|
||||
}
|
||||
@@ -271,16 +284,14 @@ class UpdateBuilderPart2(
|
||||
private val values: ContentValues
|
||||
) {
|
||||
fun where(@Language("sql") where: String, vararg whereArgs: Any): UpdateBuilderPart3 {
|
||||
require(where.isNotBlank())
|
||||
return UpdateBuilderPart3(db, tableName, values, where, SqlUtil.buildArgs(*whereArgs))
|
||||
}
|
||||
|
||||
fun where(@Language("sql") where: String, whereArgs: Array<String>): UpdateBuilderPart3 {
|
||||
require(where.isNotBlank())
|
||||
return UpdateBuilderPart3(db, tableName, values, where, whereArgs)
|
||||
}
|
||||
|
||||
fun run(conflictStrategy: Int = SQLiteDatabase.CONFLICT_NONE): Int {
|
||||
return db.update(tableName, conflictStrategy, values, null, arrayOf<String>())
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateBuilderPart3(
|
||||
@@ -296,21 +307,43 @@ class UpdateBuilderPart3(
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateAllBuilderPart1(
|
||||
private val db: SupportSQLiteDatabase,
|
||||
private val tableName: String
|
||||
) {
|
||||
fun values(values: ContentValues): UpdateAllBuilderPart2 {
|
||||
return UpdateAllBuilderPart2(db, tableName, values)
|
||||
}
|
||||
|
||||
fun values(vararg values: Pair<String, Any?>): UpdateAllBuilderPart2 {
|
||||
return UpdateAllBuilderPart2(db, tableName, contentValuesOf(*values))
|
||||
}
|
||||
}
|
||||
|
||||
class UpdateAllBuilderPart2(
|
||||
private val db: SupportSQLiteDatabase,
|
||||
private val tableName: String,
|
||||
private val values: ContentValues
|
||||
) {
|
||||
@JvmOverloads
|
||||
fun run(conflictStrategy: Int = SQLiteDatabase.CONFLICT_NONE): Int {
|
||||
return db.update(tableName, conflictStrategy, values, null, emptyArray<String>())
|
||||
}
|
||||
}
|
||||
|
||||
class DeleteBuilderPart1(
|
||||
private val db: SupportSQLiteDatabase,
|
||||
private val tableName: String
|
||||
) {
|
||||
fun where(@Language("sql") where: String, vararg whereArgs: Any): DeleteBuilderPart2 {
|
||||
require(where.isNotBlank())
|
||||
return DeleteBuilderPart2(db, tableName, where, SqlUtil.buildArgs(*whereArgs))
|
||||
}
|
||||
|
||||
fun where(@Language("sql") where: String, whereArgs: Array<String>): DeleteBuilderPart2 {
|
||||
require(where.isNotBlank())
|
||||
return DeleteBuilderPart2(db, tableName, where, whereArgs)
|
||||
}
|
||||
|
||||
fun run(): Int {
|
||||
return db.delete(tableName, null, emptyArray<String>())
|
||||
}
|
||||
}
|
||||
|
||||
class DeleteBuilderPart2(
|
||||
|
||||
Reference in New Issue
Block a user