Compare commits
No commits in common. "8a88e8b45aa6d2cc6b42e2e395afb21c9bf9aee7" and "a3b9a1bccb6f99ce11ca239c917ebba2a4c7119c" have entirely different histories.
8a88e8b45a
...
a3b9a1bccb
4 changed files with 22 additions and 72 deletions
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -1,3 +0,0 @@
|
||||||
[submodule "app/CrustyCookies"]
|
|
||||||
path = app/CrustyCookies
|
|
||||||
url = https://git.silversoft.se/Imbus/CrustyCookies.git
|
|
|
@ -1 +0,0 @@
|
||||||
Subproject commit be904a7ab3925f7bbf3b54126e25480476ae49e8
|
|
|
@ -3,27 +3,32 @@ package krusty;
|
||||||
import spark.Request;
|
import spark.Request;
|
||||||
import spark.Response;
|
import spark.Response;
|
||||||
|
|
||||||
// Likely dependencies for db operations
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.ResultSet;
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.sql.Statement;
|
|
||||||
|
|
||||||
// Likely dependencies for general operations
|
|
||||||
import java.io.File;
|
|
||||||
import java.util.StringJoiner;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.FileNotFoundException;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
public class Database {
|
public class Database {
|
||||||
// Here, we use an in-memory database. This string could be changed to
|
private static final String jdbcString = "jdbc:sqlite:krusty.sqlite3";
|
||||||
// "jdbc:sqlite:<filename>.sqlite3" to use a file-based database instead.
|
|
||||||
// Nore that ":memory:" is an **SQLite specific** magic string that tells the
|
public void connect() {
|
||||||
// underlying SQLite engine to store the database in memory.
|
Connection conn = null;
|
||||||
private static final String jdbcString = "jdbc:sqlite::memory:";
|
|
||||||
private Connection conn = null;
|
try {
|
||||||
|
conn = DriverManager.getConnection(jdbcString);
|
||||||
|
System.out.println("Connection to SQLite has been established.");
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (conn != null) {
|
||||||
|
conn.close();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Implement and change output in all methods below!
|
||||||
|
|
||||||
public String getCustomers(Request req, Response res) {
|
public String getCustomers(Request req, Response res) {
|
||||||
return "{}";
|
return "{}";
|
||||||
|
@ -52,52 +57,4 @@ public class Database {
|
||||||
public String createPallet(Request req, Response res) {
|
public String createPallet(Request req, Response res) {
|
||||||
return "{}";
|
return "{}";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Connects to the database using the configured jdbcString. */
|
|
||||||
public void connect() {
|
|
||||||
try {
|
|
||||||
conn = DriverManager.getConnection(jdbcString);
|
|
||||||
} catch (Exception e) {
|
|
||||||
System.out.println(e.getMessage());
|
|
||||||
} finally {
|
|
||||||
System.out.println("Connection to SQLite has been established.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// The script location is relative to the gradle
|
|
||||||
// build script ("build.gradle.kts", in this case).
|
|
||||||
/** Reads an sql script into the database */
|
|
||||||
public void migrateScript(String filename) {
|
|
||||||
// Open the file
|
|
||||||
StringBuilder script = new StringBuilder();
|
|
||||||
try {
|
|
||||||
File myObj = new File(filename);
|
|
||||||
Scanner myReader = new Scanner(myObj);
|
|
||||||
|
|
||||||
while (myReader.hasNextLine()) {
|
|
||||||
String data = myReader.nextLine();
|
|
||||||
script.append(data);
|
|
||||||
}
|
|
||||||
myReader.close();
|
|
||||||
} catch (FileNotFoundException e) {
|
|
||||||
System.out.println("Migration script not found...");
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
System.out.println("Migration script read successfully...");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute the script
|
|
||||||
try {
|
|
||||||
conn.setAutoCommit(false);
|
|
||||||
Statement stmt = conn.createStatement();
|
|
||||||
stmt.execute(script.toString());
|
|
||||||
conn.commit();
|
|
||||||
conn.setAutoCommit(true);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
System.out.println("Error executing migration script...");
|
|
||||||
e.printStackTrace();
|
|
||||||
} finally {
|
|
||||||
System.out.println("Migration script executed successfully...");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,9 +18,6 @@ public class ServerMain {
|
||||||
db = new Database();
|
db = new Database();
|
||||||
db.connect();
|
db.connect();
|
||||||
|
|
||||||
// Here, we can migrate an arbitrary number of SQL scripts.
|
|
||||||
db.migrateScript("CrustyCookies/Migrations/0010_users.sql");
|
|
||||||
|
|
||||||
port(PORT);
|
port(PORT);
|
||||||
|
|
||||||
enableCORS();
|
enableCORS();
|
||||||
|
|
Loading…
Reference in a new issue