Skip to content

Commit

Permalink
Make timestamp optional again
Browse files Browse the repository at this point in the history
  • Loading branch information
WiIIiam278 committed Aug 28, 2021
1 parent e200bd6 commit 7539094
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>me.William278</groupId>
<artifactId>HuskHomes</artifactId>
<version>2.8</version>
<version>2.8.1</version>
<packaging>jar</packaging>

<name>HuskHomes</name>
Expand Down
35 changes: 30 additions & 5 deletions src/main/java/me/william278/huskhomes2/data/DataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,18 @@ public static List<Home> getPlayerHomes(String playerName, Connection connection
while (resultSet.next()) {
int locationID = resultSet.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection);
long epochTime = 0;
final Timestamp timestamp = resultSet.getTimestamp("creation_time");
if (timestamp != null) {
epochTime = timestamp.getTime();
}
playerHomes.add(new Home(teleportationPoint,
playerName,
getPlayerUUID(resultSet.getInt("player_id"), connection),
resultSet.getString("name"),
resultSet.getString("description"),
resultSet.getBoolean("public"),
resultSet.getTimestamp("creation_time").getTime()));
epochTime));
}

}
Expand All @@ -187,12 +192,17 @@ public static List<Home> getPublicHomes(Connection connection) throws SQLExcepti
int playerID = resultSet.getInt("player_id");
int locationID = resultSet.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection);
long epochTime = 0;
final Timestamp timestamp = resultSet.getTimestamp("creation_time");
if (timestamp != null) {
epochTime = timestamp.getTime();
}
publicHomes.add(new Home(teleportationPoint,
getPlayerUsername(playerID, connection),
getPlayerUUID(playerID, connection),
resultSet.getString("name"),
resultSet.getString("description"), true,
resultSet.getTimestamp("creation_time").getTime()));
epochTime));
}

}
Expand All @@ -208,10 +218,15 @@ public static List<Warp> getWarps(Connection connection) throws SQLException {
while (resultSet.next()) {
int locationID = resultSet.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection);
long epochTime = 0;
final Timestamp timestamp = resultSet.getTimestamp("creation_time");
if (timestamp != null) {
epochTime = timestamp.getTime();
}
warps.add(new Warp(teleportationPoint,
resultSet.getString("name"),
resultSet.getString("description"),
resultSet.getTimestamp("creation_time").getTime()));
epochTime));
}
} else {
Bukkit.getLogger().severe("Result set of warps returned null!");
Expand All @@ -229,10 +244,15 @@ public static Warp getWarp(String name, Connection connection) throws SQLExcepti
if (resultSet.next()) {
int locationID = resultSet.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection);
long epochTime = 0;
final Timestamp timestamp = resultSet.getTimestamp("creation_time");
if (timestamp != null) {
epochTime = timestamp.getTime();
}
return new Warp(teleportationPoint,
resultSet.getString("name"),
resultSet.getString("description"),
resultSet.getTimestamp("creation_time").getTime());
epochTime);
}
}
return null;
Expand Down Expand Up @@ -505,13 +525,18 @@ public static Home getHome(String ownerUsername, String homeName, Connection con
if (resultSet.next()) {
final int locationID = resultSet.getInt("location_id");
final TeleportationPoint teleportationPoint = getTeleportationPoint(locationID, connection);
long epochTime = 0;
final Timestamp timestamp = resultSet.getTimestamp("creation_time");
if (timestamp != null) {
epochTime = timestamp.getTime();
}
final Home home = new Home(teleportationPoint,
ownerUsername,
getPlayerUUID(resultSet.getInt("player_id"), connection),
resultSet.getString("name"),
resultSet.getString("description"),
resultSet.getBoolean("public"),
resultSet.getTimestamp("creation_time").getTime());
epochTime);
statement.close();
return home;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MySQL extends Database {
"`name` varchar(16) NOT NULL," +
"`description` varchar(255) NOT NULL," +
"`public` boolean NOT NULL," +
"`creation_time` timestamp NOT NULL," +
"`creation_time` timestamp NULL," +
"PRIMARY KEY (`player_id`, `name`)," +
"FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
Expand All @@ -58,7 +58,7 @@ public class MySQL extends Database {
"`location_id` integer NOT NULL," +
"`name` varchar(16) NOT NULL UNIQUE," +
"`description` varchar(255) NOT NULL," +
"`creation_time` timestamp NOT NULL," +
"`creation_time` timestamp NULL," +
"PRIMARY KEY (`location_id`)," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");"
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class SQLite extends Database {
"`name` varchar(16) NOT NULL," +
"`description` varchar(255) NOT NULL," +
"`public` boolean NOT NULL," +
"`creation_time` timestamp NOT NULL," +
"`creation_time` timestamp NULL," +
"PRIMARY KEY (`player_id`, `name`)," +
"FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
Expand All @@ -60,7 +60,7 @@ public class SQLite extends Database {
"`location_id` integer NOT NULL," +
"`name` varchar(16) NOT NULL UNIQUE," +
"`description` varchar(255) NOT NULL," +
"`creation_time` timestamp NOT NULL," +
"`creation_time` timestamp NULL," +
"PRIMARY KEY (`location_id`)," +
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,17 @@ public static void upgradeDatabase() {
plugin.getLogger().info("Database upgrade needed: Adding creation timestamps to homes and warps...");
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
"ALTER TABLE " + HuskHomes.getSettings().getHomesDataTable()
+ " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) {
+ " ADD `creation_time` timestamp NULL DEFAULT NULL;")) {
tableUpdateStatement.execute();
try (PreparedStatement timeSettingStatement = HuskHomes.getConnection().prepareStatement(
"UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) {
timeSettingStatement.executeUpdate();
}
plugin.getLogger().info("Database upgrade in progress... (1/2)");
} catch (SQLException e) {
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
e.printStackTrace();
}
try (PreparedStatement tableUpdateStatement = HuskHomes.getConnection().prepareStatement(
"ALTER TABLE " + HuskHomes.getSettings().getWarpsDataTable()
+ " ADD `creation_time` timestamp NOT NULL DEFAULT 0;")) {
+ " ADD `creation_time` timestamp NULL DEFAULT NULL;")) {
tableUpdateStatement.execute();
try (PreparedStatement timeSettingStatement = HuskHomes.getConnection().prepareStatement(
"UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `creation_time`=CURRENT_TIMESTAMP;")) {
timeSettingStatement.executeUpdate();
}
plugin.getLogger().info("Database upgrade in progress... (2/2)");
} catch (SQLException e) {
plugin.getLogger().info("Skipped performing the database upgrade: " + e.getCause() + ". This might be because another server on your HuskHomes network already carried out the upgrade - in which case you can safely ignore this warning.");
Expand Down

0 comments on commit 7539094

Please sign in to comment.