|
|
|
@ -2,7 +2,8 @@
@@ -2,7 +2,8 @@
|
|
|
|
|
package de.srsoftware.cal.db; |
|
|
|
|
|
|
|
|
|
import static de.srsoftware.cal.db.Database.slug; |
|
|
|
|
import static de.srsoftware.tools.NotImplemented.notImplemented; |
|
|
|
|
import static de.srsoftware.cal.db.Fields.*; |
|
|
|
|
import static de.srsoftware.cal.db.Fields.ALL; |
|
|
|
|
import static de.srsoftware.tools.Optionals.*; |
|
|
|
|
import static de.srsoftware.tools.Result.transform; |
|
|
|
|
import static de.srsoftware.tools.jdbc.Condition.*; |
|
|
|
@ -19,6 +20,7 @@ import de.srsoftware.tools.Result;
@@ -19,6 +20,7 @@ import de.srsoftware.tools.Result;
|
|
|
|
|
import de.srsoftware.tools.jdbc.Query; |
|
|
|
|
import java.net.MalformedURLException; |
|
|
|
|
import java.net.URI; |
|
|
|
|
import java.net.URL; |
|
|
|
|
import java.sql.*; |
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
import java.util.*; |
|
|
|
@ -27,17 +29,9 @@ public class MariaDB implements Database {
@@ -27,17 +29,9 @@ 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 AID = "aid"; |
|
|
|
|
private static final String ALL = "*"; |
|
|
|
|
private static final String KEYWORD = "keyword"; |
|
|
|
|
private static final String APPOINTMENT_TAGS = "appointment_tags"; |
|
|
|
|
private static final String URL = "url"; |
|
|
|
|
private static final String APPOINTMENT_URLS = "appointment_urls"; |
|
|
|
|
private static final String DESCRIPTION = "description"; |
|
|
|
|
private static final String UID = "uid"; |
|
|
|
|
private static final String URLS = "urls"; |
|
|
|
|
private static final String TID = "tid"; |
|
|
|
|
private static final String MIME = "mime"; |
|
|
|
|
private static final String APPOINTMENT_ATTACHMENTS = "appointment_attachments"; |
|
|
|
|
private static final String TAGS = "tags"; |
|
|
|
|
private static final String SLUG = "slug"; |
|
|
|
@ -106,8 +100,45 @@ public class MariaDB implements Database {
@@ -106,8 +100,45 @@ public class MariaDB implements Database {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public Database add(Appointment appointment) { |
|
|
|
|
throw notImplemented(this, "add(Appointment)"); |
|
|
|
|
public Result<Appointment> add(Appointment appointment) { |
|
|
|
|
try { |
|
|
|
|
ResultSet keys = Query //
|
|
|
|
|
.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()) |
|
|
|
|
.execute(connection) |
|
|
|
|
.getGeneratedKeys(); |
|
|
|
|
Appointment saved = null; |
|
|
|
|
if (keys.next()) saved = appointment.clone(keys.getLong(1)); |
|
|
|
|
keys.close(); |
|
|
|
|
if (saved == null) return Error.of("Insert query did not return appointment id!"); |
|
|
|
|
var attachments = saved.attachments(); |
|
|
|
|
Query.InsertQuery assignQuery = null; |
|
|
|
|
|
|
|
|
|
for (var attachment : attachments){ |
|
|
|
|
if (assignQuery == null) assignQuery = Query.insertInto(APPOINTMENT_ATTACHMENTS,AID,UID,MIME); |
|
|
|
|
var urlId = getOrCreateUrl(attachment.url()); |
|
|
|
|
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(),attachment.mime()); |
|
|
|
|
} |
|
|
|
|
if (assignQuery != null) assignQuery.execute(connection); |
|
|
|
|
|
|
|
|
|
return Payload.of(saved); |
|
|
|
|
} catch (SQLException e) { |
|
|
|
|
LOG.log(ERROR,"Failed to store appointment",e); |
|
|
|
|
return Error.of("Failed to store appointment", e); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Optional<Long> getOrCreateUrl(URL url) throws SQLException { |
|
|
|
|
var rs = Query.select(UID).from(URLS).where(URL,equal(url.toString())).exec(connection); |
|
|
|
|
Long uid = null; |
|
|
|
|
if (rs.next()) uid = rs.getLong(1); |
|
|
|
|
rs.close(); |
|
|
|
|
if (uid == null){ |
|
|
|
|
rs = Query.insertInto(URLS,URL).values(url.toString()).execute(connection).getGeneratedKeys(); |
|
|
|
|
if (rs.next()) uid = rs.getLong(1); |
|
|
|
|
rs.close(); |
|
|
|
|
} |
|
|
|
|
return nullable(uid); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public static Database connect(String jdbc, String user, String pass) throws SQLException { |
|
|
|
|