use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by dbeaver.
the class ResultSetCommandHandler method execute.
@Nullable
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
final ResultSetViewer rsv = (ResultSetViewer) getActiveResultSet(HandlerUtil.getActivePart(event));
if (rsv == null) {
return null;
}
String actionId = event.getCommand().getId();
IResultSetPresentation presentation = rsv.getActivePresentation();
switch(actionId) {
case IWorkbenchCommandConstants.FILE_REFRESH:
rsv.refreshData(null);
break;
case CMD_TOGGLE_MODE:
rsv.toggleMode();
break;
case CMD_TOGGLE_PANELS:
rsv.showPanels(!rsv.isPanelsVisible());
break;
case CMD_FOCUS_FILTER:
rsv.switchFilterFocus();
break;
case CMD_SWITCH_PRESENTATION:
rsv.switchPresentation();
break;
case CMD_ROW_PREVIOUS:
case ITextEditorActionDefinitionIds.WORD_PREVIOUS:
presentation.scrollToRow(IResultSetPresentation.RowPosition.PREVIOUS);
break;
case CMD_ROW_NEXT:
case ITextEditorActionDefinitionIds.WORD_NEXT:
presentation.scrollToRow(IResultSetPresentation.RowPosition.NEXT);
break;
case CMD_ROW_FIRST:
case ITextEditorActionDefinitionIds.SELECT_WORD_PREVIOUS:
presentation.scrollToRow(IResultSetPresentation.RowPosition.FIRST);
break;
case CMD_ROW_LAST:
case ITextEditorActionDefinitionIds.SELECT_WORD_NEXT:
presentation.scrollToRow(IResultSetPresentation.RowPosition.LAST);
break;
case CMD_FETCH_PAGE:
rsv.readNextSegment();
break;
case CMD_FETCH_ALL:
rsv.readAllData();
break;
case CMD_ROW_EDIT:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).openValueEditor(false);
}
break;
case CMD_ROW_EDIT_INLINE:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).openValueEditor(true);
}
break;
case CMD_ROW_ADD:
case CMD_ROW_COPY:
{
boolean copy = actionId.equals(CMD_ROW_COPY);
boolean shiftPressed = event.getTrigger() instanceof Event && ((((Event) event.getTrigger()).stateMask & SWT.SHIFT) == SWT.SHIFT);
boolean insertAfter = rsv.getPreferenceStore().getBoolean(DBeaverPreferences.RS_EDIT_NEW_ROWS_AFTER);
if (shiftPressed)
insertAfter = !insertAfter;
rsv.addNewRow(copy, insertAfter, true);
if (insertAfter) {
presentation.scrollToRow(IResultSetPresentation.RowPosition.NEXT);
}
break;
}
case CMD_ROW_DELETE:
case IWorkbenchCommandConstants.EDIT_DELETE:
rsv.deleteSelectedRows();
break;
case CMD_CELL_SET_NULL:
case CMD_CELL_RESET:
{
IResultSetSelection selection = rsv.getSelection();
for (Object cell : selection.toArray()) {
DBDAttributeBinding attr = selection.getElementAttribute(cell);
ResultSetRow row = selection.getElementRow(cell);
if (row != null && attr != null) {
ResultSetValueController valueController = new ResultSetValueController(rsv, attr, row, IValueController.EditType.NONE, null);
if (actionId.equals(CMD_CELL_SET_NULL)) {
valueController.updateValue(BaseValueManager.makeNullValue(valueController), false);
} else {
rsv.getModel().resetCellValue(attr, row);
}
}
}
rsv.redrawData(false, false);
rsv.updatePanelsContent(false);
break;
}
case CMD_APPLY_CHANGES:
rsv.applyChanges(null);
break;
case CMD_REJECT_CHANGES:
rsv.rejectChanges();
break;
case CMD_GENERATE_SCRIPT:
{
try {
final List<DBEPersistAction> sqlScript = new ArrayList<>();
try {
DBeaverUI.runInProgressService(new DBRRunnableWithProgress() {
@Override
public void run(DBRProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
List<DBEPersistAction> script = rsv.generateChangesScript(monitor);
if (script != null) {
sqlScript.addAll(script);
}
}
});
} catch (InterruptedException e) {
// ignore
}
if (!sqlScript.isEmpty()) {
String scriptText = SQLUtils.generateScript(rsv.getDataContainer() == null ? null : rsv.getDataContainer().getDataSource(), sqlScript.toArray(new DBEPersistAction[sqlScript.size()]), false);
scriptText = SQLUtils.generateCommentLine(rsv.getExecutionContext() == null ? null : rsv.getExecutionContext().getDataSource(), "Actual parameter values may differ, what you see is a default string representation of values") + scriptText;
ViewSQLDialog dialog = new ViewSQLDialog(HandlerUtil.getActivePart(event).getSite(), rsv.getExecutionContext(), CoreMessages.editors_entity_dialog_preview_title, UIIcon.SQL_PREVIEW, scriptText);
dialog.open();
}
} catch (InvocationTargetException e) {
DBUserInterface.getInstance().showError("Script generation", "Can't generate changes script", e.getTargetException());
}
break;
}
case CMD_COPY_COLUMN_NAMES:
{
ResultSetCopySpecialHandler.CopyConfigDialog configDialog = new ResultSetCopySpecialHandler.CopyConfigDialog(HandlerUtil.getActiveShell(event), "CopyGridNamesOptionsDialog");
if (configDialog.open() != IDialogConstants.OK_ID) {
return null;
}
StringBuilder buffer = new StringBuilder();
IResultSetSelection selection = rsv.getSelection();
Collection<DBDAttributeBinding> attrs = selection.isEmpty() ? rsv.getModel().getVisibleAttributes() : selection.getSelectedAttributes();
for (DBDAttributeBinding attr : attrs) {
if (buffer.length() > 0) {
buffer.append(configDialog.copySettings.getColumnDelimiter());
}
String colName = attr.getLabel();
if (CommonUtils.isEmpty(colName)) {
colName = attr.getName();
}
buffer.append(colName);
}
ResultSetUtils.copyToClipboard(buffer.toString());
break;
}
case CMD_COPY_ROW_NAMES:
{
ResultSetCopySpecialHandler.CopyConfigDialog configDialog = new ResultSetCopySpecialHandler.CopyConfigDialog(HandlerUtil.getActiveShell(event), "CopyGridNamesOptionsDialog");
if (configDialog.open() != IDialogConstants.OK_ID) {
return null;
}
StringBuilder buffer = new StringBuilder();
IResultSetSelection selection = rsv.getSelection();
for (ResultSetRow row : selection.getSelectedRows()) {
if (buffer.length() > 0) {
buffer.append(configDialog.copySettings.getRowDelimiter());
}
buffer.append(row.getVisualNumber() + 1);
}
ResultSetUtils.copyToClipboard(buffer.toString());
break;
}
case IWorkbenchCommandConstants.EDIT_COPY:
ResultSetUtils.copyToClipboard(presentation.copySelectionToString(new ResultSetCopySettings(false, false, false, true, false, null, null, null, DBDDisplayFormat.EDIT)));
break;
case IWorkbenchCommandConstants.EDIT_PASTE:
case CoreCommands.CMD_PASTE_SPECIAL:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).pasteFromClipboard(actionId.equals(CoreCommands.CMD_PASTE_SPECIAL));
}
break;
case IWorkbenchCommandConstants.EDIT_CUT:
ResultSetUtils.copyToClipboard(presentation.copySelectionToString(new ResultSetCopySettings(false, false, true, true, false, null, null, null, DBDDisplayFormat.EDIT)));
break;
case IWorkbenchCommandConstants.FILE_PRINT:
presentation.printResultSet();
break;
case ITextEditorActionDefinitionIds.SMART_ENTER:
if (presentation instanceof IResultSetEditor) {
((IResultSetEditor) presentation).openValueEditor(false);
}
break;
case IWorkbenchCommandConstants.EDIT_FIND_AND_REPLACE:
FindReplaceAction action = new FindReplaceAction(DBeaverActivator.getCoreResourceBundle(), "Editor.FindReplace.", HandlerUtil.getActiveShell(event), rsv.getAdapter(IFindReplaceTarget.class));
action.run();
break;
case CMD_NAVIGATE_LINK:
{
final ResultSetRow row = rsv.getCurrentRow();
final DBDAttributeBinding attr = rsv.getActivePresentation().getCurrentAttribute();
if (row != null && attr != null) {
new AbstractJob("Navigate association") {
@Override
protected IStatus run(DBRProgressMonitor monitor) {
try {
rsv.navigateAssociation(monitor, attr, row, false);
} catch (DBException e) {
return GeneralUtils.makeExceptionStatus(e);
}
return Status.OK_STATUS;
}
}.schedule();
}
break;
}
case CMD_COUNT:
rsv.updateRowCount();
break;
case IWorkbenchCommandConstants.NAVIGATE_BACKWARD_HISTORY:
{
final int hp = rsv.getHistoryPosition();
if (hp > 0) {
rsv.navigateHistory(hp - 1);
}
break;
}
case IWorkbenchCommandConstants.NAVIGATE_FORWARD_HISTORY:
{
final int hp = rsv.getHistoryPosition();
if (hp < rsv.getHistorySize() - 1) {
rsv.navigateHistory(hp + 1);
}
break;
}
case ITextEditorActionDefinitionIds.LINE_GOTO:
{
ResultSetRow currentRow = rsv.getCurrentRow();
final int rowCount = rsv.getModel().getRowCount();
if (rowCount <= 0) {
break;
}
GotoLineDialog d = new GotoLineDialog(HandlerUtil.getActiveShell(event), "Go to Row", "Enter row number (1.." + rowCount + ")", String.valueOf(currentRow == null ? 1 : currentRow.getVisualNumber() + 1), new IInputValidator() {
@Override
public String isValid(String input) {
try {
int i = Integer.parseInt(input);
if (i <= 0 || rowCount < i) {
return "Row number is out of range";
}
} catch (NumberFormatException x) {
return "Not a number";
}
return null;
}
});
if (d.open() == Window.OK) {
int line = Integer.parseInt(d.getValue());
rsv.setCurrentRow(rsv.getModel().getRow(line - 1));
rsv.getActivePresentation().scrollToRow(IResultSetPresentation.RowPosition.CURRENT);
}
break;
}
case CMD_FILTER_MENU:
{
rsv.showFiltersMenu();
break;
}
case CMD_FILTER_MENU_DISTINCT:
{
DBDAttributeBinding curAttribute = rsv.getActivePresentation().getCurrentAttribute();
if (curAttribute != null) {
rsv.showFiltersDistinctMenu(curAttribute, true);
}
break;
}
case CMD_REFERENCES_MENU:
{
rsv.showReferencesMenu();
break;
}
case CMD_EXPORT:
{
List<Long> selectedRows = new ArrayList<>();
for (ResultSetRow selectedRow : rsv.getSelection().getSelectedRows()) {
selectedRows.add(Long.valueOf(selectedRow.getRowNumber()));
}
List<String> selectedAttributes = new ArrayList<>();
for (DBDAttributeBinding attributeBinding : rsv.getSelection().getSelectedAttributes()) {
selectedAttributes.add(attributeBinding.getName());
}
ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
options.setSelectedRows(selectedRows);
options.setSelectedColumns(selectedAttributes);
ResultSetDataContainer dataContainer = new ResultSetDataContainer(rsv.getDataContainer(), rsv.getModel(), options);
ActiveWizardDialog dialog = new ActiveWizardDialog(HandlerUtil.getActiveWorkbenchWindow(event), new DataTransferWizard(new IDataTransferProducer[] { new DatabaseTransferProducer(dataContainer, rsv.getModel().getDataFilter()) }, null), rsv.getSelection());
dialog.open();
break;
}
case CMD_ZOOM_IN:
case CMD_ZOOM_OUT:
{
FontRegistry fontRegistry = rsv.getSite().getWorkbenchWindow().getWorkbench().getThemeManager().getCurrentTheme().getFontRegistry();
Font font = fontRegistry.get(ThemeConstants.FONT_SQL_RESULT_SET);
if (font != null) {
FontData[] fondData = font.getFontData();
if (fondData != null) {
int zoomFactor = actionId.equals(CMD_ZOOM_IN) ? 1 : -1;
FontDescriptor fd = createFontDescriptor(fondData, zoomFactor);
fontRegistry.put(ThemeConstants.FONT_SQL_RESULT_SET, fd.getFontData());
}
}
break;
}
case CMD_TOGGLE_ORDER:
{
final DBDAttributeBinding attr = rsv.getActivePresentation().getCurrentAttribute();
if (attr != null) {
rsv.toggleSortOrder(attr, false, false);
}
break;
}
}
return null;
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by dbeaver.
the class DatabaseProducerPageInputObjects method chooseEntity.
protected boolean chooseEntity(DataTransferPipe pipe) {
DataTransferSettings settings = getWizard().getSettings();
final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
final DBNNode rootNode = DBWorkbench.getPlatform().getWorkspace().getProjects().size() == 1 ? navigatorModel.getRoot().getProjectNode(DBWorkbench.getPlatform().getWorkspace().getActiveProject()) : navigatorModel.getRoot();
boolean chooseConsumer = settings.isConsumerOptional();
DBNNode node = DBWorkbench.getPlatformUI().selectObject(UIUtils.getActiveWorkbenchShell(), chooseConsumer ? NLS.bind(DTUIMessages.database_producer_page_input_objects_node_select_target, pipe.getProducer().getDatabaseObject().getName()) : NLS.bind(DTUIMessages.database_producer_page_input_objects_node_select_source, pipe.getConsumer().getObjectName()), rootNode, lastSelection, new Class[] { DBSObjectContainer.class, DBSDataContainer.class }, new Class[] { chooseConsumer ? DBSDataManipulator.class : DBSDataContainer.class }, null);
if (node instanceof DBNDatabaseNode) {
lastSelection = (DBNDatabaseNode) node;
DBSObject object = ((DBNDatabaseNode) node).getObject();
if (chooseConsumer) {
if (object instanceof DBSDataManipulator) {
pipe.setConsumer(new DatabaseTransferConsumer((DBSDataManipulator) object));
}
} else {
if (object instanceof DBSDataContainer) {
pipe.setProducer(new DatabaseTransferProducer((DBSDataContainer) object));
}
}
return true;
}
return false;
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by dbeaver.
the class DatabaseProducerPageInputObjects method autoAssignMappings.
private void autoAssignMappings(List<DBSObject> containerObjects) {
boolean chooseConsumer = getWizard().getSettings().isConsumerOptional();
for (TableItem item : mappingTable.getItems()) {
DataTransferPipe pipe = (DataTransferPipe) item.getData();
if ((chooseConsumer && (pipe.getConsumer() == null || pipe.getConsumer().getDatabaseObject() == null)) || (!chooseConsumer && (pipe.getProducer() == null || pipe.getProducer().getDatabaseObject() == null))) {
DBSObject objectToMap = chooseConsumer ? pipe.getProducer().getDatabaseObject() : pipe.getConsumer().getDatabaseObject();
if (objectToMap == null) {
continue;
}
DBSObject object = DBUtils.findObject(containerObjects, objectToMap.getName());
if (object != null) {
if (chooseConsumer) {
if (object instanceof DBSDataManipulator) {
pipe.setConsumer(new DatabaseTransferConsumer((DBSDataManipulator) object));
}
} else {
if (object instanceof DBSDataContainer) {
pipe.setProducer(new DatabaseTransferProducer((DBSDataContainer) object));
}
}
updateItemData(item, pipe);
}
}
}
updatePageCompletion();
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by serge-rider.
the class DatabaseProducerPageInputObjects method chooseEntity.
protected boolean chooseEntity(DataTransferPipe pipe) {
DataTransferSettings settings = getWizard().getSettings();
final DBNModel navigatorModel = DBWorkbench.getPlatform().getNavigatorModel();
final DBNNode rootNode = DBWorkbench.getPlatform().getWorkspace().getProjects().size() == 1 ? navigatorModel.getRoot().getProjectNode(DBWorkbench.getPlatform().getWorkspace().getActiveProject()) : navigatorModel.getRoot();
boolean chooseConsumer = settings.isConsumerOptional();
DBNNode node = DBWorkbench.getPlatformUI().selectObject(UIUtils.getActiveWorkbenchShell(), chooseConsumer ? NLS.bind(DTUIMessages.database_producer_page_input_objects_node_select_target, pipe.getProducer().getDatabaseObject().getName()) : NLS.bind(DTUIMessages.database_producer_page_input_objects_node_select_source, pipe.getConsumer().getObjectName()), rootNode, lastSelection, new Class[] { DBSObjectContainer.class, DBSDataContainer.class }, new Class[] { chooseConsumer ? DBSDataManipulator.class : DBSDataContainer.class }, null);
if (node instanceof DBNDatabaseNode) {
lastSelection = (DBNDatabaseNode) node;
DBSObject object = ((DBNDatabaseNode) node).getObject();
if (chooseConsumer) {
if (object instanceof DBSDataManipulator) {
pipe.setConsumer(new DatabaseTransferConsumer((DBSDataManipulator) object));
}
} else {
if (object instanceof DBSDataContainer) {
pipe.setProducer(new DatabaseTransferProducer((DBSDataContainer) object));
}
}
return true;
}
return false;
}
use of org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer in project dbeaver by serge-rider.
the class ResultSetHandlerCopyAs method openResultsWith.
private static void openResultsWith(IResultSetController resultSet, DataTransferProcessorDescriptor processor) {
ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
IResultSetSelection rsSelection = resultSet.getSelection();
List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
List<DBDAttributeBinding> rsSelectedAttributes = rsSelection.getSelectedAttributes();
if (rsSelectedRows.size() > 1 || rsSelectedAttributes.size() > 1) {
List<Long> selectedRows = new ArrayList<>();
for (ResultSetRow selectedRow : rsSelectedRows) {
selectedRows.add((long) selectedRow.getRowNumber());
}
List<String> selectedAttributes = new ArrayList<>();
for (DBDAttributeBinding attributeBinding : rsSelectedAttributes) {
selectedAttributes.add(attributeBinding.getName());
}
options.setSelectedRows(selectedRows);
options.setSelectedColumns(selectedAttributes);
}
ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
if (dataContainer.getDataSource() == null) {
DBWorkbench.getPlatformUI().showError("Copy as " + processor.getName(), ModelMessages.error_not_connected_to_database);
return;
}
AbstractJob exportJob = new AbstractJob("Copy as " + processor.getName()) {
{
setUser(true);
setSystem(false);
}
@Override
protected IStatus run(DBRProgressMonitor monitor) {
monitor.beginTask("Copy data as", 3);
try {
monitor.subTask("Init");
IDataTransferProcessor processorInstance = processor.getInstance();
if (!(processorInstance instanceof IStreamDataExporter)) {
return Status.CANCEL_STATUS;
}
IStreamDataExporter exporter = (IStreamDataExporter) processorInstance;
StreamTransferConsumer consumer = new StreamTransferConsumer();
StreamConsumerSettings settings = new StreamConsumerSettings();
settings.setOutputClipboard(true);
settings.setOutputEncodingBOM(false);
settings.setOpenFolderOnFinish(false);
Map<String, Object> properties = new HashMap<>();
for (DBPPropertyDescriptor prop : processor.getProperties()) {
properties.put(prop.getId(), prop.getDefaultValue());
}
consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(processor.isBinaryFormat(), processor.isHTMLFormat()), exporter, properties);
DBDDataFilter dataFilter = resultSet.getModel().getDataFilter();
DatabaseTransferProducer producer = new DatabaseTransferProducer(dataContainer, dataFilter);
DatabaseProducerSettings producerSettings = new DatabaseProducerSettings();
producerSettings.setOpenNewConnections(false);
if (resultSet.isHasMoreData()) {
// For long resultsets we may need to open new connection
UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
if (serviceSQL != null) {
producerSettings.setOpenNewConnections(serviceSQL.useIsolatedConnections(resultSet));
}
}
producerSettings.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
producerSettings.setQueryRowCount(false);
producerSettings.setSelectedRowsOnly(!CommonUtils.isEmpty(options.getSelectedRows()));
producerSettings.setSelectedColumnsOnly(!CommonUtils.isEmpty(options.getSelectedColumns()));
monitor.worked(1);
monitor.subTask("Export data");
producer.transferData(monitor, consumer, null, producerSettings, null);
monitor.worked(1);
monitor.subTask("Finalize export");
consumer.finishTransfer(monitor, false);
consumer.finishTransfer(monitor, true);
monitor.worked(1);
} catch (Exception e) {
DBWorkbench.getPlatformUI().showError("Error opening in " + processor.getAppName(), null, e);
} finally {
monitor.done();
}
return Status.OK_STATUS;
}
};
exportJob.schedule();
}
Aggregations