diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java index 531154f7ff..3bbf1c58d6 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/WebSQLProcessor.java @@ -17,7 +17,6 @@ package io.cloudbeaver.service.sql; import io.cloudbeaver.DBWebException; -import io.cloudbeaver.WebAction; import io.cloudbeaver.model.WebConnectionInfo; import io.cloudbeaver.model.session.WebSession; import org.jkiss.code.NotNull; @@ -28,6 +27,7 @@ import org.jkiss.dbeaver.model.DBPDataKind; import org.jkiss.dbeaver.model.DBPDataSource; import org.jkiss.dbeaver.model.DBUtils; import org.jkiss.dbeaver.model.data.*; +import org.jkiss.dbeaver.model.edit.DBEPersistAction; import org.jkiss.dbeaver.model.exec.*; import org.jkiss.dbeaver.model.exec.plan.DBCPlan; import org.jkiss.dbeaver.model.exec.plan.DBCQueryPlanner; @@ -250,7 +250,6 @@ public class WebSQLProcessor { return executeInfo; } - @WebAction public WebSQLExecuteInfo updateResultsDataBatch( @NotNull DBRProgressMonitor monitor, @NotNull WebSQLContextInfo contextInfo, @@ -259,6 +258,84 @@ public class WebSQLProcessor { @Nullable List deletedRows, @Nullable List addedRows, @Nullable WebDataFormat dataFormat) throws DBException + { + List resultBatches = new ArrayList<>(); + List resultRows = new ArrayList<>(); + + DBSDataManipulator dataManipulator = generateUpdateResultsDataBatch( + monitor, contextInfo, resultsId, updatedRows, deletedRows, addedRows, dataFormat, resultBatches, resultRows); + + long totalUpdateCount = 0; + + WebSQLExecuteInfo result = new WebSQLExecuteInfo(); + List queryResults = new ArrayList<>(); + + DBCExecutionContext executionContext = getExecutionContext(dataManipulator); + try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Update data in container")) { + Map options = Collections.emptyMap(); + for (DBSDataManipulator.ExecuteBatch batch : resultBatches) { + DBCStatistics statistics = batch.execute(session, options); + + totalUpdateCount += statistics.getRowsUpdated(); + result.setDuration(result.getDuration() + statistics.getExecuteTime()); + } + } + + WebSQLQueryResultSet updatedResultSet = new WebSQLQueryResultSet(); + WebSQLResultsInfo resultsInfo = contextInfo.getResults(resultsId); + updatedResultSet.setResultsInfo(resultsInfo); + updatedResultSet.setColumns(resultsInfo.getAttributes()); + + WebSQLQueryResults updateResults = new WebSQLQueryResults(webSession, dataFormat); + updateResults.setUpdateRowCount(totalUpdateCount); + updateResults.setResultSet(updatedResultSet); + updatedResultSet.setRows(resultRows.toArray(new Object[0][])); + + queryResults.add(updateResults); + + result.setResults(queryResults.toArray(new WebSQLQueryResults[0])); + + return result; + } + + public String generateResultsDataUpdateScript( + @NotNull DBRProgressMonitor monitor, + @NotNull WebSQLContextInfo contextInfo, + @NotNull String resultsId, + @Nullable List updatedRows, + @Nullable List deletedRows, + @Nullable List addedRows, + @Nullable WebDataFormat dataFormat) throws DBException + { + List resultBatches = new ArrayList<>(); + List resultRows = new ArrayList<>(); + + DBSDataManipulator dataManipulator = generateUpdateResultsDataBatch( + monitor, contextInfo, resultsId, updatedRows, deletedRows, addedRows, dataFormat, resultBatches, resultRows); + + List actions = new ArrayList<>(); + + DBCExecutionContext executionContext = getExecutionContext(dataManipulator); + try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Update data in container")) { + Map options = Collections.emptyMap(); + for (DBSDataManipulator.ExecuteBatch batch : resultBatches) { + batch.generatePersistActions(session, actions, options); + } + } + + return SQLUtils.generateScript(executionContext.getDataSource(), actions.toArray(new DBEPersistAction[0]), true); + } + + private DBSDataManipulator generateUpdateResultsDataBatch( + @NotNull DBRProgressMonitor monitor, + @NotNull WebSQLContextInfo contextInfo, + @NotNull String resultsId, + @Nullable List updatedRows, + @Nullable List deletedRows, + @Nullable List addedRows, + @Nullable WebDataFormat dataFormat, + @NotNull List resultBatches, + @NotNull List resultRows) throws DBException { WebSQLResultsInfo resultsInfo = contextInfo.getResults(resultsId); @@ -268,11 +345,8 @@ public class WebSQLProcessor { checkDataEditAllowed(dataContainer); DBSDataManipulator dataManipulator = (DBSDataManipulator) dataContainer; - WebSQLExecuteInfo result = new WebSQLExecuteInfo(); - List queryResults = new ArrayList<>(); - DBCExecutionContext executionContext = getExecutionContext(dataManipulator); - try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Update rows in container")) { + try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.USER, "Generate data update batches")) { WebExecutionSource executionSource = new WebExecutionSource(dataManipulator, executionContext, this); DBDAttributeBinding[] allAttributes = resultsInfo.getAttributes(); @@ -282,10 +356,6 @@ public class WebSQLProcessor { updatedResultSet.setResultsInfo(resultsInfo); updatedResultSet.setColumns(resultsInfo.getAttributes()); - long totalUpdateCount = 0; - - List resultRows = new ArrayList<>(); - if (!CommonUtils.isEmpty(updatedRows)) { for (WebSQLResultsRow row : updatedRows) { @@ -329,10 +399,7 @@ public class WebSQLProcessor { DBSDataManipulator.ExecuteBatch updateBatch = dataManipulator.updateData(session, updateAttributes, keyAttributes, null, executionSource); updateBatch.add(rowValues); - DBCStatistics statistics = updateBatch.execute(session, Collections.emptyMap()); - - totalUpdateCount += statistics.getRowsUpdated(); - result.setDuration(result.getDuration() + statistics.getExecuteTime()); + resultBatches.add(updateBatch); resultRows.add(finalRow); } } @@ -363,12 +430,9 @@ public class WebSQLProcessor { finalRow[updateAttribute.getOrdinalPosition()] = WebSQLUtils.makeWebCellValue(webSession, null, realCellValue, dataFormat); } - DBSDataManipulator.ExecuteBatch updateBatch = dataManipulator.insertData(session, updateAttributes, null, executionSource, new LinkedHashMap<>()); - updateBatch.add(rowValues); - DBCStatistics statistics = updateBatch.execute(session, Collections.emptyMap()); - - totalUpdateCount += statistics.getRowsUpdated(); - result.setDuration(result.getDuration() + statistics.getExecuteTime()); + DBSDataManipulator.ExecuteBatch insertBatch = dataManipulator.insertData(session, updateAttributes, null, executionSource, new LinkedHashMap<>()); + insertBatch.add(rowValues); + resultBatches.add(insertBatch); resultRows.add(finalRow); } } @@ -394,26 +458,14 @@ public class WebSQLProcessor { addedValues.get(String.valueOf(updateAttribute.getOrdinalPosition()))); } - DBSDataManipulator.ExecuteBatch updateBatch = dataManipulator.deleteData(session, delAttributes, executionSource); - updateBatch.add(rowValues); - DBCStatistics statistics = updateBatch.execute(session, Collections.emptyMap()); - - totalUpdateCount += statistics.getRowsUpdated(); - result.setDuration(result.getDuration() + statistics.getExecuteTime()); + DBSDataManipulator.ExecuteBatch deleteBatch = dataManipulator.deleteData(session, delAttributes, executionSource); + deleteBatch.add(rowValues); + resultBatches.add(deleteBatch); } } - - WebSQLQueryResults updateResults = new WebSQLQueryResults(webSession, dataFormat); - updateResults.setUpdateRowCount(totalUpdateCount); - updateResults.setResultSet(updatedResultSet); - updatedResultSet.setRows(resultRows.toArray(new Object[0][])); - - queryResults.add(updateResults); } - result.setResults(queryResults.toArray(new WebSQLQueryResults[0])); - - return result; + return dataManipulator; } @NotNull diff --git a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java index f61bd734b5..febc8f0217 100644 --- a/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java +++ b/server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/service/sql/impl/WebServiceSQL.java @@ -231,10 +231,9 @@ public class WebServiceSQL implements DBWServiceSQL { @Override public String updateResultsDataBatchScript(@NotNull WebSQLContextInfo contextInfo, @NotNull String resultsId, @Nullable List updatedRows, @Nullable List deletedRows, @Nullable List addedRows, WebDataFormat dataFormat) throws DBWebException { try { - contextInfo.getProcessor().updateResultsDataBatch( + return contextInfo.getProcessor().generateResultsDataUpdateScript( contextInfo.getProcessor().getWebSession().getProgressMonitor(), contextInfo, resultsId, updatedRows, deletedRows, addedRows, dataFormat); - return "????"; } catch (DBException e) { throw new DBWebException("Error genering update script", e); }