Basic sqlite database being loaded.

This commit is contained in:
Winston Li 2014-11-17 13:49:19 +00:00
parent 2a02ed466a
commit 5d1c9d436a
4 changed files with 54 additions and 9 deletions

View file

@ -31,7 +31,7 @@ public class GitDirectoryContents implements WritableRepositoryContents {
@Override
public void write() throws IOException, FailedConnectionException {
WLFileStore.deleteInDirectoryApartFrom(gitDirectory, ".git");
WLFileStore.deleteInDirectoryApartFrom(gitDirectory, ".git", ".wlgb");
for (FileNode fileNode : fileNodes) {
fileNode.writeToDisk(gitDirectory);
}

View file

@ -23,11 +23,10 @@ public class WLFileStore {
private final File rootGitDirectory;
private final File attDirectory;
public WLFileStore(String rootGitDirectoryPath) {
public WLFileStore(File rootGitDirectory) {
fileStore = new HashMap<String, WLDirectoryNode>();
rootGitDirectory = new File(rootGitDirectoryPath);
rootGitDirectory.mkdirs();
deleteInDirectory(rootGitDirectory);
this.rootGitDirectory = rootGitDirectory;
deleteInDirectoryApartFrom(rootGitDirectory, ".wlgb");
attDirectory = new File(rootGitDirectory, ".wlgb/atts");
attDirectory.mkdirs();
}

View file

@ -1,16 +1,17 @@
package uk.ac.ic.wlgitbridge.writelatex.model;
import org.sqlite.JDBC;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshot;
import uk.ac.ic.wlgitbridge.bridge.CandidateSnapshotCallback;
import uk.ac.ic.wlgitbridge.bridge.RawDirectoryContents;
import uk.ac.ic.wlgitbridge.bridge.WritableRepositoryContents;
import uk.ac.ic.wlgitbridge.writelatex.api.request.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.writelatex.WLDirectoryNodeSnapshot;
import uk.ac.ic.wlgitbridge.writelatex.api.request.exception.FailedConnectionException;
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 java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
@ -21,13 +22,15 @@ import java.util.Map;
*/
public class WLDataModel implements CandidateSnapshotCallback {
private final JDBC jbdc = null;
private final Database db;
private final Map<String, WLProject> projects;
private final WLFileStore fileStore;
public WLDataModel(String rootGitDirectoryPath) {
File rootGitDirectory = initRootGitDirectory(rootGitDirectoryPath);
db = new Database(rootGitDirectory);
projects = new HashMap<String, WLProject>();
fileStore = new WLFileStore(rootGitDirectoryPath);
fileStore = new WLFileStore(rootGitDirectory);
}
public List<WritableRepositoryContents> updateProjectWithName(String name) throws FailedConnectionException, InvalidProjectException {
@ -59,4 +62,10 @@ public class WLDataModel implements CandidateSnapshotCallback {
fileStore.approveCandidateSnapshot(candidateSnapshot);
}
private File initRootGitDirectory(String rootGitDirectoryPath) {
File rootGitDirectory = new File(rootGitDirectoryPath);
rootGitDirectory.mkdirs();
return rootGitDirectory;
}
}

View file

@ -0,0 +1,37 @@
package uk.ac.ic.wlgitbridge.writelatex.model.db;
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 {
public Database(File rootGitDirectory) {
System.out.println("Loading data...");
Connection c = null;
Statement stmt = null;
try {
Class.forName("org.sqlite.JDBC");
c = DriverManager.getConnection("jdbc:sqlite:" + rootGitDirectory.getAbsolutePath() + "/.wlgb/wlgb.db");
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);
}
}
}