|
| 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