Mixtape/src/main/java/xyz/etztech/mixtape/Database.java

155 lines
5.6 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(7) NOT NULL );";
PreparedStatement pst = connection.prepareStatement(CREATE);
pst.execute();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void insert(String uuid, String from, String to, AliasType type) {
try (Connection connection = getConnection()) {
int existingId = exists(uuid, from);
if (existingId > 0) {
delete(existingId);
}
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();
} 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();
} 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);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return results;
}
public List<Alias> getAliases(String uuid, AliasType type) {
List<Alias> results = new ArrayList<>();
try (Connection connection = getConnection()) {
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ? AND `type` = ?;";
PreparedStatement pst = connection.prepareStatement(SELECT);
pst.setString(1, uuid);
pst.setString(2, type.name());
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);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return results;
}
public Alias getAlias(String uuid, String from) {
try (Connection connection = getConnection()) {
final String SELECT = "SELECT `uuid`, `from`, `to`, `type` FROM `aliases` WHERE `uuid` = ? AND `from` = ?;";
PreparedStatement pst = connection.prepareStatement(SELECT);
pst.setString(1, uuid);
pst.setString(2, from);
ResultSet rs = pst.executeQuery();
if (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")));
return alias;
}
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
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");
}
} 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();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}