165 lines
5.1 KiB
Java
165 lines
5.1 KiB
Java
package eu.oskar3123.spigot2fa.config;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.util.UUID;
|
|
|
|
public class SQLPlayersConfiguration implements PlayersConfiguration
|
|
{
|
|
|
|
private String hostname;
|
|
private String port;
|
|
private String database;
|
|
private String username;
|
|
private String password;
|
|
private String tableName;
|
|
private Connection connection;
|
|
|
|
public SQLPlayersConfiguration(String hostname, String port, String database, String username, String password, String tablePrefix) throws ClassNotFoundException, SQLException
|
|
{
|
|
this.hostname = hostname;
|
|
this.port = port;
|
|
this.database = database;
|
|
this.username = username;
|
|
this.password = password;
|
|
Class.forName("com.mysql.jdbc.Driver");
|
|
this.tableName = tablePrefix + "playerdata";
|
|
if (!connect(10))
|
|
{
|
|
throw new SQLException("Failed to connect");
|
|
}
|
|
init();
|
|
}
|
|
|
|
private boolean connect(int retries)
|
|
{
|
|
retries--;
|
|
try
|
|
{
|
|
if (connection != null && connection.isValid(1))
|
|
{
|
|
return true;
|
|
}
|
|
connection = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s", this.hostname, this.port, this.database), this.username, this.password);
|
|
return true;
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
if (retries > 0)
|
|
{
|
|
return connect(retries);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private void init() throws SQLException
|
|
{
|
|
String createSql = "CREATE TABLE IF NOT EXISTS `" + this.tableName + "` ( \n" +
|
|
" `uuid` VARCHAR(36) NOT NULL , \n" +
|
|
" `secret` VARCHAR(32) NOT NULL , \n" +
|
|
" `lastip` VARCHAR(15) NOT NULL , \n" +
|
|
" `lastlogin` BIGINT NOT NULL , \n" +
|
|
" PRIMARY KEY (`uuid`)\n" +
|
|
") ENGINE = InnoDB;";
|
|
connection.createStatement().executeUpdate(createSql);
|
|
}
|
|
|
|
@Override
|
|
public boolean hasData(UUID uuid)
|
|
{
|
|
try
|
|
{
|
|
if (!connect(10))
|
|
{
|
|
return false;
|
|
}
|
|
String querySql = "SELECT COUNT(`uuid`) FROM `" + this.tableName + "` WHERE `uuid`='" + uuid.toString() + "';";
|
|
ResultSet result = connection.createStatement().executeQuery(querySql);
|
|
if (result.next())
|
|
{
|
|
return result.getLong(1) > 0;
|
|
|
|
}
|
|
return false;
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public PlayersData getData(UUID uuid)
|
|
{
|
|
try
|
|
{
|
|
if (!connect(10))
|
|
{
|
|
return null;
|
|
}
|
|
String querySql = "SELECT `secret`, `lastip`, `lastlogin` FROM `" + this.tableName + "` WHERE `uuid`='" + uuid.toString() + "';";
|
|
ResultSet result = connection.createStatement().executeQuery(querySql);
|
|
if (!result.next())
|
|
{
|
|
return null;
|
|
}
|
|
String secret = result.getString("secret");
|
|
String lastIp = result.getString("lastip");
|
|
long lastLogin = result.getLong("lastlogin");
|
|
return new PlayersData(uuid, secret, lastIp, lastLogin);
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean setData(UUID uuid, PlayersData data)
|
|
{
|
|
try
|
|
{
|
|
if (!connect(10))
|
|
{
|
|
return false;
|
|
}
|
|
if (data == null)
|
|
{
|
|
String deleteSql = "DELETE FROM `" + this.tableName + "` WHERE `uuid`='" + uuid.toString() + "';";
|
|
System.out.println(deleteSql);
|
|
connection.isValid(1);
|
|
connection.createStatement().executeUpdate(deleteSql);
|
|
return true;
|
|
}
|
|
if (hasData(uuid))
|
|
{
|
|
String updateSql = "UPDATE `" + this.tableName + "` SET `secret`='" +
|
|
data.getSecret() + "', `lastip`='" +
|
|
data.getLastIP() + "', `lastlogin`=" +
|
|
data.getLastLogin() + " WHERE `uuid`='" +
|
|
data.getUUID().toString() + "';";
|
|
System.out.println(updateSql);
|
|
connection.createStatement().executeUpdate(updateSql);
|
|
return true;
|
|
}
|
|
String insertSql = "INSERT INTO `" + this.tableName + "` (`uuid`, `secret`, `lastip`, `lastlogin`) VALUES (" +
|
|
"'" + uuid.toString() + "', " +
|
|
"'" + data.getSecret() + "', " +
|
|
"'" + data.getLastIP() + "', " +
|
|
data.getLastLogin() + ");";
|
|
System.out.println(insertSql);
|
|
connection.createStatement().executeUpdate(insertSql);
|
|
return true;
|
|
}
|
|
catch (SQLException e)
|
|
{
|
|
System.out.println("setData failed");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
}
|