diff --git a/app/src/main/java/krusty/Database.java b/app/src/main/java/krusty/Database.java index 6a0dac1..26ef811 100644 --- a/app/src/main/java/krusty/Database.java +++ b/app/src/main/java/krusty/Database.java @@ -8,24 +8,28 @@ import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; -import java.io.BufferedReader; // Likely dependencies for general operations import java.io.IOException; -import java.io.FileReader; import java.sql.ResultSet; import java.util.StringJoiner; - +import java.util.stream.Collectors; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.stream.Stream; public class Database { // Here, we use an in-memory database. This string could be changed to // "jdbc:sqlite:.sqlite3" to use a file-based database instead. // Nore that ":memory:" is an **SQLite specific** magic string that tells the // underlying SQLite engine to store the database in memory. - private static final String jdbcString = "jdbc:sqlite::memory:"; + // private static final String jdbcString = "jdbc:sqlite::memory:"; + private static final String jdbcString = "jdbc:sqlite:krusty.db"; private Connection conn = null; public String getCustomers(Request req, Response res) { - String result = selectQuery("Customers", "customers", "name", "address"); + String result = selectQuery("customers", "customers", "customer_name", "customer_address"); + result = result.replaceAll("customer_name", "name"); + result = result.replaceAll("customer_address", "address"); return result; } @@ -34,7 +38,8 @@ public class Database { } public String getCookies(Request req, Response res) { - return "{\"cookies\":[]}"; + String result = selectQuery("recipes", "cookies", "recipe_name"); + return result; } public String getRecipes(Request req, Response res) { @@ -81,7 +86,7 @@ public class Database { } query.append(args.toString()); - query.append("\nFROM " + table + ";"); + query.append(" FROM " + table + ";"); /* Sanitization is for cowards */ @@ -98,27 +103,31 @@ public class Database { // build script ("build.gradle.kts", in this case). /** Reads an sql script into the database */ public void migrateScript(String filename) throws IOException, SQLException { - try (BufferedReader reader = new BufferedReader(new FileReader(filename))) { - StringBuilder scriptBuilder = new StringBuilder(); - String line; + try (Stream lines = Files.lines(Paths.get(filename))) { - // Read the script file line by line - while ((line = reader.readLine()) != null) { - scriptBuilder.append(line).append("\n"); - } - String script = scriptBuilder.toString().trim(); + // Combine into one big string, with all comments and empty lines removed. + String[] statements = lines.filter(line -> !line.startsWith("--") && !line.isBlank()) + .map(line -> line.replaceAll("--.*", "").replaceAll("\\s+", " ").trim()) + .collect(Collectors.joining("\n")).split(";"); - // Execute the script - try (Statement statement = conn.createStatement()) { - statement.execute(script); + for (String query : statements) { + try (Statement statement = conn.createStatement()) { + statement.execute(query); + statement.close(); + } catch (SQLException e) { + System.err.println("Error executing script: " + e.getMessage()); + throw e; + } } System.out.println(String.format("Executed script %s", filename)); + } catch (IOException e) { System.err.println("Error reading script file: " + e.getMessage()); throw e; } catch (SQLException e) { - System.err.println("Error executing script: " + e.getMessage()); + String prepend = String.format("Error executing script: %s", filename); + System.err.println(prepend + e.getMessage()); throw e; } } -} +} \ No newline at end of file