123 lines
4.1 KiB
Java
123 lines
4.1 KiB
Java
|
package xyz.etztech.mixtape;
|
||
|
|
||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||
|
import xyz.etztech.core.db.DataSource;
|
||
|
|
||
|
import java.sql.Connection;
|
||
|
import java.sql.PreparedStatement;
|
||
|
import java.sql.ResultSet;
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.List;
|
||
|
|
||
|
public class Database extends DataSource {
|
||
|
|
||
|
public enum AliasType {
|
||
|
CHAT, COMMAND
|
||
|
}
|
||
|
|
||
|
public Database(JavaPlugin plugin) {
|
||
|
super(plugin);
|
||
|
create();
|
||
|
}
|
||
|
|
||
|
private void create() {
|
||
|
try {
|
||
|
Connection connection = getConnection();
|
||
|
final String CREATE = "CREATE TABLE IF NOT EXISTS `aliases` ( `id` INT AUTO_INCREMENT PRIMARY KEY, `uuid` VARCHAR(36) NOT NULL, `from` VARCHAR(200) NOT NULL, `to` VARCHAR(200) NOT NULL, `type` VARCHAR(6) NOT NULL );";
|
||
|
PreparedStatement pst = connection.prepareStatement(CREATE);
|
||
|
pst.execute();
|
||
|
connection.close();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void insert(String uuid, String from, String to, AliasType type) {
|
||
|
try {
|
||
|
int existingId = exists(uuid, from);
|
||
|
if (existingId > 0) {
|
||
|
delete(existingId);
|
||
|
}
|
||
|
Connection connection = getConnection();
|
||
|
final String MERGE = "INSERT INTO `aliases` (`uuid`, `from`, `to`, `type`) VALUES (?, ?, ?, ?);";
|
||
|
PreparedStatement pst = connection.prepareStatement(MERGE);
|
||
|
pst.setString(1, uuid);
|
||
|
pst.setString(2, from.toLowerCase());
|
||
|
pst.setString(3, to);
|
||
|
pst.setString(4, type.name());
|
||
|
pst.executeUpdate();
|
||
|
connection.close();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public void purge(String uuid) {
|
||
|
try {
|
||
|
Connection connection = getConnection();
|
||
|
final String PURGE = "DELETE FROM `aliases` WHERE `uuid` = ?;";
|
||
|
PreparedStatement pst = connection.prepareStatement(PURGE);
|
||
|
pst.setString(1, uuid);
|
||
|
pst.executeUpdate();
|
||
|
connection.close();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public List<Alias> getAliases(String uuid) {
|
||
|
List<Alias> results = new ArrayList<>();
|
||
|
try {
|
||
|
Connection connection = getConnection();
|
||
|
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ?;";
|
||
|
PreparedStatement pst = connection.prepareStatement(SELECT);
|
||
|
pst.setString(1, uuid);
|
||
|
ResultSet rs = pst.executeQuery();
|
||
|
while (rs.next()) {
|
||
|
Alias alias = new Alias();
|
||
|
alias.setUuid(uuid);
|
||
|
alias.setFrom(rs.getString("from"));
|
||
|
alias.setTo(rs.getString("to"));
|
||
|
alias.setType(AliasType.valueOf(rs.getString("type")));
|
||
|
results.add(alias);
|
||
|
}
|
||
|
connection.close();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
return results;
|
||
|
}
|
||
|
|
||
|
public int exists(String uuid, String from) {
|
||
|
try {
|
||
|
Connection connection = getConnection();
|
||
|
final String SELECT = "SELECT `id` FROM `aliases` WHERE `uuid` = ? AND `from` = ?;";
|
||
|
PreparedStatement pst = connection.prepareStatement(SELECT);
|
||
|
pst.setString(1, uuid);
|
||
|
pst.setString(2, from.toLowerCase());
|
||
|
ResultSet rs = pst.executeQuery();
|
||
|
if (rs.next()) {
|
||
|
return rs.getInt("id");
|
||
|
}
|
||
|
connection.close();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
public void delete(int id) {
|
||
|
try {
|
||
|
Connection connection = getConnection();
|
||
|
final String DELETE = "DELETE FROM `aliases` WHERE `id` = ?;";
|
||
|
PreparedStatement pst = connection.prepareStatement(DELETE);
|
||
|
pst.setInt(1, id);
|
||
|
pst.executeUpdate();
|
||
|
connection.close();
|
||
|
} catch (Exception ex) {
|
||
|
ex.printStackTrace();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|