Skip to content

Commit f1724a4

Browse files
oshaiwumpz
authored andcommitted
Add 'SHOW COLUMNS FROM table' (#728)
1 parent 08fedf7 commit f1724a4

File tree

8 files changed

+168
-0
lines changed

8 files changed

+168
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2017 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+
/**
25+
*
26+
* @author oshai
27+
*/
28+
public class ShowStatement implements Statement {
29+
30+
private String tableName;
31+
32+
public ShowStatement(String tableName) {
33+
this.tableName = tableName;
34+
}
35+
36+
public String getTableName() {
37+
return tableName;
38+
}
39+
40+
public void setTableName(String tableName) {
41+
this.tableName = tableName;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "SHOW COLUMNS FROM " + tableName;
47+
}
48+
49+
@Override
50+
public void accept(StatementVisitor statementVisitor) {
51+
statementVisitor.visit(this);
52+
}
53+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ public interface StatementVisitor {
7373

7474
void visit(SetStatement set);
7575

76+
void visit(ShowStatement set);
77+
7678
void visit(Merge merge);
7779

7880
void visit(Select select);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ public void visit(SetStatement set) {
123123

124124
}
125125

126+
@Override
127+
public void visit(ShowStatement set) {
128+
}
129+
126130
@Override
127131
public void visit(Merge merge) {
128132

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@
9797
import net.sf.jsqlparser.statement.Block;
9898
import net.sf.jsqlparser.statement.Commit;
9999
import net.sf.jsqlparser.statement.SetStatement;
100+
import net.sf.jsqlparser.statement.ShowStatement;
100101
import net.sf.jsqlparser.statement.Statement;
101102
import net.sf.jsqlparser.statement.StatementVisitor;
102103
import net.sf.jsqlparser.statement.Statements;
@@ -747,6 +748,11 @@ public void visit(SetStatement set) {
747748
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
748749
}
749750

751+
@Override
752+
public void visit(ShowStatement set) {
753+
throw new UnsupportedOperationException(NOT_SUPPORTED_YET);
754+
}
755+
750756
@Override
751757
public void visit(RowConstructor rowConstructor) {
752758
for (Expression expr : rowConstructor.getExprList().getExpressions()) {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* #%L
3+
* JSQLParser library
4+
* %%
5+
* Copyright (C) 2004 - 2017 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.util.deparser;
23+
24+
import net.sf.jsqlparser.statement.ShowStatement;
25+
26+
public class ShowStatementDeParser {
27+
28+
protected StringBuilder buffer;
29+
30+
public ShowStatementDeParser(StringBuilder buffer) {
31+
this.buffer = buffer;
32+
}
33+
34+
public StringBuilder getBuffer() {
35+
return buffer;
36+
}
37+
38+
public void setBuffer(StringBuilder buffer) {
39+
this.buffer = buffer;
40+
}
41+
42+
public void deParse(ShowStatement show) {
43+
buffer.append("SHOW COLUMNS FROM ").append(show.getTableName());
44+
}
45+
}

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

Lines changed: 6 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.SetStatement;
28+
import net.sf.jsqlparser.statement.ShowStatement;
2829
import net.sf.jsqlparser.statement.Statement;
2930
import net.sf.jsqlparser.statement.StatementVisitor;
3031
import net.sf.jsqlparser.statement.Statements;
@@ -231,6 +232,11 @@ public void visit(UseStatement use) {
231232
new UseStatementDeParser(buffer).deParse(use);
232233
}
233234

235+
@Override
236+
public void visit(ShowStatement show) {
237+
new ShowStatementDeParser(buffer).deParse(show);
238+
}
239+
234240
@Override
235241
public void visit(Block block) {
236242
buffer.append("BEGIN\n");

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
161161
| <K_CHECK:"CHECK">
162162
| <K_CHAR:"CHAR">
163163
| <K_COLUMN:"COLUMN">
164+
| <K_COLUMNS:"COLUMNS">
164165
| <K_COMMIT:"COMMIT">
165166
| <K_COMMENT:"COMMENT">
166167
| <K_CONNECT:"CONNECT">
@@ -269,6 +270,7 @@ TOKEN: /* SQL Keywords. prefixed with K_ to avoid name clashes */
269270
| <K_SEMI : "SEMI">
270271
| <K_SEPARATOR:"SEPARATOR">
271272
| <K_SET:"SET">
273+
| <K_SHOW : "SHOW">
272274
| <K_SIBLINGS:"SIBLINGS">
273275
| <K_SKIP: "SKIP">
274276
| <K_SOME:"SOME">
@@ -424,6 +426,8 @@ Statement SingleStatement() :
424426
|
425427
stm = Set()
426428
|
429+
stm = Show()
430+
|
427431
stm = Use()
428432
|
429433
stm = Commit()
@@ -528,6 +532,16 @@ UseStatement Use(): {
528532
}
529533
}
530534

535+
ShowStatement Show(): {
536+
String tableName;
537+
}
538+
{
539+
<K_SHOW> <K_COLUMNS> <K_FROM> tableName = RelObjectNameExt()
540+
{
541+
return new ShowStatement(tableName);
542+
}
543+
}
544+
531545
ValuesStatement Values(): {
532546
List<Expression> expList = new ArrayList<Expression>();
533547
Expression exp;
@@ -1387,6 +1401,7 @@ MySQLIndexHint MySQLIndexHint():
13871401
}
13881402
{
13891403
(actionToken = <K_USE>
1404+
| actionToken = <K_SHOW>
13901405
| actionToken = <K_IGNORE>
13911406
| actionToken = <K_FORCE> )
13921407
(indexToken = <K_INDEX>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (C) 2015 JSQLParser.
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License as published by the Free Software Foundation; either
7+
* version 2.1 of the License, or (at your option) any later version.
8+
*
9+
* This library is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* Lesser General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU Lesser General Public
15+
* License along with this library; if not, write to the Free Software
16+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
17+
* MA 02110-1301 USA
18+
*/
19+
package net.sf.jsqlparser.statement;
20+
21+
import net.sf.jsqlparser.JSQLParserException;
22+
import org.junit.Test;
23+
24+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
25+
26+
/**
27+
*
28+
* @author oshai
29+
*/
30+
public class ShowStatementTest {
31+
32+
33+
@Test
34+
public void testSimpleUse() throws JSQLParserException {
35+
assertSqlCanBeParsedAndDeparsed("SHOW COLUMNS FROM mydatabase");
36+
}
37+
}

0 commit comments

Comments
 (0)