|
@@ -1,6 +1,7 @@
|
|
|
-package uk.ac.ic.wlgitbridge.data.model.db.sql;
|
|
|
|
|
|
|
+package uk.ac.ic.wlgitbridge.bridge.db.sqlite;
|
|
|
|
|
|
|
|
-import com.google.api.client.repackaged.com.google.common.base.Preconditions;
|
|
|
|
|
|
|
+import uk.ac.ic.wlgitbridge.bridge.db.DBInitException;
|
|
|
|
|
+import uk.ac.ic.wlgitbridge.bridge.db.DBStore;
|
|
|
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetLatestVersionForProjectSQLQuery;
|
|
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetLatestVersionForProjectSQLQuery;
|
|
|
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetPathForURLInProjectSQLQuery;
|
|
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetPathForURLInProjectSQLQuery;
|
|
|
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetProjectNamesSQLQuery;
|
|
import uk.ac.ic.wlgitbridge.data.model.db.sql.query.GetProjectNamesSQLQuery;
|
|
@@ -14,93 +15,123 @@ import uk.ac.ic.wlgitbridge.data.model.db.sql.update.insert.SetProjectSQLUpdate;
|
|
|
import java.io.File;
|
|
import java.io.File;
|
|
|
import java.sql.*;
|
|
import java.sql.*;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
|
|
+import java.util.stream.Stream;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* Created by Winston on 17/11/14.
|
|
* Created by Winston on 17/11/14.
|
|
|
*/
|
|
*/
|
|
|
-public class SQLiteWLDatabase {
|
|
|
|
|
|
|
+public class SqliteDBStore implements DBStore {
|
|
|
|
|
|
|
|
private final Connection connection;
|
|
private final Connection connection;
|
|
|
|
|
|
|
|
- public SQLiteWLDatabase(
|
|
|
|
|
|
|
+ public SqliteDBStore(
|
|
|
File dbFile
|
|
File dbFile
|
|
|
- ) throws SQLException, ClassNotFoundException {
|
|
|
|
|
|
|
+ ) {
|
|
|
File parentDir = dbFile.getParentFile();
|
|
File parentDir = dbFile.getParentFile();
|
|
|
- Preconditions.checkState(
|
|
|
|
|
- parentDir.exists() || parentDir.mkdirs(),
|
|
|
|
|
- parentDir.getAbsolutePath() + " directory didn't exist, " +
|
|
|
|
|
- "and unable to create. Check your permissions."
|
|
|
|
|
- );
|
|
|
|
|
- Class.forName("org.sqlite.JDBC");
|
|
|
|
|
- connection = DriverManager.getConnection(
|
|
|
|
|
- "jdbc:sqlite:" + dbFile.getAbsolutePath()
|
|
|
|
|
- );
|
|
|
|
|
|
|
+ if (!parentDir.exists() && !parentDir.mkdirs()) {
|
|
|
|
|
+ throw new DBInitException(
|
|
|
|
|
+ parentDir.getAbsolutePath() + " directory didn't exist, " +
|
|
|
|
|
+ "and unable to create. Check your permissions."
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ Class.forName("org.sqlite.JDBC");
|
|
|
|
|
+ } catch (ClassNotFoundException e) {
|
|
|
|
|
+ throw new DBInitException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ connection = DriverManager.getConnection(
|
|
|
|
|
+ "jdbc:sqlite:" + dbFile.getAbsolutePath()
|
|
|
|
|
+ );
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new DBInitException("Unable to connect to DB", e);
|
|
|
|
|
+ }
|
|
|
createTables();
|
|
createTables();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void setVersionIDForProject(
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<String> getProjectNames() {
|
|
|
|
|
+ return query(new GetProjectNamesSQLQuery());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void setLatestVersionForProject(
|
|
|
String projectName,
|
|
String projectName,
|
|
|
int versionID
|
|
int versionID
|
|
|
- ) throws SQLException {
|
|
|
|
|
|
|
+ ) {
|
|
|
update(new SetProjectSQLUpdate(projectName, versionID));
|
|
update(new SetProjectSQLUpdate(projectName, versionID));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public void addURLIndex(
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public int getLatestVersionForProject(
|
|
|
|
|
+ String projectName
|
|
|
|
|
+ ) {
|
|
|
|
|
+ return query(new GetLatestVersionForProjectSQLQuery(projectName));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void addURLIndexForProject(
|
|
|
String projectName,
|
|
String projectName,
|
|
|
String url,
|
|
String url,
|
|
|
String path
|
|
String path
|
|
|
- ) throws SQLException {
|
|
|
|
|
|
|
+ ) {
|
|
|
update(new AddURLIndexSQLUpdate(projectName, url, path));
|
|
update(new AddURLIndexSQLUpdate(projectName, url, path));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
public void deleteFilesForProject(
|
|
public void deleteFilesForProject(
|
|
|
String projectName,
|
|
String projectName,
|
|
|
String... paths
|
|
String... paths
|
|
|
- ) throws SQLException {
|
|
|
|
|
|
|
+ ) {
|
|
|
update(new DeleteFilesForProjectSQLUpdate(projectName, paths));
|
|
update(new DeleteFilesForProjectSQLUpdate(projectName, paths));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public int getVersionIDForProjectName(
|
|
|
|
|
- String projectName
|
|
|
|
|
- ) throws SQLException {
|
|
|
|
|
- return query(new GetLatestVersionForProjectSQLQuery(projectName));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
|
|
+ @Override
|
|
|
public String getPathForURLInProject(
|
|
public String getPathForURLInProject(
|
|
|
String projectName,
|
|
String projectName,
|
|
|
String url
|
|
String url
|
|
|
- ) throws SQLException {
|
|
|
|
|
|
|
+ ) {
|
|
|
return query(new GetPathForURLInProjectSQLQuery(projectName, url));
|
|
return query(new GetPathForURLInProjectSQLQuery(projectName, url));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public List<String> getProjectNames() throws SQLException {
|
|
|
|
|
- return query(new GetProjectNamesSQLQuery());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public String getOldestUnswappedProject() throws SQLException {
|
|
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public String getOldestUnswappedProject() {
|
|
|
throw new UnsupportedOperationException();
|
|
throw new UnsupportedOperationException();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ @Override
|
|
|
public void setLastAccessedTime(
|
|
public void setLastAccessedTime(
|
|
|
String projectName,
|
|
String projectName,
|
|
|
Timestamp time
|
|
Timestamp time
|
|
|
- ) throws SQLException {
|
|
|
|
|
|
|
+ ) {
|
|
|
throw new UnsupportedOperationException();
|
|
throw new UnsupportedOperationException();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void createTables() throws SQLException {
|
|
|
|
|
- final SQLUpdate[] createTableUpdates = {
|
|
|
|
|
|
|
+ private void createTables() {
|
|
|
|
|
+ Stream.of(
|
|
|
new CreateProjectsTableSQLUpdate(),
|
|
new CreateProjectsTableSQLUpdate(),
|
|
|
new CreateURLIndexStoreSQLUpdate(),
|
|
new CreateURLIndexStoreSQLUpdate(),
|
|
|
new CreateIndexURLIndexStore()
|
|
new CreateIndexURLIndexStore()
|
|
|
- };
|
|
|
|
|
|
|
+ ).forEach(this::update);
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- for (SQLUpdate update : createTableUpdates) {
|
|
|
|
|
- update(update);
|
|
|
|
|
|
|
+ private void update(SQLUpdate update) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ doUpdate(update);
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private <T> T query(SQLQuery<T> query) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ return doQuery(query);
|
|
|
|
|
+ } catch (SQLException e) {
|
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private void update(SQLUpdate update) throws SQLException {
|
|
|
|
|
|
|
+ private void doUpdate(SQLUpdate update) throws SQLException {
|
|
|
PreparedStatement statement = null;
|
|
PreparedStatement statement = null;
|
|
|
try {
|
|
try {
|
|
|
statement = connection.prepareStatement(update.getSQL());
|
|
statement = connection.prepareStatement(update.getSQL());
|
|
@@ -113,7 +144,7 @@ public class SQLiteWLDatabase {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private <T> T query(SQLQuery<T> query) throws SQLException {
|
|
|
|
|
|
|
+ private <T> T doQuery(SQLQuery<T> query) throws SQLException {
|
|
|
PreparedStatement statement = null;
|
|
PreparedStatement statement = null;
|
|
|
ResultSet results = null;
|
|
ResultSet results = null;
|
|
|
try {
|
|
try {
|