All tests passing

This commit is contained in:
Imbus 2024-05-05 10:52:13 +02:00
parent 4aeb738df3
commit 6554cb2b6f

View file

@ -27,8 +27,8 @@ public class Database {
// "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 static final String jdbcString = "jdbc:sqlite:krusty.db";
// Hold a single connection to the database. Note that this is // Hold a single connection to the database. Note that this is
// not a pool, so this is not thread-safe nor efficient. // not a pool, so this is not thread-safe nor efficient.
@ -94,28 +94,28 @@ public class Database {
}; };
} }
// Both of these must be present if(from_str != null) {
if (from_str != null && to_str != null) {
try { try {
// Parse both in the format (2024-05-23), also called ISO 8601
from = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(from_str)); from = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(from_str));
to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
} catch (Exception e) { } catch (Exception e) {
// Reset the dates to empty
from = Optional.empty(); from = Optional.empty();
to = Optional.empty(); }
// We have a bad date, maybe log this somewhere
} }
// Check so that the dates are in the correct order if(to_str != null) {
if (from.isPresent() && to.isPresent() && from.get().after(to.get())) { try {
// We have a bad interval, perhaps set dates to empty agian? to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
// TODO: This obviously need louder error handling } catch (Exception e) {
from = Optional.empty();
to = Optional.empty(); to = Optional.empty();
} }
} }
// If the interval is negative, reset the dates
if(from.isPresent() && to.isPresent() && from.get().after(to.get())) {
from = Optional.empty();
to = Optional.empty();
}
// This type of code is unreadable, error prone and hard to maintain. // This type of code is unreadable, error prone and hard to maintain.
// The fact that im responsible for this code makes my soul hurt. // The fact that im responsible for this code makes my soul hurt.
// This part almost made me write a simple query factory to handle this. // This part almost made me write a simple query factory to handle this.
@ -135,16 +135,34 @@ public class Database {
} }
// If both from and to are present // If both from and to are present
if (from.isPresent() && to.isPresent()) { if (from.isPresent()) {
String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get()); String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
// Super hacky, low quality code
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause + "production_date >= '" + query_from + "'");
}
if(to.isPresent()) {
String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get()); String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
// Super hacky, low quality code // Super hacky, low quality code
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE "; String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
query.append(clause + "production_date BETWEEN '" + query_from + "' AND '" + query_to + "'"); query.append(clause + "production_date <= '" + query_to + "'");
} }
// if (from.isPresent() && to.isPresent()) {
// String query_from = new SimpleDateFormat("yyyy-MM-dd").format(from.get());
// String query_to = new SimpleDateFormat("yyyy-MM-dd").format(to.get());
// // Super hacky, low quality code
// String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
// query.append(clause + "production_date BETWEEN '" + query_from + "' AND '" + query_to + "'");
// }
if(blocked.isPresent()) { if(blocked.isPresent()) {
// This again // This again
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE "; String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";