Search in sources :

Example 66 with DBDAttributeBinding

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

the class ReferencesResultsContainer method refreshReferenceKeyList.

/**
 * Load list of referencing keys
 */
private void refreshReferenceKeyList() {
    activeReferenceKey = null;
    referenceKeys.clear();
    UIUtils.syncExec(() -> {
        dataViewer.clearData();
        fkCombo.removeAll();
        dataViewer.showEmptyPresentation();
    });
    List<DBDAttributeBinding> visibleAttributes = parentController.getModel().getVisibleAttributes();
    if (visibleAttributes.isEmpty()) {
        return;
    }
    parentDataContainer = parentController.getDataContainer();
    if (parentDataContainer == null) {
        return;
    }
    Set<DBSEntity> allEntities = new LinkedHashSet<>();
    for (DBDAttributeBinding attr : visibleAttributes) {
        DBSEntityAttribute entityAttribute = attr.getEntityAttribute();
        if (entityAttribute != null) {
            allEntities.add(entityAttribute.getParentObject());
        }
    }
    if (allEntities.isEmpty() && parentDataContainer instanceof DBSEntity) {
        allEntities.add((DBSEntity) parentDataContainer);
    }
    List<ReferenceKeyMemo> refKeyMemos = new ArrayList<>();
    {
        DBVEntity vEntityOwner = DBVUtils.getVirtualEntity(parentDataContainer, false);
        if (vEntityOwner != null) {
            Object activeAssociations = vEntityOwner.getProperty(V_PROP_ACTIVE_ASSOCIATIONS);
            if (activeAssociations instanceof Collection) {
                for (Object refKeyMemoMap : (Collection) activeAssociations) {
                    if (refKeyMemoMap instanceof Map) {
                        refKeyMemos.add(new ReferenceKeyMemo((Map) refKeyMemoMap));
                    }
                }
            }
        }
    }
    if (!allEntities.isEmpty()) {
        new AbstractJob("Load reference keys") {

            @Override
            protected IStatus run(DBRProgressMonitor monitor) {
                monitor.beginTask("Load references", allEntities.size());
                try {
                    List<ReferenceKey> refs = new ArrayList<>();
                    for (DBSEntity entity : allEntities) {
                        monitor.subTask(entity.getName());
                        if (entity instanceof DBVEntity) {
                            // Skip virtual entities
                            continue;
                        }
                        // Foreign keys
                        Collection<? extends DBSEntityAssociation> associations = DBVUtils.getAllAssociations(monitor, entity);
                        for (DBSEntityAssociation assoc : associations) {
                            if (assoc instanceof DBSEntityReferrer) {
                                List<? extends DBSEntityAttributeRef> attrs = ((DBSEntityReferrer) assoc).getAttributeReferences(monitor);
                                if (!CommonUtils.isEmpty(attrs)) {
                                    ReferenceKey referenceKey = new ReferenceKey(monitor, false, assoc.getAssociatedEntity(), assoc, attrs);
                                    refs.add(referenceKey);
                                }
                            }
                        }
                        // References
                        Collection<? extends DBSEntityAssociation> references = DBVUtils.getAllReferences(monitor, entity);
                        {
                            for (DBSEntityAssociation assoc : references) {
                                if (assoc instanceof DBSEntityReferrer) {
                                    List<? extends DBSEntityAttributeRef> attrs = ((DBSEntityReferrer) assoc).getAttributeReferences(monitor);
                                    if (!CommonUtils.isEmpty(attrs)) {
                                        ReferenceKey referenceKey = new ReferenceKey(monitor, true, entity, assoc, attrs);
                                        refs.add(referenceKey);
                                    }
                                }
                            }
                        }
                        monitor.worked(1);
                    }
                    synchronized (referenceKeys) {
                        referenceKeys.clear();
                        referenceKeys.addAll(refs);
                        // Detect active ref key from memo
                        if (!referenceKeys.isEmpty()) {
                            if (!refKeyMemos.isEmpty()) {
                                for (ReferenceKey key : referenceKeys) {
                                    for (ReferenceKeyMemo memo : refKeyMemos) {
                                        if (key.matches(memo)) {
                                            activeReferenceKey = key;
                                            break;
                                        }
                                    }
                                    if (activeReferenceKey != null)
                                        break;
                                }
                            }
                            if (activeReferenceKey == null) {
                                activeReferenceKey = referenceKeys.get(0);
                            }
                        }
                    }
                    UIUtils.syncExec(() -> fillKeysCombo());
                } catch (DBException e) {
                    log.debug("Error reading references", e);
                // Do not show errors. References or FKs may be unsupported by current database
                } finally {
                    monitor.done();
                }
                return Status.OK_STATUS;
            }
        }.schedule();
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) IStatus(org.eclipse.core.runtime.IStatus) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) DBVEntity(org.jkiss.dbeaver.model.virtual.DBVEntity) AbstractJob(org.jkiss.dbeaver.model.runtime.AbstractJob) DBRProgressMonitor(org.jkiss.dbeaver.model.runtime.DBRProgressMonitor)

Example 67 with DBDAttributeBinding

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

the class StatisticsPresentation method refreshData.

@Override
public void refreshData(boolean refreshMetadata, boolean append, boolean keepState) {
    table.removeAll();
    if (controller.getModel().getRowCount() > 0) {
        ResultSetRow row = controller.getModel().getRow(0);
        java.util.List<DBDAttributeBinding> visibleAttributes = controller.getModel().getVisibleAttributes();
        for (int i = 0; i < visibleAttributes.size(); i++) {
            DBDAttributeBinding attr = visibleAttributes.get(i);
            Object value = row.getValues()[i];
            String valueString = DBValueFormatting.getDefaultValueDisplayString(value, DBDDisplayFormat.UI);
            String[] lines = valueString.split("\n");
            for (int k = 0; k < lines.length; k++) {
                String line = lines[k];
                if (CommonUtils.isEmptyTrimmed(line)) {
                    continue;
                }
                line = line.replace("\t", "    ");
                TableItem item = new TableItem(table, SWT.LEFT);
                item.setText(0, k == 0 ? attr.getName() : "");
                item.setText(1, line);
                item.setData(attr);
            }
        }
    }
    UIUtils.packColumns(table);
}
Also used : TableItem(org.eclipse.swt.widgets.TableItem) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 68 with DBDAttributeBinding

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

the class SQLGeneratorSelectFromData method generateSQL.

@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, IResultSetController object) {
    for (ResultSetRow firstRow : getSelectedRows()) {
        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
        sql.append("SELECT ");
        boolean hasAttr = false;
        for (DBSAttributeBase attr : getAllAttributes(monitor, object)) {
            if (hasAttr)
                sql.append(", ");
            sql.append(DBUtils.getObjectFullName(attr, DBPEvaluationContext.DML));
            hasAttr = true;
        }
        sql.append(getLineSeparator()).append("FROM ").append(getEntityName(getSingleEntity()));
        sql.append(getLineSeparator()).append("WHERE ");
        hasAttr = false;
        for (DBDAttributeBinding binding : keyAttributes) {
            if (hasAttr)
                sql.append(" AND ");
            appendValueCondition(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)

Example 69 with DBDAttributeBinding

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

the class SQLGeneratorDeleteFromData method generateSQL.

@Override
public void generateSQL(DBRProgressMonitor monitor, StringBuilder sql, IResultSetController object) {
    for (ResultSetRow firstRow : getSelectedRows()) {
        Collection<DBDAttributeBinding> keyAttributes = getKeyAttributes(monitor, object);
        sql.append("DELETE FROM ").append(getEntityName(getSingleEntity()));
        sql.append(getLineSeparator()).append("WHERE ");
        boolean hasAttr = false;
        for (DBDAttributeBinding binding : keyAttributes) {
            if (hasAttr)
                sql.append(" AND ");
            appendValueCondition(getController(), sql, binding, firstRow);
            hasAttr = true;
        }
        sql.append(";\n");
    }
}
Also used : ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding)

Example 70 with DBDAttributeBinding

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

the class SpreadsheetFindReplaceTarget method replaceSelection.

@Override
public void replaceSelection(String text, boolean regExReplace) {
    GridPos selection = (GridPos) owner.getSelection().getFirstElement();
    if (selection == null) {
        return;
    }
    GridCell cell = owner.getSpreadsheet().posToCell(selection);
    if (cell == null) {
        return;
    }
    String oldValue = CommonUtils.toString(owner.getSpreadsheet().getContentProvider().getCellValue(cell.col, cell.row, true, true));
    String newValue = text;
    if (searchPattern != null) {
        newValue = searchPattern.matcher(oldValue).replaceAll(newValue);
    }
    boolean recordMode = owner.getController().isRecordMode();
    final DBDAttributeBinding attr = (DBDAttributeBinding) (recordMode ? cell.row : cell.col);
    final ResultSetRow row = (ResultSetRow) (recordMode ? cell.col : cell.row);
    owner.getController().getModel().updateCellValue(attr, row, newValue);
    owner.getController().updatePanelsContent(false);
}
Also used : GridPos(org.jkiss.dbeaver.ui.controls.lightgrid.GridPos) ResultSetRow(org.jkiss.dbeaver.ui.controls.resultset.ResultSetRow) DBDAttributeBinding(org.jkiss.dbeaver.model.data.DBDAttributeBinding) GridCell(org.jkiss.dbeaver.ui.controls.lightgrid.GridCell)

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