Skip to content

Commit 531df80

Browse files
AntonioG70hmiguim
authored andcommitted
added query to materialized views
1 parent c5bc127 commit 531df80

File tree

5 files changed

+64
-22
lines changed

5 files changed

+64
-22
lines changed

dbptk-model/src/main/java/com/databasepreservation/Constants.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,8 @@ private Constants() {
164164

165165
/* Virtual Table */
166166
public static final String BLOB_COLUMN_NAME = "Document";
167+
168+
/* View Types */
169+
public static final String TYPE_VIEW = "VIEW";
170+
public static final String TYPE_MATERIALIZED_VIEW = "MATERIALIZED VIEW";
167171
}

dbptk-model/src/main/java/com/databasepreservation/model/structure/ViewStructure.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ public class ViewStructure {
2727

2828
private List<ColumnStructure> columns;
2929

30+
private String viewType;
31+
32+
3033
/**
3134
*
3235
*/
@@ -132,6 +135,21 @@ public ColumnStructure getColumnByName(String columnName) {
132135
return null;
133136
}
134137

138+
/**
139+
* @return the view type
140+
*/
141+
public String getViewType() {
142+
return viewType;
143+
}
144+
145+
/**
146+
* @param viewType
147+
* the view type to set
148+
*/
149+
public void setViewType(String viewType) {
150+
this.viewType = viewType;
151+
}
152+
135153
@Override
136154
public String toString() {
137155
StringBuilder builder = new StringBuilder();

dbptk-modules/dbptk-module-jdbc/src/main/java/com/databasepreservation/modules/jdbc/in/JDBCImportModule.java

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -724,31 +724,39 @@ protected List<TableStructure> getTables(SchemaStructure schema) throws SQLExcep
724724
protected List<ViewStructure> getViews(String schemaName) throws SQLException, ModuleException {
725725
List<ViewStructure> views = new ArrayList<>();
726726
if (!getModuleConfiguration().ignoreViews()) {
727-
try (ResultSet rset = getMetadata().getTables(dbStructure.getName(), schemaName, "%", new String[] {"VIEW", "MATERIALIZED VIEW"})) {
728-
while (rset.next()) {
729-
String viewName = rset.getString(3);
730-
if (getModuleConfiguration().isSelectedView(schemaName, viewName)) {
731-
ViewStructure view = new ViewStructure();
732-
view.setName(viewName);
733-
view.setDescription(rset.getString(5));
734-
try {
735-
view.setColumns(getColumns(schemaName, viewName));
736-
} catch (SQLException e) {
737-
reporter.ignored("Columns from view " + viewName + " in schema " + schemaName,
738-
"there was a problem retrieving them form the database");
739-
}
740-
if (view.getColumns().isEmpty()) {
741-
reporter.ignored("View " + viewName + " in schema " + schemaName, "it contains no columns");
742-
} else {
743-
views.add(view);
744-
}
727+
getViewByType(schemaName, "%", Constants.TYPE_VIEW, views);
728+
getViewByType(schemaName, "%", Constants.TYPE_MATERIALIZED_VIEW, views);
729+
views.addAll(getCustomViews(schemaName));
730+
}
731+
732+
return views;
733+
}
734+
735+
protected void getViewByType(String schemaName, String tableNamePattern, String type, List<ViewStructure> views)
736+
throws SQLException, ModuleException {
737+
try (ResultSet rset = getMetadata().getTables(dbStructure.getName(), schemaName, tableNamePattern,
738+
new String[] {type})) {
739+
while (rset.next()) {
740+
String viewName = rset.getString(3);
741+
if (getModuleConfiguration().isSelectedView(schemaName, viewName)) {
742+
ViewStructure view = new ViewStructure();
743+
view.setName(viewName);
744+
view.setDescription(rset.getString(5));
745+
view.setViewType(type);
746+
try {
747+
view.setColumns(getColumns(schemaName, viewName));
748+
} catch (SQLException e) {
749+
reporter.ignored("Columns from view " + viewName + " in schema " + schemaName,
750+
"there was a problem retrieving them form the database");
751+
}
752+
if (view.getColumns().isEmpty()) {
753+
reporter.ignored("View " + viewName + " in schema " + schemaName, "it contains no columns");
754+
} else {
755+
views.add(view);
745756
}
746757
}
747-
views.addAll(getCustomViews(schemaName));
748758
}
749759
}
750-
751-
return views;
752760
}
753761

754762
protected List<ViewStructure> getCustomViews(String schemaName) throws ModuleException {

dbptk-modules/dbptk-module-postgresql/src/main/java/com/databasepreservation/modules/postgresql/PostgreSQLHelper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,9 @@ public String getViewQueryOriginal(String schemaName, String viewName) {
187187
return "SELECT definition FROM pg_views WHERE schemaname='" + schemaName + "'AND viewname='" + viewName
188188
+ "'";
189189
}
190+
191+
public String getMaterializedViewQueryOriginal(String schemaName, String viewName) {
192+
return "SELECT definition FROM pg_matviews WHERE schemaname='" + schemaName + "'AND matviewname='" + viewName
193+
+ "'";
194+
}
190195
}

dbptk-modules/dbptk-module-postgresql/src/main/java/com/databasepreservation/modules/postgresql/in/PostgreSQLJDBCImportModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.List;
2828
import java.util.Set;
2929

30+
import com.databasepreservation.Constants;
3031
import org.apache.commons.lang3.RandomStringUtils;
3132
import org.apache.commons.lang3.StringUtils;
3233
import org.postgresql.PGConnection;
@@ -176,7 +177,13 @@ protected List<ViewStructure> getViews(String schemaName) throws SQLException, M
176177
if (v.getQueryOriginal() == null || v.getQueryOriginal().isEmpty()) {
177178
try {
178179
statement = getConnection().createStatement();
179-
String query = ((PostgreSQLHelper) sqlHelper).getViewQueryOriginal(schemaName, v.getName());
180+
String query = "";
181+
182+
if (v.getViewType().equals(Constants.TYPE_MATERIALIZED_VIEW)) {
183+
query = ((PostgreSQLHelper) sqlHelper).getMaterializedViewQueryOriginal(schemaName, v.getName());
184+
} else if (v.getViewType().equals(Constants.TYPE_VIEW)) {
185+
query = ((PostgreSQLHelper) sqlHelper).getViewQueryOriginal(schemaName, v.getName());
186+
}
180187

181188
rset = statement.executeQuery(query);
182189
rset.next(); // Returns only one tuple

0 commit comments

Comments
 (0)