mirror of
https://github.com/aljazceru/signal-cli.git
synced 2025-12-25 18:14:27 +01:00
Refactor KeyValueStore
This commit is contained in:
@@ -36,6 +36,22 @@ public class KeyValueStore {
|
||||
}
|
||||
|
||||
public <T> T getEntry(KeyValueEntry<T> key) {
|
||||
try (final var connection = database.getConnection()) {
|
||||
return getEntry(connection, key);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed read from pre_key store", e);
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void storeEntry(KeyValueEntry<T> key, T value) {
|
||||
try (final var connection = database.getConnection()) {
|
||||
storeEntry(connection, key, value);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed update key_value store", e);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> T getEntry(final Connection connection, final KeyValueEntry<T> key) throws SQLException {
|
||||
final var sql = (
|
||||
"""
|
||||
SELECT key, value
|
||||
@@ -43,24 +59,22 @@ public class KeyValueStore {
|
||||
WHERE p.key = ?
|
||||
"""
|
||||
).formatted(TABLE_KEY_VALUE);
|
||||
try (final var connection = database.getConnection()) {
|
||||
try (final var statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, key.key());
|
||||
try (final var statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, key.key());
|
||||
|
||||
final var result = Utils.executeQueryForOptional(statement,
|
||||
resultSet -> readValueFromResultSet(key, resultSet)).orElse(null);
|
||||
final var result = Utils.executeQueryForOptional(statement,
|
||||
resultSet -> readValueFromResultSet(key, resultSet)).orElse(null);
|
||||
|
||||
if (result == null) {
|
||||
return key.defaultValue();
|
||||
}
|
||||
return result;
|
||||
if (result == null) {
|
||||
return key.defaultValue();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed read from pre_key store", e);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void storeEntry(KeyValueEntry<T> key, T value) {
|
||||
private <T> void storeEntry(
|
||||
final Connection connection, final KeyValueEntry<T> key, final T value
|
||||
) throws SQLException {
|
||||
final var sql = (
|
||||
"""
|
||||
INSERT INTO %s (key, value)
|
||||
@@ -68,14 +82,10 @@ public class KeyValueStore {
|
||||
ON CONFLICT (key) DO UPDATE SET value=excluded.value
|
||||
"""
|
||||
).formatted(TABLE_KEY_VALUE);
|
||||
try (final var connection = database.getConnection()) {
|
||||
try (final var statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, key.key());
|
||||
setParameterValue(statement, 2, key.clazz(), value);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException("Failed update key_value store", e);
|
||||
try (final var statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, key.key());
|
||||
setParameterValue(statement, 2, key.clazz(), value);
|
||||
statement.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user