Skip to content

Commit

Permalink
implemented DescribeStatement, corrected TableNamesFinder, corrected …
Browse files Browse the repository at this point in the history
…corresponding interfaces and adapters, implemented tests.
  • Loading branch information
wumpz committed Jan 20, 2019
1 parent 25fa311 commit 1c411f4
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 7 deletions.
55 changes: 55 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/DescribeStatement.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.schema.Table;

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

private Table table;

public DescribeStatement(Table table) {
this.table = table;
}

public Table getTable() {
return table;
}

public void setTable(Table table) {
this.table = table;
}

@Override
public String toString() {
return "DESCRIBE " + table.getFullyQualifiedName();
}

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

void visit(ValuesStatement values);

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

@Override
public void visit(DescribeStatement describe) {
}
}
11 changes: 8 additions & 3 deletions src/main/java/net/sf/jsqlparser/util/TablesNamesFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.util.ArrayList;
import java.util.List;

import net.sf.jsqlparser.expression.AllComparisonExpression;
import net.sf.jsqlparser.expression.AnalyticExpression;
import net.sf.jsqlparser.expression.AnyComparisonExpression;
Expand Down Expand Up @@ -77,7 +76,6 @@
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
import net.sf.jsqlparser.expression.operators.relational.GreaterThan;
import net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
Expand All @@ -89,13 +87,15 @@
import net.sf.jsqlparser.expression.operators.relational.MinorThan;
import net.sf.jsqlparser.expression.operators.relational.MinorThanEquals;
import net.sf.jsqlparser.expression.operators.relational.MultiExpressionList;
import net.sf.jsqlparser.expression.operators.relational.NamedExpressionList;
import net.sf.jsqlparser.expression.operators.relational.NotEqualsTo;
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperator;
import net.sf.jsqlparser.expression.operators.relational.RegExpMySQLOperator;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.DescribeStatement;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.ShowStatement;
import net.sf.jsqlparser.statement.Statement;
Expand Down Expand Up @@ -841,11 +841,16 @@ public void visit(Comment comment) {
}
}
}

@Override
public void visit(ValuesStatement values) {
for (Expression expr : values.getExpressions()) {
expr.accept(this);
}
}

@Override
public void visit(DescribeStatement describe) {
describe.accept(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Iterator;
import net.sf.jsqlparser.statement.Block;
import net.sf.jsqlparser.statement.Commit;
import net.sf.jsqlparser.statement.DescribeStatement;
import net.sf.jsqlparser.statement.SetStatement;
import net.sf.jsqlparser.statement.ShowStatement;
import net.sf.jsqlparser.statement.Statement;
Expand Down Expand Up @@ -253,10 +254,16 @@ public void visit(Block block) {
public void visit(Comment comment) {
buffer.append(comment.toString());
}

@Override
public void visit(ValuesStatement values) {
expressionDeParser.setBuffer(buffer);
new ValuesStatementDeParser(expressionDeParser, buffer).deParse(values);
}

@Override
public void visit(DescribeStatement describe) {
buffer.append("DESCRIBE ");
buffer.append(describe.getTable());
}
}
13 changes: 12 additions & 1 deletion src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
| <K_DELAYED : "DELAYED">
| <K_DELETE:"DELETE">
| <K_DESC:"DESC">
| <K_DESCRIBE:"DESCRIBE">
| <K_DISABLE : "DISABLE">
| <K_DISTINCT:"DISTINCT">
| <K_DO:"DO">
Expand Down Expand Up @@ -524,6 +525,16 @@ SetStatement Set(): {
}
}

DescribeStatement Describe(): {
Table table;
} {
<K_DESCRIBE> table = Table()
{
return new DescribeStatement(table);
}
}


UseStatement Use(): {
String name;
}
Expand Down Expand Up @@ -1042,7 +1053,7 @@ String RelObjectNameWithoutValue() :
| tk=<K_INSERT> | tk=<K_INDEX> | tk=<K_PRIMARY> | tk=<K_ENABLE>
| tk=<K_UNSIGNED>
| tk=<K_TEMP> | tk=<K_TEMPORARY> | tk=<K_TYPE> | tk=<K_ISNULL>
| tk=<K_ZONE> | tk=<K_COLUMNS>
| tk=<K_ZONE> | tk=<K_COLUMNS> | tk=<K_DESCRIBE>
/* | tk=<K_PLACING> | tk=<K_BOTH> | tk=<K_LEADING> | tk=<K_TRAILING> */
)

Expand Down
17 changes: 17 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/DescribeTest.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 DescribeTest {

@Test
public void testDescribe() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("DESCRIBE foo.products");
}
}
14 changes: 12 additions & 2 deletions src/test/java/net/sf/jsqlparser/util/TablesNamesFinderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.OracleHint;

import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.DescribeStatement;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.comment.Comment;
import net.sf.jsqlparser.statement.create.table.CreateTable;
Expand All @@ -20,10 +21,10 @@
import net.sf.jsqlparser.statement.merge.Merge;
import net.sf.jsqlparser.statement.replace.Replace;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.simpleparsing.CCJSqlParserManagerTest;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.statement.upsert.Upsert;
import net.sf.jsqlparser.test.TestException;
import net.sf.jsqlparser.statement.simpleparsing.CCJSqlParserManagerTest;
import static org.junit.Assert.*;
import org.junit.Test;

Expand Down Expand Up @@ -553,4 +554,13 @@ public void testCommentColumn2() throws JSQLParserException {
List<String> tableList = finder.getTableList(comment);
assertEquals(0, tableList.size());
}

@Test
public void testDescribe() throws JSQLParserException {
DescribeStatement describe = new DescribeStatement(new Table("foo", "product"));
TablesNamesFinder finder = new TablesNamesFinder();
List<String> tableList = finder.getTableList(describe);
assertEquals(1, tableList.size());
assertEquals("foo.product", tableList.get(0));
}
}

0 comments on commit 1c411f4

Please sign in to comment.