mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
Refactor KeyValueStore
This commit is contained in:
parent
26cef99cdf
commit
fbcc1cfb50
1 changed files with 30 additions and 20 deletions
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue