This repository has been archived by the owner on Dec 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Always do release" This reverts commit a049eb6 Revert "Change visibility in signal and await method" This reverts commit b496cd4 Revert "Fix missing code by merge" This reverts commit edb375e Revert "Fix IllegalMonitorStateException" This reverts commit 8f4d83c Revert "Optimize getConnection logic to await-and-signal mode" This reverts commit a9eff9d Revert "#575 Handle ALL SQL operation in DatabaseManager to prevent connection competing issue" This reverts commit 37a2e90
- Loading branch information
Showing
17 changed files
with
585 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/org/maxgamer/quickshop/database/BufferStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* This file is a part of project QuickShop, the name is BufferStatement.java | ||
* Copyright (C) PotatoCraft Studio and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.maxgamer.quickshop.database; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.Arrays; | ||
|
||
public class BufferStatement { | ||
|
||
@NotNull | ||
private final String query; | ||
|
||
@NotNull | ||
private final Object[] values; | ||
|
||
/** | ||
* Represents a PreparedStatement in a state before preparing it (E.g. No file I/O Required) | ||
* | ||
* @param query The query to execute. E.g. INSERT INTO accounts (user, passwd) VALUES (?, ?) | ||
* @param values The values to replace ? with in query. These are in order. | ||
*/ | ||
public BufferStatement(@NotNull String query, @NotNull Object... values) { | ||
this.query = query; | ||
this.values = values; | ||
} | ||
|
||
/** | ||
* @return A string representation of this statement. Returns "Query: " + query + ", values: " + | ||
* Arrays.toString(values). | ||
*/ | ||
@Override | ||
public String toString() { | ||
return "Query: " + query + ", values: " + Arrays.toString(values); | ||
} | ||
|
||
/** | ||
* Returns a prepared statement using the given connection. Will try to return an empty statement | ||
* if something went wrong. If that fails, returns null. | ||
* | ||
* <p>This method escapes everything automatically. | ||
* | ||
* @param con The connection to prepare this on using con.prepareStatement(..) | ||
* @return The prepared statement, ready for execution. | ||
* @throws SQLException Throw exception when failed to execute something in SQL | ||
*/ | ||
PreparedStatement prepareStatement(@NotNull Connection con) throws SQLException { | ||
PreparedStatement ps; | ||
ps = con.prepareStatement(query); | ||
for (int i = 1; i <= values.length; i++) { | ||
ps.setObject(i, values[i - 1]); | ||
} | ||
return ps; | ||
} | ||
|
||
} |
153 changes: 153 additions & 0 deletions
153
src/main/java/org/maxgamer/quickshop/database/Database.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* This file is a part of project QuickShop, the name is Database.java | ||
* Copyright (C) PotatoCraft Studio and contributors | ||
* | ||
* This program is free software: you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as published by the | ||
* Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | ||
* for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
*/ | ||
|
||
package org.maxgamer.quickshop.database; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
import java.sql.*; | ||
|
||
public class Database { | ||
|
||
@NotNull | ||
private final DatabaseCore core; | ||
|
||
/** | ||
* Creates a new database and validates its connection. | ||
* | ||
* <p>If the connection is invalid, this will throw a ConnectionException. | ||
* | ||
* @param core The core for the database, either MySQL or SQLite. | ||
* @throws ConnectionException If the connection was invalid | ||
*/ | ||
public Database(@NotNull DatabaseCore core) throws ConnectionException { | ||
try { | ||
try { | ||
if (!core.getConnection().isValid(30)) { | ||
throw new ConnectionException("The database does not appear to be valid!"); | ||
} | ||
} catch (AbstractMethodError ignored) { | ||
// You don't need to validate this core. | ||
} | ||
} catch (SQLException e) { | ||
throw new ConnectionException(e.getMessage()); | ||
} | ||
this.core = core; | ||
} | ||
|
||
/** | ||
* Closes the database | ||
*/ | ||
public void close() { | ||
this.core.close(); | ||
} | ||
|
||
/** | ||
* Executes the given statement either immediately, or soon. | ||
* | ||
* @param query The query | ||
* @param objs The string values for each ? in the given query. | ||
*/ | ||
public void execute(@NotNull String query, @NotNull Object... objs) { | ||
BufferStatement bs = new BufferStatement(query, objs); | ||
core.queue(bs); | ||
} | ||
|
||
/** | ||
* Returns true if the given table has the given column | ||
* | ||
* @param table The table | ||
* @param column The column | ||
* @return True if the given table has the given column | ||
* @throws SQLException If the database isn't connected | ||
*/ | ||
public boolean hasColumn(@NotNull String table, @NotNull String column) throws SQLException { | ||
if (!hasTable(table)) { | ||
return false; | ||
} | ||
String query = "SELECT * FROM " + table + " LIMIT 1"; | ||
try { | ||
PreparedStatement ps = this.getConnection().prepareStatement(query); | ||
ResultSet rs = ps.executeQuery(); | ||
ResultSetMetaData metaData = rs.getMetaData(); | ||
for (int i = 1; i <= metaData.getColumnCount(); i++) { | ||
if (metaData.getColumnLabel(i).equals(column)) { | ||
return true; | ||
} | ||
} | ||
rs.close(); | ||
} catch (SQLException e) { | ||
return false; | ||
} | ||
return false; // Uh, wtf. | ||
} | ||
|
||
/** | ||
* Returns true if the table exists | ||
* | ||
* @param table The table to check for | ||
* @return True if the table is found | ||
* @throws SQLException Throw exception when failed execute somethins on SQL | ||
*/ | ||
@SuppressWarnings("BooleanMethodIsAlwaysInverted") | ||
boolean hasTable(@NotNull String table) throws SQLException { | ||
ResultSet rs = getConnection().getMetaData().getTables(null, null, "%", null); | ||
while (rs.next()) { | ||
if (table.equalsIgnoreCase(rs.getString("TABLE_NAME"))) { | ||
rs.close(); | ||
return true; | ||
} | ||
} | ||
rs.close(); | ||
return false; | ||
} | ||
|
||
/** | ||
* Fetches the connection to this database for querying. Try to avoid doing this in the main | ||
* thread. | ||
* | ||
* @return Fetches the connection to this database for querying. | ||
*/ | ||
public Connection getConnection() { | ||
return core.getConnection(); | ||
} | ||
|
||
/** | ||
* Returns the database core object, that this database runs on. | ||
* | ||
* @return the database core object, that this database runs on. | ||
*/ | ||
@NotNull | ||
public DatabaseCore getCore() { | ||
return core; | ||
} | ||
|
||
/** | ||
* Represents a connection error, generally when the server can't connect to MySQL or something. | ||
*/ | ||
public static final class ConnectionException extends Exception { | ||
private static final long serialVersionUID = 8348749992936357317L; | ||
|
||
private ConnectionException(String msg) { | ||
super(msg); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.