diff --git a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreViewBase.java b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreViewBase.java index 3d69b97ce027..9ee029d04510 100644 --- a/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreViewBase.java +++ b/plugins/org.jkiss.dbeaver.ext.postgresql/src/org/jkiss/dbeaver/ext/postgresql/model/PostgreViewBase.java @@ -48,72 +48,57 @@ /** * PostgreViewBase */ -public abstract class PostgreViewBase extends PostgreTableReal implements DBSView -{ - private String source; +public abstract class PostgreViewBase extends PostgreTableReal implements DBSView { + private String viewQueryResult; - public PostgreViewBase(PostgreSchema catalog) - { + public PostgreViewBase(PostgreSchema catalog) { super(catalog); } public PostgreViewBase( PostgreSchema catalog, - ResultSet dbResult) - { + ResultSet dbResult) { super(catalog, dbResult); } @NotNull @Property(viewable = true, editable = true, valueTransformer = DBObjectNameCaseTransformer.class, order = 1) @Override - public String getName() - { + public String getName() { return super.getName(); } @Override - public boolean isView() - { + public boolean isView() { return true; } @Override - public Collection getIndexes(DBRProgressMonitor monitor) throws DBException - { + public Collection getIndexes(DBRProgressMonitor monitor) throws DBException { return null; } - public String getSource() { - return source; - } - @Override @Property(hidden = true, editable = true, updatable = true, order = -1) - public String getObjectDefinitionText(DBRProgressMonitor monitor, Map options) throws DBException - { - if (source == null) { - if (isPersisted()) { - source = getDataSource().getServerType().readViewDDL(monitor, this); - if (source == null) { - try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read view definition")) { - // Do not use view id as a parameter. For some reason it doesn't work for Redshift - String definition = JDBCUtils.queryString(session, "SELECT pg_get_viewdef(" + getObjectId() + ", true)"); - if (definition == null) { - throw new DBException("View '" + getName() + "' doesn't exist"); - } - this.source = PostgreUtils.getViewDDL(monitor, this, definition, options); - String extDefinition = readExtraDefinition(session, options); - if (extDefinition != null) { - this.source += "\n" + extDefinition; - } - } catch (SQLException e) { - throw new DBException("Error reading view definition: " + e.getMessage(), e); + public String getObjectDefinitionText(DBRProgressMonitor monitor, Map options) throws DBException { + String source; + if (isPersisted()) { + source = getDataSource().getServerType().readViewDDL(monitor, this); + if (source == null) { + try (JDBCSession session = DBUtils.openMetaSession(monitor, this, "Read view definition")) { + + fetchViewQueryResult(session); + source = PostgreUtils.getViewDDL(monitor, this, viewQueryResult, options); + String extDefinition = readExtraDefinition(session, options); + if (extDefinition != null) { + source += "\n" + extDefinition; } + } catch (SQLException e) { + throw new DBException("Error reading view definition: " + e.getMessage(), e); } - } else { - source = ""; } + } else { + source = ""; } List actions = new ArrayList<>(); @@ -155,18 +140,28 @@ public String getObjectDefinitionText(DBRProgressMonitor monitor, Map options) throws DBException { return null; } @Override public void setObjectDefinitionText(String sourceText) { - this.source = sourceText; + viewQueryResult = sourceText; } @Override public DBSObject refreshObject(@NotNull DBRProgressMonitor monitor) throws DBException { - this.source = null; + this.viewQueryResult = null; return super.refreshObject(monitor); } }