Skip to content

Commit a369537

Browse files
committed
1 parent e570a91 commit a369537

File tree

2 files changed

+140
-3
lines changed

2 files changed

+140
-3
lines changed

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import net.sf.jsqlparser.statement.create.view.CreateView;
2525
import net.sf.jsqlparser.statement.select.PlainSelect;
26+
import net.sf.jsqlparser.statement.select.SelectVisitor;
2627

2728
/**
2829
* A class to de-parse (that is, tranform from JSqlParser hierarchy into a
@@ -31,14 +32,25 @@
3132
public class CreateViewDeParser {
3233

3334
private StringBuilder buffer;
34-
35+
private SelectVisitor selectVisitor;
36+
3537
/**
3638
* @param buffer the buffer that will be filled with the select
3739
*/
3840
public CreateViewDeParser(StringBuilder buffer) {
39-
this.buffer = buffer;
41+
SelectDeParser selectDeParser = new SelectDeParser();
42+
selectDeParser.setBuffer(buffer);
43+
ExpressionDeParser expressionDeParser = new ExpressionDeParser(selectDeParser, buffer);
44+
selectDeParser.setExpressionVisitor(expressionDeParser);
45+
selectVisitor = selectDeParser;
46+
this.buffer = buffer;
4047
}
4148

49+
public CreateViewDeParser(StringBuilder buffer, SelectVisitor selectVisitor) {
50+
this.buffer = buffer;
51+
this.selectVisitor = selectVisitor;
52+
}
53+
4254
public void deParse(CreateView createView) {
4355
buffer.append("CREATE ");
4456
if (createView.isOrReplace()) {
@@ -52,7 +64,8 @@ public void deParse(CreateView createView) {
5264
buffer.append(PlainSelect.getStringList(createView.getColumnNames(), true, true));
5365
}
5466
buffer.append(" AS ");
55-
buffer.append(createView.getSelectBody().toString());
67+
68+
createView.getSelectBody().accept(selectVisitor);
5669
}
5770

5871
public StringBuilder getBuffer() {
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C) 2015 JSQLParser.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package net.sf.jsqlparser.util.deparser;
20+
21+
import static junit.framework.TestCase.assertEquals;
22+
import net.sf.jsqlparser.JSQLParserException;
23+
import net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor;
24+
import net.sf.jsqlparser.parser.CCJSqlParserTreeConstants;
25+
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
26+
import net.sf.jsqlparser.parser.SimpleNode;
27+
import net.sf.jsqlparser.schema.Column;
28+
import net.sf.jsqlparser.schema.Table;
29+
import net.sf.jsqlparser.statement.create.view.CreateView;
30+
import org.junit.After;
31+
import org.junit.AfterClass;
32+
import org.junit.Before;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
36+
/**
37+
*
38+
* @author tw
39+
*/
40+
public class CreateViewDeParserTest {
41+
42+
public CreateViewDeParserTest() {
43+
}
44+
45+
@BeforeClass
46+
public static void setUpClass() {
47+
}
48+
49+
@AfterClass
50+
public static void tearDownClass() {
51+
}
52+
53+
@Before
54+
public void setUp() {
55+
}
56+
57+
@After
58+
public void tearDown() {
59+
}
60+
61+
/**
62+
* Test of deParse method, of class CreateViewDeParser.
63+
*/
64+
@Test
65+
public void testUseExtrnalExpressionDeparser() throws JSQLParserException {
66+
StringBuilder b = new StringBuilder();
67+
SelectDeParser selectDeParser = new SelectDeParser();
68+
selectDeParser.setBuffer(b);
69+
ExpressionDeParser expressionDeParser = new ExpressionDeParser(selectDeParser, b) {
70+
71+
@Override
72+
public void visit(Column tableColumn) {
73+
final Table table = tableColumn.getTable();
74+
String tableName = null;
75+
if (table != null) {
76+
if (table.getAlias() != null) {
77+
tableName = table.getAlias().getName();
78+
} else {
79+
tableName = table.getFullyQualifiedName();
80+
}
81+
}
82+
if (tableName != null && !tableName.isEmpty()) {
83+
getBuffer().append("\"").append(tableName).append("\"").append(".");
84+
}
85+
86+
getBuffer().append("\"").append(tableColumn.getColumnName()).append("\"");
87+
}
88+
};
89+
90+
selectDeParser.setExpressionVisitor(expressionDeParser);
91+
92+
CreateViewDeParser instance = new CreateViewDeParser(b, selectDeParser);
93+
CreateView vc = (CreateView) CCJSqlParserUtil.parse("CREATE VIEW test AS SELECT a, b FROM mytable");
94+
instance.deParse(vc);
95+
96+
assertEquals("CREATE VIEW test AS SELECT a, b FROM mytable", vc.toString());
97+
assertEquals("CREATE VIEW test AS SELECT \"a\", \"b\" FROM mytable", instance.getBuffer().toString());
98+
}
99+
100+
@Test
101+
public void testCreateViewASTNode() throws JSQLParserException {
102+
String sql = "CREATE VIEW test AS SELECT a, b FROM mytable";
103+
final StringBuilder b = new StringBuilder(sql);
104+
SimpleNode node = (SimpleNode) CCJSqlParserUtil.parseAST(sql);
105+
node.dump("*");
106+
assertEquals(CCJSqlParserTreeConstants.JJTSTATEMENT, node.getId());
107+
108+
node.jjtAccept(new CCJSqlParserDefaultVisitor() {
109+
int idxDelta = 0;
110+
@Override
111+
public Object visit(SimpleNode node, Object data) {
112+
if (CCJSqlParserTreeConstants.JJTCOLUMN == node.getId()) {
113+
b.insert(node.jjtGetFirstToken().beginColumn - 1 + idxDelta, '"');
114+
idxDelta++;
115+
b.insert(node.jjtGetLastToken().endColumn + idxDelta, '"');
116+
idxDelta++;
117+
}
118+
return super.visit(node, data);
119+
}
120+
}, null);
121+
122+
assertEquals("CREATE VIEW test AS SELECT \"a\", \"b\" FROM mytable", b.toString());
123+
}
124+
}

0 commit comments

Comments
 (0)