Fix sql error when attempting to reinsert same report mapping

This commit is contained in:
Keir Nellyer 2016-08-19 16:21:44 +01:00
parent 2a62abfd33
commit b6f0e84a02
1 changed files with 9 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import mineplex.core.common.util.UtilTime; import mineplex.core.common.util.UtilTime;
@ -24,7 +25,7 @@ public class SnapshotRepository
private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (senderId, `server`, `time`, message, snapshotType) VALUES (?, ?, ?, ?, ?);"; private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (senderId, `server`, `time`, message, snapshotType) VALUES (?, ?, ?, ?, ?);";
private static final String INSERT_SNAPSHOT_RECIPIENT = "INSERT INTO snapshotRecipients (snapshotId, recipientId) VALUES (?, ?);"; private static final String INSERT_SNAPSHOT_RECIPIENT = "INSERT INTO snapshotRecipients (snapshotId, recipientId) VALUES (?, ?);";
private static final String INSERT_REPORT_SNAPSHOT_MAPPING = "INSERT INTO reportSnapshots (reportId, snapshotId) VALUES (?, ?);"; private static final String INSERT_REPORT_SNAPSHOT_MAPPING = "INSERT IGNORE INTO reportSnapshots (reportId, snapshotId) VALUES (?, ?);";
private final String _serverName; private final String _serverName;
@ -80,9 +81,10 @@ public class SnapshotRepository
// this allows snapshots to be inserted in batch // this allows snapshots to be inserted in batch
private void insertSnapshot(Connection connection, PreparedStatement insertSnapshotStatement, long reportId, Snapshot snapshot) throws SQLException private void insertSnapshot(Connection connection, PreparedStatement insertSnapshotStatement, long reportId, Snapshot snapshot) throws SQLException
{ {
long snapshotId = snapshot.getId().orElse((long) -1); Optional<Long> snapshotIdOptional = snapshot.getId();
long snapshotId;
if (snapshotId == -1) if (!snapshotIdOptional.isPresent())
{ {
insertSnapshotStatement.setInt(1, snapshot.getSenderId()); insertSnapshotStatement.setInt(1, snapshot.getSenderId());
insertSnapshotStatement.setString(2, _serverName); insertSnapshotStatement.setString(2, _serverName);
@ -104,6 +106,10 @@ public class SnapshotRepository
insertRecipients(connection, snapshotId, snapshot.getRecipientIds()); insertRecipients(connection, snapshotId, snapshot.getRecipientIds());
} }
else
{
snapshotId = snapshotIdOptional.get();
}
insertReportMapping(connection, reportId, snapshotId); insertReportMapping(connection, reportId, snapshotId);
} }