From d98ce8b622dc707f394f7f70740ddef9fd595a08 Mon Sep 17 00:00:00 2001 From: JaniruTEC <52893617+JaniruTEC@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:52:16 +0100 Subject: [PATCH] Specified contract for "SupportSQLiteDatabase.insert" Also moved compat method for "ContentValues.isEmpty" *This commit is related to issue #529 [1]* [1] https://github.com/cryptomator/android/issues/529 --- .../data/db/sqlmapping/AOP_SQLiteDatabase.java | 13 +++++-------- .../org/cryptomator/data/db/sqlmapping/Helpers.kt | 12 ++++++++++++ .../db/sqlmapping/MappingSupportSQLiteDatabase.kt | 7 +++++++ 3 files changed, 24 insertions(+), 8 deletions(-) create mode 100644 data/src/main/java/org/cryptomator/data/db/sqlmapping/Helpers.kt diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/AOP_SQLiteDatabase.java b/data/src/main/java/org/cryptomator/data/db/sqlmapping/AOP_SQLiteDatabase.java index dd78c0fe3..b297828b2 100644 --- a/data/src/main/java/org/cryptomator/data/db/sqlmapping/AOP_SQLiteDatabase.java +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/AOP_SQLiteDatabase.java @@ -28,7 +28,8 @@ package org.cryptomator.data.db.sqlmapping; import android.content.ContentValues; -import android.os.Build; + +import static org.cryptomator.data.db.sqlmapping.HelpersKt.compatIsEmpty; final class AOP_SQLiteDatabase { @@ -46,7 +47,7 @@ InsertStatement insertWithOnConflict(String table, String nullColumnHack, Conten Object[] bindArgs = null; //int size = (initialValues != null && !initialValues.isEmpty()) ? initialValues.size() : 0; - int size = (initialValues != null && !isEmpty(initialValues)) ? initialValues.size() : 0; + int size = (initialValues != null && !compatIsEmpty(initialValues)) ? initialValues.size() : 0; if (size > 0) { bindArgs = new Object[size]; int i = 0; @@ -71,12 +72,8 @@ InsertStatement insertWithOnConflict(String table, String nullColumnHack, Conten } } - private boolean isEmpty(ContentValues contentValues) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { - return contentValues.isEmpty(); - } else { - return contentValues.size() == 0; - } + boolean isValidConflictAlgorithm(int conflictAlgorithm) { + return conflictAlgorithm >= 0 && conflictAlgorithm < CONFLICT_VALUES.length; } static class InsertStatement { diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/Helpers.kt b/data/src/main/java/org/cryptomator/data/db/sqlmapping/Helpers.kt new file mode 100644 index 000000000..8c55d254f --- /dev/null +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/Helpers.kt @@ -0,0 +1,12 @@ +package org.cryptomator.data.db.sqlmapping + +import android.content.ContentValues +import android.os.Build + +internal fun ContentValues.compatIsEmpty(): Boolean { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + isEmpty + } else { + size() == 0 + } +} \ No newline at end of file diff --git a/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt b/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt index 7fca3e115..1ba009fd2 100644 --- a/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt +++ b/data/src/main/java/org/cryptomator/data/db/sqlmapping/MappingSupportSQLiteDatabase.kt @@ -2,6 +2,7 @@ package org.cryptomator.data.db.sqlmapping import android.content.ContentValues import android.database.Cursor +import android.database.sqlite.SQLiteException import android.os.CancellationSignal import androidx.sqlite.db.SimpleSQLiteQuery import androidx.sqlite.db.SupportSQLiteDatabase @@ -40,6 +41,12 @@ internal class MappingSupportSQLiteDatabase( } override fun insert(table: String, conflictAlgorithm: Int, values: ContentValues): Long { + if (values.compatIsEmpty()) { + throw SQLiteException("Can't insert empty set of values") + } + if (!helper.isValidConflictAlgorithm(conflictAlgorithm)) { + throw SQLiteException("Invalid conflict algorithm") + } val processed = helper.insertWithOnConflict(table, null, values, conflictAlgorithm) val statement = MappingSupportSQLiteStatement(processed.sql) SimpleSQLiteQuery.bind(statement, processed.bindArgs)