|
9 | 9 | */ |
10 | 10 | package net.sf.jsqlparser.statement.select; |
11 | 11 |
|
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
12 | 14 | import net.sf.jsqlparser.JSQLParserException; |
13 | 15 | import net.sf.jsqlparser.parser.CCJSqlParserDefaultVisitor; |
14 | 16 | import net.sf.jsqlparser.parser.CCJSqlParserTreeConstants; |
|
17 | 19 | import net.sf.jsqlparser.parser.Token; |
18 | 20 | import net.sf.jsqlparser.schema.Column; |
19 | 21 | import net.sf.jsqlparser.statement.Statement; |
| 22 | +import static org.assertj.core.api.Assertions.assertThat; |
20 | 23 | import static org.junit.jupiter.api.Assertions.assertEquals; |
21 | 24 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
22 | 25 | import org.junit.jupiter.api.Test; |
@@ -179,4 +182,39 @@ public Object visit(SimpleNode node, Object data) { |
179 | 182 | assertEquals(30, subSelectStart.beginColumn); |
180 | 183 | assertEquals(49, subSelectEnd.endColumn); |
181 | 184 | } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void testSelectASTExtractWithCommentsIssue1580() throws JSQLParserException { |
| 188 | + String sql = "SELECT /* testcomment */ \r\n a, b FROM -- testcomment2 \r\n mytable \r\n order by b, c"; |
| 189 | + SimpleNode root = (SimpleNode) CCJSqlParserUtil.parseAST(sql); |
| 190 | + List<Token> comments = new ArrayList<>(); |
| 191 | + |
| 192 | + root.jjtAccept(new CCJSqlParserDefaultVisitor() { |
| 193 | + @Override |
| 194 | + public Object visit(SimpleNode node, Object data) { |
| 195 | + if (node.jjtGetFirstToken().specialToken != null) { |
| 196 | + //needed since for different nodes we got the same first token |
| 197 | + if (!comments.contains(node.jjtGetFirstToken().specialToken)) { |
| 198 | + comments.add(node.jjtGetFirstToken().specialToken); |
| 199 | + } |
| 200 | + } |
| 201 | + return super.visit(node, data); |
| 202 | + } |
| 203 | + }, null); |
| 204 | + |
| 205 | + assertThat(comments) |
| 206 | + .extracting(token -> token.image) |
| 207 | + .containsExactly("/* testcomment */", "-- testcomment2 "); |
| 208 | + } |
| 209 | + |
| 210 | + @Test |
| 211 | + public void testSelectASTExtractWithCommentsIssue1580_2() throws JSQLParserException { |
| 212 | + String sql = "/* I want this comment */\n" |
| 213 | + + "SELECT order_detail_id, quantity\n" |
| 214 | + + "/* But ignore this one safely */\n" |
| 215 | + + "FROM order_details;"; |
| 216 | + SimpleNode root = (SimpleNode) CCJSqlParserUtil.parseAST(sql); |
| 217 | + |
| 218 | + assertThat(root.jjtGetFirstToken().specialToken.image).isEqualTo("/* I want this comment */"); |
| 219 | + } |
182 | 220 | } |
0 commit comments