Skip to content

Fixes #1684: Support CREATE MATERIALIZED VIEW with AUTO REFRESH #1691

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,11 @@ public static List<String> getReservedKeywords(int restriction) {

, { "NEXTVAL", RESTRICTED_JSQLPARSER }

, { "AUTO", RESTRICTED_JSQLPARSER }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those really restricted Keywords which must not been used?
Or are those just keywords? Why exactly have those Tokens been added?

, { "REFRESH", RESTRICTED_JSQLPARSER }
, { "YES", RESTRICTED_JSQLPARSER }
, { "NO", RESTRICTED_JSQLPARSER }

//@todo: Object Names should not start with Hex-Prefix, we shall not find that Token
, { "0x", RESTRICTED_JSQLPARSER }
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*-
* #%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.create.view;

public enum AutoRefreshOption {
NONE,

YES,

NO
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CreateView implements Statement {
private boolean materialized = false;
private ForceOption force = ForceOption.NONE;
private TemporaryOption temp = TemporaryOption.NONE;
private AutoRefreshOption autoRefresh = AutoRefreshOption.NONE;
private boolean withReadOnly = false;
private boolean ifNotExists = false;

Expand Down Expand Up @@ -96,6 +97,14 @@ public void setTemporary(TemporaryOption temp) {
this.temp = temp;
}

public AutoRefreshOption getAutoRefresh() {
return autoRefresh;
}

public void setAutoRefresh(AutoRefreshOption autoRefresh) {
this.autoRefresh = autoRefresh;
}

public boolean isWithReadOnly() {
return withReadOnly;
}
Expand All @@ -118,16 +127,7 @@ public String toString() {
if (isOrReplace()) {
sql.append("OR REPLACE ");
}
switch (force) {
case FORCE:
sql.append("FORCE ");
break;
case NO_FORCE:
sql.append("NO FORCE ");
break;
default:
// nothing
}
appendForceOptionIfApplicable(sql);

if (temp != TemporaryOption.NONE) {
sql.append(temp.name()).append(" ");
Expand All @@ -141,6 +141,9 @@ public String toString() {
if (ifNotExists) {
sql.append(" IF NOT EXISTS");
}
if (autoRefresh != AutoRefreshOption.NONE) {
sql.append(" AUTO REFRESH ").append(autoRefresh.name());
}
if (columnNames != null) {
sql.append(PlainSelect.getStringList(columnNames, true, true));
}
Expand All @@ -151,6 +154,19 @@ public String toString() {
return sql.toString();
}

private void appendForceOptionIfApplicable(StringBuilder sql) {
switch (force) {
case FORCE:
sql.append("FORCE ");
break;
case NO_FORCE:
sql.append("NO FORCE ");
break;
default:
// nothing
}
}

public CreateView withView(Table view) {
this.setView(view);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.create.view.TemporaryOption;
import net.sf.jsqlparser.statement.create.view.AutoRefreshOption;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectVisitor;
Expand Down Expand Up @@ -63,6 +64,9 @@ public void deParse(CreateView createView) {
if (createView.isIfNotExists()) {
buffer.append(" IF NOT EXISTS");
}
if (createView.getAutoRefresh() != AutoRefreshOption.NONE) {
buffer.append(" AUTO REFRESH ").append(createView.getAutoRefresh().name());
}
if (createView.getColumnNames() != null) {
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_AT: "AT">
| <K_ASC:"ASC">
| <K_AUTHORIZATION:"AUTHORIZATION">
| <K_AUTO:"AUTO">
| <K_BEGIN:"BEGIN">
| <K_BETWEEN:"BETWEEN">
| <K_BINARY: "BINARY">
Expand Down Expand Up @@ -346,6 +347,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_RECYCLEBIN: "RECYCLEBIN">
| <K_RECURSIVE:"RECURSIVE">
| <K_REFERENCES:"REFERENCES">
| <K_REFRESH:"REFRESH">
| <K_REGEXP: "REGEXP">
| <K_RLIKE: "RLIKE">
| <K_REGISTER: "REGISTER">
Expand Down Expand Up @@ -444,6 +446,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_XMLAGG:"XMLAGG">
| <K_XMLTEXT:"XMLTEXT">
| <K_YAML:"YAML">
| <K_YES:"YES">
| <K_ZONE:"ZONE">
}

Expand Down Expand Up @@ -5472,6 +5475,7 @@ CreateView CreateView():
Table view = null;
Select select = null;
List<String> columnNames = null;
Token tk = null;
}
{
<K_CREATE>
Expand All @@ -5486,7 +5490,8 @@ CreateView CreateView():
]
[ <K_MATERIALIZED> { createView.setMaterialized(true);} ]
<K_VIEW> view=Table() { createView.setView(view); }
[ LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);} ]
[LOOKAHEAD(3) <K_AUTO> <K_REFRESH> (tk=<K_YES> | tk=<K_NO>) { createView.setAutoRefresh(AutoRefreshOption.valueOf(tk.image)); } ]
[LOOKAHEAD(3) <K_IF> <K_NOT> <K_EXISTS> {createView.setIfNotExists(true);}]
[ columnNames = ColumnsNamesList() { createView.setColumnNames(columnNames); } ]
<K_AS>
select=SelectWithWithItems( ) { createView.setSelect(select); }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@
package net.sf.jsqlparser.statement.create;

import java.io.StringReader;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.parser.ParseException;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.create.view.AutoRefreshOption;
import net.sf.jsqlparser.statement.create.view.CreateView;
import net.sf.jsqlparser.statement.select.PlainSelect;
import static net.sf.jsqlparser.test.TestUtils.*;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -124,6 +131,60 @@ public void testCreateWithReadOnlyViewIssue838() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE VIEW v14(c1, c2) AS SELECT c1, C2 FROM t1 WITH READ ONLY");
}

@Test
public void testCreateViewAutoRefreshNone() throws JSQLParserException {
String stmt = "CREATE VIEW myview AS SELECT * FROM mytab";
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
assertEquals(createView.getAutoRefresh(), AutoRefreshOption.NONE);
}

@Test
public void testCreateViewAutoRefreshYes() throws JSQLParserException {
String stmt = "CREATE VIEW myview AUTO REFRESH YES AS SELECT * FROM mytab";
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
assertEquals(createView.getAutoRefresh(), AutoRefreshOption.YES);
}

@Test
public void testCreateViewAutoRefreshNo() throws JSQLParserException {
String stmt = "CREATE VIEW myview AUTO REFRESH NO AS SELECT * FROM mytab";
CreateView createView = (CreateView) assertSqlCanBeParsedAndDeparsed(stmt);
assertEquals(createView.getAutoRefresh(), AutoRefreshOption.NO);
}

@Test
public void testCreateViewAutoFails() throws JSQLParserException {
String stmt = "CREATE VIEW myview AUTO AS SELECT * FROM mytab";

ThrowingCallable throwingCallable = () -> CCJSqlParserUtil.parse(stmt);

assertThatThrownBy(throwingCallable).isInstanceOf(JSQLParserException.class)
.hasRootCauseInstanceOf(ParseException.class).rootCause()
.hasMessageStartingWith("Encountered unexpected token");
}

@Test
public void testCreateViewRefreshFails() throws JSQLParserException {
String stmt = "CREATE VIEW myview REFRESH AS SELECT * FROM mytab";

ThrowingCallable throwingCallable = () -> CCJSqlParserUtil.parse(stmt);

assertThatThrownBy(throwingCallable).isInstanceOf(JSQLParserException.class)
.hasRootCauseInstanceOf(ParseException.class).rootCause()
.hasMessageStartingWith("Encountered unexpected token");
}

@Test
public void testCreateViewAutoRefreshFails() throws JSQLParserException {
String stmt = "CREATE VIEW myview AUTO REFRESH AS SELECT * FROM mytab";

ThrowingCallable throwingCallable = () -> CCJSqlParserUtil.parse(stmt);

assertThatThrownBy(throwingCallable).isInstanceOf(JSQLParserException.class)
.hasRootCauseInstanceOf(ParseException.class).rootCause()
.hasMessageStartingWith("Encountered unexpected token");
}

@Test
public void testCreateViewIfNotExists() throws JSQLParserException {
String stmt = "CREATE VIEW myview IF NOT EXISTS AS SELECT * FROM mytab";
Expand All @@ -138,4 +199,5 @@ public void testCreateMaterializedViewIfNotExists() throws JSQLParserException {
assertTrue(createView.isMaterialized());
assertTrue(createView.isIfNotExists());
}

}