Browse Source

now closing statements

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
main
Stephan Richter 2 months ago
parent
commit
f80e6d9ea4
  1. 2
      de.srsoftware.cal.app/build.gradle.kts
  2. 2
      de.srsoftware.cal.db/build.gradle.kts
  3. 30
      de.srsoftware.cal.db/src/main/java/de/srsoftware/cal/db/MariaDB.java

2
de.srsoftware.cal.app/build.gradle.kts

@ -16,7 +16,7 @@ dependencies {
implementation("de.srsoftware:tools.http:1.2.4") implementation("de.srsoftware:tools.http:1.2.4")
implementation("de.srsoftware:tools.logging:1.2.0") implementation("de.srsoftware:tools.logging:1.2.0")
implementation("de.srsoftware:tools.plugin:1.0.1") implementation("de.srsoftware:tools.plugin:1.0.2")
implementation("de.srsoftware:tools.util:1.3.0") implementation("de.srsoftware:tools.util:1.3.0")
implementation("de.srsoftware:tools.web:1.3.14") implementation("de.srsoftware:tools.web:1.3.14")
implementation("com.mysql:mysql-connector-j:9.1.0") implementation("com.mysql:mysql-connector-j:9.1.0")

2
de.srsoftware.cal.db/build.gradle.kts

@ -4,7 +4,7 @@ dependencies {
implementation(project(":de.srsoftware.cal.api")) implementation(project(":de.srsoftware.cal.api"))
implementation(project(":de.srsoftware.cal.base")) implementation(project(":de.srsoftware.cal.base"))
implementation("de.srsoftware:tools.jdbc:1.1.5") implementation("de.srsoftware:tools.jdbc:1.3.0")
implementation("de.srsoftware:tools.optionals:1.0.0") implementation("de.srsoftware:tools.optionals:1.0.0")
implementation("de.srsoftware:tools.util:1.3.0") implementation("de.srsoftware:tools.util:1.3.0")
} }

30
de.srsoftware.cal.db/src/main/java/de/srsoftware/cal/db/MariaDB.java

@ -52,6 +52,7 @@ public class MariaDB implements Database {
if (rs.next()) { if (rs.next()) {
version = rs.getInt(VALUE); version = rs.getInt(VALUE);
} }
rs.getStatement().close();
rs.close(); rs.close();
switch (version) { switch (version) {
case 0: case 0:
@ -66,8 +67,10 @@ public class MariaDB implements Database {
} }
private void createTagSearch() throws SQLException { private void createTagSearch() throws SQLException {
connection.prepareStatement("CREATE VIEW tag_search AS SELECT aid, GROUP_CONCAT(keyword) AS tags FROM appointment_tags LEFT JOIN tags ON appointment_tags.tid = tags.tid GROUP BY aid").execute(); var stmt = connection.prepareStatement("CREATE VIEW tag_search AS SELECT aid, GROUP_CONCAT(keyword) AS tags FROM appointment_tags LEFT JOIN tags ON appointment_tags.tid = tags.tid GROUP BY aid");
Query.update(CONFIG).set(VALUE).where(KEYNAME,equal(DB_VERSION)).prepare(connection).apply(2); stmt.execute();
stmt.close();
Query.update(CONFIG).set(VALUE).where(KEYNAME,equal(DB_VERSION)).prepare(connection).apply(2).close();
} }
@Override @Override
@ -83,6 +86,7 @@ public class MariaDB implements Database {
.getGeneratedKeys(); .getGeneratedKeys();
Appointment saved = null; Appointment saved = null;
if (keys.next()) saved = appointment.clone(keys.getLong(1)); if (keys.next()) saved = appointment.clone(keys.getLong(1));
keys.getStatement().close();
keys.close(); keys.close();
if (saved == null) return error("Insert query did not return appointment id!"); if (saved == null) return error("Insert query did not return appointment id!");
@ -105,7 +109,7 @@ public class MariaDB implements Database {
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_ATTACHMENTS, AID, UID, MIME); if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_ATTACHMENTS, AID, UID, MIME);
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), attachment.mime()); if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), attachment.mime());
} }
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection).close();
} }
private void writeLinks(Appointment saved) throws SQLException { // link to links private void writeLinks(Appointment saved) throws SQLException { // link to links
@ -116,7 +120,7 @@ public class MariaDB implements Database {
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_URLS, AID, UID, DESCRIPTION); if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_URLS, AID, UID, DESCRIPTION);
if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), link.desciption()); if (urlId.isPresent()) assignQuery.values(saved.id(), urlId.get(), link.desciption());
} }
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection).close();
} }
private void writeTags(Appointment saved) throws SQLException { private void writeTags(Appointment saved) throws SQLException {
@ -127,17 +131,19 @@ public class MariaDB implements Database {
if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_TAGS, AID, TID); if (assignQuery == null) assignQuery = insertInto(APPOINTMENT_TAGS, AID, TID);
if (tagId.isPresent()) assignQuery.values(saved.id(), tagId.get()); if (tagId.isPresent()) assignQuery.values(saved.id(), tagId.get());
} }
if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection); if (assignQuery != null) assignQuery.ignoreDuplicates().execute(connection).close();
} }
private Optional<Long> getOrCreateUrl(URL url) throws SQLException { private Optional<Long> getOrCreateUrl(URL url) throws SQLException {
var rs = select(UID).from(URLS).where(URL, equal(url.toString())).exec(connection); var rs = select(UID).from(URLS).where(URL, equal(url.toString())).exec(connection);
Long uid = null; Long uid = null;
if (rs.next()) uid = rs.getLong(1); if (rs.next()) uid = rs.getLong(1);
rs.getStatement().close();
rs.close(); rs.close();
if (uid == null) { if (uid == null) {
rs = insertInto(URLS, URL).values(url.toString()).execute(connection).getGeneratedKeys(); rs = insertInto(URLS, URL).values(url.toString()).execute(connection).getGeneratedKeys();
if (rs.next()) uid = rs.getLong(1); if (rs.next()) uid = rs.getLong(1);
rs.getStatement().close();
rs.close(); rs.close();
} }
return nullable(uid); return nullable(uid);
@ -147,10 +153,12 @@ public class MariaDB implements Database {
var rs = select(TID).from(TAGS).where(KEYWORD, equal(tag)).exec(connection); var rs = select(TID).from(TAGS).where(KEYWORD, equal(tag)).exec(connection);
Long tid = null; Long tid = null;
if (rs.next()) tid = rs.getLong(1); if (rs.next()) tid = rs.getLong(1);
rs.getStatement().close();
rs.close(); rs.close();
if (tid == null) { if (tid == null) {
rs = insertInto(TAGS, KEYWORD).values(tag).execute(connection).getGeneratedKeys(); rs = insertInto(TAGS, KEYWORD).values(tag).execute(connection).getGeneratedKeys();
if (rs.next()) tid = rs.getLong(1); if (rs.next()) tid = rs.getLong(1);
rs.getStatement().close();
rs.close(); rs.close();
} }
return nullable(tid); return nullable(tid);
@ -166,6 +174,7 @@ public class MariaDB implements Database {
List<String> results = new ArrayList<>(); List<String> results = new ArrayList<>();
var rs = select(KEYWORD).from(TAGS).where(KEYWORD, like("%%%s%%".formatted(infix))).sort(KEYWORD).exec(connection); var rs = select(KEYWORD).from(TAGS).where(KEYWORD, like("%%%s%%".formatted(infix))).sort(KEYWORD).exec(connection);
while (rs.next()) results.add(rs.getString(KEYWORD)); while (rs.next()) results.add(rs.getString(KEYWORD));
rs.getStatement().close();
rs.close(); rs.close();
return Payload.of(results); return Payload.of(results);
} catch (SQLException e) { } catch (SQLException e) {
@ -184,6 +193,7 @@ public class MariaDB implements Database {
var rs = query.exec(connection); var rs = query.exec(connection);
aids = new ArrayList<>(); aids = new ArrayList<>();
while (rs.next()) aids.add(rs.getLong(AID)); while (rs.next()) aids.add(rs.getLong(AID));
rs.getStatement().close();
rs.close(); rs.close();
} catch (SQLException e) { } catch (SQLException e) {
return SqlError.of(e,"Failed to read appointment ids for tags %s",tags); return SqlError.of(e,"Failed to read appointment ids for tags %s",tags);
@ -204,6 +214,7 @@ public class MariaDB implements Database {
var results = query.exec(connection); var results = query.exec(connection);
var list = new ArrayList<Appointment>(); var list = new ArrayList<Appointment>();
while (results.next()) createAppointmentOf(results).optional().ifPresent(list::add); while (results.next()) createAppointmentOf(results).optional().ifPresent(list::add);
results.getStatement().close();
results.close(); results.close();
addAttachments(list); addAttachments(list);
return Payload.of(list); return Payload.of(list);
@ -235,6 +246,7 @@ public class MariaDB implements Database {
LOG.log(WARNING,"Failed to create URL object from %s",uri); LOG.log(WARNING,"Failed to create URL object from %s",uri);
} }
} }
rs.getStatement().close();
rs.close(); rs.close();
} catch (Exception e) { } catch (Exception e) {
LOG.log(WARNING,"Failed to load attachments.",e); LOG.log(WARNING,"Failed to load attachments.",e);
@ -246,6 +258,7 @@ public class MariaDB implements Database {
try { try {
var rs = select(ALL).from(APPOINTMENTS).where(AID, equal(id)).exec(connection); var rs = select(ALL).from(APPOINTMENTS).where(AID, equal(id)).exec(connection);
Result<Appointment> result = rs.next() ? createAppointmentOf(rs).map(this::loadExtra) : NotFound.of("Failed to find appointment with id %s", id); Result<Appointment> result = rs.next() ? createAppointmentOf(rs).map(this::loadExtra) : NotFound.of("Failed to find appointment with id %s", id);
rs.getStatement().close();
rs.close(); rs.close();
return result; return result;
} catch (SQLException e) { } catch (SQLException e) {
@ -258,6 +271,7 @@ public class MariaDB implements Database {
try { try {
var rs = select(ALL).from(APPOINTMENTS).where(LOCATION, equal(location)).where(START, equal(Timestamp.valueOf(start))).exec(connection); 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(this::loadExtra) : error("Failed to find appointment starting %s @ %s", start, location); Result<Appointment> result = rs.next() ? createAppointmentOf(rs).map(this::loadExtra) : error("Failed to find appointment starting %s @ %s", start, location);
rs.getStatement().close();
rs.close(); rs.close();
return result; return result;
} catch (SQLException e) { } catch (SQLException e) {
@ -276,6 +290,7 @@ public class MariaDB implements Database {
try { try {
var rs = select(KEYWORD).from(APPOINTMENT_TAGS).leftJoin(TID, "tags", TID).where(AID, equal(id)).exec(connection); var rs = select(KEYWORD).from(APPOINTMENT_TAGS).leftJoin(TID, "tags", TID).where(AID, equal(id)).exec(connection);
while (rs.next()) event.tags(rs.getString(1)); while (rs.next()) event.tags(rs.getString(1));
rs.getStatement().close();
rs.close(); rs.close();
return Payload.of(event); return Payload.of(event);
} catch (SQLException e) { } catch (SQLException e) {
@ -299,6 +314,7 @@ public class MariaDB implements Database {
LOG.log(WARNING, () -> "Failed to convert %s to URI!".formatted(u)); LOG.log(WARNING, () -> "Failed to convert %s to URI!".formatted(u));
} }
} }
rs.getStatement().close();
rs.close(); rs.close();
return Payload.of(event); return Payload.of(event);
} catch (SQLException e) { } catch (SQLException e) {
@ -322,6 +338,7 @@ public class MariaDB implements Database {
LOG.log(WARNING, () -> "Failed to convert %s to URI!".formatted(u)); LOG.log(WARNING, () -> "Failed to convert %s to URI!".formatted(u));
} }
} }
rs.getStatement().close();
rs.close(); rs.close();
return Payload.of(event); return Payload.of(event);
} catch (SQLException e) { } catch (SQLException e) {
@ -384,7 +401,8 @@ public class MariaDB implements Database {
.set(TITLE, DESCRIPTION, START, END, LOCATION, COORDS) .set(TITLE, DESCRIPTION, START, END, LOCATION, COORDS)
.where(AID, equal(id)) .where(AID, equal(id))
.prepare(connection) .prepare(connection)
.apply(event.title(), event.description(), start, end, location, coords); .apply(event.title(), event.description(), start, end, location, coords)
.close();
delete().from(APPOINTMENT_TAGS).where(AID, equal(id)).execute(connection); delete().from(APPOINTMENT_TAGS).where(AID, equal(id)).execute(connection);
writeTags(event); writeTags(event);

Loading…
Cancel
Save