diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/Main.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/Main.java index 5dd5a7e941..d4cd17955d 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/Main.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/Main.java @@ -9,6 +9,7 @@ public class Main { public static void main(String[] args) { new WLGitBridgeApplication(args).run(); + } } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/FileIndexStore.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/FileIndexStore.java index 5b35684f44..a5fcf30e62 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/FileIndexStore.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/filestore/store/FileIndexStore.java @@ -49,3 +49,11 @@ public class FileIndexStore implements FileNodeIndexer { } } + +/*Winston is really cool + and he's a cat +meow +miaow +meaaaoowww +=^.^= +*/ \ No newline at end of file diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java index 06a408f043..e841c778d2 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLDataModel.java @@ -9,11 +9,12 @@ import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionExc import uk.ac.ic.wlgitbridge.writelatex.api.request.getdoc.exception.InvalidProjectException; import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException; import uk.ac.ic.wlgitbridge.writelatex.filestore.store.WLFileStore; -import uk.ac.ic.wlgitbridge.writelatex.model.db.Database; +import uk.ac.ic.wlgitbridge.writelatex.model.db.SQLiteWLDatabase; import uk.ac.ic.wlgitbridge.writelatex.model.db.WLDatabase; import java.io.File; import java.io.IOException; +import java.sql.SQLException; import java.util.List; /** @@ -21,13 +22,19 @@ import java.util.List; */ public class WLDataModel implements CandidateSnapshotCallback { - private final WLDatabase db; + private WLDatabase db; private final WLProjectStore projectStore; private final WLFileStore fileStore; public WLDataModel(String rootGitDirectoryPath) { File rootGitDirectory = initRootGitDirectory(rootGitDirectoryPath); - db = new Database(rootGitDirectory); + try { + db = new SQLiteWLDatabase(rootGitDirectory); + } catch (SQLException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } projectStore = db.loadProjectStore(); fileStore = db.loadFileStore(); } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProjectStore.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProjectStore.java index 8532fae3cc..ec3e48b9d9 100644 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProjectStore.java +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/WLProjectStore.java @@ -1,12 +1,15 @@ package uk.ac.ic.wlgitbridge.writelatex.model; +import uk.ac.ic.wlgitbridge.writelatex.model.db.WLDatabase; +import uk.ac.ic.wlgitbridge.writelatex.model.db.WLDatabaseSource; + import java.util.HashMap; import java.util.Map; /** * Created by Winston on 17/11/14. */ -public class WLProjectStore { +public class WLProjectStore implements WLDatabaseSource { private final Map projects; @@ -25,4 +28,9 @@ public class WLProjectStore { return project; } + @Override + public void initFromDatabase(WLDatabase database) { + + } + } diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/Database.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/Database.java deleted file mode 100644 index f119db8402..0000000000 --- a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/Database.java +++ /dev/null @@ -1,54 +0,0 @@ -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 java.io.File; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.Statement; - -/** - * Created by Winston on 17/11/14. - */ -public class Database implements WLDatabase { - - private final File rootGitDirectory; - - public Database(File rootGitDirectory) { - this.rootGitDirectory = rootGitDirectory; - File databaseFile = new File(rootGitDirectory, "/.wlgb/wlgb.db"); - System.out.println("Loading data..."); - Connection c = null; - Statement stmt = null; - try { - Class.forName("org.sqlite.JDBC"); - c = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath()); - - stmt = c.createStatement(); - String sql = "CREATE TABLE IF NOT EXISTS COMPANY " + - "(ID INT PRIMARY KEY NOT NULL," + - " NAME TEXT NOT NULL, " + - " AGE INT NOT NULL, " + - " ADDRESS CHAR(50), " + - " SALARY REAL)"; - stmt.executeUpdate(sql); - stmt.close(); - c.close(); - } catch ( Exception e ) { - System.err.println( e.getClass().getName() + ": " + e.getMessage() ); - System.exit(0); - } - } - - @Override - public WLProjectStore loadProjectStore() { - return new WLProjectStore(); - } - - @Override - public WLFileStore loadFileStore() { - return new WLFileStore(rootGitDirectory); - } - -} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/SQLiteWLDatabase.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/SQLiteWLDatabase.java new file mode 100644 index 0000000000..6e699c7771 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/SQLiteWLDatabase.java @@ -0,0 +1,93 @@ +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 java.io.File; +import java.sql.*; + +/** + * Created by Winston on 17/11/14. + */ +public class SQLiteWLDatabase implements WLDatabase { + + private static final String[] CREATE_TABLE_STATEMENTS = { + "CREATE TABLE IF NOT EXISTS `projects` (\n" + + " `name` varchar(10) NOT NULL DEFAULT '',\n" + + " PRIMARY KEY (`name`)\n" + + ")", + "CREATE TABLE IF NOT EXISTS `snapshots` (\n" + + " `project_name` varchar(10) NOT NULL DEFAULT '',\n" + + " `version_id` int(11) NOT NULL,\n" + + " PRIMARY KEY (`project_name`,`version_id`),\n" + + " CONSTRAINT `snapshots_ibfk_1` FOREIGN KEY (`project_name`) REFERENCES `projects` (`name`) ON DELETE CASCADE ON UPDATE CASCADE\n" + + ")", + "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" + + " `is_blob` tinyint(1) NOT NULL,\n" + + " `blob` blob,\n" + + " `url` varchar(128) DEFAULT NULL,\n" + + " PRIMARY KEY (`project_name`,`file_name`),\n" + + " CONSTRAINT `file_node_table_ibfk_1` FOREIGN KEY (`project_name`) REFERENCES `projects` (`name`) ON DELETE CASCADE ON UPDATE CASCADE\n" + + ")", + "CREATE TABLE IF NOT EXISTS `url_index_store` (\n"+ + " `project_name` varchar(10) NOT NULL DEFAULT '',\n"+ + " `url` varchar(128) NOT NULL,\n"+ + " `blob` blob NOT NULL,\n"+ + " PRIMARY KEY (`project_name`,`url`),\n"+ + " CONSTRAINT `url_index_store_ibfk_1` FOREIGN KEY (`project_name`) REFERENCES `projects` (`name`) ON DELETE CASCADE ON UPDATE CASCADE\n"+ + ")" + }; + + private static final String addProject = + "INSERT INTO `projects` (`name`) VALUES (?);\n"; + + private final File rootGitDirectory; + private final Connection connection; + private PreparedStatement addProjectStatement; + + public SQLiteWLDatabase(File rootGitDirectory) throws SQLException, ClassNotFoundException { + this.rootGitDirectory = rootGitDirectory; + File databaseFile = new File(rootGitDirectory, "/.wlgb/wlgb.db"); + System.out.println("Loading data..."); + Class.forName("org.sqlite.JDBC"); + connection = DriverManager.getConnection("jdbc:sqlite:" + databaseFile.getAbsolutePath()); + createTables(); + prepareStatements(); + test(); + } + + private void createTables() throws SQLException { + for (String createTableStatement : CREATE_TABLE_STATEMENTS) { + PreparedStatement preparedStatement = connection.prepareStatement(createTableStatement); + preparedStatement.executeUpdate(); + } + } + + private void prepareStatements() throws SQLException { + addProjectStatement = connection.prepareStatement(addProject); + } + + public void addProject(String name) throws SQLException { + addProjectStatement.setString(1, name); + addProjectStatement.executeUpdate(); + addProjectStatement.clearParameters(); + } + + private void test() throws SQLException { + addProject("testproj12"); + + } + + @Override + public WLProjectStore loadProjectStore() { + return new WLProjectStore(); + } + + @Override + public WLFileStore loadFileStore() { + return new WLFileStore(rootGitDirectory); + } + +} diff --git a/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/WLDatabaseSource.java b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/WLDatabaseSource.java new file mode 100644 index 0000000000..0fdc824888 --- /dev/null +++ b/services/git-bridge/src/uk/ac/ic/wlgitbridge/writelatex/model/db/WLDatabaseSource.java @@ -0,0 +1,10 @@ +package uk.ac.ic.wlgitbridge.writelatex.model.db; + +/** + * Created by Winston on 19/11/14. + */ +public interface WLDatabaseSource { + + public void initFromDatabase(WLDatabase database); + +}