Skip to content

Commit

Permalink
Refactored "Sql" to follow the API contract of "SupportSQLiteDatabase"
Browse files Browse the repository at this point in the history
This also makes the class compatible with "MappingSupportSQLiteDatabase"

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

[1] #529
  • Loading branch information
JaniruTEC committed Apr 21, 2024
1 parent bfcca43 commit 6af9811
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
28 changes: 20 additions & 8 deletions data/src/main/java/org/cryptomator/data/db/migrations/Sql.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import androidx.sqlite.db.SupportSQLiteDatabase;

import org.cryptomator.data.util.Utils;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -121,6 +123,7 @@ public static class SqlQueryBuilder {
private List<String> columns = new ArrayList<>();
private String groupBy;
private String having;
private String orderBy;
private String limit;

public SqlQueryBuilder(String tableName) {
Expand All @@ -141,17 +144,22 @@ public SqlQueryBuilder where(String column, Criterion criterion) {
}

public SqlQueryBuilder groupBy(String groupBy) {
this.groupBy = groupBy;
this.groupBy = Utils.requireNullOrNotBlank(groupBy);
return this;
}

public SqlQueryBuilder having(String having) {
this.having = having;
this.having = Utils.requireNullOrNotBlank(having);
return this;
}

public SqlQueryBuilder orderBy(String orderBy) {
this.orderBy = Utils.requireNullOrNotBlank(orderBy);
return this;
}

public SqlQueryBuilder limit(String limit) {
this.limit = limit;
this.limit = Utils.requireNullOrNotBlank(limit);
return this;
}

Expand All @@ -162,11 +170,11 @@ public Cursor executeOn(SupportSQLiteDatabase db) {
String query = SQLiteQueryBuilder.buildQueryString( //
/* distinct */ false, //
tableName, //
columns.toArray(new String[columns.size()]), //
whereClause.toString(), //
Utils.emptyToNull(columns.toArray(new String[columns.size()])), //
Utils.blankToNull(whereClause.toString()), //
groupBy, //
having, //
/* orderBy */ null, //
orderBy, //
limit //
);
//In contrast to "SupportSQLiteDatabase#update" "query" doesn't define how the contents of "whereArgs" are bound.
Expand Down Expand Up @@ -210,7 +218,7 @@ public void executeOn(SupportSQLiteDatabase db) {
//The internal binding methods are type-safe, but resolve to just putting all args into an "Array<Object/Any>" in "SQLiteProgram" anyway.
//This array is also used by "SQLiteDatabase#update". Apparently the contents of the array are then bound as "Strings".
//As of now we always pass an "Array<String>", but all of this has to be kept in mind if we ever change this.
db.update(tableName, SQLiteDatabase.CONFLICT_NONE, contentValues, whereClause.toString(), whereArgs.toArray(new String[whereArgs.size()]));
db.update(tableName, SQLiteDatabase.CONFLICT_NONE, contentValues, Utils.blankToNull(whereClause.toString()), whereArgs.toArray(new String[whereArgs.size()]));
}
}

Expand Down Expand Up @@ -536,6 +544,10 @@ public SqlInsertBuilder integer(String column, Integer value) {
}

public Long executeOn(SupportSQLiteDatabase db) {
if (contentValues.size() == 0) {
throw new IllegalStateException("At least one value must be set");
}

//In contrast to "SupportSQLiteDatabase#update" "insert" doesn't define how the contents of "contentValues" are bound.
//As opposed to the other methods in this class, we do actually pass "Integers" and "Strings" here and again they appear
//to end up in the "Array<Object/Any>" in "SQLiteProgram". Currently there is no issue,
Expand Down Expand Up @@ -566,7 +578,7 @@ public SqlDeleteBuilder where(String column, Criterion criterion) {
public void executeOn(SupportSQLiteDatabase db) {
//"SupportSQLiteDatabase#delete" always binds the contents of "whereArgs" as "Strings".
//As of now we always pass an "Array<String>", but this has to be kept in mind if we ever change this. See: "SqlUpdateBuilder#executeOn"
db.delete(tableName, whereClause.toString(), whereArgs.toArray(new String[whereArgs.size()]));
db.delete(tableName, Utils.blankToNull(whereClause.toString()), whereArgs.toArray(new String[whereArgs.size()]));
}
}
}
18 changes: 18 additions & 0 deletions data/src/main/java/org/cryptomator/data/util/Utils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@file:JvmName("Utils")

package org.cryptomator.data.util

fun String?.blankToNull(): String? {
return if (isNullOrBlank()) null else this
}

fun <T> Array<T>?.emptyToNull(): Array<T>? {
return if (isNullOrEmpty()) null else this
}

fun String?.requireNullOrNotBlank(): String? {
if (this != null && this.isBlank()) {
throw IllegalArgumentException("String is blank")
}
return this
}

0 comments on commit 6af9811

Please sign in to comment.