Change Session ID to Integer so that Sqlite Primary key works.

This commit is contained in:
Rsl1122 2017-08-24 22:43:22 +03:00
parent 63cc2b2b0e
commit db935fb981
9 changed files with 27 additions and 27 deletions

View File

@ -26,7 +26,7 @@ import java.util.List;
*/ */
public class Session { public class Session {
private Long sessionID; private Integer sessionID;
private WorldTimes worldTimes; private WorldTimes worldTimes;
private final long sessionStart; private final long sessionStart;
private long sessionEnd; private long sessionEnd;
@ -54,7 +54,7 @@ public class Session {
* @param sessionStart Epoch millisecond the session was started. * @param sessionStart Epoch millisecond the session was started.
* @param sessionEnd Epoch millisecond the session ended. * @param sessionEnd Epoch millisecond the session ended.
*/ */
public Session(long id, long sessionStart, long sessionEnd, int mobKills, int deaths) { public Session(int id, long sessionStart, long sessionEnd, int mobKills, int deaths) {
this.sessionID = id; this.sessionID = id;
this.sessionStart = sessionStart; this.sessionStart = sessionStart;
this.sessionEnd = sessionEnd; this.sessionEnd = sessionEnd;
@ -195,11 +195,11 @@ public class Session {
* @return ID if present. * @return ID if present.
* @throws NullPointerException if Session was not fetched from DB. Check using {@code isFetchedFromDB} * @throws NullPointerException if Session was not fetched from DB. Check using {@code isFetchedFromDB}
*/ */
public long getSessionID() { public int getSessionID() {
return sessionID; return sessionID;
} }
public void setSessionID(long sessionID) { public void setSessionID(int sessionID) {
this.sessionID = sessionID; this.sessionID = sessionID;
} }
} }

View File

@ -71,12 +71,12 @@ public class TableSqlParser extends SqlParser {
return this; return this;
} }
public TableSqlParser primaryKeyIDColumn(boolean mySQL, String column, String type) { public TableSqlParser primaryKeyIDColumn(boolean mySQL, String column) {
if (columns > 0) { if (columns > 0) {
append(", "); append(", ");
} }
append(column).addSpace(); append(column).addSpace();
append(type).addSpace(); append(Sql.INT).addSpace();
append((mySQL) ? "NOT NULL AUTO_INCREMENT" : "PRIMARY KEY"); append((mySQL) ? "NOT NULL AUTO_INCREMENT" : "PRIMARY KEY");
columns++; columns++;
return this; return this;

View File

@ -43,7 +43,7 @@ public class CommandUseTable extends Table {
public boolean createTable() { public boolean createTable() {
ServerTable serverTable = db.getServerTable(); ServerTable serverTable = db.getServerTable();
return createTable(TableSqlParser.createTable(tableName) return createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnCommandId, Sql.INT) .primaryKeyIDColumn(usingMySQL, columnCommandId)
.column(columnCommand, Sql.varchar(20)).notNull() .column(columnCommand, Sql.varchar(20)).notNull()
.column(columnTimesUsed, Sql.INT).notNull() .column(columnTimesUsed, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull()

View File

@ -47,7 +47,7 @@ public class KillsTable extends UserIDTable {
.column(columnVictimUserID, Sql.INT).notNull() .column(columnVictimUserID, Sql.INT).notNull()
.column(columnWeapon, Sql.varchar(30)).notNull() .column(columnWeapon, Sql.varchar(30)).notNull()
.column(columnDate, Sql.LONG).notNull() .column(columnDate, Sql.LONG).notNull()
.column(columnSessionID, Sql.LONG).notNull() .column(columnSessionID, Sql.INT).notNull()
.foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID()) .foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID()) .foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnSessionID, sessionsTable.getTableName(), sessionsTable.getColumnID()) .foreignKey(columnSessionID, sessionsTable.getTableName(), sessionsTable.getColumnID())
@ -76,7 +76,7 @@ public class KillsTable extends UserIDTable {
} }
} }
public void savePlayerKills(UUID uuid, long sessionID, List<PlayerKill> playerKills) throws SQLException { public void savePlayerKills(UUID uuid, int sessionID, List<PlayerKill> playerKills) throws SQLException {
if (Verify.isEmpty(playerKills)) { if (Verify.isEmpty(playerKills)) {
return; return;
} }
@ -98,7 +98,7 @@ public class KillsTable extends UserIDTable {
String weapon = kill.getWeapon(); String weapon = kill.getWeapon();
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.setString(2, victim.toString()); statement.setString(2, victim.toString());
statement.setLong(3, sessionID); statement.setInt(3, sessionID);
statement.setLong(4, date); statement.setLong(4, date);
statement.setString(5, weapon); statement.setString(5, weapon);
statement.addBatch(); statement.addBatch();
@ -111,7 +111,7 @@ public class KillsTable extends UserIDTable {
} }
} }
public void addKillsToSessions(UUID uuid, Map<Long, Session> sessions) throws SQLException { public void addKillsToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet set = null; ResultSet set = null;
try { try {
@ -131,7 +131,7 @@ public class KillsTable extends UserIDTable {
commit(statement.getConnection()); commit(statement.getConnection());
while (set.next()) { while (set.next()) {
long sessionID = set.getLong(columnSessionID); int sessionID = set.getInt(columnSessionID);
Session session = sessions.get(sessionID); Session session = sessions.get(sessionID);
if (session == null) { if (session == null) {
continue; continue;

View File

@ -49,7 +49,7 @@ public class ServerTable extends Table {
@Override @Override
public boolean createTable() { public boolean createTable() {
return createTable(TableSqlParser.createTable(tableName) return createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnServerID, Sql.INT) .primaryKeyIDColumn(usingMySQL, columnServerID)
.column(columnServerUUID, Sql.varchar(36)).notNull().unique() .column(columnServerUUID, Sql.varchar(36)).notNull().unique()
.column(columnServerName, Sql.varchar(100)) .column(columnServerName, Sql.varchar(100))
.column(columnWebserverAddress, Sql.varchar(100)) .column(columnWebserverAddress, Sql.varchar(100))

View File

@ -45,7 +45,7 @@ public class SessionsTable extends UserIDTable {
@Override @Override
public boolean createTable() { public boolean createTable() {
return createTable(TableSqlParser.createTable(this.tableName) return createTable(TableSqlParser.createTable(this.tableName)
.primaryKeyIDColumn(usingMySQL, columnID, Sql.LONG) .primaryKeyIDColumn(usingMySQL, columnID)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnServerID, Sql.INT).notNull() .column(columnServerID, Sql.INT).notNull()
.column(columnSessionStart, Sql.LONG).notNull() .column(columnSessionStart, Sql.LONG).notNull()
@ -70,7 +70,7 @@ public class SessionsTable extends UserIDTable {
*/ */
public void saveSession(UUID uuid, Session session) throws SQLException { public void saveSession(UUID uuid, Session session) throws SQLException {
saveSessionInformation(uuid, session); saveSessionInformation(uuid, session);
long sessionID = getSessionID(uuid, session); int sessionID = getSessionID(uuid, session);
if (sessionID == -1) { if (sessionID == -1) {
throw new IllegalStateException("Session was not Saved!"); throw new IllegalStateException("Session was not Saved!");
} }
@ -124,7 +124,7 @@ public class SessionsTable extends UserIDTable {
* @param session session inserted. * @param session session inserted.
* @return ID of the inserted session or -1 if session has not been inserted. * @return ID of the inserted session or -1 if session has not been inserted.
*/ */
private long getSessionID(UUID uuid, Session session) throws SQLException { private int getSessionID(UUID uuid, Session session) throws SQLException {
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet set = null; ResultSet set = null;
try { try {
@ -137,9 +137,9 @@ public class SessionsTable extends UserIDTable {
statement.setLong(3, session.getSessionEnd()); statement.setLong(3, session.getSessionEnd());
set = statement.executeQuery(); set = statement.executeQuery();
if (set.next()) { if (set.next()) {
return set.getLong(columnID); return set.getInt(columnID);
} }
return -1L; return -1;
} finally { } finally {
endTransaction(statement); endTransaction(statement);
close(set, statement); close(set, statement);
@ -168,7 +168,7 @@ public class SessionsTable extends UserIDTable {
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
set = statement.executeQuery(); set = statement.executeQuery();
while (set.next()) { while (set.next()) {
long id = set.getLong(columnID); int id = set.getInt(columnID);
long start = set.getLong(columnSessionStart); long start = set.getLong(columnSessionStart);
long end = set.getLong(columnSessionEnd); long end = set.getLong(columnSessionEnd);
String serverName = serverNames.get(set.getInt(columnServerID)); String serverName = serverNames.get(set.getInt(columnServerID));
@ -192,7 +192,7 @@ public class SessionsTable extends UserIDTable {
public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException { public Map<String, List<Session>> getSessions(UUID uuid) throws SQLException {
Map<String, List<Session>> sessions = getSessionInformation(uuid); Map<String, List<Session>> sessions = getSessionInformation(uuid);
Map<Long, Session> allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toMap(Session::getSessionID, Function.identity())); Map<Integer, Session> allSessions = sessions.values().stream().flatMap(Collection::stream).collect(Collectors.toMap(Session::getSessionID, Function.identity()));
db.getKillsTable().addKillsToSessions(uuid, allSessions); db.getKillsTable().addKillsToSessions(uuid, allSessions);
db.getWorldTimesTable().addWorldTimesToSessions(uuid, allSessions); db.getWorldTimesTable().addWorldTimesToSessions(uuid, allSessions);

View File

@ -37,7 +37,7 @@ public class UsersTable extends UserIDTable {
@Override @Override
public boolean createTable() { public boolean createTable() {
return createTable(TableSqlParser.createTable(tableName) return createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnID, Sql.INT) .primaryKeyIDColumn(usingMySQL, columnID)
.column(columnUUID, Sql.varchar(36)).notNull().unique() .column(columnUUID, Sql.varchar(36)).notNull().unique()
.column(columnRegistered, Sql.LONG).notNull() .column(columnRegistered, Sql.LONG).notNull()
.column(columnName, Sql.varchar(16)).notNull() .column(columnName, Sql.varchar(16)).notNull()

View File

@ -42,7 +42,7 @@ public class WorldTable extends Table {
@Override @Override
public boolean createTable() { public boolean createTable() {
return createTable(TableSqlParser.createTable(tableName) return createTable(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnWorldId, Sql.INT) .primaryKeyIDColumn(usingMySQL, columnWorldId)
.column(columnWorldName, Sql.varchar(100)).notNull() .column(columnWorldName, Sql.varchar(100)).notNull()
.primaryKey(usingMySQL, columnWorldId) .primaryKey(usingMySQL, columnWorldId)
.toString() .toString()

View File

@ -51,7 +51,7 @@ public class WorldTimesTable extends UserIDTable {
return createTable(TableSqlParser.createTable(tableName) return createTable(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull() .column(columnUserID, Sql.INT).notNull()
.column(columnWorldId, Sql.INT).notNull() .column(columnWorldId, Sql.INT).notNull()
.column(columnSessionID, Sql.LONG).notNull() .column(columnSessionID, Sql.INT).notNull()
.column(columnSurvival, Sql.LONG).notNull().defaultValue("0") .column(columnSurvival, Sql.LONG).notNull().defaultValue("0")
.column(columnCreative, Sql.LONG).notNull().defaultValue("0") .column(columnCreative, Sql.LONG).notNull().defaultValue("0")
.column(columnAdventure, Sql.LONG).notNull().defaultValue("0") .column(columnAdventure, Sql.LONG).notNull().defaultValue("0")
@ -63,7 +63,7 @@ public class WorldTimesTable extends UserIDTable {
); );
} }
public void saveWorldTimes(UUID uuid, long sessionID, WorldTimes worldTimes) throws SQLException { public void saveWorldTimes(UUID uuid, int sessionID, WorldTimes worldTimes) throws SQLException {
Map<String, GMTimes> worldTimesMap = worldTimes.getWorldTimes(); Map<String, GMTimes> worldTimesMap = worldTimes.getWorldTimes();
if (Verify.isEmpty(worldTimesMap)) { if (Verify.isEmpty(worldTimesMap)) {
return; return;
@ -92,7 +92,7 @@ public class WorldTimesTable extends UserIDTable {
GMTimes gmTimes = entry.getValue(); GMTimes gmTimes = entry.getValue();
statement.setString(1, uuid.toString()); statement.setString(1, uuid.toString());
statement.setString(2, worldName); statement.setString(2, worldName);
statement.setLong(3, sessionID); statement.setInt(3, sessionID);
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
statement.setLong(4, gmTimes.getTime(gms[0])); statement.setLong(4, gmTimes.getTime(gms[0]));
@ -109,7 +109,7 @@ public class WorldTimesTable extends UserIDTable {
} }
} }
public void addWorldTimesToSessions(UUID uuid, Map<Long, Session> sessions) throws SQLException { public void addWorldTimesToSessions(UUID uuid, Map<Integer, Session> sessions) throws SQLException {
PreparedStatement statement = null; PreparedStatement statement = null;
ResultSet set = null; ResultSet set = null;
try { try {
@ -131,7 +131,7 @@ public class WorldTimesTable extends UserIDTable {
String[] gms = GMTimes.getGMKeyArray(); String[] gms = GMTimes.getGMKeyArray();
while (set.next()) { while (set.next()) {
long sessionID = set.getLong(columnSessionID); int sessionID = set.getInt(columnSessionID);
Session session = sessions.get(sessionID); Session session = sessions.get(sessionID);
if (session == null) { if (session == null) {