Skip to content

Commit daa56ed

Browse files
refactor: SHOW statement, supporting any RDBMS specific implementation
- returns any RDBMS specific implementation as `UnsupportedStatement` - fixes #1702
1 parent 01a5fb7 commit daa56ed

3 files changed

Lines changed: 57 additions & 30 deletions

File tree

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
package net.sf.jsqlparser.statement;
1212

13+
import java.util.ArrayList;
1314
import java.util.List;
1415
import java.util.Objects;
1516

@@ -22,19 +23,28 @@ public class UnsupportedStatement implements Statement {
2223
private List<String> declarations;
2324

2425
public UnsupportedStatement(List<String> declarations) {
25-
this.declarations = Objects.requireNonNull(declarations, "The List of Tokens must not be null.");
26+
this.declarations =
27+
Objects.requireNonNull(declarations, "The List of Tokens must not be null.");
2628
}
2729

28-
@Override
30+
public UnsupportedStatement(String upfront, List<String> declarations) {
31+
this.declarations = new ArrayList<>();
32+
this.declarations.add(upfront);
33+
this.declarations.addAll(
34+
Objects.requireNonNull(declarations, "The List of Tokens must not be null."));
35+
}
36+
37+
@Override
2938
public void accept(StatementVisitor statementVisitor) {
3039
statementVisitor.visit(this);
3140
}
32-
33-
@SuppressWarnings({"PMD.MissingBreakInSwitch", "PMD.SwitchStmtsShouldHaveDefault", "PMD.CyclomaticComplexity"})
41+
42+
@SuppressWarnings({"PMD.MissingBreakInSwitch", "PMD.SwitchStmtsShouldHaveDefault",
43+
"PMD.CyclomaticComplexity"})
3444
public StringBuilder appendTo(StringBuilder builder) {
35-
int i=0;
36-
for (String s:declarations) {
37-
if (i>0) {
45+
int i = 0;
46+
for (String s : declarations) {
47+
if (i > 0) {
3848
builder.append(" ");
3949
}
4050
builder.append(s);

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

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -682,15 +682,6 @@ Statement SingleStatement() :
682682
|
683683
stm = Reset()
684684
|
685-
LOOKAHEAD(ShowColumns())
686-
stm = ShowColumns()
687-
|
688-
LOOKAHEAD(ShowIndex())
689-
stm = ShowIndex()
690-
|
691-
LOOKAHEAD(ShowTables())
692-
stm = ShowTables()
693-
|
694685
stm = Show()
695686
|
696687
stm = Use()
@@ -1170,11 +1161,40 @@ UseStatement Use(): {
11701161
}
11711162
}
11721163

1164+
Statement Show():
1165+
{
1166+
Statement statement;
1167+
List<String> captureRest;
1168+
}
1169+
{
1170+
<K_SHOW>
1171+
(
1172+
LOOKAHEAD(2) statement = ShowColumns()
1173+
|
1174+
LOOKAHEAD(2) statement = ShowIndex()
1175+
|
1176+
LOOKAHEAD(2) statement = ShowTables()
1177+
|
1178+
// any of the RDBMS specific SHOW syntax
1179+
captureRest = captureRest()
1180+
{
1181+
if (captureRest.size()==1) {
1182+
statement = new ShowStatement(captureRest.get(1));
1183+
} else {
1184+
statement = new UnsupportedStatement("SHOW", captureRest);
1185+
}
1186+
}
1187+
)
1188+
{
1189+
return statement;
1190+
}
1191+
}
1192+
11731193
ShowColumnsStatement ShowColumns(): {
11741194
String tableName;
11751195
}
11761196
{
1177-
<K_SHOW> <K_COLUMNS> <K_FROM> tableName = RelObjectNameExt()
1197+
<K_COLUMNS> <K_FROM> tableName = RelObjectNameExt()
11781198
{
11791199
return new ShowColumnsStatement(tableName);
11801200
}
@@ -1184,7 +1204,7 @@ ShowIndexStatement ShowIndex(): {
11841204
String tableName;
11851205
}
11861206
{
1187-
<K_SHOW> <K_INDEX> <K_FROM> tableName = RelObjectNameExt()
1207+
<K_INDEX> <K_FROM> tableName = RelObjectNameExt()
11881208
{
11891209
return new ShowIndexStatement(tableName);
11901210
}
@@ -1200,7 +1220,6 @@ ShowTablesStatement ShowTables(): {
12001220
Expression whereCondition = null;
12011221
}
12021222
{
1203-
<K_SHOW>
12041223
[ <K_EXTENDED> { modifiers.add(ShowTablesStatement.Modifiers.EXTENDED); } ]
12051224
[ <K_FULL> { modifiers.add(ShowTablesStatement.Modifiers.FULL); } ]
12061225
<K_TABLES>
@@ -1223,16 +1242,6 @@ ShowTablesStatement ShowTables(): {
12231242
}
12241243
}
12251244

1226-
ShowStatement Show(): {
1227-
String name;
1228-
}
1229-
{
1230-
<K_SHOW> name = RelObjectNameExt()
1231-
{
1232-
return new ShowStatement(name);
1233-
}
1234-
}
1235-
12361245
Values Values(): {
12371246
ExpressionList expressions;
12381247
} {

src/test/java/net/sf/jsqlparser/statement/ShowStatementTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
package net.sf.jsqlparser.statement;
1111

1212
import net.sf.jsqlparser.JSQLParserException;
13-
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
1413
import org.junit.jupiter.api.Test;
1514

15+
import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
16+
1617
/**
1718
*
1819
* @author oshai
@@ -28,4 +29,11 @@ public void testSimpleUse() throws JSQLParserException {
2829
public void testSimpleUse2() throws JSQLParserException {
2930
assertSqlCanBeParsedAndDeparsed("SHOW transaction_isolation");
3031
}
32+
33+
@Test
34+
void testShowIndexesFromTable() throws JSQLParserException {
35+
String sqlStr =
36+
"show indexes from my_table";
37+
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
38+
}
3139
}

0 commit comments

Comments
 (0)