diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseTypes.java b/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseTypes.java
new file mode 100644
index 000000000..3a807bd56
--- /dev/null
+++ b/bramble-core/src/main/java/org/briarproject/bramble/db/DatabaseTypes.java
@@ -0,0 +1,34 @@
+package org.briarproject.bramble.db;
+
+class DatabaseTypes {
+
+ private final String hashType, secretType, binaryType;
+ private final String counterType, stringType;
+
+ public DatabaseTypes(String hashType, String secretType, String binaryType,
+ String counterType, String stringType) {
+ this.hashType = hashType;
+ this.secretType = secretType;
+ this.binaryType = binaryType;
+ this.counterType = counterType;
+ this.stringType = stringType;
+ }
+
+ /**
+ * Replaces database type placeholders in a statement with the actual types.
+ * These placeholders are currently supported:
+ *
_HASH
+ * _SECRET
+ * _BINARY
+ * _COUNTER
+ * _STRING
+ */
+ String replaceTypes(String s) {
+ s = s.replaceAll("_HASH", hashType);
+ s = s.replaceAll("_SECRET", secretType);
+ s = s.replaceAll("_BINARY", binaryType);
+ s = s.replaceAll("_COUNTER", counterType);
+ s = s.replaceAll("_STRING", stringType);
+ return s;
+ }
+}
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java b/bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
index 85bebb67c..1d1c4c9f9 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/db/H2Database.java
@@ -30,6 +30,8 @@ class H2Database extends JdbcDatabase {
private static final String BINARY_TYPE = "BINARY";
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
private static final String STRING_TYPE = "VARCHAR";
+ private static final DatabaseTypes dbTypes = new DatabaseTypes(HASH_TYPE,
+ SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE);
private final DatabaseConfig config;
private final String url;
@@ -40,8 +42,7 @@ class H2Database extends JdbcDatabase {
@Inject
H2Database(DatabaseConfig config, MessageFactory messageFactory,
Clock clock) {
- super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE,
- messageFactory, clock);
+ super(dbTypes, messageFactory, clock);
this.config = config;
File dir = config.getDatabaseDirectory();
String path = new File(dir, "db").getAbsolutePath();
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java b/bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
index e8ba7b33d..30b6d050c 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/db/HyperSqlDatabase.java
@@ -30,6 +30,8 @@ class HyperSqlDatabase extends JdbcDatabase {
private static final String COUNTER_TYPE =
"INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1)";
private static final String STRING_TYPE = "VARCHAR";
+ private static final DatabaseTypes dbTypes = new DatabaseTypes(HASH_TYPE,
+ SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE);
private final DatabaseConfig config;
private final String url;
@@ -40,8 +42,7 @@ class HyperSqlDatabase extends JdbcDatabase {
@Inject
HyperSqlDatabase(DatabaseConfig config, MessageFactory messageFactory,
Clock clock) {
- super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE,
- messageFactory, clock);
+ super(dbTypes, messageFactory, clock);
this.config = config;
File dir = config.getDatabaseDirectory();
String path = new File(dir, "db").getAbsolutePath();
@@ -78,7 +79,7 @@ class HyperSqlDatabase extends JdbcDatabase {
}
@Override
- public long getFreeSpace() throws DbException {
+ public long getFreeSpace() {
File dir = config.getDatabaseDirectory();
long maxSize = config.getMaxSize();
long free = dir.getFreeSpace();
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
index e9d388d9f..f893d0a7f 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/db/JdbcDatabase.java
@@ -312,10 +312,9 @@ abstract class JdbcDatabase implements Database {
Logger.getLogger(JdbcDatabase.class.getName());
// Different database libraries use different names for certain types
- private final String hashType, secretType, binaryType;
- private final String counterType, stringType;
private final MessageFactory messageFactory;
private final Clock clock;
+ private final DatabaseTypes dbTypes;
// Locking: connectionsLock
private final LinkedList connections = new LinkedList<>();
@@ -330,14 +329,9 @@ abstract class JdbcDatabase implements Database {
private final Lock connectionsLock = new ReentrantLock();
private final Condition connectionsChanged = connectionsLock.newCondition();
- JdbcDatabase(String hashType, String secretType, String binaryType,
- String counterType, String stringType,
- MessageFactory messageFactory, Clock clock) {
- this.hashType = hashType;
- this.secretType = secretType;
- this.binaryType = binaryType;
- this.counterType = counterType;
- this.stringType = stringType;
+ JdbcDatabase(DatabaseTypes databaseTypes, MessageFactory messageFactory,
+ Clock clock) {
+ this.dbTypes = databaseTypes;
this.messageFactory = messageFactory;
this.clock = clock;
}
@@ -432,7 +426,7 @@ abstract class JdbcDatabase implements Database {
return Arrays.asList(
new Migration38_39(),
new Migration39_40(),
- new Migration40_41()
+ new Migration40_41(dbTypes)
);
}
@@ -492,20 +486,20 @@ abstract class JdbcDatabase implements Database {
Statement s = null;
try {
s = txn.createStatement();
- s.executeUpdate(insertTypeNames(CREATE_SETTINGS));
- s.executeUpdate(insertTypeNames(CREATE_LOCAL_AUTHORS));
- s.executeUpdate(insertTypeNames(CREATE_CONTACTS));
- s.executeUpdate(insertTypeNames(CREATE_GROUPS));
- s.executeUpdate(insertTypeNames(CREATE_GROUP_METADATA));
- s.executeUpdate(insertTypeNames(CREATE_GROUP_VISIBILITIES));
- s.executeUpdate(insertTypeNames(CREATE_MESSAGES));
- s.executeUpdate(insertTypeNames(CREATE_MESSAGE_METADATA));
- s.executeUpdate(insertTypeNames(CREATE_MESSAGE_DEPENDENCIES));
- s.executeUpdate(insertTypeNames(CREATE_OFFERS));
- s.executeUpdate(insertTypeNames(CREATE_STATUSES));
- s.executeUpdate(insertTypeNames(CREATE_TRANSPORTS));
- s.executeUpdate(insertTypeNames(CREATE_OUTGOING_KEYS));
- s.executeUpdate(insertTypeNames(CREATE_INCOMING_KEYS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_SETTINGS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_LOCAL_AUTHORS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_CONTACTS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_GROUPS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_GROUP_METADATA));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_GROUP_VISIBILITIES));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGES));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_METADATA));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_DEPENDENCIES));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_OFFERS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_STATUSES));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_TRANSPORTS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_OUTGOING_KEYS));
+ s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_KEYS));
s.close();
} catch (SQLException e) {
tryToClose(s);
@@ -530,15 +524,6 @@ abstract class JdbcDatabase implements Database {
}
}
- private String insertTypeNames(String s) {
- s = s.replaceAll("_HASH", hashType);
- s = s.replaceAll("_SECRET", secretType);
- s = s.replaceAll("_BINARY", binaryType);
- s = s.replaceAll("_COUNTER", counterType);
- s = s.replaceAll("_STRING", stringType);
- return s;
- }
-
@Override
public Connection startTransaction() throws DbException {
Connection txn;
diff --git a/bramble-core/src/main/java/org/briarproject/bramble/db/Migration40_41.java b/bramble-core/src/main/java/org/briarproject/bramble/db/Migration40_41.java
index 4d5ecea18..59bc567cb 100644
--- a/bramble-core/src/main/java/org/briarproject/bramble/db/Migration40_41.java
+++ b/bramble-core/src/main/java/org/briarproject/bramble/db/Migration40_41.java
@@ -17,6 +17,12 @@ class Migration40_41 implements Migration {
private static final Logger LOG = getLogger(Migration40_41.class.getName());
+ private final DatabaseTypes dbTypes;
+
+ public Migration40_41(DatabaseTypes databaseTypes) {
+ this.dbTypes = databaseTypes;
+ }
+
@Override
public int getStartVersion() {
return 40;
@@ -33,8 +39,7 @@ class Migration40_41 implements Migration {
try {
s = txn.createStatement();
s.execute("ALTER TABLE contacts"
- // TODO how to insertTypeNames _STRING ?
- + " ADD alias VARCHAR");
+ + dbTypes.replaceTypes(" ADD alias VARCHAR"));
} catch (SQLException e) {
tryToClose(s);
throw new DbException(e);