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.
|
// "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,26 +94,26 @@ 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));
|
||||||
|
} catch (Exception e) {
|
||||||
|
from = Optional.empty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(to_str != null) {
|
||||||
|
try {
|
||||||
to = Optional.of(new SimpleDateFormat("yyyy-MM-dd").parse(to_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();
|
|
||||||
to = 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 the interval is negative, reset the dates
|
||||||
if (from.isPresent() && to.isPresent() && from.get().after(to.get())) {
|
if(from.isPresent() && to.isPresent() && from.get().after(to.get())) {
|
||||||
// We have a bad interval, perhaps set dates to empty agian?
|
from = Optional.empty();
|
||||||
// TODO: This obviously need louder error handling
|
to = Optional.empty();
|
||||||
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.
|
||||||
|
@ -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 ";
|
||||||
|
|
Loading…
Reference in a new issue