Skip to content

Commit

Permalink
added query to materialized views
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonioG70 committed Jan 2, 2025
1 parent b29577b commit 76c525f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,8 @@ private Constants() {

/* Virtual Table */
public static final String BLOB_COLUMN_NAME = "Document";

/* View Types */
public static final String TYPE_VIEW = "VIEW";
public static final String TYPE_MATERIALIZED_VIEW = "MATERIALIZED VIEW";
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public class ViewStructure {

private List<ColumnStructure> columns;

private String viewType;


/**
*
*/
Expand Down Expand Up @@ -132,6 +135,21 @@ public ColumnStructure getColumnByName(String columnName) {
return null;
}

/**
* @return the view type
*/
public String getViewType() {
return viewType;
}

/**
* @param viewType
* the view type to set
*/
public void setViewType(String viewType) {
this.viewType = viewType;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -724,31 +724,39 @@ protected List<TableStructure> getTables(SchemaStructure schema) throws SQLExcep
protected List<ViewStructure> getViews(String schemaName) throws SQLException, ModuleException {
List<ViewStructure> views = new ArrayList<>();
if (!getModuleConfiguration().ignoreViews()) {
try (ResultSet rset = getMetadata().getTables(dbStructure.getName(), schemaName, "%", new String[] {"VIEW", "MATERIALIZED VIEW"})) {
while (rset.next()) {
String viewName = rset.getString(3);
if (getModuleConfiguration().isSelectedView(schemaName, viewName)) {
ViewStructure view = new ViewStructure();
view.setName(viewName);
view.setDescription(rset.getString(5));
try {
view.setColumns(getColumns(schemaName, viewName));
} catch (SQLException e) {
reporter.ignored("Columns from view " + viewName + " in schema " + schemaName,
"there was a problem retrieving them form the database");
}
if (view.getColumns().isEmpty()) {
reporter.ignored("View " + viewName + " in schema " + schemaName, "it contains no columns");
} else {
views.add(view);
}
getViewByType(schemaName, "%", Constants.TYPE_VIEW, views);
getViewByType(schemaName, "%", Constants.TYPE_MATERIALIZED_VIEW, views);
views.addAll(getCustomViews(schemaName));
}

return views;
}

protected void getViewByType(String schemaName, String tableNamePattern, String type, List<ViewStructure> views)
throws SQLException, ModuleException {
try (ResultSet rset = getMetadata().getTables(dbStructure.getName(), schemaName, tableNamePattern,
new String[] {type})) {
while (rset.next()) {
String viewName = rset.getString(3);
if (getModuleConfiguration().isSelectedView(schemaName, viewName)) {
ViewStructure view = new ViewStructure();
view.setName(viewName);
view.setDescription(rset.getString(5));
view.setViewType(type);
try {
view.setColumns(getColumns(schemaName, viewName));
} catch (SQLException e) {
reporter.ignored("Columns from view " + viewName + " in schema " + schemaName,
"there was a problem retrieving them form the database");
}
if (view.getColumns().isEmpty()) {
reporter.ignored("View " + viewName + " in schema " + schemaName, "it contains no columns");
} else {
views.add(view);
}
}
views.addAll(getCustomViews(schemaName));
}
}

return views;
}

protected List<ViewStructure> getCustomViews(String schemaName) throws ModuleException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,9 @@ public String getViewQueryOriginal(String schemaName, String viewName) {
return "SELECT definition FROM pg_views WHERE schemaname='" + schemaName + "'AND viewname='" + viewName
+ "'";
}

public String getMaterializedViewQueryOriginal(String schemaName, String viewName) {
return "SELECT definition FROM pg_matviews WHERE schemaname='" + schemaName + "'AND matviewname='" + viewName
+ "'";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.List;
import java.util.Set;

import com.databasepreservation.Constants;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.postgresql.PGConnection;
Expand Down Expand Up @@ -176,7 +177,13 @@ protected List<ViewStructure> getViews(String schemaName) throws SQLException, M
if (v.getQueryOriginal() == null || v.getQueryOriginal().isEmpty()) {
try {
statement = getConnection().createStatement();
String query = ((PostgreSQLHelper) sqlHelper).getViewQueryOriginal(schemaName, v.getName());
String query = "";

if (v.getViewType().equals(Constants.TYPE_MATERIALIZED_VIEW)) {
query = ((PostgreSQLHelper) sqlHelper).getMaterializedViewQueryOriginal(schemaName, v.getName());
} else if (v.getViewType().equals(Constants.TYPE_VIEW)) {
query = ((PostgreSQLHelper) sqlHelper).getViewQueryOriginal(schemaName, v.getName());
}

rset = statement.executeQuery(query);
rset.next(); // Returns only one tuple
Expand Down

0 comments on commit 76c525f

Please sign in to comment.