Commit 97d51e18 authored by vehjelmtvedt's avatar vehjelmtvedt
Browse files

EventStorage now use prepared statements

parent 93a75c5a
package inf226.inchat;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.time.Instant;
import java.util.UUID;
import java.util.function.Consumer;
......@@ -35,25 +32,32 @@ public final class EventStorage
throws SQLException {
final Stored<Channel.Event> stored = new Stored<Channel.Event>(event);
String sql = "INSERT INTO Event VALUES('" + stored.identity + "','"
+ stored.version + "','"
+ event.channel + "','"
+ event.type.code + "','"
+ event.time + "')";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("INSERT INTO Event VALUES(?,?,?,?,?)");
statement.setObject(1, stored.identity);
statement.setObject(2, stored.version);
statement.setObject(3, event.channel);
statement.setInt(4, event.type.code);
statement.setObject(5, event.time);
statement.executeUpdate();
PreparedStatement eventStmt = null;
switch (event.type) {
case message:
sql = "INSERT INTO Message VALUES('" + stored.identity + "','"
+ event.sender + "','"
+ event.message +"')";
eventStmt = connection.prepareStatement("INSERT INTO Message VALUES(?,?,?)");
eventStmt.setObject(1, stored.identity);
eventStmt.setString(2, event.sender);
eventStmt.setString(3, event.message);
break;
case join:
sql = "INSERT INTO Joined VALUES('" + stored.identity + "','"
+ event.sender +"')";
eventStmt = connection.prepareStatement("INSERT INTO Joined VALUES(?,?)");
eventStmt.setObject(1, stored.identity);
eventStmt.setString(2, event.sender);
break;
}
connection.createStatement().executeUpdate(sql);
eventStmt.executeUpdate();
return stored;
}
......@@ -66,24 +70,32 @@ public final class EventStorage
final Stored<Channel.Event> current = get(event.identity);
final Stored<Channel.Event> updated = current.newVersion(new_event);
if(current.version.equals(event.version)) {
String sql = "UPDATE Event SET" +
" (version,channel,time,type) =('"
+ updated.version + "','"
+ new_event.channel + "','"
+ new_event.time + "','"
+ new_event.type.code
+ "') WHERE id='"+ updated.identity + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("UPDATE Event SET (version,channel,time,type) =(?,?,?,?) WHERE id=?");
statement.setObject(1, updated.version);
statement.setObject(2, new_event.channel);
statement.setObject(3, new_event.time);
statement.setInt(4, new_event.type.code);
statement.setObject(5, updated.identity);
statement.executeUpdate();
PreparedStatement eventStmt = null;
switch (new_event.type) {
case message:
sql = "UPDATE Message SET (sender,content)=('" + new_event.sender + "','"
+ new_event.message +"') WHERE id='"+ updated.identity + "'";
eventStmt = connection.prepareStatement("UPDATE Message SET (sender,content)=(?,?) WHERE id=?");
eventStmt.setString(1, new_event.sender);
eventStmt.setString(2, new_event.message);
eventStmt.setObject(3, updated.identity);
break;
case join:
sql = "UPDATE Joined SET (sender)=('" + new_event.sender +"') WHERE id='"+ updated.identity + "'";
eventStmt = connection.prepareStatement("UPDATE Joined SET (sender)=(?) WHERE id=?");
eventStmt.setString(1, new_event.sender);
eventStmt.setObject(2, updated.identity);
break;
}
connection.createStatement().executeUpdate(sql);
eventStmt.executeUpdate();
} else {
throw new UpdatedException(current);
}
......@@ -97,8 +109,9 @@ public final class EventStorage
SQLException {
final Stored<Channel.Event> current = get(event.identity);
if(current.version.equals(event.version)) {
String sql = "DELETE FROM Event WHERE id ='" + event.identity + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("DELETE FROM Event WHERE id =?");
statement.setObject(1, event.identity);
statement.executeUpdate();
} else {
throw new UpdatedException(current);
}
......@@ -107,9 +120,10 @@ public final class EventStorage
public Stored<Channel.Event> get(UUID id)
throws DeletedException,
SQLException {
final String sql = "SELECT version,channel,time,type FROM Event WHERE id = '" + id.toString() + "'";
final Statement statement = connection.createStatement();
final ResultSet rs = statement.executeQuery(sql);
PreparedStatement statement = connection.prepareStatement("SELECT version,channel,time,type FROM Event WHERE id = ?");
statement.setString(1, id.toString());
final ResultSet rs = statement.executeQuery();
if(rs.next()) {
final UUID version = UUID.fromString(rs.getString("version"));
......@@ -120,19 +134,21 @@ public final class EventStorage
final Instant time =
Instant.parse(rs.getString("time"));
final Statement mstatement = connection.createStatement();
PreparedStatement mstatement = null;
switch(type) {
case message:
final String msql = "SELECT sender,content FROM Message WHERE id = '" + id.toString() + "'";
final ResultSet mrs = mstatement.executeQuery(msql);
mstatement = connection.prepareStatement("SELECT sender,content FROM Message WHERE id = ?");
mstatement.setString(1, id.toString());
final ResultSet mrs = mstatement.executeQuery();
mrs.next();
return new Stored<Channel.Event>(
Channel.Event.createMessageEvent(channel,time,mrs.getString("sender"),mrs.getString("content")),
id,
version);
case join:
final String asql = "SELECT sender FROM Joined WHERE id = '" + id.toString() + "'";
final ResultSet ars = mstatement.executeQuery(asql);
mstatement = connection.prepareStatement("SELECT sender FROM Joined WHERE id = ?");
mstatement.setString(1, id.toString());
final ResultSet ars = mstatement.executeQuery();
ars.next();
return new Stored<Channel.Event>(
Channel.Event.createJoinEvent(channel,time,ars.getString("sender")),
......
<?xml version="1.0" encoding="UTF-8"?>
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="inf226.inchat.InchatTest" time="0.434" tests="1" errors="0" skipped="0" failures="0">
<testsuite xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://maven.apache.org/surefire/maven-surefire-plugin/xsd/surefire-test-report.xsd" name="inf226.inchat.InchatTest" time="0.395" tests="1" errors="0" skipped="0" failures="0">
<properties>
<property name="java.specification.version" value="15"/>
<property name="sun.jnu.encoding" value="UTF-8"/>
......@@ -12,7 +12,7 @@
<property name="sun.java.launcher" value="SUN_STANDARD"/>
<property name="user.country" value="GB"/>
<property name="sun.boot.library.path" value="/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/lib"/>
<property name="sun.java.command" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter4110517799755141503.jar /Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire 2021-11-08T17-02-17_469-jvmRun1 surefire14857712398614288888tmp surefire_013856166078824509588tmp"/>
<property name="sun.java.command" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter16480299950810032626.jar /Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire 2021-11-08T17-30-57_389-jvmRun1 surefire278319907713983728tmp surefire_02992833018309938155tmp"/>
<property name="http.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="jdk.debug" value="release"/>
<property name="surefire.test.class.path" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/test-classes:/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/classes:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-server/9.4.9.v20180320/jetty-server-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-http/9.4.9.v20180320/jetty-http-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-util/9.4.9.v20180320/jetty-util-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/org/eclipse/jetty/jetty-io/9.4.9.v20180320/jetty-io-9.4.9.v20180320.jar:/Users/vetle/.m2/repository/org/junit/jupiter/junit-jupiter-api/5.4.2/junit-jupiter-api-5.4.2.jar:/Users/vetle/.m2/repository/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar:/Users/vetle/.m2/repository/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar:/Users/vetle/.m2/repository/org/junit/platform/junit-platform-commons/1.4.2/junit-platform-commons-1.4.2.jar:/Users/vetle/.m2/repository/org/junit/jupiter/junit-jupiter-engine/5.4.2/junit-jupiter-engine-5.4.2.jar:/Users/vetle/.m2/repository/org/junit/platform/junit-platform-engine/1.4.2/junit-platform-engine-1.4.2.jar:/Users/vetle/.m2/repository/org/xerial/sqlite-jdbc/3.28.0/sqlite-jdbc-3.28.0.jar:/Users/vetle/.m2/repository/com/lambdaworks/scrypt/1.4.0/scrypt-1.4.0.jar:"/>
......@@ -28,7 +28,7 @@
<property name="line.separator" value="&#10;"/>
<property name="java.specification.name" value="Java Platform API Specification"/>
<property name="java.vm.specification.vendor" value="Oracle Corporation"/>
<property name="surefire.real.class.path" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter4110517799755141503.jar"/>
<property name="surefire.real.class.path" value="/Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire/surefirebooter16480299950810032626.jar"/>
<property name="sun.management.compiler" value="HotSpot 64-Bit Tiered Compilers"/>
<property name="ftp.nonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="java.runtime.version" value="15.0.1+9-18"/>
......@@ -54,5 +54,5 @@
<property name="socksNonProxyHosts" value="local|*.local|169.254/16|*.169.254/16"/>
<property name="java.class.version" value="59.0"/>
</properties>
<testcase name="chatSetup" classname="inf226.inchat.InchatTest" time="0.422"/>
<testcase name="chatSetup" classname="inf226.inchat.InchatTest" time="0.387"/>
</testsuite>
\ No newline at end of file
-------------------------------------------------------------------------------
Test set: inf226.inchat.InchatTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.434 s - in inf226.inchat.InchatTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.395 s - in inf226.inchat.InchatTest
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment