Commit f4cf8482 authored by Magne.Stenseng's avatar Magne.Stenseng
Browse files

Replace EventStorage.java

parent 040aba3f
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;
......@@ -18,9 +15,8 @@ public final class EventStorage
implements Storage<Channel.Event,SQLException> {
private final Connection connection;
public EventStorage(Connection connection)
throws SQLException {
public EventStorage(Connection connection) throws SQLException {
this.connection = connection;
connection.createStatement()
.executeUpdate("CREATE TABLE IF NOT EXISTS Event (id TEXT PRIMARY KEY, version TEXT, channel TEXT, type INTEGER, time TEXT, FOREIGN KEY(channel) REFERENCES Channel(id) ON DELETE CASCADE)");
......@@ -31,8 +27,7 @@ public final class EventStorage
}
@Override
public Stored<Channel.Event> save(Channel.Event event)
throws SQLException {
public Stored<Channel.Event> save(Channel.Event event) throws SQLException {
final Stored<Channel.Event> stored = new Stored<Channel.Event>(event);
......@@ -58,11 +53,8 @@ public final class EventStorage
}
@Override
public synchronized Stored<Channel.Event> update(Stored<Channel.Event> event,
Channel.Event new_event)
throws UpdatedException,
DeletedException,
SQLException {
public synchronized Stored<Channel.Event> update(Stored<Channel.Event> event, Channel.Event new_event)
throws UpdatedException, DeletedException, SQLException {
final Stored<Channel.Event> current = get(event.identity);
final Stored<Channel.Event> updated = current.newVersion(new_event);
if(current.version.equals(event.version)) {
......@@ -92,24 +84,23 @@ public final class EventStorage
@Override
public synchronized void delete(Stored<Channel.Event> event)
throws UpdatedException,
DeletedException,
SQLException {
throws UpdatedException, DeletedException, 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);
String sql = "DELETE FROM Event WHERE id ='" + event.identity + "'";
connection.createStatement().executeUpdate(sql);
} else {
throw new UpdatedException(current);
}
}
@Override
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);
public Stored<Channel.Event> get(UUID id) throws DeletedException, SQLException {
final String sqlQuery = "SELECT version,channel,time,type FROM Event WHERE id = ? ";
final String sql = id.toString();
final PreparedStatement statement = connection.prepareStatement(sqlQuery);
statement.setString(1, sql);
final ResultSet rs = statement.executeQuery();
if(rs.next()) {
final UUID version = UUID.fromString(rs.getString("version"));
......@@ -120,19 +111,25 @@ public final class EventStorage
final Instant time =
Instant.parse(rs.getString("time"));
final Statement mstatement = connection.createStatement();
final PreparedStatement mstatement;
switch(type) {
case message:
final String msql = "SELECT sender,content FROM Message WHERE id = '" + id.toString() + "'";
final ResultSet mrs = mstatement.executeQuery(msql);
final String msqlQuery = "SELECT sender,content FROM Message WHERE id = ? ";
final String msql = id.toString();
mstatement = connection.prepareStatement(msqlQuery);
mstatement.setString(1, msql);
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);
final String asqlQuery = "SELECT sender FROM Joined WHERE id = ? ";
final String asql = id.toString();
mstatement = connection.prepareStatement(asqlQuery);
mstatement.setString(1, asql);
final ResultSet ars = mstatement.executeQuery();
ars.next();
return new Stored<Channel.Event>(
Channel.Event.createJoinEvent(channel,time,ars.getString("sender")),
......
Markdown is supported
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