Commit 93a75c5a authored by vehjelmtvedt's avatar vehjelmtvedt
Browse files

ChannelStorage now use prepared statements

parent 8ae05d92
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.TreeMap;
......@@ -43,10 +40,11 @@ public final class ChannelStorage
throws SQLException {
final Stored<Channel> stored = new Stored<Channel>(channel);
String sql = "INSERT INTO Channel VALUES('" + stored.identity + "','"
+ stored.version + "','"
+ channel.name + "')";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("INSERT INTO Channel VALUES(?,?,?)");
statement.setObject(1, stored.identity);
statement.setObject(2, stored.version);
statement.setObject(3, channel.name);
statement.executeUpdate();
return stored;
}
......@@ -59,12 +57,12 @@ public final class ChannelStorage
final Stored<Channel> current = get(channel.identity);
final Stored<Channel> updated = current.newVersion(new_channel);
if(current.version.equals(channel.version)) {
String sql = "UPDATE Channel SET" +
" (version,name) =('"
+ updated.version + "','"
+ new_channel.name
+ "') WHERE id='"+ updated.identity + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("UPDATE Channel SET (version,name) =(?,?) WHERE id=?");
statement.setObject(1, updated.version);
statement.setString(2, new_channel.name);
statement.setObject(3, updated.version);
statement.executeUpdate();
} else {
throw new UpdatedException(current);
}
......@@ -79,8 +77,9 @@ public final class ChannelStorage
SQLException {
final Stored<Channel> current = get(channel.identity);
if(current.version.equals(channel.version)) {
String sql = "DELETE FROM Channel WHERE id ='" + channel.identity + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("DELETE FROM Channel WHERE id =?");
statement.setObject(1, channel.identity);
statement.executeUpdate();
} else {
throw new UpdatedException(current);
}
......@@ -90,14 +89,15 @@ public final class ChannelStorage
throws DeletedException,
SQLException {
final String channelsql = "SELECT version,name FROM Channel WHERE id = '" + id.toString() + "'";
final String eventsql = "SELECT id,rowid FROM Event WHERE channel = '" + id.toString() + "' ORDER BY rowid ASC";
PreparedStatement channelStmt = connection.prepareStatement("SELECT version,name FROM Channel WHERE id = ?");
channelStmt.setString(1, id.toString());
PreparedStatement eventStmt = connection.prepareStatement("SELECT id,rowid FROM Event WHERE channel = ? ORDER BY rowid ASC");
eventStmt.setString(1, id.toString());
final Statement channelStatement = connection.createStatement();
final Statement eventStatement = connection.createStatement();
final ResultSet channelResult = channelStatement.executeQuery(channelsql);
final ResultSet eventResult = eventStatement.executeQuery(eventsql);
final ResultSet channelResult = channelStmt.executeQuery();
final ResultSet eventResult = eventStmt.executeQuery();
if(channelResult.next()) {
final UUID version =
......@@ -125,7 +125,10 @@ public final class ChannelStorage
throws SQLException, DeletedException {
String sql = "UPDATE Channel SET" +
" (version) =('" + UUID.randomUUID() + "') WHERE id='"+ channelId + "'";
connection.createStatement().executeUpdate(sql);
PreparedStatement statement = connection.prepareStatement("UPDATE Channel SET (version) =(?) WHERE id=?");
statement.setObject(1, UUID.randomUUID());
statement.setObject(2, channelId);
statement.executeUpdate();
Stored<Channel> channel = get(channelId);
giveNextVersion(channel);
return channel;
......@@ -139,10 +142,10 @@ public final class ChannelStorage
throws DeletedException,
SQLException {
final String channelsql = "SELECT version FROM Channel WHERE id = '" + id.toString() + "'";
final Statement channelStatement = connection.createStatement();
PreparedStatement statement = connection.prepareStatement("SELECT version FROM Channel WHERE id = ?");
statement.setString(1, id.toString());
final ResultSet channelResult = channelStatement.executeQuery(channelsql);
final ResultSet channelResult = statement.executeQuery();
if(channelResult.next()) {
return UUID.fromString(
channelResult.getString("version"));
......@@ -215,8 +218,10 @@ public final class ChannelStorage
*/
public Stored<Channel> lookupChannelForEvent(Stored<Channel.Event> e)
throws SQLException, DeletedException {
String sql = "SELECT channel FROM ChannelEvent WHERE event='" + e.identity + "'";
final ResultSet rs = connection.createStatement().executeQuery(sql);
PreparedStatement statement = connection.prepareStatement("SELECT channel FROM ChannelEvent WHERE event=?");
statement.setObject(1, e.identity);
final ResultSet rs = statement.executeQuery();
if(rs.next()) {
final UUID channelId = UUID.fromString(rs.getString("channel"));
return get(channelId);
......
<?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.433" 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.434" 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/surefirebooter12262613770582604243.jar /Users/vetle/Desktop/Skole/5. Semester/INF226/Oblig 3/inf226-2021-inchat/target/surefire 2021-11-08T16-42-24_025-jvmRun1 surefire15041844421380734356tmp surefire_01889992399637029475tmp"/>
<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="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/surefirebooter12262613770582604243.jar"/>
<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="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.425"/>
<testcase name="chatSetup" classname="inf226.inchat.InchatTest" time="0.422"/>
</testsuite>
\ No newline at end of file
-------------------------------------------------------------------------------
Test set: inf226.inchat.InchatTest
-------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.433 s - in inf226.inchat.InchatTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.434 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