|
|
@@ -1,15 +1,20 @@
|
|
|
package uk.ac.ic.wlgitbridge.writelatex.model.db;
|
|
|
|
|
|
-import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore;
|
|
|
-import uk.ac.ic.wlgitbridge.writelatex.model.WLProjectStore;
|
|
|
+import uk.ac.ic.wlgitbridge.writelatex.filestore.node.AttachmentNode;
|
|
|
+import uk.ac.ic.wlgitbridge.writelatex.filestore.node.BlobNode;
|
|
|
+import uk.ac.ic.wlgitbridge.writelatex.filestore.node.FileNode;
|
|
|
|
|
|
import java.io.File;
|
|
|
import java.sql.*;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.LinkedList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Created by Winston on 17/11/14.
|
|
|
*/
|
|
|
-public class SQLiteWLDatabase implements WLDatabase {
|
|
|
+public class SQLiteWLDatabase {
|
|
|
|
|
|
private static final String[] CREATE_TABLE_STATEMENTS = {
|
|
|
"CREATE TABLE IF NOT EXISTS `projects` (\n" +
|
|
|
@@ -25,6 +30,7 @@ public class SQLiteWLDatabase implements WLDatabase {
|
|
|
"CREATE TABLE IF NOT EXISTS `file_node_table` (\n" +
|
|
|
" `project_name` varchar(10) NOT NULL DEFAULT '',\n" +
|
|
|
" `file_name` varchar(255) NOT NULL DEFAULT '',\n" +
|
|
|
+ " `changed` tinyint(1) NOT NULL,\n" +
|
|
|
" `is_blob` tinyint(1) NOT NULL,\n" +
|
|
|
" `blob` blob,\n" +
|
|
|
" `url` varchar(128) DEFAULT NULL,\n" +
|
|
|
@@ -40,12 +46,39 @@ public class SQLiteWLDatabase implements WLDatabase {
|
|
|
")"
|
|
|
};
|
|
|
|
|
|
- private static final String addProject =
|
|
|
+ private static final String ADD_PROJECT =
|
|
|
"INSERT INTO `projects` (`name`) VALUES (?);\n";
|
|
|
+ private static final String ADD_SNAPSHOT =
|
|
|
+ "INSERT INTO `snapshots` (`project_name`, `version_id`) VALUES (?, ?);\n";
|
|
|
+ private static final String ADD_FILE_NODE_BLOB =
|
|
|
+ "INSERT INTO `file_node_table` (`project_name`, `file_name`, `changed`, `is_blob`, `blob`, `url`) VALUES (?, ?, ?, '1', ?, NULL);\n";
|
|
|
+ private static final String ADD_FILE_NODE_EXTERNAL =
|
|
|
+ "INSERT INTO `file_node_table` (`project_name`, `file_name`, `changed`, `is_blob`, `blob`, `url`) VALUES (?, ?, ?, '0', NULL, ?);\n";
|
|
|
+ private static final String ADD_URL_INDEX =
|
|
|
+ "INSERT INTO `url_index_store` (`project_name`, `url`, `blob`) VALUES (?, ?, ?);\n";
|
|
|
+
|
|
|
+ private static final String GET_PROJECT_NAMES =
|
|
|
+ "SELECT * FROM `projects`;\n";
|
|
|
+ private static final String GET_VERSION_IDS_FOR_PROJECT_NAME =
|
|
|
+ "SELECT `version_id` FROM `snapshots` WHERE `project_name` = ?";
|
|
|
+ private static final String GET_FILE_NODES_FOR_PROJECT_NAME =
|
|
|
+ "SELECT `file_name`, `changed`, `is_blob`, `blob`, `url` FROM `file_node_table` WHERE `project_name` = ?";
|
|
|
+ private static final String GET_URL_INDEXES_FOR_PROJECT_NAME =
|
|
|
+ "SELECT `url`, `blob` FROM `url_index_store` WHERE `project_name` = ?";
|
|
|
|
|
|
private final File rootGitDirectory;
|
|
|
private final Connection connection;
|
|
|
+
|
|
|
private PreparedStatement addProjectStatement;
|
|
|
+ private PreparedStatement addSnapshotStatement;
|
|
|
+ private PreparedStatement addFileNodeBlobStatement;
|
|
|
+ private PreparedStatement addFileNodeExternalStatement;
|
|
|
+ private PreparedStatement addURLIndexStatement;
|
|
|
+
|
|
|
+ private PreparedStatement getProjectNamesStatement;
|
|
|
+ private PreparedStatement getVersionIDsForProjectNameStatement;
|
|
|
+ private PreparedStatement getFileNodesForProjectNameStatement;
|
|
|
+ private PreparedStatement getURLIndexesForProjectNameStatement;
|
|
|
|
|
|
public SQLiteWLDatabase(File rootGitDirectory) throws SQLException, ClassNotFoundException {
|
|
|
this.rootGitDirectory = rootGitDirectory;
|
|
|
@@ -55,7 +88,6 @@ public class SQLiteWLDatabase implements WLDatabase {
|
|
|
connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath());
|
|
|
createTables();
|
|
|
prepareStatements();
|
|
|
- test();
|
|
|
}
|
|
|
|
|
|
private void createTables() throws SQLException {
|
|
|
@@ -66,28 +98,129 @@ public class SQLiteWLDatabase implements WLDatabase {
|
|
|
}
|
|
|
|
|
|
private void prepareStatements() throws SQLException {
|
|
|
- addProjectStatement = connection.prepareStatement(addProject);
|
|
|
+ addProjectStatement = connection.prepareStatement(ADD_PROJECT);
|
|
|
+ addSnapshotStatement = connection.prepareStatement(ADD_SNAPSHOT);
|
|
|
+ addFileNodeBlobStatement = connection.prepareStatement(ADD_FILE_NODE_BLOB);
|
|
|
+ addFileNodeExternalStatement = connection.prepareStatement(ADD_FILE_NODE_EXTERNAL);
|
|
|
+ addURLIndexStatement = connection.prepareStatement(ADD_URL_INDEX);
|
|
|
+
|
|
|
+ getProjectNamesStatement = connection.prepareStatement(GET_PROJECT_NAMES);
|
|
|
+ getVersionIDsForProjectNameStatement = connection.prepareStatement(GET_VERSION_IDS_FOR_PROJECT_NAME);
|
|
|
+ getFileNodesForProjectNameStatement = connection.prepareStatement(GET_FILE_NODES_FOR_PROJECT_NAME);
|
|
|
+ getURLIndexesForProjectNameStatement = connection.prepareStatement(GET_URL_INDEXES_FOR_PROJECT_NAME);
|
|
|
}
|
|
|
|
|
|
public void addProject(String name) throws SQLException {
|
|
|
+ addProjectStatement.clearParameters();
|
|
|
addProjectStatement.setString(1, name);
|
|
|
addProjectStatement.executeUpdate();
|
|
|
- addProjectStatement.clearParameters();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addSnapshot(String projectName, int versionID) throws SQLException {
|
|
|
+ addSnapshotStatement.clearParameters();
|
|
|
+ addSnapshotStatement.setString(1, projectName);
|
|
|
+ addSnapshotStatement.setInt(2, versionID);
|
|
|
+ addSnapshotStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addFileNodeBlob(String projectName, String fileName, int changed, byte[] blob) throws SQLException {
|
|
|
+ addFileNodeBlobStatement.clearParameters();
|
|
|
+ addFileNodeBlobStatement.setString(1, projectName);
|
|
|
+ addFileNodeBlobStatement.setString(2, fileName);
|
|
|
+ addFileNodeBlobStatement.setInt(3, changed);
|
|
|
+ addFileNodeBlobStatement.setBytes(4, blob);
|
|
|
+ addFileNodeBlobStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addFileNodeExternal(String projectName, String fileName, int changed, String url) throws SQLException {
|
|
|
+ addFileNodeExternalStatement.clearParameters();
|
|
|
+ addFileNodeExternalStatement.setString(1, projectName);
|
|
|
+ addFileNodeExternalStatement.setString(2, fileName);
|
|
|
+ addFileNodeExternalStatement.setInt(3, changed);
|
|
|
+ addFileNodeExternalStatement.setString(4, url);
|
|
|
+ addFileNodeExternalStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void addURLIndex(String projectName, String url, byte[] blob) throws SQLException {
|
|
|
+ addURLIndexStatement.clearParameters();
|
|
|
+ addURLIndexStatement.setString(1, projectName);
|
|
|
+ addURLIndexStatement.setString(2, url);
|
|
|
+ addURLIndexStatement.setBytes(3, blob);
|
|
|
+ addURLIndexStatement.executeUpdate();
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<String> getProjectNames() throws SQLException {
|
|
|
+ List<String> projectNames = new LinkedList<String>();
|
|
|
+ ResultSet results = getProjectNamesStatement.executeQuery();
|
|
|
+ while (results.next()) {
|
|
|
+ projectNames.add(results.getString("name"));
|
|
|
+ }
|
|
|
+ return projectNames;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<Integer> getVersionIDsForProjectName(String projectName) throws SQLException {
|
|
|
+ List<Integer> versionIDs = new LinkedList<Integer>();
|
|
|
+ getVersionIDsForProjectNameStatement.clearParameters();
|
|
|
+ getVersionIDsForProjectNameStatement.setString(1, projectName);
|
|
|
+ ResultSet results = getVersionIDsForProjectNameStatement.executeQuery();
|
|
|
+ while (results.next()) {
|
|
|
+ versionIDs.add(results.getInt("version_id"));
|
|
|
+ }
|
|
|
+ return versionIDs;
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<FileNode> getFileNodesForProjectName(String projectName) throws SQLException {
|
|
|
+ List<FileNode> fileNodes = new LinkedList<FileNode>();
|
|
|
+ getFileNodesForProjectNameStatement.clearParameters();
|
|
|
+ getFileNodesForProjectNameStatement.setString(1, projectName);
|
|
|
+ ResultSet results = getFileNodesForProjectNameStatement.executeQuery();
|
|
|
+ while (results.next()) {
|
|
|
+ boolean isBlob = intToBoolean(results.getInt("is_blob"));
|
|
|
+ FileNode fileNode;
|
|
|
+ String fileName = results.getString("file_name");
|
|
|
+ boolean changed = intToBoolean(results.getInt("changed"));
|
|
|
+ if (isBlob) {
|
|
|
+ fileNode = new BlobNode(fileName, changed, results.getBytes("blob"));
|
|
|
+ } else {
|
|
|
+ fileNode = new AttachmentNode(fileName, changed, results.getString("url"));
|
|
|
+ }
|
|
|
+ fileNodes.add(fileNode);
|
|
|
+ }
|
|
|
+ return fileNodes;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, FileNode> getURLIndexTableForProjectName(String projectName) throws SQLException {
|
|
|
+ Map<String, FileNode> urlIndexTable = new HashMap<String, FileNode>();
|
|
|
+ getURLIndexesForProjectNameStatement.clearParameters();
|
|
|
+ getURLIndexesForProjectNameStatement.setString(1, projectName);
|
|
|
+ ResultSet results = getURLIndexesForProjectNameStatement.executeQuery();
|
|
|
+ while (results.next()) {
|
|
|
+ String url = results.getString("url");
|
|
|
+ byte[] blob = results.getBytes("blob");
|
|
|
+ urlIndexTable.put(url, new AttachmentNode(url, blob));
|
|
|
+ }
|
|
|
+ return urlIndexTable;
|
|
|
}
|
|
|
|
|
|
private void test() throws SQLException {
|
|
|
addProject("testproj12");
|
|
|
-
|
|
|
+ addSnapshot("testproj12", 0);
|
|
|
+ addSnapshot("testproj12", 1);
|
|
|
+ addFileNodeBlob("testproj12", "filename.tex", 1, "hello".getBytes());
|
|
|
+ addFileNodeExternal("testproj12", "urlname.jpg", 1, "http://someurl.com/urlname.jpg");
|
|
|
+ addURLIndex("testproj12", "http://someurl.com/urlname.jpg", "thebytes".getBytes());
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public WLProjectStore loadProjectStore() {
|
|
|
- return new WLProjectStore();
|
|
|
+ private static int booleanToInt(boolean b) {
|
|
|
+ if (b) {
|
|
|
+ return 1;
|
|
|
+ } else {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public WLFileStore loadFileStore() {
|
|
|
- return new WLFileStore(rootGitDirectory);
|
|
|
+ private static boolean intToBoolean(int i) {
|
|
|
+ return i != 0;
|
|
|
}
|
|
|
|
|
|
}
|