From 2766713ff9b5b3a60b2e57fb65ac6f07ab65e768 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Wed, 29 Mar 2017 20:42:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DSQL=E9=A2=84=E5=A4=84?= =?UTF-8?q?=E7=90=86=E8=AF=AD=E5=8F=A5=E6=89=A7=E8=A1=8C=E9=94=99=E8=AF=AF?= =?UTF-8?q?=20=E8=B0=83=E6=95=B4=E5=91=BD=E4=BB=A4=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/java/pw/yumc/YumCore/bukkit/Log.java | 21 ++++++ .../pw/yumc/YumCore/commands/CommandMain.java | 2 +- .../yumc/YumCore/commands/CommandParse.java | 4 +- .../pw/yumc/YumCore/commands/CommandSub.java | 2 +- .../yumc/YumCore/sql/core/DataBaseCore.java | 75 +++++++++++++------ 5 files changed, 78 insertions(+), 26 deletions(-) diff --git a/src/main/java/pw/yumc/YumCore/bukkit/Log.java b/src/main/java/pw/yumc/YumCore/bukkit/Log.java index 853a1bb..ec31f3a 100644 --- a/src/main/java/pw/yumc/YumCore/bukkit/Log.java +++ b/src/main/java/pw/yumc/YumCore/bukkit/Log.java @@ -2,6 +2,7 @@ package pw.yumc.YumCore.bukkit; import java.io.File; import java.util.Arrays; +import java.util.List; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; @@ -363,4 +364,24 @@ public class Log { public static boolean isGlobalDebug() { return globalDebug; } + + public static String osn(List classes) { + StringBuilder str = new StringBuilder("["); + classes.forEach(c -> str.append(c.getClass().getSimpleName()).append(", ")); + return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]"; + } + + public static String osn(Object... classes) { + return osn(Arrays.asList(classes)); + } + + public static String csn(List classes) { + StringBuilder str = new StringBuilder("["); + classes.forEach(c -> str.append(c.getSimpleName()).append(", ")); + return classes.isEmpty() ? "[]" : str.substring(0, str.length() - 2) + "]"; + } + + public static String csn(Class[] classes) { + return csn(Arrays.asList(classes)); + } } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandMain.java b/src/main/java/pw/yumc/YumCore/commands/CommandMain.java index 0a4349b..5810c03 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandMain.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandMain.java @@ -78,7 +78,7 @@ public class CommandMain implements CommandExecutor { if (ci != null) { injectPluginCommand(ci); Class[] params = method.getParameterTypes(); - Log.d("注册主命令 %s 参数类型: %s", ci.getName(), Arrays.toString(params)); + Log.d("注册主命令 %s 参数类型: %s", ci.getName(), Log.csn(params)); try { Class sender = params[0]; cmds.add(ci); diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java index 9e99753..1d95924 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandParse.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandParse.java @@ -65,7 +65,7 @@ public class CommandParse { if (parse == null) { throw new ParseException(String.format("存在无法解析的参数类型 %s", clazz.getName())); } this.parse.add(parse.clone().parseAnnotation(annotations).handleAttrs()); } - Log.d("命令解析器 %s", String.valueOf(parse)); + Log.d("命令解析器 %s", Log.osn(parse)); } public static CommandParse get(Method method) { @@ -110,7 +110,7 @@ public class CommandParse { throw new ParseException(String.format("第 %s 个参数 %s", isMain ? 1 : 2 + i, e.getMessage())); } } - Log.d("解析参数: %s => %s", Arrays.toString(args), pobjs); + Log.d("解析参数: %s => %s", Arrays.toString(args), Log.osn(pobjs)); return pobjs.toArray(); } diff --git a/src/main/java/pw/yumc/YumCore/commands/CommandSub.java b/src/main/java/pw/yumc/YumCore/commands/CommandSub.java index 6adc0b3..6b9afe7 100644 --- a/src/main/java/pw/yumc/YumCore/commands/CommandSub.java +++ b/src/main/java/pw/yumc/YumCore/commands/CommandSub.java @@ -235,7 +235,7 @@ public class CommandSub implements TabExecutor { CommandInfo ci = CommandInfo.parse(method, clazz); if (ci != null) { Class[] params = method.getParameterTypes(); - Log.d("注册子命令: %s 参数类型: %s", ci.getName(), Arrays.toString(params)); + Log.d("注册子命令: %s 参数类型: %s", ci.getName(), Log.csn(params)); try { Class sender = params[0]; // 用于消除unuse警告 diff --git a/src/main/java/pw/yumc/YumCore/sql/core/DataBaseCore.java b/src/main/java/pw/yumc/YumCore/sql/core/DataBaseCore.java index cb880c3..a294526 100644 --- a/src/main/java/pw/yumc/YumCore/sql/core/DataBaseCore.java +++ b/src/main/java/pw/yumc/YumCore/sql/core/DataBaseCore.java @@ -5,6 +5,7 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; +import java.util.Arrays; import pw.yumc.YumCore.bukkit.Log; @@ -59,12 +60,12 @@ public abstract class DataBaseCore { * SQL执行异常 */ public boolean execute(String sql, Object... obj) throws SQLException { - debug(sql); + debug(sql, obj); PreparedStatement ps = prepareStatement(sql); for (int i = 0; i < obj.length; i++) { ps.setObject(i + 1, obj[i]); } - boolean result = ps.execute(sql); + boolean result = ps.execute(); ps.close(); return result; } @@ -98,6 +99,24 @@ public abstract class DataBaseCore { return st.executeQuery(sql); } + /** + * 查询数据库 + * + * @param sql + * SQL查询语句 + * @return 查询结果 + * @throws SQLException + * SQL查询异常 + */ + public ResultSet query(String sql, Object[] params) throws SQLException { + debug(sql, params); + PreparedStatement pst = prepareStatement(sql); + for (int i = 0; i < params.length; i++) { + pst.setObject(i + 1, params[i]); + } + return pst.executeQuery(); + } + /** * 更新数据库内的数据 * @@ -127,16 +146,40 @@ public abstract class DataBaseCore { * SQL执行异常 */ public int update(String sql, Object[] obj) throws SQLException { - debug(sql); + debug(sql, obj); PreparedStatement ps = prepareStatement(sql); for (int i = 0; i < obj.length; i++) { ps.setObject(i + 1, obj[i]); } - int result = ps.executeUpdate(sql); + int result = ps.executeUpdate(); ps.close(); return result; } + /** + * 获得数据操作对象 + * + * @return 操作对象 + * @throws SQLException + * SQL执行异常 + */ + public Statement getStatement() throws SQLException { + return getConnection().createStatement(); + } + + /** + * 获得数据操作对象(预处理) + * + * @param sql + * SQL语句 + * @return 操作对象 + * @throws SQLException + * SQL执行异常 + */ + public PreparedStatement prepareStatement(String sql) throws SQLException { + return getConnection().prepareStatement(sql); + } + /** * 发送警告 * @@ -158,26 +201,14 @@ public abstract class DataBaseCore { } /** - * 获得数据操作对象 - * - * @return 操作对象 - * @throws SQLException - * SQL执行异常 - */ - protected Statement getStatement() throws SQLException { - return getConnection().createStatement(); - } - - /** - * 获得数据操作对象(预处理) - * + * SQL调试消息 + * * @param sql * SQL语句 - * @return 操作对象 - * @throws SQLException - * SQL执行异常 + * @param params + * 参数 */ - protected PreparedStatement prepareStatement(String sql) throws SQLException { - return getConnection().prepareStatement(sql); + private void debug(String sql, Object... params) { + Log.d("[SQL] %s 参数: %s", sql, Arrays.toString(params)); } }