Search in sources :

Example 31 with DBDAttributeBinding

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

the class SQLGeneratorUpdateFromData method generateSQL.

@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, IResultSetController object) throws DBException {
    for (ResultSetRow firstRow : getSelectedRows()) {
        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
        Collection<? extends DBSAttributeBase> valueAttributes = getValueAttributes(monitor, object, keyAttributes);
        sql.append("UPDATE ").append(getEntityName(getSingleEntity()));
        sql.append(getLineSeparator()).append("SET ");
        boolean hasAttr = false;
        for (DBSAttributeBase attr : valueAttributes) {
            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                continue;
            }
            if (hasAttr)
                sql.append(", ");
            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML)).append("=");
            DBDAttributeBinding binding = getController().getModel().getAttributeBinding(attr);
            if (binding == null) {
                appendDefaultValue(sql, attr);
            } else {
                appendAttributeValue(getController(), sql, binding, firstRow);
            }
            hasAttr = true;
        }
        sql.append(getLineSeparator()).append("WHERE ");
        hasAttr = false;
        for (DBDAttributeBinding attr : keyAttributes) {
            if (hasAttr)
                sql.append(" AND ");
            appendValueCondition(getController(), sql, attr, firstRow);
            hasAttr = true;
        }
        sql.append(";\n");
    }
}
Also used : DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 32 with DBDAttributeBinding

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

the class ResultSetHandlerOpenWith 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("Open " + processor.getAppName(), ModelMessages.error_not_connected_to_database);
        return;
    }
    DBPPreferenceStore preferenceStore = DBWorkbench.getPlatform().getPreferenceStore();
    String prevActiveApp = preferenceStore.getString(PARAM_ACTIVE_APP);
    if (!CommonUtils.equalObjects(prevActiveApp, processor.getFullId())) {
    // preferenceStore.setValue(PARAM_ACTIVE_APP, processor.getFullId());
    // resultSet.updateEditControls();
    // resultSet.getControl().layout(true);
    }
    AbstractJob exportJob = new AbstractJob("Open " + processor.getAppName()) {

        {
            setUser(true);
            setSystem(false);
        }

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBWorkbench.getPlatform().getTempFolder(monitor, "data-files");
                File tempFile = new File(tempDir, new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + "." + processor.getAppFileExtension());
                tempFile.deleteOnExit();
                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.setOutputEncodingBOM(false);
                settings.setOpenFolderOnFinish(false);
                settings.setOutputFolder(tempDir.getAbsolutePath());
                settings.setOutputFilePattern(tempFile.getName());
                Map<String, Object> properties = new HashMap<>();
                // Default values from wizard
                IDialogSettings dtSettings = DataTransferWizard.getWizardDialogSettings();
                IDialogSettings procListSection = dtSettings.getSection("processors");
                IDialogSettings procSettings = null;
                if (procListSection != null) {
                    procSettings = procListSection.getSection("stream_consumer:" + processor.getId());
                }
                for (DBPPropertyDescriptor prop : processor.getProperties()) {
                    Object defValue = procSettings == null ? null : procSettings.get(CommonUtils.toString(prop.getId()));
                    properties.put(prop.getId(), defValue != null ? defValue : prop.getDefaultValue());
                }
                // Remove extension property (we specify file name directly)
                properties.remove(StreamConsumerSettings.PROP_FILE_EXTENSION);
                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.setExtractType(DatabaseProducerSettings.ExtractType.SINGLE_QUERY);
                producerSettings.setQueryRowCount(false);
                // disable OpenNewconnection by default (#6432)
                producerSettings.setOpenNewConnections(false);
                producerSettings.setSelectedRowsOnly(!CommonUtils.isEmpty(options.getSelectedRows()));
                producerSettings.setSelectedColumnsOnly(!CommonUtils.isEmpty(options.getSelectedColumns()));
                producer.transferData(monitor, consumer, null, producerSettings, null);
                consumer.finishTransfer(monitor, false);
                UIUtils.asyncExec(() -> {
                    if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                        DBWorkbench.getPlatformUI().showError("Open " + processor.getAppName(), "Can't open " + processor.getAppFileExtension() + " file '" + tempFile.getAbsolutePath() + "'");
                    }
                });
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in " + processor.getAppName(), null, e);
            }
            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) DBPPreferenceStore(org.jkiss.dbeaver.model.preferences.DBPPreferenceStore) 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) IDialogSettings(org.eclipse.jface.dialogs.IDialogSettings) 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) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 33 with DBDAttributeBinding

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

the class TransformerSettingsDialog method updateTransformItem.

private void updateTransformItem(TableItem attrItem) {
    DBDAttributeBinding attr = (DBDAttributeBinding) attrItem.getData();
    String transformStr = "";
    DBVEntityAttribute vAttr = vEntity.getVirtualAttribute(attr, false);
    if (vAttr != null) {
        DBVTransformSettings settings = vAttr.getTransformSettings();
        if (settings != null) {
            if (!CommonUtils.isEmpty(settings.getIncludedTransformers())) {
                transformStr = String.join(",", settings.getIncludedTransformers());
            } else if (!CommonUtils.isEmpty(settings.getCustomTransformer())) {
                DBDAttributeTransformerDescriptor td = DBWorkbench.getPlatform().getValueHandlerRegistry().getTransformer(settings.getCustomTransformer());
                if (td != null) {
                    transformStr = td.getName();
                }
            }
        }
    }
    attrItem.setText(1, transformStr);
}
Also used : DBDAttributeTransformerDescriptor(org.jkiss.dbeaver.model.data.DBDAttributeTransformerDescriptor) DBVEntityAttribute(org.jkiss.dbeaver.model.virtual.DBVEntityAttribute) DBVTransformSettings(org.jkiss.dbeaver.model.virtual.DBVTransformSettings) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 34 with DBDAttributeBinding

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

the class ColorSettingsDialog method createAttributeSelectorArea.

private void createAttributeSelectorArea(Composite composite) {
    Composite panel = UIUtils.createComposite(composite, 1);
    attributeTable = new Table(panel, SWT.FULL_SELECTION | SWT.BORDER);
    attributeTable.setHeaderVisible(true);
    GridData gd = new GridData(GridData.FILL_BOTH);
    gd.widthHint = 400;
    attributeTable.setLayoutData(gd);
    UIUtils.executeOnResize(attributeTable, () -> UIUtils.packColumns(attributeTable, true));
    UIUtils.createTableColumn(attributeTable, SWT.LEFT, "Name");
    UIUtils.createTableColumn(attributeTable, SWT.LEFT, "Colors");
    for (DBDAttributeBinding attr : resultSetViewer.getModel().getVisibleAttributes()) {
        TableItem attrItem = new TableItem(attributeTable, SWT.NONE);
        attrItem.setData(attr);
        attrItem.setText(0, attr.getName());
        attrItem.setImage(0, DBeaverIcons.getImage(DBValueFormatting.getObjectImage(attr, true)));
        if (this.attribute == attr) {
            attributeTable.setSelection(attrItem);
        }
    // updateColumnItem(attrItem);
    }
    attributeTable.addListener(SWT.PaintItem, event -> {
        if (event.index == 1) {
            int x = event.x + 4;
            DBDAttributeBinding attr = (DBDAttributeBinding) event.item.getData();
            List<DBVColorOverride> coList = vEntity.getColorOverrides(attr.getName());
            if (!coList.isEmpty()) {
                for (DBVColorOverride co : coList) {
                    List<String> coStrings = new ArrayList<>();
                    if (co.getAttributeValues() != null) {
                        for (Object value : co.getAttributeValues()) {
                            coStrings.add(CommonUtils.toString(value));
                        }
                    }
                    // String colorSettings = "   ";//String.join(", ", coStrings);
                    // Point textSize = event.gc.stringExtent(colorSettings);
                    int boxSize = attributeTable.getItemHeight() - 4;
                    Point textSize = new Point(boxSize, boxSize);
                    // getColorTableForeground(co);
                    Color fg = attributeTable.getDisplay().getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
                    Color bg = getColorTableBackground(co);
                    if (fg != null)
                        event.gc.setForeground(fg);
                    if (bg != null)
                        event.gc.setBackground(bg);
                    event.gc.fillRectangle(x, event.y + 2, textSize.x, textSize.y);
                    event.gc.drawRectangle(x, event.y + 2, textSize.x - 1, textSize.y - 1);
                    // event.gc.drawText(colorSettings, x, event.y);
                    x += textSize.x + 4;
                }
            }
        }
    });
    attributeTable.addSelectionListener(new SelectionAdapter() {

        @Override
        public void widgetSelected(SelectionEvent e) {
            updateAttributeSelection();
        }
    });
}
Also used : Color(org.eclipse.swt.graphics.Color) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) Point(org.eclipse.swt.graphics.Point) DBVColorOverride(org.jkiss.dbeaver.model.virtual.DBVColorOverride) GridData(org.eclipse.swt.layout.GridData) SelectionEvent(org.eclipse.swt.events.SelectionEvent) DBVColorOverride(org.jkiss.dbeaver.model.virtual.DBVColorOverride)

Example 35 with DBDAttributeBinding

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

the class SQLGeneratorInsertFromData method generateSQL.

@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, IResultSetController object) {
    for (ResultSetRow firstRow : getSelectedRows()) {
        Collection<? extends DBSAttributeBase> allAttributes = getAllAttributes(monitor, object);
        sql.append("INSERT INTO ").append(getEntityName(getSingleEntity()));
        sql.append(getLineSeparator()).append("(");
        boolean hasAttr = false;
        for (DBSAttributeBase attr : allAttributes) {
            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                continue;
            }
            if (isExcludeAutoGeneratedColumn() && attr.isAutoGenerated()) {
                continue;
            }
            if (hasAttr)
                sql.append(", ");
            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
            hasAttr = true;
        }
        sql.append(")").append(getLineSeparator()).append("VALUES(");
        hasAttr = false;
        for (DBSAttributeBase attr : allAttributes) {
            if (DBUtils.isPseudoAttribute(attr) || DBUtils.isHiddenObject(attr)) {
                continue;
            }
            if (isExcludeAutoGeneratedColumn() && attr.isAutoGenerated()) {
                continue;
            }
            if (hasAttr)
                sql.append(", ");
            DBDAttributeBinding binding = getController().getModel().getAttributeBinding(attr);
            if (binding == null) {
                appendDefaultValue(sql, attr);
            } else {
                appendAttributeValue(getController(), sql, binding, firstRow);
            }
            hasAttr = true;
        }
        sql.append(");\n");
    }
}
Also used : DBSAttributeBase(org.jkiss.dbeaver.model.struct.DBSAttributeBase) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

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