Skip to content

Commit fb45dd7

Browse files
committed
implemented explain select
1 parent 04db124 commit fb45dd7

File tree

7 files changed

+102
-0
lines changed

7 files changed

+102
-0
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.statement.select.Select;
25+
26+
/**
27+
*
28+
* @author tw
29+
*/
30+
public class ExplainStatement implements Statement {
31+
32+
private Select select;
33+
34+
public ExplainStatement(Select select) {
35+
this.select = select;
36+
}
37+
38+
public Select getStatement() {
39+
return select;
40+
}
41+
42+
public void setStatement(Select select) {
43+
this.select = select;
44+
}
45+
46+
@Override
47+
public String toString() {
48+
return "EXPLAIN " + select.toString();
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
@@ -88,4 +88,6 @@ public interface StatementVisitor {
8888
void visit(ValuesStatement values);
8989

9090
void visit(DescribeStatement describe);
91+
92+
public void visit(ExplainStatement aThis);
9193
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,8 @@ public void visit(ValuesStatement values) {
155155
@Override
156156
public void visit(DescribeStatement describe) {
157157
}
158+
159+
@Override
160+
public void visit(ExplainStatement aThis) {
161+
}
158162
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import net.sf.jsqlparser.statement.Block;
9797
import net.sf.jsqlparser.statement.Commit;
9898
import net.sf.jsqlparser.statement.DescribeStatement;
99+
import net.sf.jsqlparser.statement.ExplainStatement;
99100
import net.sf.jsqlparser.statement.SetStatement;
100101
import net.sf.jsqlparser.statement.ShowStatement;
101102
import net.sf.jsqlparser.statement.Statement;
@@ -853,4 +854,9 @@ public void visit(ValuesStatement values) {
853854
public void visit(DescribeStatement describe) {
854855
describe.getTable().accept(this);
855856
}
857+
858+
@Override
859+
public void visit(ExplainStatement explain) {
860+
explain.getStatement().accept(this);
861+
}
856862
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import net.sf.jsqlparser.statement.Block;
2626
import net.sf.jsqlparser.statement.Commit;
2727
import net.sf.jsqlparser.statement.DescribeStatement;
28+
import net.sf.jsqlparser.statement.ExplainStatement;
2829
import net.sf.jsqlparser.statement.SetStatement;
2930
import net.sf.jsqlparser.statement.ShowStatement;
3031
import net.sf.jsqlparser.statement.Statement;
@@ -266,4 +267,10 @@ public void visit(DescribeStatement describe) {
266267
buffer.append("DESCRIBE ");
267268
buffer.append(describe.getTable());
268269
}
270+
271+
@Override
272+
public void visit(ExplainStatement explain) {
273+
buffer.append("EXPLAIN ");
274+
explain.getStatement().accept(this);
275+
}
269276
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
190190
| <K_EXEC: "EXEC">
191191
| <K_EXECUTE: "EXECUTE">
192192
| <K_EXISTS:"EXISTS">
193+
| <K_EXPLAIN:"EXPLAIN">
193194
| <K_EXTRACT:"EXTRACT">
194195
| <K_FETCH:"FETCH">
195196
| <K_FIRST: "FIRST">
@@ -436,6 +437,8 @@ Statement SingleStatement() :
436437
stm = Comment()
437438
|
438439
stm = Describe()
440+
|
441+
stm = Explain()
439442
)
440443
{ return stm; }
441444
} catch (ParseException e) {
@@ -534,6 +537,14 @@ DescribeStatement Describe(): {
534537
}
535538
}
536539

540+
ExplainStatement Explain(): {
541+
Select select;
542+
} {
543+
<K_EXPLAIN> select = Select()
544+
{
545+
return new ExplainStatement(select);
546+
}
547+
}
537548

538549
UseStatement Use(): {
539550
String name;
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 ExplainTest {
12+
13+
@Test
14+
public void testDescribe() throws JSQLParserException {
15+
assertSqlCanBeParsedAndDeparsed("EXPLAIN SELECT * FROM mytable");
16+
}
17+
}

0 commit comments

Comments
 (0)