完善 SQL 工具类:
- SQLHost - SQLTable 通过 SQLTable 可以快速执行数据库操作并自动回收 PreparedStatement 和 ResultSet。 使用方式可在 SQLExample 中查看。 调整 TeanslateUUID 工具内的部分代码。
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package me.skymc.taboolib.mysql.builder;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-07-02 23:43
|
||||
*/
|
||||
public class SQLExample extends JavaPlugin {
|
||||
|
||||
private SQLHost sqlHost;
|
||||
private SQLTable sqlTable;
|
||||
private HikariDataSource dataSource;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
int value = sqlTable.executeQuery("select * from table where username = ?")
|
||||
.dataSource(dataSource)
|
||||
.statement(statement -> statement.setString(1, "BlackSKY"))
|
||||
.resultNext(result -> result.getInt("value"))
|
||||
.run(0, 0);
|
||||
|
||||
sqlTable.executeUpdate("statement table set value = ? where username = ?")
|
||||
.dataSource(dataSource)
|
||||
.statement(statement -> {
|
||||
statement.setInt(1, 999);
|
||||
statement.setString(2, "BlackSKY");
|
||||
}).run();
|
||||
}
|
||||
}
|
||||
@@ -74,16 +74,28 @@ public class SQLHost {
|
||||
return false;
|
||||
}
|
||||
SQLHost sqlHost = (SQLHost) o;
|
||||
return Objects.equals(getHost(), sqlHost.getHost()) && Objects.equals(getPort(), sqlHost.getPort()) && Objects.equals(getUser(), sqlHost.getUser()) && Objects.equals(getPassword(), sqlHost.getPassword()) && Objects.equals(getDatabase(), sqlHost.getDatabase());
|
||||
return Objects.equals(getHost(), sqlHost.getHost()) &&
|
||||
Objects.equals(getUser(), sqlHost.getUser()) &&
|
||||
Objects.equals(getPort(), sqlHost.getPort()) &&
|
||||
Objects.equals(getPassword(), sqlHost.getPassword()) &&
|
||||
Objects.equals(getDatabase(), sqlHost.getDatabase()) &&
|
||||
Objects.equals(getPlugin(), sqlHost.getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(getHost(), getUser(), getPort(), getPassword(), getDatabase());
|
||||
return Objects.hash(getHost(), getUser(), getPort(), getPassword(), getDatabase(), getPlugin());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return MessageFormat.format("SQLHost'{'host=''{0}'', user=''{1}'', port=''{2}'', password=''{3}'', database=''{4}'', plugin={5}'}'", host, user, port, password, database, plugin);
|
||||
return "SQLHost{" +
|
||||
"host='" + host + '\'' +
|
||||
", user='" + user + '\'' +
|
||||
", port='" + port + '\'' +
|
||||
", password='" + password + '\'' +
|
||||
", database='" + database + '\'' +
|
||||
", plugin=" + plugin +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ package me.skymc.taboolib.mysql.builder;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.ilummc.tlib.util.Strings;
|
||||
import me.skymc.taboolib.mysql.builder.query.RunnableQuery;
|
||||
import me.skymc.taboolib.mysql.builder.query.RunnableUpdate;
|
||||
import me.skymc.taboolib.string.ArrayUtils;
|
||||
|
||||
/**
|
||||
@@ -52,6 +54,14 @@ public class SQLTable {
|
||||
return Strings.replaceWithOrder("truncate table `{0}`", tableName);
|
||||
}
|
||||
|
||||
public RunnableUpdate executeUpdate(String query) {
|
||||
return new RunnableUpdate(query);
|
||||
}
|
||||
|
||||
public RunnableQuery executeQuery(String query) {
|
||||
return new RunnableQuery(query);
|
||||
}
|
||||
|
||||
// *********************************
|
||||
//
|
||||
// Getter and Setter
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
package me.skymc.taboolib.mysql.builder.query;
|
||||
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
import me.skymc.taboolib.mysql.builder.SQLExecutor;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-07-03 21:29
|
||||
*/
|
||||
public class RunnableQuery {
|
||||
|
||||
private DataSource dataSource;
|
||||
private TaskStatement statement;
|
||||
private TaskResult result;
|
||||
private TaskResult resultNext;
|
||||
private Connection connection;
|
||||
private boolean autoClose;
|
||||
private String query;
|
||||
|
||||
public RunnableQuery(String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public RunnableQuery dataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RunnableQuery statement(TaskStatement statement) {
|
||||
this.statement = statement;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RunnableQuery result(TaskResult result) {
|
||||
this.result = result;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RunnableQuery resultNext(TaskResult result) {
|
||||
this.resultNext = result;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RunnableQuery connection(Connection connection) {
|
||||
return connection(connection, false);
|
||||
}
|
||||
|
||||
public RunnableQuery connection(Connection connection, boolean autoClose) {
|
||||
this.connection = connection;
|
||||
this.autoClose = autoClose;
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T> T run(Object def, T translate) {
|
||||
Object object = run(def);
|
||||
return object == null ? def == null ? null : (T) def : (T) object;
|
||||
}
|
||||
|
||||
public Object run() {
|
||||
return run(null);
|
||||
}
|
||||
|
||||
public Object run(Object def) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
if (dataSource != null) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
preparedStatement = connection.prepareStatement(query);
|
||||
if (statement != null) {
|
||||
statement.execute(preparedStatement);
|
||||
}
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
return getResult(resultSet);
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
SQLExecutor.freeStatement(preparedStatement, resultSet);
|
||||
}
|
||||
} else if (connection != null) {
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(query);
|
||||
if (statement != null) {
|
||||
statement.execute(preparedStatement);
|
||||
}
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
return getResult(resultSet);
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
SQLExecutor.freeStatement(preparedStatement, resultSet);
|
||||
if (autoClose) {
|
||||
SQLExecutor.freeConnection(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
private void printException(Exception e) {
|
||||
TLogger.getGlobalLogger().error("An exception occurred in the database. (" + query + ")");
|
||||
TLogger.getGlobalLogger().error("Reason: " + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
private Object getResult(ResultSet resultSet) throws SQLException {
|
||||
if (resultNext != null && resultSet.next()) {
|
||||
return resultNext.execute(resultSet);
|
||||
} else if (result != null) {
|
||||
return result.execute(resultSet);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package me.skymc.taboolib.mysql.builder.query;
|
||||
|
||||
import com.ilummc.tlib.logger.TLogger;
|
||||
import me.skymc.taboolib.mysql.builder.SQLExecutor;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
|
||||
/**
|
||||
* F
|
||||
*
|
||||
* @Author sky
|
||||
* @Since 2018-07-03 21:29
|
||||
*/
|
||||
public class RunnableUpdate {
|
||||
|
||||
private DataSource dataSource;
|
||||
private TaskStatement statement;
|
||||
private Connection connection;
|
||||
private boolean autoClose;
|
||||
private String query;
|
||||
|
||||
public RunnableUpdate(String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public RunnableUpdate dataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RunnableUpdate statement(TaskStatement task) {
|
||||
this.statement = task;
|
||||
return this;
|
||||
}
|
||||
|
||||
public RunnableUpdate connection(Connection connection) {
|
||||
return connection(connection, false);
|
||||
}
|
||||
|
||||
public RunnableUpdate connection(Connection connection, boolean autoClose) {
|
||||
this.connection = connection;
|
||||
this.autoClose = autoClose;
|
||||
return this;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
PreparedStatement preparedStatement = null;
|
||||
if (dataSource != null) {
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
preparedStatement = connection.prepareStatement(query);
|
||||
if (statement != null) {
|
||||
statement.execute(preparedStatement);
|
||||
}
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
SQLExecutor.freeStatement(preparedStatement, null);
|
||||
}
|
||||
} else if (connection != null) {
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(query);
|
||||
if (statement != null) {
|
||||
statement.execute(preparedStatement);
|
||||
}
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (Exception e) {
|
||||
printException(e);
|
||||
} finally {
|
||||
SQLExecutor.freeStatement(preparedStatement, null);
|
||||
if (autoClose) {
|
||||
SQLExecutor.freeConnection(connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void printException(Exception e) {
|
||||
TLogger.getGlobalLogger().error("An exception occurred in the database. (" + query + ")");
|
||||
TLogger.getGlobalLogger().error("Reason: " + e.toString());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.skymc.taboolib.mysql.builder.query;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-07-03 22:02
|
||||
*/
|
||||
public interface TaskResult {
|
||||
|
||||
Object execute(ResultSet resultSet) throws SQLException;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.skymc.taboolib.mysql.builder.query;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
|
||||
/**
|
||||
* @Author sky
|
||||
* @Since 2018-07-03 22:02
|
||||
*/
|
||||
public interface TaskStatement {
|
||||
|
||||
void execute(PreparedStatement preparedStatement) throws SQLException;
|
||||
|
||||
}
|
||||
@@ -159,15 +159,7 @@ public class TranslateUUID {
|
||||
// *********************************
|
||||
|
||||
private static void createTable() {
|
||||
PreparedStatement preparedStatement = null;
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
preparedStatement = connection.prepareStatement(sqlTable.createQuery());
|
||||
preparedStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
TLogger.getGlobalLogger().error("Database error: " + e.toString());
|
||||
} finally {
|
||||
SQLExecutor.freeStatement(preparedStatement, null);
|
||||
}
|
||||
sqlTable.executeUpdate(sqlTable.createQuery()).dataSource(dataSource).run();
|
||||
}
|
||||
|
||||
private static String getDefaultWorldName() {
|
||||
@@ -194,21 +186,11 @@ public class TranslateUUID {
|
||||
}
|
||||
|
||||
private static String translateInternal(Connection connection, String input, String output, String command) {
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(Strings.replaceWithOrder(command, sqlTable.getTableName()));
|
||||
preparedStatement.setString(1, input);
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
while (resultSet.next()) {
|
||||
return resultSet.getString(output);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
TLogger.getGlobalLogger().error("Database error: " + e.toString());
|
||||
} finally {
|
||||
SQLExecutor.freeStatement(preparedStatement, resultSet);
|
||||
}
|
||||
return null;
|
||||
return sqlTable.executeQuery(Strings.replaceWithOrder(command, sqlTable.getTableName()))
|
||||
.connection(connection)
|
||||
.statement(statement -> statement.setString(1, input))
|
||||
.resultNext(result -> result.getString(output))
|
||||
.run(null, "");
|
||||
}
|
||||
|
||||
// *********************************
|
||||
|
||||
Reference in New Issue
Block a user