| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -42,7 +42,7 @@ public class DemoSQLExecutor extends AbstractSQLExecutor { | |||
| 42 | 42 | } catch (ClassNotFoundException e) { | |
| 43 | 43 | e.printStackTrace(); | |
| 44 | 44 | } | |
| 45 | - | ||
| 45 | + | ||
| 46 | 46 | try { //加载驱动程序 | |
| 47 | 47 | Class.forName("org.postgresql.Driver"); | |
| 48 | 48 | Log.d(TAG, "成功加载 PostgresSQL 驱动!"); | |
@@ -65,6 +65,8 @@ public int executeUpdate(@NotNull SQLConfig config) throws Exception { | |||
| 65 | 65 | } | |
| 66 | 66 | ||
| 67 | 67 | ||
| 68 | + //TODO 根据不同数据库来分组存 connection 和 statement,例如 Map<database, connection>, | ||
| 69 | + // 解决一次请求中有2个以上不同数据库类型导致后面的查询都用第一个的数据库类型 | ||
| 68 | 70 | private Connection connection = null; | |
| 69 | 71 | private PreparedStatement statement = null; | |
| 70 | 72 | /** | |
@@ -76,7 +78,6 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti | |||
| 76 | 78 | if (connection == null || connection.isClosed()) { | |
| 77 | 79 | Log.i(TAG, "select connection " + (connection == null ? " = null" : ("isClosed = " + connection.isClosed()))) ; | |
| 78 | 80 | ||
| 79 | - | ||
| 80 | 81 | if (DemoSQLConfig.DATABASE_POSTGRESQL.equalsIgnoreCase(config.getDatabase())) { //PostgreSQL 不允许 cross-database | |
| 81 | 82 | connection = DriverManager.getConnection(config.getDBUri() + "/" + config.getSchema(), config.getDBAccount(), config.getDBPassword()); | |
| 82 | 83 | } | |
@@ -85,14 +86,14 @@ private PreparedStatement getStatement(@NotNull SQLConfig config) throws Excepti | |||
| 85 | 86 | + config.getDBAccount() + "&password=" + config.getDBPassword()); | |
| 86 | 87 | } | |
| 87 | 88 | } | |
| 88 | - | ||
| 89 | + | ||
| 89 | 90 | statement = connection.prepareStatement(config.getSQL(config.isPrepared())); //创建Statement对象 | |
| 90 | 91 | List<Object> valueList = config.isPrepared() ? config.getPreparedValueList() : null; | |
| 91 | - | ||
| 92 | + | ||
| 92 | 93 | if (valueList != null && valueList.isEmpty() == false) { | |
| 93 | - | ||
| 94 | + | ||
| 94 | 95 | for (int i = 0; i < valueList.size(); i++) { | |
| 95 | - | ||
| 96 | + | ||
| 96 | 97 | if (DemoSQLConfig.DATABASE_POSTGRESQL.equalsIgnoreCase(config.getDatabase())) { | |
| 97 | 98 | statement.setObject(i + 1, valueList.get(i)); //PostgreSQL JDBC 不支持隐式类型转换 tinyint = varchar 报错 | |
| 98 | 99 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -131,6 +131,11 @@ public AbstractParser setGlobleRole(RequestRole globleRole) { | |||
| 131 | 131 | this.globleRole = globleRole; | |
| 132 | 132 | return this; | |
| 133 | 133 | } | |
| 134 | + protected String globleDatabase; | ||
| 135 | + public AbstractParser setGlobleDatabase(String globleDatabase) { | ||
| 136 | + this.globleDatabase = globleDatabase; | ||
| 137 | + return this; | ||
| 138 | + } | ||
| 134 | 139 | ||
| 135 | 140 | @Override | |
| 136 | 141 | public boolean isNoVerify() { | |
@@ -257,6 +262,13 @@ public JSONObject parseResponse(JSONObject request) { | |||
| 257 | 262 | return extendErrorResult(requestObject, e); | |
| 258 | 263 | } | |
| 259 | 264 | } | |
| 265 | + | ||
| 266 | + try { | ||
| 267 | + setGlobleDatabase(requestObject.getString(JSONRequest.KEY_DATABASE)); | ||
| 268 | + requestObject.remove(JSONRequest.KEY_DATABASE); | ||
| 269 | + } catch (Exception e) { | ||
| 270 | + return extendErrorResult(requestObject, e); | ||
| 271 | + } | ||
| 260 | 272 | ||
| 261 | 273 | final String requestString = JSON.toJSONString(request);//request传进去解析后已经变了 | |
| 262 | 274 | ||
@@ -1096,6 +1108,11 @@ public synchronized JSONObject executeSQL(SQLConfig config) throws Exception { | |||
| 1096 | 1108 | } | |
| 1097 | 1109 | verifier.verify(config); | |
| 1098 | 1110 | } | |
| 1111 | + | ||
| 1112 | + if (config.getDatabase() == null && globleDatabase != null) { | ||
| 1113 | + config.setDatabase(globleDatabase); | ||
| 1114 | + } | ||
| 1115 | + | ||
| 1099 | 1116 | return parseCorrectResponse(config.getTable(), sqlExecutor.execute(config)); | |
| 1100 | 1117 | } | |
| 1101 | 1118 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments