PandaLib/Core/src/main/java/fr/pandacube/lib/core/players/SQLPlayerNameHistory.java

54 lines
1.4 KiB
Java

package fr.pandacube.lib.core.players;
import java.util.UUID;
import fr.pandacube.lib.db.DB;
import fr.pandacube.lib.db.DBException;
import fr.pandacube.lib.db.SQLElement;
import fr.pandacube.lib.db.SQLFKField;
import fr.pandacube.lib.db.SQLField;
import fr.pandacube.lib.util.Log;
public class SQLPlayerNameHistory extends SQLElement<SQLPlayerNameHistory> {
public SQLPlayerNameHistory() {
super();
}
public SQLPlayerNameHistory(int id) {
super(id);
}
@Override
protected String tableName() {
return "player_name_history";
}
public static final SQLFKField<SQLPlayerNameHistory, UUID, SQLPlayer> playerId = foreignKey(false, SQLPlayer.class, SQLPlayer.playerId);
public static final SQLField<SQLPlayerNameHistory, String> playerName = field(VARCHAR(16), false);
public static final SQLField<SQLPlayerNameHistory, Long> timeChanged = field(BIGINT, true);
public static void updateIfNeeded(UUID player, String name, long time) {
SQLPlayerNameHistory histEl;
try {
histEl = DB.getFirst(SQLPlayerNameHistory.class, playerId.eq(player).and(playerName.eq(name)));
if (histEl == null) {
histEl = new SQLPlayerNameHistory();
histEl.set(playerId, player);
histEl.set(playerName, name);
histEl.set(timeChanged, time);
histEl.save();
}
else if (time < histEl.get(timeChanged)) {
histEl.set(timeChanged, time);
histEl.save();
}
} catch (DBException e) {
Log.severe(e);
}
}
}