ArbitraryDataProvider: distinguish between null value and nonexisting entities

Not the same thing!

Signed-off-by: Álvaro Brey <alvaro.brey@nextcloud.com>
This commit is contained in:
Álvaro Brey 2022-11-17 12:57:27 +01:00
parent 9c218ed18a
commit ec4067c199
3 changed files with 10 additions and 8 deletions

View file

@ -56,11 +56,11 @@ class ArbitraryDataProviderIT : AbstractIT() {
fun testBoolean() {
val key = "DUMMY_KEY"
var value = true
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value.toString())
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value)
assertEquals(value, arbitraryDataProvider.getBooleanValue(user.accountName, key))
value = false
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value.toString())
arbitraryDataProvider.storeOrUpdateKeyValue(user.accountName, key, value)
assertEquals(value, arbitraryDataProvider.getBooleanValue(user.accountName, key))
}

View file

@ -24,14 +24,15 @@ package com.nextcloud.client.database.dao
import androidx.room.Dao
import androidx.room.Query
import com.nextcloud.client.database.entity.ArbitraryDataEntity
@Dao
interface ArbitraryDataDao {
@Query("INSERT INTO arbitrary_data(cloud_id, `key`, value) VALUES(:accountName, :key, :value)")
fun insertValue(accountName: String, key: String, value: String?)
@Query("SELECT value FROM arbitrary_data WHERE cloud_id = :accountName AND `key` = :key LIMIT 1")
fun getValue(accountName: String, key: String): String?
@Query("SELECT * FROM arbitrary_data WHERE cloud_id = :accountName AND `key` = :key LIMIT 1")
fun getByAccountAndKey(accountName: String, key: String): ArbitraryDataEntity?
@Query("UPDATE arbitrary_data SET value = :value WHERE cloud_id = :accountName AND `key` = :key ")
fun updateValue(accountName: String, key: String, value: String?)

View file

@ -25,6 +25,7 @@ import android.content.Context;
import com.nextcloud.client.account.User;
import com.nextcloud.client.database.NextcloudDatabase;
import com.nextcloud.client.database.dao.ArbitraryDataDao;
import com.nextcloud.client.database.entity.ArbitraryDataEntity;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -71,7 +72,7 @@ public class ArbitraryDataProviderImpl implements ArbitraryDataProvider {
public void storeOrUpdateKeyValue(@NonNull String accountName,
@NonNull String key,
@Nullable String newValue) {
final String currentValue = arbitraryDataDao.getValue(accountName, key);
final ArbitraryDataEntity currentValue = arbitraryDataDao.getByAccountAndKey(accountName, key);
if (currentValue != null) {
arbitraryDataDao.updateValue(accountName, key, newValue);
} else {
@ -138,10 +139,10 @@ public class ArbitraryDataProviderImpl implements ArbitraryDataProvider {
@Override
@NonNull
public String getValue(@NonNull String accountName, @NonNull String key) {
final String value = arbitraryDataDao.getValue(accountName, key);
if (value == null) {
final ArbitraryDataEntity entity = arbitraryDataDao.getByAccountAndKey(accountName, key);
if (entity == null || entity.getValue() == null) {
return "";
}
return value;
return entity.getValue();
}
}