mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-22 23:59:54 +01:00
[bramble] Factor out database type placeholder replacement
to make it available in database schema migrations
This commit is contained in:
@@ -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:
|
||||||
|
* <li> _HASH
|
||||||
|
* <li> _SECRET
|
||||||
|
* <li> _BINARY
|
||||||
|
* <li> _COUNTER
|
||||||
|
* <li> _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;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -30,6 +30,8 @@ class H2Database extends JdbcDatabase {
|
|||||||
private static final String BINARY_TYPE = "BINARY";
|
private static final String BINARY_TYPE = "BINARY";
|
||||||
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
|
private static final String COUNTER_TYPE = "INT NOT NULL AUTO_INCREMENT";
|
||||||
private static final String STRING_TYPE = "VARCHAR";
|
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 DatabaseConfig config;
|
||||||
private final String url;
|
private final String url;
|
||||||
@@ -40,8 +42,7 @@ class H2Database extends JdbcDatabase {
|
|||||||
@Inject
|
@Inject
|
||||||
H2Database(DatabaseConfig config, MessageFactory messageFactory,
|
H2Database(DatabaseConfig config, MessageFactory messageFactory,
|
||||||
Clock clock) {
|
Clock clock) {
|
||||||
super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE,
|
super(dbTypes, messageFactory, clock);
|
||||||
messageFactory, clock);
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
File dir = config.getDatabaseDirectory();
|
File dir = config.getDatabaseDirectory();
|
||||||
String path = new File(dir, "db").getAbsolutePath();
|
String path = new File(dir, "db").getAbsolutePath();
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ class HyperSqlDatabase extends JdbcDatabase {
|
|||||||
private static final String COUNTER_TYPE =
|
private static final String COUNTER_TYPE =
|
||||||
"INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1)";
|
"INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY(START WITH 1)";
|
||||||
private static final String STRING_TYPE = "VARCHAR";
|
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 DatabaseConfig config;
|
||||||
private final String url;
|
private final String url;
|
||||||
@@ -40,8 +42,7 @@ class HyperSqlDatabase extends JdbcDatabase {
|
|||||||
@Inject
|
@Inject
|
||||||
HyperSqlDatabase(DatabaseConfig config, MessageFactory messageFactory,
|
HyperSqlDatabase(DatabaseConfig config, MessageFactory messageFactory,
|
||||||
Clock clock) {
|
Clock clock) {
|
||||||
super(HASH_TYPE, SECRET_TYPE, BINARY_TYPE, COUNTER_TYPE, STRING_TYPE,
|
super(dbTypes, messageFactory, clock);
|
||||||
messageFactory, clock);
|
|
||||||
this.config = config;
|
this.config = config;
|
||||||
File dir = config.getDatabaseDirectory();
|
File dir = config.getDatabaseDirectory();
|
||||||
String path = new File(dir, "db").getAbsolutePath();
|
String path = new File(dir, "db").getAbsolutePath();
|
||||||
@@ -78,7 +79,7 @@ class HyperSqlDatabase extends JdbcDatabase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getFreeSpace() throws DbException {
|
public long getFreeSpace() {
|
||||||
File dir = config.getDatabaseDirectory();
|
File dir = config.getDatabaseDirectory();
|
||||||
long maxSize = config.getMaxSize();
|
long maxSize = config.getMaxSize();
|
||||||
long free = dir.getFreeSpace();
|
long free = dir.getFreeSpace();
|
||||||
|
|||||||
@@ -312,10 +312,9 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
Logger.getLogger(JdbcDatabase.class.getName());
|
Logger.getLogger(JdbcDatabase.class.getName());
|
||||||
|
|
||||||
// Different database libraries use different names for certain types
|
// 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 MessageFactory messageFactory;
|
||||||
private final Clock clock;
|
private final Clock clock;
|
||||||
|
private final DatabaseTypes dbTypes;
|
||||||
|
|
||||||
// Locking: connectionsLock
|
// Locking: connectionsLock
|
||||||
private final LinkedList<Connection> connections = new LinkedList<>();
|
private final LinkedList<Connection> connections = new LinkedList<>();
|
||||||
@@ -330,14 +329,9 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
private final Lock connectionsLock = new ReentrantLock();
|
private final Lock connectionsLock = new ReentrantLock();
|
||||||
private final Condition connectionsChanged = connectionsLock.newCondition();
|
private final Condition connectionsChanged = connectionsLock.newCondition();
|
||||||
|
|
||||||
JdbcDatabase(String hashType, String secretType, String binaryType,
|
JdbcDatabase(DatabaseTypes databaseTypes, MessageFactory messageFactory,
|
||||||
String counterType, String stringType,
|
Clock clock) {
|
||||||
MessageFactory messageFactory, Clock clock) {
|
this.dbTypes = databaseTypes;
|
||||||
this.hashType = hashType;
|
|
||||||
this.secretType = secretType;
|
|
||||||
this.binaryType = binaryType;
|
|
||||||
this.counterType = counterType;
|
|
||||||
this.stringType = stringType;
|
|
||||||
this.messageFactory = messageFactory;
|
this.messageFactory = messageFactory;
|
||||||
this.clock = clock;
|
this.clock = clock;
|
||||||
}
|
}
|
||||||
@@ -432,7 +426,7 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
return Arrays.asList(
|
return Arrays.asList(
|
||||||
new Migration38_39(),
|
new Migration38_39(),
|
||||||
new Migration39_40(),
|
new Migration39_40(),
|
||||||
new Migration40_41()
|
new Migration40_41(dbTypes)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -492,20 +486,20 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
Statement s = null;
|
Statement s = null;
|
||||||
try {
|
try {
|
||||||
s = txn.createStatement();
|
s = txn.createStatement();
|
||||||
s.executeUpdate(insertTypeNames(CREATE_SETTINGS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_SETTINGS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_LOCAL_AUTHORS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_LOCAL_AUTHORS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_CONTACTS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_CONTACTS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_GROUPS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_GROUPS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_GROUP_METADATA));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_GROUP_METADATA));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_GROUP_VISIBILITIES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_GROUP_VISIBILITIES));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_MESSAGES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGES));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_MESSAGE_METADATA));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_METADATA));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_MESSAGE_DEPENDENCIES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_MESSAGE_DEPENDENCIES));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_OFFERS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_OFFERS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_STATUSES));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_STATUSES));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_TRANSPORTS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_TRANSPORTS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_OUTGOING_KEYS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_OUTGOING_KEYS));
|
||||||
s.executeUpdate(insertTypeNames(CREATE_INCOMING_KEYS));
|
s.executeUpdate(dbTypes.replaceTypes(CREATE_INCOMING_KEYS));
|
||||||
s.close();
|
s.close();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s);
|
tryToClose(s);
|
||||||
@@ -530,15 +524,6 @@ abstract class JdbcDatabase implements Database<Connection> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
@Override
|
||||||
public Connection startTransaction() throws DbException {
|
public Connection startTransaction() throws DbException {
|
||||||
Connection txn;
|
Connection txn;
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ class Migration40_41 implements Migration<Connection> {
|
|||||||
|
|
||||||
private static final Logger LOG = getLogger(Migration40_41.class.getName());
|
private static final Logger LOG = getLogger(Migration40_41.class.getName());
|
||||||
|
|
||||||
|
private final DatabaseTypes dbTypes;
|
||||||
|
|
||||||
|
public Migration40_41(DatabaseTypes databaseTypes) {
|
||||||
|
this.dbTypes = databaseTypes;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getStartVersion() {
|
public int getStartVersion() {
|
||||||
return 40;
|
return 40;
|
||||||
@@ -33,8 +39,7 @@ class Migration40_41 implements Migration<Connection> {
|
|||||||
try {
|
try {
|
||||||
s = txn.createStatement();
|
s = txn.createStatement();
|
||||||
s.execute("ALTER TABLE contacts"
|
s.execute("ALTER TABLE contacts"
|
||||||
// TODO how to insertTypeNames _STRING ?
|
+ dbTypes.replaceTypes(" ADD alias VARCHAR"));
|
||||||
+ " ADD alias VARCHAR");
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
tryToClose(s);
|
tryToClose(s);
|
||||||
throw new DbException(e);
|
throw new DbException(e);
|
||||||
|
|||||||
Reference in New Issue
Block a user