Search in sources :

Example 1 with DBDDataFilter

use of org.jkiss.dbeaver.model.data.DBDDataFilter in project dbeaver by dbeaver.

the class AbstractDataEditor method activatePart.

@Override
public void activatePart() {
    createResultSetView();
    if (!loaded && !isSuspendDataQuery()) {
        if (isReadyToRun()) {
            resultSetView.setStatus(getDataQueryMessage());
            DBDDataFilter dataFilter = getEditorDataFilter();
            if (dataFilter == null) {
                resultSetView.refresh();
            } else {
                resultSetView.refreshWithFilter(dataFilter);
            }
            loaded = true;
        }
    }
// resultSetView.setSelection(resultSetView.getSelection());
}
Also used : DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter)

Example 2 with DBDDataFilter

use of org.jkiss.dbeaver.model.data.DBDDataFilter in project dbeaver by dbeaver.

the class ResultSetFilterPanel method setCustomDataFilter.

private void setCustomDataFilter() {
    DBCExecutionContext context = viewer.getExecutionContext();
    if (context == null) {
        return;
    }
    String condition = filtersText.getText();
    StringBuilder currentCondition = new StringBuilder();
    SQLUtils.appendConditionString(viewer.getModel().getDataFilter(), context.getDataSource(), null, currentCondition, true);
    if (currentCondition.toString().trim().equals(condition.trim())) {
        // The same
        return;
    }
    DBDDataFilter newFilter = new DBDDataFilter(viewer.getModel().getDataFilter());
    for (DBDAttributeConstraint ac : newFilter.getConstraints()) {
        ac.setCriteria(null);
    }
    newFilter.setWhere(condition);
    viewer.setDataFilter(newFilter, true);
// viewer.getControl().setFocus();
}
Also used : DBCExecutionContext(org.jkiss.dbeaver.model.exec.DBCExecutionContext) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint)

Example 3 with DBDDataFilter

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

the class OpenSpreadsheetHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    IResultSetController resultSet = ResultSetHandlerMain.getActiveResultSet(HandlerUtil.getActivePart(event));
    if (resultSet == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", "No active results viewer");
        return null;
    }
    ResultSetDataContainerOptions options = new ResultSetDataContainerOptions();
    IResultSetSelection rsSelection = resultSet.getSelection();
    List<ResultSetRow> rsSelectedRows = rsSelection.getSelectedRows();
    if (rsSelectedRows.size() > 1) {
        List<Long> selectedRows = new ArrayList<>();
        for (ResultSetRow selectedRow : rsSelectedRows) {
            selectedRows.add((long) selectedRow.getRowNumber());
        }
        List<String> selectedAttributes = new ArrayList<>();
        for (DBDAttributeBinding attributeBinding : rsSelection.getSelectedAttributes()) {
            selectedAttributes.add(attributeBinding.getName());
        }
        options.setSelectedRows(selectedRows);
        options.setSelectedColumns(selectedAttributes);
    }
    ResultSetDataContainer dataContainer = new ResultSetDataContainer(resultSet, options);
    if (dataContainer.getDataSource() == null) {
        DBWorkbench.getPlatformUI().showError("Open Excel", ModelMessages.error_not_connected_to_database);
        return null;
    }
    AbstractJob exportJob = new AbstractJob("Open Excel") {

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

        @Override
        protected IStatus run(DBRProgressMonitor monitor) {
            try {
                File tempDir = DBWorkbench.getPlatform().getTempFolder(monitor, "office-files");
                File tempFile = new File(tempDir, CommonUtils.escapeFileName(CommonUtils.truncateString(dataContainer.getName(), 32)) + "." + new SimpleDateFormat("yyyyMMdd-HHmmss").format(System.currentTimeMillis()) + ".xlsx");
                tempFile.deleteOnExit();
                StreamExporterAbstract exporter = new DataExporterXLSX();
                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 = DataExporterXLSX.getDefaultProperties();
                consumer.initTransfer(dataContainer, settings, new IDataTransferConsumer.TransferParameters(true, false), 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);
                producerSettings.setSelectedRowsOnly(true);
                producerSettings.setSelectedColumnsOnly(true);
                producer.transferData(monitor, consumer, null, producerSettings, null);
                consumer.finishTransfer(monitor, false);
                UIUtils.asyncExec(() -> {
                    if (!UIUtils.launchProgram(tempFile.getAbsolutePath())) {
                        DBWorkbench.getPlatformUI().showError("Open XLSX", "Can't open XLSX file '" + tempFile.getAbsolutePath() + "'");
                    }
                });
            } catch (Exception e) {
                DBWorkbench.getPlatformUI().showError("Error opening in Excel", null, e);
            }
            return Status.OK_STATUS;
        }
    };
    exportJob.schedule();
    return null;
}
Also used : StreamExporterAbstract(org.jkiss.dbeaver.tools.transfer.stream.exporter.StreamExporterAbstract) ArrayList(java.util.ArrayList) 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) ExecutionException(org.eclipse.core.commands.ExecutionException) DataExporterXLSX(org.jkiss.dbeaver.data.office.export.DataExporterXLSX) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DatabaseTransferProducer(org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer) DatabaseProducerSettings(org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat)

Example 4 with DBDDataFilter

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

the class SchedulerJobLogEditor method getEditorDataFilter.

@Override
protected DBDDataFilter getEditorDataFilter() {
    OracleSchedulerJob job = getDatabaseObject();
    OracleTableBase logView = getJobLogView();
    if (logView == null) {
        return null;
    }
    List<DBDAttributeConstraint> constraints = new ArrayList<>();
    try {
        DBRProgressMonitor monitor = new VoidProgressMonitor();
        OracleTableColumn ownerAttr = logView.getAttribute(monitor, "OWNER");
        if (ownerAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(ownerAttr, ownerAttr.getOrdinalPosition());
            ac.setVisible(false);
            ac.setOperator(DBCLogicalOperator.EQUALS);
            ac.setValue(job.getOwner());
            constraints.add(ac);
        }
        OracleTableColumn jobNameAttr = logView.getAttribute(monitor, "JOB_NAME");
        if (jobNameAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(jobNameAttr, jobNameAttr.getOrdinalPosition());
            ac.setVisible(false);
            ac.setOperator(DBCLogicalOperator.EQUALS);
            ac.setValue(job.getName());
            constraints.add(ac);
        }
        OracleTableColumn logDateAttr = logView.getAttribute(monitor, "LOG_DATE");
        if (logDateAttr != null) {
            DBDAttributeConstraint ac = new DBDAttributeConstraint(logDateAttr, logDateAttr.getOrdinalPosition());
            ac.setOrderPosition(1);
            ac.setOrderDescending(true);
            ac.setVisible(true);
            constraints.add(ac);
        }
    } catch (DBException e) {
        log.error(e);
    }
    return new DBDDataFilter(constraints);
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) ArrayList(java.util.ArrayList) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor) VoidProgressMonitor(org.jkiss.dbeaver.model.runtime.VoidProgressMonitor)

Example 5 with DBDDataFilter

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

the class SearchDataQuery method findRows.

private DBCStatistics findRows(@NotNull DBCSession session, @NotNull DBSDataContainer dataContainer, @NotNull TestDataReceiver dataReceiver) throws DBCException {
    DBSEntity entity;
    if (dataContainer instanceof DBSEntity) {
        entity = (DBSEntity) dataContainer;
    } else {
        log.warn("Data container " + dataContainer + " isn't entity");
        return null;
    }
    try {
        List<DBDAttributeConstraint> constraints = new ArrayList<>();
        for (DBSEntityAttribute attribute : CommonUtils.safeCollection(entity.getAttributes(session.getProgressMonitor()))) {
            if (params.fastSearch) {
                if (DBUtils.findAttributeIndex(session.getProgressMonitor(), attribute) == null) {
                    continue;
                }
            }
            if (DBUtils.isPseudoAttribute(attribute) || DBUtils.isHiddenObject(attribute)) {
                continue;
            }
            DBCLogicalOperator[] supportedOperators = DBUtils.getAttributeOperators(attribute);
            DBCLogicalOperator operator;
            Object value;
            switch(attribute.getDataKind()) {
                case BOOLEAN:
                    continue;
                case NUMERIC:
                    if (!params.searchNumbers) {
                        continue;
                    }
                    if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                        continue;
                    }
                    operator = DBCLogicalOperator.EQUALS;
                    try {
                        value = Integer.valueOf(params.searchString);
                    } catch (NumberFormatException e) {
                        try {
                            value = Long.valueOf(params.searchString);
                        } catch (NumberFormatException e1) {
                            try {
                                value = Double.valueOf(params.searchString);
                            } catch (NumberFormatException e2) {
                                try {
                                    value = new BigDecimal(params.searchString);
                                } catch (Exception e3) {
                                    // Not a number
                                    continue;
                                }
                            }
                        }
                    }
                    break;
                case CONTENT:
                case BINARY:
                    if (!params.searchLOBs) {
                        continue;
                    }
                case STRING:
                    if (!params.isCaseSensitive() && ArrayUtils.contains(supportedOperators, DBCLogicalOperator.ILIKE)) {
                        operator = DBCLogicalOperator.ILIKE;
                        value = "%" + params.searchString + "%";
                    } else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.LIKE)) {
                        operator = DBCLogicalOperator.LIKE;
                        value = "%" + params.searchString + "%";
                    } else if (ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                        operator = DBCLogicalOperator.EQUALS;
                        value = params.searchString;
                    } else {
                        continue;
                    }
                    break;
                default:
                    {
                        // On success search by exact match
                        if (!ArrayUtils.contains(supportedOperators, DBCLogicalOperator.EQUALS)) {
                            continue;
                        }
                        String typeName = attribute.getTypeName();
                        if (typeName.equals(DBConstants.TYPE_NAME_UUID) || typeName.equals(DBConstants.TYPE_NAME_UUID2)) {
                            try {
                                UUID uuid = UUID.fromString(params.searchString);
                                operator = DBCLogicalOperator.EQUALS;
                                value = uuid.toString();
                            } catch (Exception e) {
                                // No a UUID
                                continue;
                            }
                        } else {
                            continue;
                        }
                    }
            }
            DBDAttributeConstraint constraint = new DBDAttributeConstraint(attribute, constraints.size());
            constraint.setOperator(operator);
            constraint.setValue(value);
            constraint.setVisible(true);
            constraints.add(constraint);
        }
        if (constraints.isEmpty()) {
            return null;
        }
        dataReceiver.filter = new DBDDataFilter(constraints);
        dataReceiver.filter.setAnyConstraint(true);
        DBCExecutionSource searchSource = new AbstractExecutionSource(dataContainer, session.getExecutionContext(), this);
        return dataContainer.readData(searchSource, session, dataReceiver, dataReceiver.filter, -1, -1, 0, 0);
    } catch (DBException e) {
        throw new DBCException("Error finding rows", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) BigDecimal(java.math.BigDecimal) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) DBException(org.jkiss.dbeaver.DBException) DBDDataFilter(org.jkiss.dbeaver.model.data.DBDDataFilter) DBSEntityAttribute(org.jkiss.dbeaver.model.struct.DBSEntityAttribute) AbstractExecutionSource(org.jkiss.dbeaver.model.impl.AbstractExecutionSource) DBSEntity(org.jkiss.dbeaver.model.struct.DBSEntity)

Aggregations

DBDDataFilter (org.jkiss.dbeaver.model.data.DBDDataFilter)14 DBDAttributeConstraint (org.jkiss.dbeaver.model.data.DBDAttributeConstraint)8 DBException (org.jkiss.dbeaver.DBException)5 DBRProgressMonitor (org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)5 ArrayList (java.util.ArrayList)3 ExecutionException (org.eclipse.core.commands.ExecutionException)3 DBDAttributeBinding (org.jkiss.dbeaver.model.data.DBDAttributeBinding)3 AbstractJob (org.jkiss.dbeaver.model.runtime.AbstractJob)3 IDataTransferConsumer (org.jkiss.dbeaver.tools.transfer.IDataTransferConsumer)3 DatabaseProducerSettings (org.jkiss.dbeaver.tools.transfer.database.DatabaseProducerSettings)3 DatabaseTransferProducer (org.jkiss.dbeaver.tools.transfer.database.DatabaseTransferProducer)3 StreamConsumerSettings (org.jkiss.dbeaver.tools.transfer.stream.StreamConsumerSettings)3 StreamTransferConsumer (org.jkiss.dbeaver.tools.transfer.stream.StreamTransferConsumer)3 File (java.io.File)2 BigDecimal (java.math.BigDecimal)2 SimpleDateFormat (java.text.SimpleDateFormat)2 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)2 DBCExecutionContext (org.jkiss.dbeaver.model.exec.DBCExecutionContext)2 AbstractExecutionSource (org.jkiss.dbeaver.model.impl.AbstractExecutionSource)2 DBPPropertyDescriptor (org.jkiss.dbeaver.model.preferences.DBPPropertyDescriptor)2