Skip to content

Commit

Permalink
Move doesColumnExist to SQLiteUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed Aug 16, 2024
1 parent 13917ee commit fa0819a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ import org.odk.collect.db.sqlite.DatabaseConnection
import org.odk.collect.db.sqlite.DatabaseMigrator
import org.odk.collect.db.sqlite.SQLiteColumns.ROW_ID
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.delete
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.doesColumnExist
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.query
import org.odk.collect.db.sqlite.SQLiteUtils
import org.odk.collect.entities.storage.EntitiesRepository
import org.odk.collect.entities.storage.Entity

Expand Down Expand Up @@ -214,7 +214,7 @@ class DatabaseEntitiesRepository(context: Context, dbPath: String) : EntitiesRep
return emptyList()
}

return if (SQLiteUtils.doesColumnExist(databaseConnection.readableDatabase, list, property)) {
return if (databaseConnection.readableDatabase.doesColumnExist(list, property)) {
queryWithAttachedRowId(
list,
selectionColumn = property,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.odk.collect.android.database.instances.DatabaseInstanceColumns.LAST_STATUS_CHANGE_DATE;
import static org.odk.collect.android.database.instances.DatabaseInstanceColumns.STATUS;
import static org.odk.collect.android.database.instances.DatabaseInstanceColumns.SUBMISSION_URI;
import static org.odk.collect.db.sqlite.SQLiteDatabaseExt.doesColumnExist;

public class InstanceDatabaseMigrator implements DatabaseMigrator {
private static final String[] COLUMN_NAMES_V5 = {_ID, DISPLAY_NAME, SUBMISSION_URI, CAN_EDIT_WHEN_COMPLETE,
Expand Down Expand Up @@ -71,7 +72,7 @@ public void onDowngrade(SQLiteDatabase db) {
}

private void upgradeToVersion2(SQLiteDatabase db) {
if (!SQLiteUtils.doesColumnExist(db, INSTANCES_TABLE_NAME, CAN_EDIT_WHEN_COMPLETE)) {
if (!doesColumnExist(db, INSTANCES_TABLE_NAME, CAN_EDIT_WHEN_COMPLETE)) {
SQLiteUtils.addColumn(db, INSTANCES_TABLE_NAME, CAN_EDIT_WHEN_COMPLETE, "text");

db.execSQL("UPDATE " + INSTANCES_TABLE_NAME + " SET "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ object SQLiteDatabaseExt {
fun SQLiteDatabase.delete(table: String) {
this.delete(table, null, null)
}

@JvmStatic
fun SQLiteDatabase.doesColumnExist(table: String, column: String): Boolean {
return SQLiteUtils.getColumnNames(this, table).contains(column)
}
}
10 changes: 6 additions & 4 deletions db/src/main/java/org/odk/collect/db/sqlite/SQLiteUtils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package org.odk.collect.db.sqlite;

import static org.odk.collect.db.sqlite.SQLiteDatabaseExt.doesColumnExist;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

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

/**
* @deprecated use {@link SQLiteDatabaseExt} instead.
*/
@Deprecated
public final class SQLiteUtils {
private SQLiteUtils() {
}
Expand All @@ -30,10 +36,6 @@ public static boolean doesTableExist(SQLiteDatabase db, String tableName) {
return foundTable;
}

public static boolean doesColumnExist(SQLiteDatabase db, String tableName, String columnName) {
return getColumnNames(db, tableName).contains(columnName);
}

public static List<String> getColumnNames(SQLiteDatabase db, String tableName) {
String[] columnNames;
try (Cursor c = db.query(tableName, null, null, null, null, null, null)) {
Expand Down
13 changes: 0 additions & 13 deletions db/src/test/java/org/odk/collect/db/sqlite/SQLiteUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,6 @@
@RunWith(AndroidJUnit4.class)
public class SQLiteUtilsTest {

@Test
public void doesColumnExistTest() {
String tableName = "testTable";

SQLiteDatabase db = ApplicationProvider.getApplicationContext().openOrCreateDatabase("testDatabase", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE " + tableName + " (id integer, column1 text);");

assertTrue(SQLiteUtils.doesColumnExist(db, tableName, "id"));
assertTrue(SQLiteUtils.doesColumnExist(db, tableName, "column1"));

TestCase.assertFalse(SQLiteUtils.doesColumnExist(db, tableName, "column2"));
}

@Test
public void doesTableExistTest() {
final String tableName = "testTable";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.odk.collect.db.sqlite

import android.content.Context
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.odk.collect.db.sqlite.SQLiteDatabaseExt.doesColumnExist

@RunWith(AndroidJUnit4::class)
class SqliteDatabaseExtTest {

@Test
fun doesColumnExistTest() {
val tableName = "testTable"
val db = ApplicationProvider.getApplicationContext<Context>()
.openOrCreateDatabase("testDatabase", Context.MODE_PRIVATE, null)
db.execSQL("CREATE TABLE $tableName (id integer, column1 text);")

assertThat(db.doesColumnExist(tableName, "id"), equalTo(true))
assertThat(db.doesColumnExist(tableName, "column1"), equalTo(true))
assertThat(db.doesColumnExist(tableName, "blah"), equalTo(false))
}
}

0 comments on commit fa0819a

Please sign in to comment.