mirror of
https://github.com/TotalFreedomMC/TFGuilds.git
synced 2024-06-24 06:50:34 +00:00
123 lines
3.5 KiB
Java
123 lines
3.5 KiB
Java
package me.totalfreedom.tfguilds.sql;
|
|
|
|
import java.sql.Connection;
|
|
import java.sql.PreparedStatement;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import me.totalfreedom.tfguilds.TFGuilds;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.World;
|
|
|
|
public class SQLWorldData
|
|
{
|
|
private static final TFGuilds plugin = TFGuilds.getPlugin();
|
|
|
|
private final String SELECT = "SELECT * FROM `worlds` WHERE name=?";
|
|
private final String SELECT_ID = "SELECT * FROM `worlds` WHERE id=?";
|
|
private final String INSERT = "INSERT INTO `worlds` (`id`, `name`) VALUES (?, ?);";
|
|
private final String COUNT = "SELECT COUNT(*) AS rows FROM `worlds`";
|
|
|
|
public boolean exists(World world)
|
|
{
|
|
Connection connection = plugin.sql.getConnection();
|
|
try
|
|
{
|
|
PreparedStatement statement = connection.prepareStatement(SELECT);
|
|
statement.setString(1, world.getName());
|
|
ResultSet set = statement.executeQuery();
|
|
return set.next();
|
|
}
|
|
catch (SQLException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public boolean existsID(int id)
|
|
{
|
|
Connection connection = plugin.sql.getConnection();
|
|
try
|
|
{
|
|
PreparedStatement statement = connection.prepareStatement(SELECT_ID);
|
|
statement.setInt(1, id);
|
|
ResultSet set = statement.executeQuery();
|
|
return set.next();
|
|
}
|
|
catch (SQLException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int getWorldID(World world)
|
|
{
|
|
Connection connection = plugin.sql.getConnection();
|
|
try
|
|
{
|
|
if (!exists(world))
|
|
{
|
|
PreparedStatement statement = connection.prepareStatement(INSERT);
|
|
statement.setInt(1, getWorldCount() + 1);
|
|
statement.setString(2, world.getName());
|
|
statement.execute();
|
|
}
|
|
PreparedStatement statement = connection.prepareStatement(SELECT);
|
|
statement.setString(1, world.getName());
|
|
ResultSet set = statement.executeQuery();
|
|
set.next();
|
|
int id = set.getInt("id");
|
|
set.close();
|
|
return id;
|
|
}
|
|
catch (SQLException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
public World getWorld(int id)
|
|
{
|
|
Connection connection = plugin.sql.getConnection();
|
|
try
|
|
{
|
|
if (!existsID(id))
|
|
{
|
|
return null;
|
|
}
|
|
PreparedStatement statement = connection.prepareStatement(SELECT_ID);
|
|
statement.setInt(1, id);
|
|
ResultSet set = statement.executeQuery();
|
|
set.next();
|
|
String name = set.getString("name");
|
|
set.close();
|
|
return Bukkit.getWorld(name);
|
|
}
|
|
catch (SQLException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public int getWorldCount()
|
|
{
|
|
Connection connection = plugin.sql.getConnection();
|
|
try
|
|
{
|
|
PreparedStatement statement = connection.prepareStatement(COUNT);
|
|
ResultSet set = statement.executeQuery();
|
|
set.next();
|
|
int count = set.getInt("rows");
|
|
set.close();
|
|
return count;
|
|
}
|
|
catch (SQLException ex)
|
|
{
|
|
ex.printStackTrace();
|
|
}
|
|
return 0;
|
|
}
|
|
} |