Search in sources :

Example 31 with UIServiceSQL

use of org.jkiss.dbeaver.runtime.ui.UIServiceSQL in project dbeaver by dbeaver.

the class SaveScriptDialog method populateSQL.

private void populateSQL() {
    try {
        final List<DBEPersistAction> sqlScript = new ArrayList<>();
        UIUtils.runInProgressService(monitor -> {
            List<DBEPersistAction> script = viewer.generateChangesScript(monitor, saveSettings);
            if (script != null) {
                sqlScript.addAll(script);
            }
        });
        scriptText = "";
        if (!sqlScript.isEmpty()) {
            scriptText = SQLUtils.generateScript(viewer.getDataSource(), sqlScript.toArray(new DBEPersistAction[0]), false);
            scriptText = SQLUtils.generateCommentLine(viewer.getDataSource(), "Auto-generated SQL script #" + new SimpleDateFormat(GeneralUtils.DEFAULT_TIMESTAMP_PATTERN).format(new Date())) + scriptText;
            UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
            if (serviceSQL != null) {
                serviceSQL.setSQLPanelText(sqlPanel, scriptText);
            }
        }
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Can't generate SQL script", "Error generating SQL script from data changes", e);
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ArrayList(java.util.ArrayList) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 32 with UIServiceSQL

use of org.jkiss.dbeaver.runtime.ui.UIServiceSQL in project dbeaver by dbeaver.

the class SavePreviewDialog method populateSQL.

private void populateSQL() {
    try {
        final List<DBEPersistAction> sqlScript = new ArrayList<>();
        UIUtils.runInProgressService(monitor -> {
            List<DBEPersistAction> script = viewer.generateChangesScript(monitor, saveSettings);
            if (script != null) {
                sqlScript.addAll(script);
            }
        });
        String scriptText = "";
        if (!sqlScript.isEmpty()) {
            scriptText = SQLUtils.generateScript(viewer.getDataSource(), sqlScript.toArray(new DBEPersistAction[0]), false);
            scriptText = SQLUtils.generateCommentLine(viewer.getDataSource(), "Auto-generated SQL script. Actual values for binary/complex data types may differ - what you see is the default string representation of values.") + scriptText;
            UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
            if (serviceSQL != null) {
                serviceSQL.setSQLPanelText(sqlPanel, scriptText);
            }
        }
    } catch (Exception e) {
        DBWorkbench.getPlatformUI().showError("Can't generalte SQL script", "Error generating SQL script from changes", e);
    }
}
Also used : DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ArrayList(java.util.ArrayList) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL)

Example 33 with UIServiceSQL

use of org.jkiss.dbeaver.runtime.ui.UIServiceSQL in project dbeaver by dbeaver.

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();
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) StreamConsumerSettings(org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) StreamTransferConsumer(org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer) IDataTransferConsumer(org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer) IDataTransferProcessor(org.jkiss.dbeaver.tools.transfer.IDataTransferProcessor) ExecutionException(org.eclipse.core.commands.ExecutionException) DBPPropertyDescriptor(org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) IStreamDataExporter(org.jkiss.dbeaver.tools.transfer.stream.IStreamDataExporter) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL)

Example 34 with UIServiceSQL

use of org.jkiss.dbeaver.runtime.ui.UIServiceSQL in project dbeaver by dbeaver.

the class NavigatorObjectsDeleter method showScriptWindow.

boolean showScriptWindow() {
    final String sql = collectSQL();
    boolean result = false;
    if (sql.length() > 0) {
        UIServiceSQL serviceSQL = DBWorkbench.getService(UIServiceSQL.class);
        if (serviceSQL != null) {
            result = serviceSQL.openSQLViewer(// command context is not null because sql is not empty, see appendSQL()
            commandContext.getExecutionContext(), UINavigatorMessages.actions_navigator_delete_script, UIIcon.SQL_PREVIEW, sql, true, false) == IDialogConstants.PROCEED_ID;
        }
    } else {
        result = UIUtils.confirmAction(window.getShell(), UINavigatorMessages.actions_navigator_delete_script, UINavigatorMessages.question_no_sql_available);
    }
    commandContext.resetChanges(!result);
    return result;
}
Also used : UIServiceSQL(org.jkiss.dbeaver.runtime.ui.UIServiceSQL)

Aggregations

UIServiceSQL (org.jkiss.dbeaver.runtime.ui.UIServiceSQL)34 GridData (org.eclipse.swt.layout.GridData)12 DBException (org.jkiss.dbeaver.DBException)12 FillLayout (org.eclipse.swt.layout.FillLayout)10 DBEPersistAction (org.jkiss.dbeaver.model.edit.DBEPersistAction)10 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 Composite (org.eclipse.swt.widgets.Composite)6 ArrayList (java.util.ArrayList)4 IDialogSettings (org.eclipse.jface.dialogs.IDialogSettings)4 TextViewer (org.eclipse.jface.text.TextViewer)4 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)4 StyledText (org.eclipse.swt.custom.StyledText)4 SelectionAdapter (org.eclipse.swt.events.SelectionAdapter)4 SelectionEvent (org.eclipse.swt.events.SelectionEvent)4 GridLayout (org.eclipse.swt.layout.GridLayout)4 DBSDataContainer (org.jkiss.dbeaver.model.struct.DBSDataContainer)3 File (java.io.File)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2