Skip to content

Commit

Permalink
Specified contract for "SupportSQLiteDatabase.insert"
Browse files Browse the repository at this point in the history
Also moved compat method for "ContentValues.isEmpty"

*This commit is related to issue #529 [1]*

[1] #529
  • Loading branch information
JaniruTEC committed Apr 13, 2024
1 parent c89f4b1 commit d98ce8b
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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;
Expand All @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions data/src/main/java/org/cryptomator/data/db/sqlmapping/Helpers.kt
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d98ce8b

Please sign in to comment.