got rid of slug idea, as its functionality can be implemented without an additional db field

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
2024-12-29 00:21:48 +01:00
parent 2e1f1c9697
commit d797de60c1
8 changed files with 64 additions and 147 deletions
@@ -2,9 +2,7 @@
package de.srsoftware.cal.db;
import de.srsoftware.cal.api.Appointment;
import de.srsoftware.tools.Calc;
import de.srsoftware.tools.Result;
import de.srsoftware.tools.Strings;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;
@@ -50,11 +48,7 @@ public interface Database {
Result<Appointment> loadEvent(long id);
Result<Appointment> loadEvent(String slug);
Result<Appointment> loadEvent(String location, LocalDateTime start);
Result<List<String>> findTags(String infix);
public static String slug(String location, LocalDateTime start) {
return Calc.sha256(start + "@" + location).map(Strings::base64).orElse(null);
}
}
@@ -10,7 +10,6 @@ public class Fields {
public static final String KEYWORD = "keyword";
public static final String LOCATION = "location";
public static final String MIME = "mime";
public static final String SLUG = "slug";
public static final String START = "start";
public static final String TID = "tid";
public static final String TITLE = "title";
@@ -1,7 +1,6 @@
/* © SRSoftware 2024 */
package de.srsoftware.cal.db;
import static de.srsoftware.cal.db.Database.slug;
import static de.srsoftware.cal.db.Fields.*;
import static de.srsoftware.cal.db.Fields.ALL;
import static de.srsoftware.tools.Optionals.*;
@@ -27,14 +26,12 @@ import java.util.*;
public class MariaDB implements Database {
private static final System.Logger LOG = System.getLogger(MariaDB.class.getSimpleName());
private static final String ADD_SLUG = "ALTER TABLE appointments ADD slug VARCHAR(255) UNIQUE";
private static final String APPOINTMENTS = "appointments";
private static final String APPOINTMENT_TAGS = "appointment_tags";
private static final String APPOINTMENT_URLS = "appointment_urls";
private static final String URLS = "urls";
private static final String APPOINTMENT_ATTACHMENTS = "appointment_attachments";
private static final String TAGS = "tags";
private static final String SLUG = "slug";
private static Connection connection;
private MariaDB(Connection conn) throws SQLException {
@@ -53,47 +50,9 @@ public class MariaDB implements Database {
switch (version) {
case 0:
createTables();
case 1:
update1();
}
}
private void update1() throws SQLException {
LOG.log(INFO, "Updating db scheme from version 1 to 2…");
var list = new ArrayList<Appointment>();
connection.setAutoCommit(false);
LOG.log(DEBUG, "Adding slug column…");
connection.prepareStatement(ADD_SLUG).execute();
var slugMap = new HashMap<Long, String>();
LOG.log(DEBUG, "Reading existing appointments…");
var rs = select(ALL).from("appointments").exec(connection);
while (rs.next()) {
var id = rs.getLong(AID);
var location = nullable(nullIfEmpty(rs.getString("location")));
if (location.isEmpty()) continue;
var title = rs.getString("title");
LOG.log(TRACE, () -> "%s: %s".formatted(id, title));
var descr = rs.getString("description");
if (allEmpty(title, descr)) continue;
var start = nullable(rs.getTimestamp("start")).map(Timestamp::toLocalDateTime);
if (start.isEmpty()) continue;
var slug = slug(location.get(), start.get());
slugMap.put(id, slug);
}
rs.close();
LOG.log(DEBUG, "Creating slugs…");
var query = updateIgnore("appointments").set("slug").where(AID, equal(MARK)).prepare(connection);
for (var entry : slugMap.entrySet()) {
query.apply(entry.getValue(), entry.getKey());
}
LOG.log(DEBUG, "Writing new db version marker…");
update("config").set("value").where("keyname", equal("dbversion")).prepare(connection).apply(2);
connection.setAutoCommit(true);
}
private void createTables() {
throw new RuntimeException("%s.createTables() not implemented!");
}
@@ -102,8 +61,8 @@ public class MariaDB implements Database {
@Override
public Result<Appointment> add(Appointment appointment) {
try {
ResultSet keys = insertInto(APPOINTMENTS, TITLE, DESCRIPTION, START, END, LOCATION, COORDS, SLUG) //
.values(appointment.title(), appointment.description(), appointment.start(), appointment.end().orElse(null), appointment.location(), appointment.coords().orElse(null), appointment.slug())
ResultSet keys = insertInto(APPOINTMENTS, TITLE, DESCRIPTION, START, END, LOCATION, COORDS) //
.values(appointment.title(), appointment.description(), appointment.start(), appointment.end().orElse(null), appointment.location(), appointment.coords().orElse(null))
.execute(connection)
.getGeneratedKeys();
Appointment saved = null;
@@ -215,14 +174,14 @@ public class MariaDB implements Database {
}
@Override
public Result<Appointment> loadEvent(String slug) {
public Result<Appointment> loadEvent(String location, LocalDateTime start) {
try {
var rs = select(ALL).from(APPOINTMENTS).where(SLUG, equal(slug)).exec(connection);
Result<Appointment> result = rs.next() ? createAppointmentOf(rs).map(MariaDB::loadExtra) : Error.format("Failed to find appointment with slug %s", slug);
var rs = select(ALL).from(APPOINTMENTS).where(LOCATION, equal(location)).where(START, equal(Timestamp.valueOf(start))).exec(connection);
Result<Appointment> result = rs.next() ? createAppointmentOf(rs).map(MariaDB::loadExtra) : Error.format("Failed to find appointment starting %s @ %s".formatted(start, location));
rs.close();
return result;
} catch (SQLException e) {
return Error.of("Failed to load appointment with slug = %s".formatted(slug), e);
return Error.of("Failed to load appointment starting %s @ %s".formatted(start, location), e);
}
}
@@ -295,12 +254,10 @@ public class MariaDB implements Database {
var title = results.getString("title");
var description = results.getString("description");
if (allEmpty(title, description)) return Error.format("Title and Description of appointment %s are empty", id);
var start = results.getTimestamp("start").toLocalDateTime();
var end = nullable(results.getTimestamp("end")).map(Timestamp::toLocalDateTime).orElse(null);
var location = results.getString("location");
var slug = results.getString("slug");
if (slug == null) slug = slug(location, start);
var appointment = new BaseAppointment(id, title, description, start, end, location, slug);
var start = results.getTimestamp("start").toLocalDateTime();
var end = nullable(results.getTimestamp("end")).map(Timestamp::toLocalDateTime).orElse(null);
var location = results.getString("location");
var appointment = new BaseAppointment(id, title, description, start, end, location);
try {
var tags = nullIfEmpty(results.getString("tags"));
if (tags != null) appointment.tags(tags.split(","));