|
2 | 2 |
|
3 | 3 | import com.alibaba.druid.sql.parser.ParserException; |
4 | 4 | import com.google.gson.Gson; |
| 5 | +import com.google.gson.JsonParseException; |
5 | 6 | import lombok.extern.slf4j.Slf4j; |
6 | 7 | import org.apache.commons.lang3.StringUtils; |
7 | 8 | import org.jumpserver.chen.framework.console.action.DataViewAction; |
8 | 9 | import org.jumpserver.chen.framework.console.action.QueryConsoleAction; |
| 10 | +import org.jumpserver.chen.framework.console.action.SQLChunkData; |
9 | 11 | import org.jumpserver.chen.framework.console.dataview.DataView; |
10 | 12 | import org.jumpserver.chen.framework.console.dataview.QueryDataViewTableEditContextFactory; |
11 | 13 | import org.jumpserver.chen.framework.console.dataview.UpdateDataView; |
@@ -359,16 +361,27 @@ private void onAction(QueryConsoleAction action) { |
359 | 361 | private int expectedChunks = -1; |
360 | 362 |
|
361 | 363 | private void handleSQLChunk(QueryConsoleAction action) { |
362 | | - var data = (Map<String, Object>) action.getData(); |
363 | | - var chunk = (String) data.get("chunk"); |
364 | | - var index = (Integer) data.get("index"); |
365 | | - var total = (Integer) data.get("total"); |
| 364 | + SQLChunkData data; |
| 365 | + try { |
| 366 | + data = GSON.fromJson(GSON.toJson(action.getData()), SQLChunkData.class); |
| 367 | + } catch (JsonParseException e) { |
| 368 | + this.getConsoleLogger().error("invalid sql chunk"); |
| 369 | + this.resetSQLChunks(); |
| 370 | + return; |
| 371 | + } |
366 | 372 |
|
367 | | - if (chunk == null || index == null || total == null || total <= 0) { |
| 373 | + if (data == null |
| 374 | + || data.getChunk() == null |
| 375 | + || data.getIndex() == null |
| 376 | + || data.getTotal() == null |
| 377 | + || data.getTotal() <= 0) { |
368 | 378 | this.getConsoleLogger().error("invalid sql chunk"); |
369 | 379 | this.resetSQLChunks(); |
370 | 380 | return; |
371 | 381 | } |
| 382 | + var chunk = data.getChunk(); |
| 383 | + var index = data.getIndex(); |
| 384 | + var total = data.getTotal(); |
372 | 385 | if (expectedChunks == -1) { |
373 | 386 | expectedChunks = total; |
374 | 387 | } |
@@ -617,9 +630,9 @@ public void onCancel() { |
617 | 630 | this.getState().setExecutionStatus(EXECUTION_STATUS_CANCELLED); |
618 | 631 | try { |
619 | 632 | var plan = this.currentPlan; |
620 | | - if (plan != null && plan.getStatement() != null) { |
| 633 | + if (plan != null) { |
621 | 634 | plan.cancel(); |
622 | | - this.getConsoleLogger().error("cancel query: %s", plan.getTargetSQL()); |
| 635 | + this.getConsoleLogger().warn("cancel query: %s", plan.getTargetSQL()); |
623 | 636 | } |
624 | 637 | } catch (SQLException | RuntimeException e) { |
625 | 638 | log.error("cancel failed ", e); |
@@ -775,9 +788,9 @@ public void onSQL(String sql) { |
775 | 788 | } catch (SQLException e) { |
776 | 789 | if (!StringUtils.equals(this.getState().getExecutionStatus(), EXECUTION_STATUS_CANCELLED)) { |
777 | 790 | this.getState().setExecutionStatus(EXECUTION_STATUS_ERROR); |
| 791 | + this.getConsoleLogger().error("%s: %s", MessageUtils.get("ExecuteError"), e.getMessage()); |
| 792 | + this.getPacketIO().sendPacket("message", Message.error(MessageUtils.get("ExecuteError"), e.getMessage())); |
778 | 793 | } |
779 | | - this.getConsoleLogger().error("%s: %s", MessageUtils.get("ExecuteError"), e.getMessage()); |
780 | | - this.getPacketIO().sendPacket("message", Message.error(MessageUtils.get("ExecuteError"), e.getMessage())); |
781 | 794 | } finally { |
782 | 795 | if (StringUtils.equals(this.getState().getExecutionStatus(), EXECUTION_STATUS_RUNNING)) { |
783 | 796 | this.getState().setExecutionStatus(EXECUTION_STATUS_SUCCESS); |
@@ -893,15 +906,13 @@ private DataView runSingleSQL(String sql, ACLResult aclResult) throws SQLExcepti |
893 | 906 | .createPlan(SQL.of(sourceSQL)); |
894 | 907 | plan.setAclResult(aclResult); |
895 | 908 | plan.setSqlQueryParams(sqlQueryParams); |
896 | | - plan.generateTargetSQL(); |
897 | | - this.getConsoleLogger().info("execute sql: %s", plan.getTargetSQL()); |
898 | | - |
899 | 909 | this.currentPlan = plan; |
900 | | - |
901 | 910 | this.getState().setCanCancel(true); |
902 | 911 | this.stateManager.commit(); |
903 | 912 |
|
904 | 913 | try { |
| 914 | + plan.generateTargetSQL(); |
| 915 | + this.getConsoleLogger().info("execute sql: %s", plan.getTargetSQL()); |
905 | 916 | var result = plan.executeWithAudit(); |
906 | 917 | this.getConsoleLogger().success(result); |
907 | 918 | return result; |
|
0 commit comments