Search in sources :

Example 1 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.

the class DataExporterHTML method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    out.write("<tr" + (rowCount++ % 2 == 0 ? " class=\"odd\"" : "") + ">");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(i);
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null, false);
        } else if (row[i] instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                out.write("<td>");
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        writeCellValue(cs.getContentReader());
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
                out.write("</td>");
            } finally {
                content.release();
            }
        } else {
            String stringValue = super.getValueDisplayString(column, row[i]);
            boolean isImage = row[i] instanceof File && stringValue != null && stringValue.endsWith(".jpg");
            if (isImage) {
                writeImageCell((File) row[i]);
            } else {
                writeTextCell(stringValue, false);
            }
        }
    }
    out.write("</tr>\n");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) File(java.io.File) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 2 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.

the class DataExporterXML method exportRow.

@Override
public void exportRow(DBCSession session, Object[] row) throws DBException, IOException {
    out.write("  <DATA_RECORD>\n");
    for (int i = 0; i < row.length; i++) {
        DBDAttributeBinding column = columns.get(i);
        String columnName = escapeXmlElementName(column.getName());
        out.write("    <" + columnName + ">");
        if (DBUtils.isNullValue(row[i])) {
            writeTextCell(null);
        } else if (row[i] instanceof DBDContent) {
            // Content
            // Inline textual content and handle binaries in some special way
            DBDContent content = (DBDContent) row[i];
            try {
                DBDContentStorage cs = content.getContents(session.getProgressMonitor());
                if (cs != null) {
                    if (ContentUtils.isTextContent(content)) {
                        try (Reader reader = cs.getContentReader()) {
                            writeCellValue(reader);
                        }
                    } else {
                        getSite().writeBinaryData(cs);
                    }
                }
            } finally {
                content.release();
            }
        } else {
            writeTextCell(super.getValueDisplayString(column, row[i]));
        }
        out.write("</" + columnName + ">\n");
    }
    out.write("  </DATA_RECORD>\n");
}
Also used : DBDContent(org.jkiss.dbeaver.model.data.DBDContent) Reader(java.io.Reader) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBDContentStorage(org.jkiss.dbeaver.model.data.DBDContentStorage)

Example 3 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.

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;
    }
    boolean shiftPressed = event.getTrigger() instanceof Event && ((((Event) event.getTrigger()).stateMask & SWT.SHIFT) == SWT.SHIFT);
    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_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:
            rsv.addNewRow(false, shiftPressed);
            break;
        case CMD_ROW_COPY:
            rsv.addNewRow(true, shiftPressed);
            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) {
                    UIUtils.showErrorDialog(HandlerUtil.getActiveShell(event), "Script generation", "Can't generate changes script", e.getTargetException());
                }
                break;
            }
        case CMD_COPY_COLUMN_NAMES:
            {
                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("\t");
                    }
                    buffer.append(attr.getName());
                }
                ResultSetUtils.copyToClipboard(buffer.toString());
                break;
            }
        case CMD_COPY_ROW_NAMES:
            {
                StringBuilder buffer = new StringBuilder();
                IResultSetSelection selection = rsv.getSelection();
                for (ResultSetRow row : ((IResultSetSelection) selection).getSelectedRows()) {
                    if (buffer.length() > 0) {
                        buffer.append("\n");
                    }
                    buffer.append(row.getVisualNumber() + 1);
                }
                ResultSetUtils.copyToClipboard(buffer.toString());
                break;
            }
        case IWorkbenchCommandConstants.EDIT_COPY:
            ResultSetUtils.copyToClipboard(presentation.copySelectionToString(new ResultSetCopySettings(false, false, false, true, 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, 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;
            }
    }
    return null;
}
Also used : DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) FindReplaceAction(org.eclipse.ui.texteditor.FindReplaceAction) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) ArrayList(java.util.ArrayList) List(java.util.List) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBEPersistAction(org.jkiss.dbeaver.model.edit.DBEPersistAction) ViewSQLDialog(org.jkiss.dbeaver.ui.dialogs.sql.ViewSQLDialog) ExecutionEvent(org.eclipse.core.commands.ExecutionEvent) Event(org.eclipse.swt.widgets.Event) DBRRunnableWithProgress(org.jkiss.dbeaver.model.runtime.DBRRunnableWithProgress) Collection(java.util.Collection) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) IFindReplaceTarget(org.eclipse.jface.text.IFindReplaceTarget) IInputValidator(org.eclipse.jface.dialogs.IInputValidator) Nullable(org.jkiss.code.Nullable)

Example 4 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.

the class AggregateColumnsPanel method aggregateSelection.

private void aggregateSelection(IResultSetSelection selection) {
    ResultSetModel model = presentation.getController().getModel();
    if (groupByColumns) {
        Map<DBDAttributeBinding, List<Object>> attrValues = new LinkedHashMap<>();
        for (Object element : selection.toList()) {
            DBDAttributeBinding attr = selection.getElementAttribute(element);
            ResultSetRow row = selection.getElementRow(element);
            Object cellValue = model.getCellValue(attr, row);
            List<Object> values = attrValues.get(attr);
            if (values == null) {
                values = new ArrayList<>();
                attrValues.put(attr, values);
            }
            values.add(cellValue);
        }
        for (Map.Entry<DBDAttributeBinding, List<Object>> entry : attrValues.entrySet()) {
            TreeItem attrItem = new TreeItem(aggregateTable, SWT.NONE);
            attrItem.setText(entry.getKey().getName());
            attrItem.setImage(DBeaverIcons.getImage(DBValueFormatting.getObjectImage(entry.getKey())));
            aggregateValues(attrItem, entry.getValue());
            attrItem.setExpanded(true);
        }
    } else {
        List<Object> allValues = new ArrayList<>(selection.size());
        for (Object element : selection.toList()) {
            DBDAttributeBinding attr = selection.getElementAttribute(element);
            ResultSetRow row = selection.getElementRow(element);
            Object cellValue = model.getCellValue(attr, row);
            allValues.add(cellValue);
        }
        aggregateValues(null, allValues);
    }
}
Also used : DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) List(java.util.List)

Example 5 with DBDAttributeBinding

use of org.jkiss.dbeaver.model.data.DBDAttributeBinding in project dbeaver by serge-rider.

the class MetaDataPanel method createContents.

@Override
public Control createContents(final IResultSetPresentation presentation, Composite parent) {
    this.presentation = presentation;
    this.colorDisabled = presentation.getControl().getDisplay().getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
    this.attributeList = new MetaDataTable(parent);
    this.attributeList.setFitWidth(false);
    this.attributeList.getItemsViewer().addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            DBDAttributeBinding attr = getSelectedAttribute();
            if (attr != null && !updateSelection) {
                if (isAttributeVisible(attr)) {
                    updateSelection = true;
                    try {
                        presentation.setCurrentAttribute(attr);
                    } finally {
                        updateSelection = false;
                    }
                }
            }
        }
    });
    if (this.presentation instanceof ISelectionProvider) {
        final ISelectionChangedListener listener = new ISelectionChangedListener() {

            @Override
            public void selectionChanged(SelectionChangedEvent event) {
                if (!updateSelection && MetaDataPanel.this.presentation.getController().getVisiblePanel() == MetaDataPanel.this) {
                    DBDAttributeBinding attr = presentation.getCurrentAttribute();
                    if (attr != null && attr != getSelectedAttribute()) {
                        updateSelection = true;
                        try {
                            attributeList.getItemsViewer().setSelection(new StructuredSelection(attr));
                        } finally {
                            updateSelection = false;
                        }
                    }
                }
            }
        };
        ((ISelectionProvider) this.presentation).addSelectionChangedListener(listener);
        attributeList.getControl().addDisposeListener(new DisposeListener() {

            @Override
            public void widgetDisposed(DisposeEvent e) {
                ((ISelectionProvider) presentation).removeSelectionChangedListener(listener);
            }
        });
    }
    return this.attributeList;
}
Also used : DisposeListener(org.eclipse.swt.events.DisposeListener) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DisposeEvent(org.eclipse.swt.events.DisposeEvent)

Aggregations

DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)147 ArrayList (java.util.ArrayList)32 DBException (org.jkiss.dbeaver.DBException)29 ResultSetRow (org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow)24 DBDContent (org.jkiss.dbeaver.model.data.DBDContent)23 DBDContentStorage (org.jkiss.dbeaver.model.data.DBDContentStorage)23 List (java.util.List)18 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)16 DBSAttributeBase (org.jkiss.dbeaver.model.struct.DBSAttributeBase)14 Reader (java.io.Reader)13 PrintWriter (java.io.PrintWriter)12 GridData (org.eclipse.swt.layout.GridData)12 Nullable (org.jkiss.code.Nullable)12 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)12 Date (java.util.Date)10 SWT (org.eclipse.swt.SWT)10 NotNull (org.jkiss.code.NotNull)10 DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)10 DBPPreferenceStore (org.jkiss.dbeaver.model.preferences.DBPPreferenceStore)10 DBSEntityAttribute (org.jkiss.dbeaver.model.struct.DBSEntityAttribute)10