Fixing migration method
This commit is contained in:
parent
0416a1d3da
commit
0b9c183a2d
1 changed files with 29 additions and 20 deletions
|
@ -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:<filename>.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<String> 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
|
||||
for (String query : statements) {
|
||||
try (Statement statement = conn.createStatement()) {
|
||||
statement.execute(script);
|
||||
}
|
||||
System.out.println(String.format("Executed script %s", filename));
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading script file: " + e.getMessage());
|
||||
throw e;
|
||||
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) {
|
||||
String prepend = String.format("Error executing script: %s", filename);
|
||||
System.err.println(prepend + e.getMessage());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue