diff --git a/.gitignore b/.gitignore index 8dcca34..26e3528 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build/ +instance/ *.class +*.db diff --git a/Main.java b/Main.java index 5a5f303..789fa8c 100644 --- a/Main.java +++ b/Main.java @@ -1,5 +1,11 @@ +import java.sql.Connection; +import java.sql.SQLException; + +import src.db.Database; + public class Main { - public static void main(String[] args) { - System.out.println("Hello, World!"); + public static void main(String[] args) throws SQLException { + Connection conn = Database.getConnection(); + Database.close(); } } diff --git a/extern/sqlite-jdbc-3.50.3.0.jar b/extern/sqlite-jdbc-3.50.3.0.jar new file mode 100644 index 0000000..f8ebc14 Binary files /dev/null and b/extern/sqlite-jdbc-3.50.3.0.jar differ diff --git a/run.sh b/run.sh index b7405dc..50f7c4a 100755 --- a/run.sh +++ b/run.sh @@ -8,9 +8,15 @@ if ! command -v java >/dev/null 2>&1; then exit 1 fi -if ! javac -d build Main.java; then - echo "Error: javac failed to compile Main.java." +find src -name "*.java" > sources.txt +if ! javac -d build -cp "./extern/sqlite-jdbc-3.50.3.0.jar" @sources.txt Main.java; then + echo "Error: javac failed to compile sources." exit 1 fi +rm sources.txt -java -cp build Main +if [ ! -d "instance" ]; then + mkdir instance +fi + +java -cp "build:./extern/sqlite-jdbc-3.50.3.0.jar" Main diff --git a/src/db/Database.java b/src/db/Database.java new file mode 100644 index 0000000..92495ce --- /dev/null +++ b/src/db/Database.java @@ -0,0 +1,29 @@ +package src.db; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class Database { + private static Connection conn; + + public static Connection getConnection() throws SQLException { + if (conn == null || conn.isClosed()) { + String url = "jdbc:sqlite:instance/test.db"; + conn = DriverManager.getConnection(url); + Migration.run(conn); + } + + return conn; + } + + public static void close() { + try { + if (conn != null && !conn.isClosed()) { + conn.close(); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } +} diff --git a/src/db/Migration.java b/src/db/Migration.java new file mode 100644 index 0000000..fd2ee7f --- /dev/null +++ b/src/db/Migration.java @@ -0,0 +1,59 @@ +package src.db; + +import java.sql.Connection; +import java.sql.Statement; + +public class Migration { + public static void run(Connection conn) { + try (Statement stmt = conn.createStatement()) { + stmt.execute(""" + CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + email TEXT NOT NULL UNIQUE, + password_hash TEXT NOT NULL + ) + """); + + stmt.execute(""" + CREATE TABLE IF NOT EXISTS accounts ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + owner_id INTEGER NOT NULL, + type TEXT CHECK(type IN ('CHECKING','SAVINGS','CREDIT')) NOT NULL, + account_number TEXT NOT NULL UNIQUE, + bank_code TEXT NOT NULL, + balance REAL NOT NULL DEFAULT 0, + FOREIGN KEY(owner_id) REFERENCES users(id) + ) + """); + + stmt.execute(""" + CREATE TABLE IF NOT EXISTS giro_accounts ( + id INTEGER PRIMARY KEY, + overdraft_limit REAL DEFAULT 0, + FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE + ) + """); + + stmt.execute(""" + CREATE TABLE IF NOT EXISTS spar_accounts ( + id INTEGER PRIMARY KEY, + interest_rate REAL DEFAULT 0, + FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE + ) + """); + + stmt.execute(""" + CREATE TABLE IF NOT EXISTS kredit_accounts ( + id INTEGER PRIMARY KEY, + credit_limit REAL DEFAULT 0, + repayment_plan TEXT, + FOREIGN KEY(id) REFERENCES accounts(id) ON DELETE CASCADE + ) + """); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/src/models/Model.java b/src/models/Model.java new file mode 100644 index 0000000..e99ecab --- /dev/null +++ b/src/models/Model.java @@ -0,0 +1,16 @@ +package src.models; + +import java.sql.Connection; +import src.db.Database; + +public abstract class Model { + protected Connection conn; + + public Model() { + try { + this.conn = Database.getConnection(); + } catch (java.sql.SQLException e) { + throw new RuntimeException("Failed to get DB connection", e); + } + } +}