Updated SQL handling

This commit is contained in:
BuildTools committed 2020-12-08 17:56:03 +00:00
1 parent 559e38fc4b
commit 9d902a8453
4 files changed
+138 -94

No files matched your search

@@ -14,9 +14,7 @@ import org.bukkit.entity.Player;
public abstract class Database { public abstract class Database {
Main plugin; Main plugin;
Connection connection; Connection connection;
// The name of the table we created back in SQLite class.
public String table = "table_name";
public int tokens = 0;
public Database(Main instance){ public Database(Main instance){
plugin = instance; plugin = instance;
} }
@@ -28,31 +26,24 @@ public abstract class Database {
public void initialize(){ public void initialize(){
connection = getSQLConnection(); connection = getSQLConnection();
try{ try{
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE player = ?"); PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE uuid = ?");
ResultSet rs = ps.executeQuery(); ResultSet rs = ps.executeQuery();
close(ps,rs); close(ps,rs);
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Unable to retreive connection", ex); plugin.getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
} }
} }
// These are the methods you can use to get things out of your database. You of course can make new ones to return different things in the database. public ResultSet queryDatabase(String query) {
// This returns the number of people the player killed.
public Integer getTokens(String string) {
Connection conn = null; Connection conn = null;
PreparedStatement ps = null; PreparedStatement ps = null;
ResultSet rs = null; ResultSet rs;
try { try {
conn = getSQLConnection(); conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';"); ps = conn.prepareStatement(query);
rs = ps.executeQuery(); rs = ps.executeQuery();
while(rs.next()){ return rs;
if(rs.getString("player").equalsIgnoreCase(string.toLowerCase())){ // Tell database to search for the player you sent into the method. e.g getTokens(sam) It will look for sam.
return rs.getInt("kills"); // Return the players ammount of kills. If you wanted to get total (just a random number for an example for you guys) You would change this to total!
}
}
} catch (SQLException ex) { } catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally { } finally {
@@ -65,37 +56,11 @@ public abstract class Database {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
} }
} }
return 0; return null;
} }
// Exact same method here, Except as mentioned above i am looking for total!
public Integer getTotal(String string) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getSQLConnection();
ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';");
rs = ps.executeQuery();
while(rs.next()){
if(rs.getString("player").equalsIgnoreCase(string.toLowerCase())){
return rs.getInt("total");
}
}
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
} finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
}
}
return 0;
}
// Now we need methods to save things to the database // Now we need methods to save things to the database
public void setTokens(Player player, Integer tokens, Integer total) { public void setTokens(Player player, Integer tokens, Integer total) {
@@ -133,7 +98,7 @@ public abstract class Database {
} }
public void close(PreparedStatement ps,ResultSet rs){ public void close(PreparedStatement ps, ResultSet rs){
try { try {
if (ps != null) if (ps != null)
ps.close(); ps.close();
@@ -1,6 +1,7 @@
package me.william278.huskhomes2.Data.SQLite; package me.william278.huskhomes2.Data.SQLite;
import me.william278.huskhomes2.Main; import me.william278.huskhomes2.Main;
import me.william278.huskhomes2.dataManager;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
@@ -18,48 +19,6 @@ public class SQLite extends Database {
super(instance); super(instance);
} }
public String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerTable() + " (" +
"`player_id` integer PRIMARY KEY," +
"`uuid` text NOT NULL UNIQUE," +
"`username` text NOT NULL," +
"`home_count` integer NOT NULL," +
"`home_slots` integer NOT NULL," +
"`rtp_cooldown` integer NOT NULL," +
"`home_count` integer NOT NULL" +
");";
public String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsTable() + " (" +
"`location_id` integer PRIMARY KEY," +
"`world` text NOT NULL," +
"`server` text NOT NULL," +
"`x` double NOT NULL," +
"`y` double NOT NULL," +
"`z` double NOT NULL," +
"`yaw` float NOT NULL," +
"`pitch` float NOT NULL" +
");";
public String createHomesTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getHomesTable() + " (" +
"`player_id` integer NOT NULL," +
"`location_id` integer NOT NULL," +
"`name` text NOT NULL," +
"`description` double NOT NULL," +
"`public` boolean NOT NULL," +
"PRIMARY KEY (`player_id`, `location_id`)," +
"FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
");";
public String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsTable() + " (" +
"`location_id` integer NOT NULL," +
"`name` text NOT NULL," +
"`description` double NOT NULL," +
"PRIMARY KEY (`location_id`)," +
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
");";
// SQL creation stuff, You can leave the blow stuff untouched. // SQL creation stuff, You can leave the blow stuff untouched.
public Connection getSQLConnection() { public Connection getSQLConnection() {
File dataFolder = new File(plugin.getDataFolder(), databaseName + ".db"); File dataFolder = new File(plugin.getDataFolder(), databaseName + ".db");
@@ -89,10 +48,10 @@ public class SQLite extends Database {
connection = getSQLConnection(); connection = getSQLConnection();
try { try {
Statement s = connection.createStatement(); Statement s = connection.createStatement();
s.executeUpdate(createPlayerTable); s.executeUpdate(dataManager.createPlayerTable);
s.executeUpdate(createLocationsTable); s.executeUpdate(dataManager.createLocationsTable);
s.executeUpdate(createHomesTable); s.executeUpdate(dataManager.createHomesTable);
s.executeUpdate(createWarpsTable); s.executeUpdate(dataManager.createWarpsTable);
s.close(); s.close();
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
@@ -24,6 +24,14 @@ public class Home extends TeleportationPoint {
this.isPublic = isPublic; this.isPublic = isPublic;
} }
public Home(TeleportationPoint teleportationPoint, String ownerUsername, String ownerUUID, String name, String description, boolean isPublic) {
super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server);
this.ownerUsername = ownerUsername;
this.ownerUUID = UUID.fromString(ownerUUID);
this.name = name;
this.isPublic = isPublic;
}
public boolean isPublic() { public boolean isPublic() {
return isPublic; return isPublic;
} }
@@ -2,11 +2,58 @@ package me.william278.huskhomes2;
import me.william278.huskhomes2.Data.SQLite.Database; import me.william278.huskhomes2.Data.SQLite.Database;
import me.william278.huskhomes2.Data.SQLite.SQLite; import me.william278.huskhomes2.Data.SQLite.SQLite;
import me.william278.huskhomes2.Objects.Home;
import me.william278.huskhomes2.Objects.TeleportationPoint;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import java.sql.ResultSet;
import java.sql.SQLException;
// This class handles the saving of data; whether that be through SQLite or SQL // This class handles the saving of data; whether that be through SQLite or SQL
public class dataManager { public class dataManager {
public static String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerTable() + " (" +
"`player_id` integer PRIMARY KEY," +
"`uuid` text NOT NULL UNIQUE," +
"`username` text NOT NULL," +
"`home_count` integer NOT NULL," +
"`home_slots` integer NOT NULL," +
"`rtp_cooldown` integer NOT NULL," +
"`home_count` integer NOT NULL" +
");";
public static String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsTable() + " (" +
"`location_id` integer PRIMARY KEY," +
"`world` text NOT NULL," +
"`server` text NOT NULL," +
"`x` double NOT NULL," +
"`y` double NOT NULL," +
"`z` double NOT NULL," +
"`yaw` float NOT NULL," +
"`pitch` float NOT NULL" +
");";
public static String createHomesTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getHomesTable() + " (" +
"`player_id` integer NOT NULL," +
"`location_id` integer NOT NULL," +
"`name` text NOT NULL," +
"`description` double NOT NULL," +
"`public` boolean NOT NULL," +
"PRIMARY KEY (`player_id`, `location_id`)," +
"FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
");";
public static String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsTable() + " (" +
"`location_id` integer NOT NULL," +
"`name` text NOT NULL," +
"`description` double NOT NULL," +
"PRIMARY KEY (`location_id`)," +
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
");";
private static Database sqliteDatabase; private static Database sqliteDatabase;
private static void initializeSQLite() { private static void initializeSQLite() {
@@ -18,10 +65,75 @@ public class dataManager {
} }
public ResultSet queryDatabase(String query) {
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) {
return null;
} else {
return sqliteDatabase.queryDatabase(query);
}
}
public Home getHome(String ownerUsername, String homeName) {
try {
Integer playerID = getPlayerId(ownerUsername);
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + (playerID) + " AND `name`=" + homeName +";");
if (rs.next()) {
return new Home(getTransportationPoint(rs.getInt("location_id")), ownerUsername, getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), rs.getBoolean("public"));
} else {
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
return null;
}
}
private Integer getPlayerId(String playerUsername) {
try {
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=" + (playerUsername) + ";");
if (rs.next()) {
return rs.getInt("player_id");
} else {
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a player ID!");
return null;
}
}
public String getPlayerUUID(int playerID) {
try {
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `player_id`=" + (playerID) + ";");
if (rs.next()) {
return rs.getString("uuid");
} else {
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a player UUID!");
return null;
}
}
public TeleportationPoint getTransportationPoint(int locationID) {
try {
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `location_id`=" + (locationID) + ";");
if (rs.next()) {
return new TeleportationPoint(rs.getString("world"), rs.getDouble("x"), rs.getDouble("y"), rs.getDouble("z"), rs.getFloat("yaw"), rs.getFloat("pitch"), rs.getString("server"));
} else {
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a transportation point!");
return null;
}
}
public static void setupStorage(String storageType) { public static void setupStorage(String storageType) {
if (storageType.toLowerCase().equals("sqlite")) { if (storageType.equalsIgnoreCase("sqlite")) {
initializeSQLite(); initializeSQLite();
} else if (storageType.toLowerCase().equals("mysql")) { } else if (storageType.equalsIgnoreCase("mysql")) {
initializeMySQL(); initializeMySQL();
} else { } else {
Bukkit.getLogger().warning("Invalid storage method set in config.yml; defaulting to SQLite"); Bukkit.getLogger().warning("Invalid storage method set in config.yml; defaulting to SQLite");