Skip to content

Commit 58d3965

Browse files
ted-johnsonwumpz
authored andcommitted
REPLACE VIEW as a synonym to ALTER VIEW (#711)
1 parent 7b6cff1 commit 58d3965

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

src/main/java/net/sf/jsqlparser/statement/create/view/AlterView.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class AlterView implements Statement {
3535

3636
private Table view;
3737
private SelectBody selectBody;
38+
private boolean useReplace = false;
3839
private List<String> columnNames = null;
3940

4041
@Override
@@ -74,9 +75,23 @@ public void setColumnNames(List<String> columnNames) {
7475
this.columnNames = columnNames;
7576
}
7677

78+
public boolean isUseReplace() {
79+
return useReplace;
80+
}
81+
82+
public void setUseReplace(boolean useReplace) {
83+
this.useReplace = useReplace;
84+
}
85+
86+
7787
@Override
7888
public String toString() {
79-
StringBuilder sql = new StringBuilder("ALTER ");
89+
StringBuilder sql;
90+
if(useReplace){
91+
sql = new StringBuilder("REPLACE ");
92+
}else{
93+
sql = new StringBuilder("ALTER ");
94+
}
8095
sql.append("VIEW ");
8196
sql.append(view);
8297
if (columnNames != null) {

src/main/java/net/sf/jsqlparser/util/deparser/AlterViewDeParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public AlterViewDeParser(StringBuilder buffer, SelectVisitor selectVisitor) {
5252
}
5353

5454
public void deParse(AlterView alterView) {
55-
buffer.append("ALTER ");
55+
if(alterView.isUseReplace()){
56+
buffer.append("REPLACE ");
57+
}else{
58+
buffer.append("ALTER ");
59+
}
5660
buffer.append("VIEW ").append(alterView.getView().getFullyQualifiedName());
5761
if (alterView.getColumnNames() != null) {
5862
buffer.append(PlainSelect.getStringList(alterView.getColumnNames(), true, true));

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ Statement SingleStatement() :
396396
|
397397
stm = Delete()
398398
|
399+
LOOKAHEAD(2)
399400
stm = Replace()
400401
|
401402
LOOKAHEAD(2)
@@ -618,7 +619,7 @@ Replace Replace():
618619
Expression exp = null;
619620
}
620621
{
621-
<K_REPLACE> [<K_INTO> { replace.setUseIntoTables(true); }] table=Table()
622+
( <K_REPLACE> [LOOKAHEAD(2) <K_INTO> { replace.setUseIntoTables(true); }] table=Table() )
622623

623624
(
624625
(
@@ -3445,7 +3446,7 @@ AlterView AlterView():
34453446
List<String> columnNames = null;
34463447
}
34473448
{
3448-
<K_ALTER>
3449+
( (<K_ALTER> ) | (<K_REPLACE> {alterView.setUseReplace(true);}) )
34493450
<K_VIEW> view=Table() { alterView.setView(view); }
34503451
[ columnNames = ColumnsNamesList() { alterView.setColumnNames(columnNames); } ]
34513452
<K_AS>

src/test/java/net/sf/jsqlparser/statement/create/AlterViewTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@ public void testAlterView() throws JSQLParserException {
1111
String stmt = "ALTER VIEW myview AS SELECT * FROM mytab";
1212
assertSqlCanBeParsedAndDeparsed(stmt);
1313
}
14+
15+
@Test
16+
public void testReplaceView() throws JSQLParserException {
17+
String stmt = "REPLACE VIEW myview AS SELECT * FROM mytab";
18+
assertSqlCanBeParsedAndDeparsed(stmt);
19+
}
1420
}

0 commit comments

Comments
 (0)