Skip to content

Commit

Permalink
feat: Add support for REFRESH MATERIALIZED VIEW (#1911)
Browse files Browse the repository at this point in the history
* Support REFRESH MATERIALIZED VIEW

* Fix

* Rename

* Rename

* fmt

* fmt

* Add test

* Fix jjt

* Fix jjt

* Fix jjt

* typo

* rebase master
  • Loading branch information
jxnu-liguobin authored Dec 11, 2023
1 parent 085d750 commit 425c72e
Show file tree
Hide file tree
Showing 20 changed files with 483 additions and 42 deletions.
9 changes: 9 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/feature/Feature.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import net.sf.jsqlparser.statement.execute.Execute;
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.Fetch;
import net.sf.jsqlparser.statement.select.First;
import net.sf.jsqlparser.statement.select.KSQLWindow;
Expand Down Expand Up @@ -391,6 +392,14 @@ public enum Feature {
* @see AlterView
*/
alterView,

/**
* SQL "REFRESH MATERIALIZED VIEW" statement is allowed
*
* @see RefreshMaterializedViewStatement
*/
refreshMaterializedView, refreshMaterializedWithDataView, refreshMaterializedWithNoDataView,

/**
* SQL "REPLACE VIEW" statement is allowed
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
Expand Down Expand Up @@ -68,6 +69,8 @@ public interface StatementVisitor {

void visit(AlterView alterView);

void visit(RefreshMaterializedViewStatement materializedView);

void visit(Alter alter);

void visit(Statements stmts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
import net.sf.jsqlparser.statement.show.ShowTablesStatement;
Expand Down Expand Up @@ -222,4 +223,9 @@ public void visit(AlterSystemStatement alterSystemStatement) {}
public void visit(UnsupportedStatement unsupportedStatement) {

}

@Override
public void visit(RefreshMaterializedViewStatement materializedView) {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.refresh;

import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;

/**
* REFRESH MATERIALIZED VIEW [ CONCURRENTLY ] name [ WITH [ NO ] DATA ]
* <p>
* https://www.postgresql.org/docs/16/sql-refreshmaterializedview.html
*
* @author jxnu-liguobin
*/

public class RefreshMaterializedViewStatement implements Statement {

private Table view;
private RefreshMode refreshMode;
private boolean concurrently = false;

public RefreshMaterializedViewStatement() {}

public RefreshMaterializedViewStatement(Table view, boolean concurrently,
RefreshMode refreshMode) {
this.refreshMode = refreshMode;
this.concurrently = concurrently;
this.view = view;
}

public Table getView() {
return view;
}

public void setView(Table view) {
this.view = view;
}

public RefreshMode getRefreshMode() {
return refreshMode;
}

public void setRefreshMode(RefreshMode refreshMode) {
this.refreshMode = refreshMode;
}

public boolean isConcurrently() {
return concurrently;
}

public void setConcurrently(boolean concurrently) {
this.concurrently = concurrently;
}

@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("REFRESH MATERIALIZED VIEW ");
if (this.refreshMode == null) {
if (concurrently) {
builder.append("CONCURRENTLY ");
}
builder.append(view);
return builder.toString();
}
switch (this.refreshMode) {
case WITH_DATA:
if (concurrently) {
builder.append("CONCURRENTLY ");
}
builder.append(view);
builder.append(" WITH DATA");
break;
case WITH_NO_DATA:
builder.append(view);
if (!concurrently) {
builder.append(" WITH NO DATA");
}
break;
}
return builder.toString();
}

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

public RefreshMaterializedViewStatement withTableName(Table view) {
this.setView(view);
return this;
}

public RefreshMaterializedViewStatement withConcurrently(boolean concurrently) {
this.setConcurrently(concurrently);
return this;
}
}
19 changes: 19 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/refresh/RefreshMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2022 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.refresh;

public enum RefreshMode {

DEFAULT, WITH_DATA, WITH_NO_DATA;

public static RefreshMode from(String type) {
return Enum.valueOf(RefreshMode.class, type.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2023 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.select;

/**
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
package net.sf.jsqlparser.util;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.AllValue;
import net.sf.jsqlparser.expression.AnalyticExpression;
Expand Down Expand Up @@ -146,6 +151,7 @@
import net.sf.jsqlparser.statement.grant.Grant;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.AllColumns;
import net.sf.jsqlparser.statement.select.AllTableColumns;
import net.sf.jsqlparser.statement.select.FromItemVisitor;
Expand All @@ -169,12 +175,6 @@
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Find all used tables within an select statement.
*
Expand Down Expand Up @@ -986,6 +986,11 @@ public void visit(AlterView alterView) {
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
}

@Override
public void visit(RefreshMaterializedViewStatement materializedView) {
visit(materializedView.getView());
}

@Override
public void visit(TimeKeyExpression timeKeyExpression) {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.util.deparser;

import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;

/**
* @author jxnu-liguobin
*/

public class RefreshMaterializedViewStatementDeParser
extends AbstractDeParser<RefreshMaterializedViewStatement> {

public RefreshMaterializedViewStatementDeParser(StringBuilder buffer) {
super(buffer);
}

@SuppressWarnings("PMD.SwitchStmtsShouldHaveDefault")
@Override
public void deParse(RefreshMaterializedViewStatement view) {
buffer.append("REFRESH MATERIALIZED VIEW ");
if (view.getRefreshMode() == null) {
if (view.isConcurrently()) {
buffer.append("CONCURRENTLY ");
}
buffer.append(view.getView());
return;
}
switch (view.getRefreshMode()) {
case WITH_DATA:
if (view.isConcurrently()) {
buffer.append("CONCURRENTLY ");
}
buffer.append(view.getView());
buffer.append(" WITH DATA");
break;
case WITH_NO_DATA:
buffer.append(view.getView());
if (view.isConcurrently()) {
buffer.append(" WITH NO DATA");
}
break;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
package net.sf.jsqlparser.util.deparser;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.CreateFunctionalStatement;
Expand Down Expand Up @@ -50,6 +53,7 @@
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.merge.MergeInsert;
import net.sf.jsqlparser.statement.merge.MergeUpdate;
import net.sf.jsqlparser.statement.refresh.RefreshMaterializedViewStatement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.WithItem;
import net.sf.jsqlparser.statement.show.ShowIndexStatement;
Expand All @@ -58,10 +62,6 @@
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;

import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

public class StatementDeParser extends AbstractDeParser<Statement> implements StatementVisitor {

private final ExpressionDeParser expressionDeParser;
Expand Down Expand Up @@ -104,6 +104,11 @@ public void visit(CreateView createView) {
createViewDeParser.deParse(createView);
}

@Override
public void visit(RefreshMaterializedViewStatement materializedViewStatement) {
new RefreshMaterializedViewStatementDeParser(buffer).deParse(materializedViewStatement);
}

@Override
public void visit(AlterView alterView) {
AlterViewDeParser alterViewDeParser = new AlterViewDeParser(buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.Set;

import net.sf.jsqlparser.parser.feature.Feature;

/**
Expand All @@ -33,7 +32,8 @@ public enum PostgresqlVersion implements Version {
Feature.exprSimilarTo,
// https://www.postgresql.org/docs/current/sql-select.html
Feature.select,
Feature.selectGroupBy, Feature.function, Feature.tableFunction, Feature.lateralSubSelect,
Feature.selectGroupBy, Feature.function, Feature.tableFunction,
Feature.lateralSubSelect,
Feature.selectHaving,
// https://www.postgresql.org/docs/current/queries-table-expressions.html#QUERIES-GROUPING-SETS
Feature.selectGroupByGroupingSets,
Expand Down Expand Up @@ -107,6 +107,11 @@ public enum PostgresqlVersion implements Version {
// https://www.postgresql.org/docs/current/sql-alterview.html
// Feature.alterView,

// https://www.postgresql.org/docs/16/sql-refreshmaterializedview.html
Feature.refreshMaterializedView,
Feature.refreshMaterializedWithNoDataView,
Feature.refreshMaterializedWithDataView,

// https://www.postgresql.org/docs/current/sql-insert.html
Feature.insert,
Feature.insertValues,
Expand Down Expand Up @@ -151,12 +156,9 @@ public enum PostgresqlVersion implements Version {
// https://www.postgresql.org/docs/current/sql-reset.html
Feature.reset,
// https://www.postgresql.org/docs/current/sql-commit.html
Feature.commit
)),
V11("11", V10.copy().getFeatures()),
V12("12", V11.copy().getFeatures()),
V13("13", V12.copy().getFeatures()),
V14("14", V13.copy().getFeatures());
Feature.commit)), V11("11", V10.copy().getFeatures()), V12("12",
V11.copy().getFeatures()), V13("13",
V12.copy().getFeatures()), V14("14", V13.copy().getFeatures());

private Set<Feature> features;
private String versionString;
Expand All @@ -176,7 +178,8 @@ public enum PostgresqlVersion implements Version {
* @param unsupported
* @see #copy() to copy from previous version
*/
PostgresqlVersion(String versionString, Set<Feature> featuresSupported, Set<Feature> unsupported) {
PostgresqlVersion(String versionString, Set<Feature> featuresSupported,
Set<Feature> unsupported) {
this.versionString = versionString;
this.features = featuresSupported;
this.features.removeAll(unsupported);
Expand Down
Loading

0 comments on commit 425c72e

Please sign in to comment.