Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Revert DatabaseHelper and Manager changes, because it broken
Browse files Browse the repository at this point in the history
Former-commit-id: 8b8aac9
  • Loading branch information
Ghost-chu committed Mar 2, 2020
1 parent d357755 commit 3f3aac1
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 173 deletions.
268 changes: 120 additions & 148 deletions src/main/java/org/maxgamer/quickshop/Database/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,40 +52,6 @@ public DatabaseHelper(@NotNull QuickShop plugin, @NotNull Database db) throws SQ
checkColumns();
}

/**
* Creates the database table 'shops'.
*/
private void createShopsTable() {
try {
Statement st = db.getConnection().createStatement();
String createTable = "CREATE TABLE " + QuickShop.instance
.getDbPrefix() + "shops (owner VARCHAR(255) NOT NULL, price double(32, 2) NOT NULL, itemConfig TEXT CHARSET utf8 NOT NULL, x INTEGER(32) NOT NULL, y INTEGER(32) NOT NULL, z INTEGER(32) NOT NULL, world VARCHAR(32) NOT NULL, unlimited boolean, type boolean, PRIMARY KEY (x, y, z, world) );";
st.execute(createTable);
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Creates the database table 'messages'
*
* @return Create failed or successed.
*/
private void createMessagesTable() {
try {
Statement st = db.getConnection().createStatement();
String createTable = "CREATE TABLE " + QuickShop.instance.getDbPrefix()
+ "messages (owner VARCHAR(255) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );";
if (plugin.getDatabase().getCore() instanceof MySQLCore) {
createTable = "CREATE TABLE " + QuickShop.instance.getDbPrefix()
+ "messages (owner VARCHAR(255) NOT NULL, message TEXT(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , time BIGINT(32) NOT NULL );";
}
st.execute(createTable);
} catch (SQLException e) {
e.printStackTrace();
}
}

/**
* Verifies that all required columns exist.
*/
Expand Down Expand Up @@ -119,83 +85,98 @@ private void checkColumns() {
//ignore
}
}

}

public void cleanMessage(long weekAgo) {
plugin.getDatabaseManager().add(() -> {
try {
//QuickShop.instance.getDB().execute("DELETE FROM " + QuickShop.instance
// .getDbPrefix() + "messages WHERE time < ?", weekAgo);
String sqlString = "DELETE FROM " + QuickShop.instance
.getDbPrefix() + "messages WHERE time < ?";
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setLong(1, weekAgo);
ps.execute();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
});
try {
//QuickShop.instance.getDB().execute("DELETE FROM " + QuickShop.instance
// .getDbPrefix() + "messages WHERE time < ?", weekAgo);
String sqlString = "DELETE FROM " + QuickShop.instance
.getDbPrefix() + "messages WHERE time < ?";
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setLong(1, weekAgo);
plugin.getDatabaseManager().add(ps);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}

public void cleanMessageForPlayer(@NotNull UUID player) {
plugin.getDatabaseManager().add(() -> {
try {
String sqlString = "DELETE FROM " + QuickShop.instance.getDbPrefix() + "messages WHERE owner = ?";
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, player.toString());
ps.execute();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
});
}

public boolean createShop(@NotNull String owner, double price, @NotNull ItemStack item, int unlimited, int shopType, @NotNull String world, int x, int y, int z) {
try {
removeShop(x, y, z, world); //First purge old exist shop before create new shop.
String sqlString = "INSERT INTO " + QuickShop.instance
.getDbPrefix() + "shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
//QuickShop.instance.getDB().execute(q, owner, price, Util.serialize(item), x, y, z, world, unlimited, shopType);
String sqlString = "DELETE FROM " + QuickShop.instance.getDbPrefix() + "messages WHERE owner = ?";
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, owner);
ps.setDouble(2, price);
ps.setString(3, Util.serialize(item));
ps.setInt(4, x);
ps.setInt(5, y);
ps.setInt(6, z);
ps.setString(7, world);
ps.setInt(8, unlimited);
ps.setInt(9, shopType);
return ps.execute();
} catch (SQLException e) {
e.printStackTrace();
return false;
ps.setString(1, player.toString());
plugin.getDatabaseManager().add(ps);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}

public void removeShop(int x, int y, int z, @NotNull String worldName) throws SQLException {
/**
* Creates the database table 'messages'
*
* @return Create failed or successed.
* @throws SQLException If the connection is invalid
*/
private boolean createMessagesTable() throws SQLException {
Statement st = db.getConnection().createStatement();
String createTable = "CREATE TABLE " + QuickShop.instance.getDbPrefix()
+ "messages (owner VARCHAR(255) NOT NULL, message TEXT(25) NOT NULL, time BIGINT(32) NOT NULL );";
if (plugin.getDatabase().getCore() instanceof MySQLCore) {
createTable = "CREATE TABLE " + QuickShop.instance.getDbPrefix()
+ "messages (owner VARCHAR(255) NOT NULL, message TEXT(25) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL , time BIGINT(32) NOT NULL );";
}
return st.execute(createTable);
}

public void createShop(@NotNull String owner, double price, @NotNull ItemStack item, int unlimited, int shopType, @NotNull String world, int x, int y, int z)
throws SQLException {
removeShop(x, y, z, world); //First purge old exist shop before create new shop.
String sqlString = "INSERT INTO " + QuickShop.instance
.getDbPrefix() + "shops (owner, price, itemConfig, x, y, z, world, unlimited, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
//QuickShop.instance.getDB().execute(q, owner, price, Util.serialize(item), x, y, z, world, unlimited, shopType);
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, owner);
ps.setDouble(2, price);
ps.setString(3, Util.serialize(item));
ps.setInt(4, x);
ps.setInt(5, y);
ps.setInt(6, z);
ps.setString(7, world);
ps.setInt(8, unlimited);
ps.setInt(9, shopType);
plugin.getDatabaseManager().add(ps);
}

/**
* Creates the database table 'shops'.
*
* @throws SQLException If the connection is invalid.
*/
private void createShopsTable() throws SQLException {
Statement st = db.getConnection().createStatement();
String createTable = "CREATE TABLE " + QuickShop.instance
.getDbPrefix() + "shops (owner VARCHAR(255) NOT NULL, price double(32, 2) NOT NULL, itemConfig TEXT CHARSET utf8 NOT NULL, x INTEGER(32) NOT NULL, y INTEGER(32) NOT NULL, z INTEGER(32) NOT NULL, world VARCHAR(32) NOT NULL, unlimited boolean, type boolean, PRIMARY KEY (x, y, z, world) );";
st.execute(createTable);
}

public boolean removeShop(int x, int y, int z, @NotNull String worldName) throws SQLException {
// db.getConnection().createStatement()
// .executeUpdate("DELETE FROM " + QuickShop.instance.getDbPrefix() + "shops WHERE x = " + x + " AND y = " + y
// + " AND z = " + z + " AND world = \"" + worldName + "\""
// + (db.getCore() instanceof MySQLCore ? " LIMIT 1" : ""));
plugin.getDatabaseManager().add(() -> {
try {
String sqlString = "DELETE FROM " + QuickShop.instance
.getDbPrefix() + "shops WHERE x = ? AND y = ? AND z = ? AND world = ?" + (db.getCore() instanceof MySQLCore ?
" LIMIT 1" :
"");

PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setInt(1, x);
ps.setInt(2, y);
ps.setInt(3, z);
ps.setString(4, worldName);
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
}
}
);
String sqlString = "DELETE FROM " + QuickShop.instance
.getDbPrefix() + "shops WHERE x = ? AND y = ? AND z = ? AND world = ?" + (db.getCore() instanceof MySQLCore ?
" LIMIT 1" :
"");

PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setInt(1, x);
ps.setInt(2, y);
ps.setInt(3, z);
ps.setString(4, worldName);
return ps.execute();
}

public ResultSet selectAllMessages() throws SQLException {
Expand All @@ -211,67 +192,58 @@ public ResultSet selectAllShops() throws SQLException {
}

public void sendMessage(@NotNull UUID player, @NotNull String message, long time) {
plugin.getDatabaseManager().add(() -> {
try {
String sqlString = "INSERT INTO " + QuickShop.instance
.getDbPrefix() + "messages (owner, message, time) VALUES (?, ?, ?)";
//QuickShop.instance.getDB().execute(q, player.toString(), message, System.currentTimeMillis());
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, player.toString());
ps.setString(2, message);
ps.setLong(3, time);
ps.execute();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
});
try {
String sqlString = "INSERT INTO " + QuickShop.instance
.getDbPrefix() + "messages (owner, message, time) VALUES (?, ?, ?)";
//QuickShop.instance.getDB().execute(q, player.toString(), message, System.currentTimeMillis());
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, player.toString());
ps.setString(2, message);
ps.setLong(3, time);
plugin.getDatabaseManager().add(ps);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}

public void updateOwner2UUID(@NotNull String ownerUUID, int x, int y, int z, @NotNull String worldName) {
public void updateOwner2UUID(@NotNull String ownerUUID, int x, int y, int z, @NotNull String worldName)
throws SQLException {
//QuickShop.instance.getDB().getConnection().createStatement()
// .executeUpdate("UPDATE " + QuickShop.instance.getDbPrefix() + "shops SET owner = \"" + ownerUUID.toString()
// + "\" WHERE x = " + x + " AND y = " + y + " AND z = " + z
// + " AND world = \"" + worldName + "\" LIMIT 1");
plugin.getDatabaseManager().add(() -> {
try {
String sqlString = "UPDATE " + QuickShop.instance
.getDbPrefix() + "shops SET owner = ? WHERE x = ? AND y = ? AND z = ? AND world = ?" + (db
.getCore() instanceof MySQLCore ? " LIMIT 1" : "");
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, ownerUUID);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
ps.setString(5, worldName);
ps.execute();
} catch (SQLException sqle) {
sqle.printStackTrace();
}
});
String sqlString = "UPDATE " + QuickShop.instance
.getDbPrefix() + "shops SET owner = ? WHERE x = ? AND y = ? AND z = ? AND world = ?" + (db
.getCore() instanceof MySQLCore ? " LIMIT 1" : "");
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, ownerUUID);
ps.setInt(2, x);
ps.setInt(3, y);
ps.setInt(4, z);
ps.setString(5, worldName);
plugin.getDatabaseManager().add(ps);
}

public void updateShop(@NotNull String owner, @NotNull ItemStack item, int unlimited, int shopType,
double price, int x, int y, int z, String world) {
plugin.getDatabaseManager().add(() -> {
try {
String sqlString = "UPDATE " + QuickShop.instance
.getDbPrefix() + "shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ? WHERE x = ? AND y = ? and z = ? and world = ?";
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, owner);
ps.setString(2, Util.serialize(item));
ps.setInt(3, unlimited);
ps.setInt(4, shopType);
ps.setDouble(5, price);
ps.setInt(6, x);
ps.setInt(7, y);
ps.setInt(8, z);
ps.setString(9, world);
ps.execute();
//db.execute(q, owner, Util.serialize(item), unlimited, shopType, price, x, y, z, world);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
});
}
try {
String sqlString = "UPDATE " + QuickShop.instance
.getDbPrefix() + "shops SET owner = ?, itemConfig = ?, unlimited = ?, type = ?, price = ? WHERE x = ? AND y = ? and z = ? and world = ?";
PreparedStatement ps = db.getConnection().prepareStatement(sqlString);
ps.setString(1, owner);
ps.setString(2, Util.serialize(item));
ps.setInt(3, unlimited);
ps.setInt(4, shopType);
ps.setDouble(5, price);
ps.setInt(6, x);
ps.setInt(7, y);
ps.setInt(8, z);
ps.setString(9, world);
plugin.getDatabaseManager().add(ps);
//db.execute(q, owner, Util.serialize(item), unlimited, shopType, price, x, y, z, world);
} catch (SQLException sqle) {
sqle.printStackTrace();
}

}
}
Loading

0 comments on commit 3f3aac1

Please sign in to comment.