41 lines
1.1 KiB
Java
41 lines
1.1 KiB
Java
package de.vanitasvitae.fasel.db;
|
|
|
|
import java.sql.SQLException;
|
|
import java.util.List;
|
|
|
|
import com.j256.ormlite.support.ConnectionSource;
|
|
import com.j256.ormlite.table.TableUtils;
|
|
|
|
public abstract class AbstractDatabase {
|
|
|
|
private final ConnectionSource connectionSource;
|
|
|
|
protected AbstractDatabase() throws SQLException {
|
|
connectionSource = DatabaseService.getConnectionSource(getDatabaseName());
|
|
setupTables();
|
|
}
|
|
|
|
public ConnectionSource getConnectionSource() {
|
|
return connectionSource;
|
|
}
|
|
|
|
protected abstract List<Class<?>> getTables();
|
|
|
|
protected abstract String getDatabaseName();
|
|
|
|
private void setupTables() throws SQLException {
|
|
List<Class<?>> tableClasses = getTables();
|
|
if (tableClasses == null) {
|
|
throw new IllegalStateException("getTables cannot return null!");
|
|
}
|
|
|
|
for (Class<?> clazz : tableClasses) {
|
|
createTableIfNotExists(clazz);
|
|
}
|
|
}
|
|
|
|
private void createTableIfNotExists(Class<?> clazz) throws SQLException {
|
|
TableUtils.createTableIfNotExists(connectionSource, clazz);
|
|
}
|
|
}
|