Skip to content

Commit 1c411f4

Browse files
committed
implemented DescribeStatement, corrected TableNamesFinder, corrected corresponding interfaces and adapters, implemented tests.
1 parent 25fa311 commit 1c411f4

File tree

8 files changed

+118
-7
lines changed

8 files changed

+118
-7
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2019 JSQLParser
6+
* %%
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Lesser General Public License as
9+
* published by the Free Software Foundation, either version 2.1 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Lesser Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Lesser Public
18+
* License along with this program. If not, see
19+
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
20+
* #L%
21+
*/
22+
package net.sf.jsqlparser.statement;
23+
24+
import net.sf.jsqlparser.schema.Table;
25+
26+
/**
27+
*
28+
* @author tw
29+
*/
30+
public class DescribeStatement implements Statement {
31+
32+
private Table table;
33+
34+
public DescribeStatement(Table table) {
35+
this.table = table;
36+
}
37+
38+
public Table getTable() {
39+
return table;
40+
}
41+
42+
public void setTable(Table table) {
43+
this.table = table;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "DESCRIBE " + table.getFullyQualifiedName();
49+
}
50+
51+
@Override
52+
public void accept(StatementVisitor statementVisitor) {
53+
statementVisitor.visit(this);
54+
}
55+
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,6 @@ public interface StatementVisitor {
8686
void visit(Block block);
8787

8888
void visit(ValuesStatement values);
89+
90+
void visit(DescribeStatement describe);
8991
}

src/main/java/net/sf/jsqlparser/statement/StatementVisitorAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,8 @@ public void visit(Block block) {
151151
@Override
152152
public void visit(ValuesStatement values) {
153153
}
154+
155+
@Override
156+
public void visit(DescribeStatement describe) {
157+
}
154158
}

src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java

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

2424
import java.util.ArrayList;
2525
import java.util.List;
26-
2726
import net.sf.jsqlparser.expression.AllComparisonExpression;
2827
import net.sf.jsqlparser.expression.AnalyticExpression;
2928
import net.sf.jsqlparser.expression.AnyComparisonExpression;
@@ -77,7 +76,6 @@
7776
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
7877
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
7978
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
80-
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
8179
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
8280
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
8381
import net.sf.jsqlparser.expression.operators.relational.InExpression;
@@ -89,13 +87,15 @@
8987
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
9088
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
9189
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
90+
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
9291
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
9392
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
9493
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
9594
import net.sf.jsqlparser.schema.Column;
9695
import net.sf.jsqlparser.schema.Table;
9796
import net.sf.jsqlparser.statement.Block;
9897
import net.sf.jsqlparser.statement.Commit;
98+
import net.sf.jsqlparser.statement.DescribeStatement;
9999
import net.sf.jsqlparser.statement.SetStatement;
100100
import net.sf.jsqlparser.statement.ShowStatement;
101101
import net.sf.jsqlparser.statement.Statement;
@@ -841,11 +841,16 @@ public void visit(Comment comment) {
841841
}
842842
}
843843
}
844-
844+
845845
@Override
846846
public void visit(ValuesStatement values) {
847847
for (Expression expr : values.getExpressions()) {
848848
expr.accept(this);
849849
}
850850
}
851+
852+
@Override
853+
public void visit(DescribeStatement describe) {
854+
describe.accept(this);
855+
}
851856
}

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Iterator;
2525
import net.sf.jsqlparser.statement.Block;
2626
import net.sf.jsqlparser.statement.Commit;
27+
import net.sf.jsqlparser.statement.DescribeStatement;
2728
import net.sf.jsqlparser.statement.SetStatement;
2829
import net.sf.jsqlparser.statement.ShowStatement;
2930
import net.sf.jsqlparser.statement.Statement;
@@ -253,10 +254,16 @@ public void visit(Block block) {
253254
public void visit(Comment comment) {
254255
buffer.append(comment.toString());
255256
}
256-
257+
257258
@Override
258259
public void visit(ValuesStatement values) {
259260
expressionDeParser.setBuffer(buffer);
260261
new ValuesStatementDeParser(expressionDeParser, buffer).deParse(values);
261262
}
263+
264+
@Override
265+
public void visit(DescribeStatement describe) {
266+
buffer.append("DESCRIBE ");
267+
buffer.append(describe.getTable());
268+
}
262269
}

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
174174
| <K_DELAYED : "DELAYED">
175175
| <K_DELETE:"DELETE">
176176
| <K_DESC:"DESC">
177+
| <K_DESCRIBE:"DESCRIBE">
177178
| <K_DISABLE : "DISABLE">
178179
| <K_DISTINCT:"DISTINCT">
179180
| <K_DO:"DO">
@@ -524,6 +525,16 @@ SetStatement Set(): {
524525
}
525526
}
526527

528+
DescribeStatement Describe(): {
529+
Table table;
530+
} {
531+
<K_DESCRIBE> table = Table()
532+
{
533+
return new DescribeStatement(table);
534+
}
535+
}
536+
537+
527538
UseStatement Use(): {
528539
String name;
529540
}
@@ -1042,7 +1053,7 @@ String RelObjectNameWithoutValue() :
10421053
| tk=<K_INSERT> | tk=<K_INDEX> | tk=<K_PRIMARY> | tk=<K_ENABLE>
10431054
| tk=<K_UNSIGNED>
10441055
| tk=<K_TEMP> | tk=<K_TEMPORARY> | tk=<K_TYPE> | tk=<K_ISNULL>
1045-
| tk=<K_ZONE> | tk=<K_COLUMNS>
1056+
| tk=<K_ZONE> | tk=<K_COLUMNS> | tk=<K_DESCRIBE>
10461057
/* | tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
10471058
)
10481059

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package net.sf.jsqlparser.statement;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import static net.sf.jsqlparser.test.TestUtils.*;
5+
import org.junit.Test;
6+
7+
/**
8+
*
9+
* @author tw
10+
*/
11+
public class DescribeTest {
12+
13+
@Test
14+
public void testDescribe() throws JSQLParserException {
15+
assertSqlCanBeParsedAndDeparsed("DESCRIBE foo.products");
16+
}
17+
}

src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
import net.sf.jsqlparser.JSQLParserException;
99
import net.sf.jsqlparser.expression.Expression;
1010
import net.sf.jsqlparser.expression.OracleHint;
11-
1211
import net.sf.jsqlparser.parser.CCJSqlParserManager;
1312
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
1413
import net.sf.jsqlparser.schema.Column;
14+
import net.sf.jsqlparser.schema.Table;
15+
import net.sf.jsqlparser.statement.DescribeStatement;
1516
import net.sf.jsqlparser.statement.Statement;
1617
import net.sf.jsqlparser.statement.comment.Comment;
1718
import net.sf.jsqlparser.statement.create.table.CreateTable;
@@ -20,10 +21,10 @@
2021
import net.sf.jsqlparser.statement.merge.Merge;
2122
import net.sf.jsqlparser.statement.replace.Replace;
2223
import net.sf.jsqlparser.statement.select.Select;
24+
import net.sf.jsqlparser.statement.simpleparsing.CCJSqlParserManagerTest;
2325
import net.sf.jsqlparser.statement.update.Update;
2426
import net.sf.jsqlparser.statement.upsert.Upsert;
2527
import net.sf.jsqlparser.test.TestException;
26-
import net.sf.jsqlparser.statement.simpleparsing.CCJSqlParserManagerTest;
2728
import static org.junit.Assert.*;
2829
import org.junit.Test;
2930

@@ -553,4 +554,13 @@ public void testCommentColumn2() throws JSQLParserException {
553554
List<String> tableList = finder.getTableList(comment);
554555
assertEquals(0, tableList.size());
555556
}
557+
558+
@Test
559+
public void testDescribe() throws JSQLParserException {
560+
DescribeStatement describe = new DescribeStatement(new Table("foo", "product"));
561+
TablesNamesFinder finder = new TablesNamesFinder();
562+
List<String> tableList = finder.getTableList(describe);
563+
assertEquals(1, tableList.size());
564+
assertEquals("foo.product", tableList.get(0));
565+
}
556566
}

0 commit comments

Comments
 (0)