Skip to content

Commit

Permalink
massive sql improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
svenwiltink committed Sep 6, 2019
1 parent 670327d commit 3142231
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions src/main/java/modreq/managers/TicketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,19 @@

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class TicketHandler {

public static ModReq plugin = ModReq.getInstance();
private Connection connection;
private static final Logger logger = Logger.getLogger("Minecraft");
private boolean useMysql;

public TicketHandler() {
useMysql = plugin.getConfig().getBoolean("use-mysql");
}

private Connection getConnection() {
try {
Expand All @@ -43,7 +49,7 @@ private Connection getConnection() {
}
}
Class.forName("org.sqlite.JDBC");
if (plugin.getConfig().getBoolean("use-mysql")) {
if (useMysql) {
String ip = plugin.getConfig().getString("mysql.ip");
String user = plugin.getConfig().getString("mysql.user");
String pass = plugin.getConfig().getString("mysql.pass");
Expand All @@ -52,21 +58,22 @@ private Connection getConnection() {
connection = DriverManager.getConnection("jdbc:mysql://"
+ ip, user, pass);
Statement stat = connection.createStatement();
stat.execute("CREATE TABLE IF NOT EXISTS " + table1 + " (id INT, submitter TEXT, message TEXT, date TEXT, status TEXT, location TEXT, staff TEXT)");
stat.execute("CREATE TABLE IF NOT EXISTS " + table2 + " (id INT, commenter TEXT, message TEXT, date TEXT)");
stat.execute("CREATE TABLE IF NOT EXISTS " + table1 + " (id INTEGER NOT NULL AUTO_INCREMENT, submitter TEXT, message TEXT, date TEXT, status TEXT, location TEXT, staff TEXT, PRIMARY KEY(id))");
stat.execute("CREATE TABLE IF NOT EXISTS " + table2 + " (id INTEGER, commenter TEXT, message TEXT, date TEXT)");
KillConnection();
return connection;
} else {
connection = DriverManager
.getConnection("jdbc:sqlite:plugins/ModReq/DataBase.sql");
Statement stat = connection.createStatement();
stat.execute("CREATE TABLE IF NOT EXISTS requests (id int, submitter String, message String, date String, status String, location String, staff String)");
stat.execute("CREATE TABLE IF NOT EXISTS comments (id int, commenter String, message String, date String)");
stat.execute("CREATE TABLE IF NOT EXISTS requests (id INTEGER PRIMARY KEY AUTOINCREMENT, submitter TEXT, message TEXT, date TEXT, status TEXT, location TEXT, staff TEXT)");
stat.execute("CREATE TABLE IF NOT EXISTS comments (id INTEGER, commenter String, message String, date String)");
KillConnection();
return connection;
}

} catch (Exception e) {
e.printStackTrace();
logger.severe("[ModReq] no connection could be made with the database. Shutting down plugin D:");
plugin.getServer().getPluginManager().disablePlugin(plugin);
return null;
Expand Down Expand Up @@ -153,7 +160,7 @@ public void sendPlayerPage(int page, Status status, Player p) {
try {
Connection conn = getConnection();
Statement stat = conn.createStatement();
ArrayList<Integer> tickets = new ArrayList<Integer>();
List<Ticket> tickets = new ArrayList<Ticket>();
int nmbr = page * 10;
ResultSet resultOC;//Result of open and closed tickets
ResultSet resultP;//Result of pending tickets
Expand All @@ -172,7 +179,7 @@ public void sendPlayerPage(int page, Status status, Player p) {
break;
}

tickets.add(resultP.getInt(1));
tickets.add(getTicketByResultSet(resultP));
}
}
resultOC = stat.executeQuery("SELECT * FROM requests WHERE " + statusSelect.toString() + " limit " + nmbr);
Expand All @@ -182,15 +189,16 @@ public void sendPlayerPage(int page, Status status, Player p) {
.executeQuery("SELECT * FROM requests WHERE status = '"
+ status.getStatusString() + "' limit " + nmbr);
}

while (resultOC.next()) {
if (resultOC.getRow() > nmbr - 10 && tickets.size() < 10) {
tickets.add(resultOC.getInt(1));
tickets.add(getTicketByResultSet(resultOC));
}
}
p.sendMessage(ChatColor.GOLD + "-----List-of-"
+ status.getStatusString() + "-Requests-----");
for (int i = 0; i < tickets.size(); i++) {
getTicketById(tickets.get(i)).sendSummarytoPlayer(p);
for (Ticket t : tickets) {
t.sendSummarytoPlayer(p);
}
p.sendMessage(ChatColor.GOLD + "do /check <page> to see more");
} catch (SQLException e) {
Expand Down Expand Up @@ -236,19 +244,28 @@ public int addTicket(String submitter, String message, String date,
Status status, String location) throws SQLException {
Connection conn = getConnection();

PreparedStatement prep = conn.prepareStatement("INSERT INTO requests VALUES (?, ?, ?, ?, ?,?,?)");
int id = getTicketCount() + 1;
prep.setInt(1, id);
prep.setString(2, submitter);
prep.setString(3, message);
prep.setString(4, date);
prep.setString(5, status.getStatusString());
prep.setString(6, location);
prep.setString(7, "no staff member yet");
PreparedStatement prep = conn.prepareStatement("INSERT INTO requests (submitter, message, date, status, location, staff) VALUES (?, ?, ?, ?, ?, ?)");
prep.setString(1, submitter);
prep.setString(2, message);
prep.setString(3, date);
prep.setString(4, status.getStatusString());
prep.setString(5, location);
prep.setString(6, "no staff member yet");
prep.addBatch();

prep.executeBatch();
return id;

Statement statement = conn.createStatement();
String query = "select last_insert_rowid()";
if (useMysql) {
query = "SELECT LAST_INSERT_ID()";
}
ResultSet set = statement.executeQuery(query);
if (!set.next()) {
throw new SQLException("not data returned by " + query);
}

return set.getInt(1);
}

public Ticket getTicketById(int id) {
Expand Down Expand Up @@ -315,17 +332,18 @@ private void addCommentsToTicket(Connection conn, Ticket t)
}

/**
* TODO: simplify. This should append the last comment if it does not yet exist
* TODO: simplify. This should append the last comment if it does not yet exist. Needs a schema change though
*/
private void updateComments(Connection conn, Ticket t) throws SQLException {
if (t.getComments().isEmpty()) {
return;
}
PreparedStatement prep = conn
.prepareStatement("INSERT INTO comments VALUES (?, ?, ?, ?)");
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery("SELECT * FROM comments WHERE id = '"
+ t.getId() + "'");
PreparedStatement stat = conn.prepareStatement("SELECT * FROM comments WHERE id = ?");
stat.setInt(1, t.getId());
ResultSet rs = stat.executeQuery();

Comment A = new Comment();
while (rs.next()) {
String commenter = rs.getString(2);
Expand All @@ -350,6 +368,7 @@ private void updateComments(Connection conn, Ticket t) throws SQLException {
if (A.equalsComment(B)) {
return;
}

prep.setInt(1, t.getId());
prep.setString(2, B.getCommenter());
prep.setString(3, B.getComment());
Expand Down

0 comments on commit 3142231

Please sign in to comment.