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.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.io.BufferedReader;
|
|
||||||
// Likely dependencies for general operations
|
// Likely dependencies for general operations
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.FileReader;
|
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.util.StringJoiner;
|
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 {
|
public class Database {
|
||||||
// Here, we use an in-memory database. This string could be changed to
|
// Here, we use an in-memory database. This string could be changed to
|
||||||
// "jdbc:sqlite:<filename>.sqlite3" to use a file-based database instead.
|
// "jdbc:sqlite:<filename>.sqlite3" to use a file-based database instead.
|
||||||
// Nore that ":memory:" is an **SQLite specific** magic string that tells the
|
// Nore that ":memory:" is an **SQLite specific** magic string that tells the
|
||||||
// underlying SQLite engine to store the database in memory.
|
// 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;
|
private Connection conn = null;
|
||||||
|
|
||||||
public String getCustomers(Request req, Response res) {
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +38,8 @@ public class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCookies(Request req, Response res) {
|
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) {
|
public String getRecipes(Request req, Response res) {
|
||||||
|
@ -81,7 +86,7 @@ public class Database {
|
||||||
}
|
}
|
||||||
|
|
||||||
query.append(args.toString());
|
query.append(args.toString());
|
||||||
query.append("\nFROM " + table + ";");
|
query.append(" FROM " + table + ";");
|
||||||
|
|
||||||
/* Sanitization is for cowards */
|
/* Sanitization is for cowards */
|
||||||
|
|
||||||
|
@ -98,26 +103,30 @@ public class Database {
|
||||||
// build script ("build.gradle.kts", in this case).
|
// build script ("build.gradle.kts", in this case).
|
||||||
/** Reads an sql script into the database */
|
/** Reads an sql script into the database */
|
||||||
public void migrateScript(String filename) throws IOException, SQLException {
|
public void migrateScript(String filename) throws IOException, SQLException {
|
||||||
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
|
try (Stream<String> lines = Files.lines(Paths.get(filename))) {
|
||||||
StringBuilder scriptBuilder = new StringBuilder();
|
|
||||||
String line;
|
|
||||||
|
|
||||||
// Read the script file line by line
|
// Combine into one big string, with all comments and empty lines removed.
|
||||||
while ((line = reader.readLine()) != null) {
|
String[] statements = lines.filter(line -> !line.startsWith("--") && !line.isBlank())
|
||||||
scriptBuilder.append(line).append("\n");
|
.map(line -> line.replaceAll("--.*", "").replaceAll("\\s+", " ").trim())
|
||||||
}
|
.collect(Collectors.joining("\n")).split(";");
|
||||||
String script = scriptBuilder.toString().trim();
|
|
||||||
|
|
||||||
// Execute the script
|
for (String query : statements) {
|
||||||
try (Statement statement = conn.createStatement()) {
|
try (Statement statement = conn.createStatement()) {
|
||||||
statement.execute(script);
|
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));
|
System.out.println(String.format("Executed script %s", filename));
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.err.println("Error reading script file: " + e.getMessage());
|
System.err.println("Error reading script file: " + e.getMessage());
|
||||||
throw e;
|
throw e;
|
||||||
} catch (SQLException 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;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue