Skip to content

Commit

Permalink
implemented explain select
Browse files Browse the repository at this point in the history
  • Loading branch information
wumpz committed Jan 22, 2019
1 parent 04db124 commit fb45dd7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 0 deletions.
55 changes: 55 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/ExplainStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.statement.select.Select;

/**
*
* @author tw
*/
public class ExplainStatement implements Statement {

private Select select;

public ExplainStatement(Select select) {
this.select = select;
}

public Select getStatement() {
return select;
}

public void setStatement(Select select) {
this.select = select;
}

@Override
public String toString() {
return "EXPLAIN " + select.toString();
}

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,6 @@ public interface StatementVisitor {
void visit(ValuesStatement values);

void visit(DescribeStatement describe);

public void visit(ExplainStatement aThis);
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,8 @@ public void visit(ValuesStatement values) {
@Override
public void visit(DescribeStatement describe) {
}

@Override
public void visit(ExplainStatement aThis) {
}
}
6 changes: 6 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.DescribeStatement;
import net.sf.jsqlparser.statement.ExplainStatement;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.ShowStatement;
import net.sf.jsqlparser.statement.Statement;
Expand Down Expand Up @@ -853,4 +854,9 @@ public void visit(ValuesStatement values) {
public void visit(DescribeStatement describe) {
describe.getTable().accept(this);
}

@Override
public void visit(ExplainStatement explain) {
explain.getStatement().accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.DescribeStatement;
import net.sf.jsqlparser.statement.ExplainStatement;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.ShowStatement;
import net.sf.jsqlparser.statement.Statement;
Expand Down Expand Up @@ -266,4 +267,10 @@ public void visit(DescribeStatement describe) {
buffer.append("DESCRIBE ");
buffer.append(describe.getTable());
}

@Override
public void visit(ExplainStatement explain) {
buffer.append("EXPLAIN ");
explain.getStatement().accept(this);
}
}
11 changes: 11 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_EXEC: "EXEC">
| <K_EXECUTE: "EXECUTE">
| <K_EXISTS:"EXISTS">
| <K_EXPLAIN:"EXPLAIN">
| <K_EXTRACT:"EXTRACT">
| <K_FETCH:"FETCH">
| <K_FIRST: "FIRST">
Expand Down Expand Up @@ -436,6 +437,8 @@ Statement SingleStatement() :
stm = Comment()
|
stm = Describe()
|
stm = Explain()
)
{ return stm; }
} catch (ParseException e) {
Expand Down Expand Up @@ -534,6 +537,14 @@ DescribeStatement Describe(): {
}
}

ExplainStatement Explain(): {
Select select;
} {
<K_EXPLAIN> select = Select()
{
return new ExplainStatement(select);
}
}

UseStatement Use(): {
String name;
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/ExplainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.sf.jsqlparser.statement;

import net.sf.jsqlparser.JSQLParserException;
import static net.sf.jsqlparser.test.TestUtils.*;
import org.junit.Test;

/**
*
* @author tw
*/
public class ExplainTest {

@Test
public void testDescribe() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("EXPLAIN SELECT * FROM mytable");
}
}

0 comments on commit fb45dd7

Please sign in to comment.