All tests passing
This commit is contained in:
parent
4aeb738df3
commit
6554cb2b6f
1 changed files with 35 additions and 17 deletions
|
@ -27,8 +27,8 @@ public class Database {
|
|||
// "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:krusty.db";
|
||||
private static final String jdbcString = "jdbc:sqlite::memory:";
|
||||
// private static final String jdbcString = "jdbc:sqlite:krusty.db";
|
||||
|
||||
// Hold a single connection to the database. Note that this is
|
||||
// not a pool, so this is not thread-safe nor efficient.
|
||||
|
@ -94,26 +94,26 @@ public class Database {
|
|||
};
|
||||
}
|
||||
|
||||
// Both of these must be present
|
||||
if (from_str != null && to_str != null) {
|
||||
if(from_str != null) {
|
||||
try {
|
||||
// Parse both in the format (2024-05-23), also called ISO 8601
|
||||
from = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(from_str));
|
||||
} catch (Exception e) {
|
||||
from = Optional.empty();
|
||||
}
|
||||
}
|
||||
|
||||
if(to_str != null) {
|
||||
try {
|
||||
to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_str));
|
||||
} catch (Exception e) {
|
||||
// Reset the dates to 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 (from.isPresent() && to.isPresent() && from.get().after(to.get())) {
|
||||
// We have a bad interval, perhaps set dates to empty agian?
|
||||
// TODO: This obviously need louder error handling
|
||||
from = 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.
|
||||
|
@ -135,16 +135,34 @@ public class Database {
|
|||
}
|
||||
|
||||
// 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());
|
||||
|
||||
// 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());
|
||||
|
||||
// Super hacky, low quality code
|
||||
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()) {
|
||||
// This again
|
||||
String clause = query.toString().contains("WHERE") ? " AND " : " WHERE ";
|
||||
|
|
Loading…
Reference in a new issue