From 9c4f69c395030aaf044bc5eb83e4f03b1da90ebb Mon Sep 17 00:00:00 2001 From: Serge Rider Date: Wed, 7 Aug 2019 16:20:51 +0200 Subject: [PATCH] Dictionary/enum reading refactoring (support cross-database queries) Former-commit-id: 4f1ee1c74e0aa2bf6822b2ef06b7332c441b0c4d --- .../model/impl/jdbc/struct/JDBCTable.java | 105 +++++++++--------- .../dbeaver/model/struct/DBSDictionary.java | 8 +- .../dbeaver/model/virtual/DBVEntity.java | 24 ++-- .../valuefilter/GenericFilterValueEdit.java | 33 +++--- .../ui/data/editors/ReferenceValueEditor.java | 27 ++--- 5 files changed, 103 insertions(+), 94 deletions(-) diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java index 685eb6fa73..fc1d245922 100644 --- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java +++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/jdbc/struct/JDBCTable.java @@ -24,6 +24,7 @@ import org.jkiss.dbeaver.ModelPreferences; import org.jkiss.dbeaver.model.*; import org.jkiss.dbeaver.model.data.*; import org.jkiss.dbeaver.model.exec.*; +import org.jkiss.dbeaver.model.exec.jdbc.JDBCSession; import org.jkiss.dbeaver.model.exec.jdbc.JDBCStatement; import org.jkiss.dbeaver.model.impl.DBObjectNameCaseTransformer; import org.jkiss.dbeaver.model.impl.data.ExecuteBatchImpl; @@ -554,7 +555,7 @@ public abstract class JDBCTable getDictionaryEnumeration( - @NotNull DBCSession session, + @NotNull DBRProgressMonitor monitor, @NotNull DBSEntityAttribute keyColumn, Object keyPattern, List preceedingKeys, @@ -576,7 +577,7 @@ public abstract class JDBCTable getDictionaryValues( - @NotNull DBCSession session, + @NotNull DBRProgressMonitor monitor, @NotNull DBSEntityAttribute keyColumn, @NotNull List keyValues, @Nullable List preceedingKeys, boolean sortByValue, boolean sortAsc) throws DBException { - DBDValueHandler keyValueHandler = DBUtils.findValueHandler(session, keyColumn); + DBDValueHandler keyValueHandler = DBUtils.findValueHandler(keyColumn.getDataSource(), keyColumn); StringBuilder query = new StringBuilder(); query.append("SELECT ").append(DBUtils.getQuotedIdentifier(keyColumn)); - String descColumns = DBVUtils.getDictionaryDescriptionColumns(session.getProgressMonitor(), keyColumn); + String descColumns = DBVUtils.getDictionaryDescriptionColumns(monitor, keyColumn); if (descColumns != null) { query.append(", ").append(descColumns); } @@ -633,30 +634,32 @@ public abstract class JDBCTable readKeyEnumeration( - DBCSession session, + DBRProgressMonitor monitor, DBSEntityAttribute keyColumn, Object keyPattern, List preceedingKeys, @@ -669,7 +672,7 @@ public abstract class JDBCTable descAttributes = null; if (descColumns != null) { - descAttributes = DBVEntity.getDescriptionColumns(session.getProgressMonitor(), this, descColumns); + descAttributes = DBVEntity.getDescriptionColumns(monitor, this, descColumns); query.append(", ").append(descColumns); } query.append(" FROM ").append(DBUtils.getObjectFullName(this, DBPEvaluationContext.DML)); @@ -791,38 +794,40 @@ public abstract class JDBCTable getDictionaryEnumeration( - @NotNull DBCSession session, + @NotNull DBRProgressMonitor monitor, @NotNull DBSEntityAttribute keyColumn, Object keyPattern, @Nullable List preceedingKeys, @@ -62,7 +62,7 @@ public interface DBSDictionary @NotNull List getDictionaryValues( - @NotNull DBCSession session, + @NotNull DBRProgressMonitor monitor, @NotNull DBSEntityAttribute keyColumn, @NotNull List keyValues, @Nullable List preceedingKeys, diff --git a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/virtual/DBVEntity.java b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/virtual/DBVEntity.java index 5340f3931e..4ed2bef9de 100644 --- a/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/virtual/DBVEntity.java +++ b/plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/virtual/DBVEntity.java @@ -602,19 +602,27 @@ public class DBVEntity extends DBVObject implements DBSEntity, DBPQualifiedObjec @NotNull @Override - public List getDictionaryEnumeration(@NotNull DBCSession session, @NotNull DBSEntityAttribute keyColumn, Object keyPattern, @Nullable List preceedingKeys, boolean sortByValue, boolean sortAsc, int maxResults) throws DBException { - DBSEntity realEntity = getRealEntity(session.getProgressMonitor()); - return realEntity instanceof DBSDictionary ? - ((DBSDictionary) realEntity).getDictionaryEnumeration(session, keyColumn, keyPattern, preceedingKeys, sortByValue, sortAsc, maxResults) : - Collections.emptyList(); + public List getDictionaryEnumeration(@NotNull DBRProgressMonitor monitor, @NotNull DBSEntityAttribute keyColumn, Object keyPattern, @Nullable List preceedingKeys, boolean sortByValue, boolean sortAsc, int maxResults) throws DBException { + DBSEntity realEntity = getRealEntity(monitor); + if (realEntity instanceof DBSDictionary) { + return ((DBSDictionary) realEntity).getDictionaryEnumeration( + monitor, + keyColumn, + keyPattern, + preceedingKeys, + sortByValue, + sortAsc, + maxResults); + } + return Collections.emptyList(); } @NotNull @Override - public List getDictionaryValues(@NotNull DBCSession session, @NotNull DBSEntityAttribute keyColumn, @NotNull List keyValues, @Nullable List preceedingKeys, boolean sortByValue, boolean sortAsc) throws DBException { - DBSEntity realEntity = getRealEntity(session.getProgressMonitor()); + public List getDictionaryValues(@NotNull DBRProgressMonitor monitor, @NotNull DBSEntityAttribute keyColumn, @NotNull List keyValues, @Nullable List preceedingKeys, boolean sortByValue, boolean sortAsc) throws DBException { + DBSEntity realEntity = getRealEntity(monitor); return realEntity instanceof DBSDictionary ? - ((DBSDictionary) realEntity).getDictionaryValues(session, keyColumn, keyValues, preceedingKeys, sortByValue, sortAsc) : + ((DBSDictionary) realEntity).getDictionaryValues(monitor, keyColumn, keyValues, preceedingKeys, sortByValue, sortAsc) : Collections.emptyList(); } } diff --git a/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/controls/resultset/valuefilter/GenericFilterValueEdit.java b/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/controls/resultset/valuefilter/GenericFilterValueEdit.java index bdebe829cd..40bebb1828 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/controls/resultset/valuefilter/GenericFilterValueEdit.java +++ b/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/controls/resultset/valuefilter/GenericFilterValueEdit.java @@ -39,7 +39,6 @@ import org.jkiss.dbeaver.model.data.DBDAttributeConstraint; import org.jkiss.dbeaver.model.data.DBDDisplayFormat; import org.jkiss.dbeaver.model.data.DBDLabelValuePair; import org.jkiss.dbeaver.model.exec.DBCExecutionContext; -import org.jkiss.dbeaver.model.exec.DBCExecutionPurpose; import org.jkiss.dbeaver.model.exec.DBCLogicalOperator; import org.jkiss.dbeaver.model.exec.DBCSession; import org.jkiss.dbeaver.model.runtime.AbstractJob; @@ -181,12 +180,12 @@ class GenericFilterValueEdit { private void loadConstraintEnum(final DBSEntityReferrer refConstraint) { loadJob = new KeyLoadJob("Load constraint '" + refConstraint.getName() + "' values") { @Override - List readEnumeration(DBCSession session) throws DBException { + List readEnumeration(DBRProgressMonitor monitor) throws DBException { final DBSEntityAttribute tableColumn = attr.getEntityAttribute(); if (tableColumn == null) { return null; } - final DBSEntityAttributeRef fkColumn = DBUtils.getConstraintAttribute(session.getProgressMonitor(), refConstraint, tableColumn); + final DBSEntityAttributeRef fkColumn = DBUtils.getConstraintAttribute(monitor, refConstraint, tableColumn); if (fkColumn == null) { return null; } @@ -196,7 +195,7 @@ class GenericFilterValueEdit { } else { return null; } - final DBSEntityAttribute refColumn = DBUtils.getReferenceAttribute(session.getProgressMonitor(), association, tableColumn, false); + final DBSEntityAttribute refColumn = DBUtils.getReferenceAttribute(monitor, association, tableColumn, false); if (refColumn == null) { return null; } @@ -205,13 +204,13 @@ class GenericFilterValueEdit { final DBSDictionary enumConstraint = (DBSDictionary) refConstraint.getParentObject(); if (fkAttribute != null && enumConstraint != null) { return enumConstraint.getDictionaryEnumeration( - session, - refColumn, - filterPattern, - null, - true, - true, - MAX_MULTI_VALUES); + monitor, + refColumn, + filterPattern, + null, + true, + true, + MAX_MULTI_VALUES); } return null; } @@ -226,8 +225,10 @@ class GenericFilterValueEdit { tableViewer.getTable().getColumn(1).setText("Count"); loadJob = new KeyLoadJob("Load '" + attr.getName() + "' values") { @Override - List readEnumeration(DBCSession session) throws DBException { - return attributeEnumerable.getValueEnumeration(session, filterPattern, MAX_MULTI_VALUES); + List readEnumeration(DBRProgressMonitor monitor) throws DBException { + try (DBCSession session = DBUtils.openUtilSession(monitor, attributeEnumerable, "Read value enumeration")) { + return attributeEnumerable.getValueEnumeration(session, filterPattern, MAX_MULTI_VALUES); + } } }; loadJob.schedule(); @@ -382,8 +383,8 @@ class GenericFilterValueEdit { if (executionContext == null) { return Status.OK_STATUS; } - try (DBCSession session = executionContext.openSession(monitor, DBCExecutionPurpose.UTIL, "Read value enumeration")) { - final List valueEnumeration = readEnumeration(session); + try { + final List valueEnumeration = readEnumeration(monitor); if (valueEnumeration == null) { return Status.OK_STATUS; } else { @@ -397,7 +398,7 @@ class GenericFilterValueEdit { } @Nullable - abstract List readEnumeration(DBCSession session) throws DBException; + abstract List readEnumeration(DBRProgressMonitor monitor) throws DBException; boolean mergeResultsWithData() { return CommonUtils.isEmpty(filterPattern); diff --git a/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/data/editors/ReferenceValueEditor.java b/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/data/editors/ReferenceValueEditor.java index 4f3648dc3f..7fdbe389b4 100644 --- a/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/data/editors/ReferenceValueEditor.java +++ b/plugins/org.jkiss.dbeaver.ui.editors.data/src/org/jkiss/dbeaver/ui/data/editors/ReferenceValueEditor.java @@ -491,27 +491,22 @@ public class ReferenceValueEditor { final DBSEntityConstraint refConstraint = association.getReferencedConstraint(); final DBSDictionary enumConstraint = (DBSDictionary) refConstraint.getParentObject(); if (fkAttribute != null && enumConstraint != null) { - try (DBCSession session = valueController.getExecutionContext().openSession( + Collection enumValues = enumConstraint.getDictionaryEnumeration( monitor, - DBCExecutionPurpose.UTIL, - NLS.bind(ResultSetMessages.dialog_value_view_context_name, fkAttribute.getName()))) { - Collection enumValues = enumConstraint.getDictionaryEnumeration( - session, - refColumn, - pattern, - precedingKeys, - sortByValue, - sortAsc, - 200); + refColumn, + pattern, + precedingKeys, + sortByValue, + sortAsc, + 200); // for (DBDLabelValuePair pair : enumValues) { // keyValues.put(pair.getValue(), pair.getLabel()); // } - if (monitor.isCanceled()) { - return null; - } - final DBDValueHandler colHandler = DBUtils.findValueHandler(session, fkAttribute); - return new EnumValuesData(enumValues, fkColumn, colHandler); + if (monitor.isCanceled()) { + return null; } + final DBDValueHandler colHandler = DBUtils.findValueHandler(fkAttribute.getDataSource(), fkAttribute); + return new EnumValuesData(enumValues, fkColumn, colHandler); } } catch (DBException e) { -- GitLab