Skip to content

Commit 7a90cfb

Browse files
Statement Classification QUERY, DML, DDL, BLOCK, UNPARSED
Introduce a Statement Classification in order to distinguish QUERY, DML, DDL, BLOCK, UNPARSED Introduce streamlined appendTo() method in order to avoid the creation of multiple StringBuilders Improve Test Coverage for all Statements
1 parent 346eea5 commit 7a90cfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+866
-640
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@
2020
*.yml~
2121
/nbproject/
2222

23+
/gradle
2324
/.gradle

build.gradle

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,34 @@ jacocoTestReport {
6262
csv.required = false
6363
html.outputLocation = layout.buildDirectory.dir('reports/jacoco')
6464
}
65+
66+
afterEvaluate {
67+
classDirectories.setFrom(files(classDirectories.files.collect {
68+
fileTree(dir: it, exclude: [
69+
"**/*Adapter.class",
70+
"**/SimpleCharStream.class",
71+
])
72+
}))
73+
}
6574
}
6675

6776
jacocoTestCoverageVerification {
6877
violationRules {
6978
rule {
7079
limit {
71-
minimum = 0.840
80+
minimum = 0.845
7281
}
7382
}
7483
}
84+
85+
afterEvaluate {
86+
classDirectories.setFrom(files(classDirectories.files.collect {
87+
fileTree(dir: it, exclude: [
88+
"**/*Adapter.class",
89+
"**/SimpleCharStream.class",
90+
])
91+
}))
92+
}
7593
}
7694

7795
spotbugsMain {
@@ -133,15 +151,15 @@ task renderRR() {
133151

134152
javaexec {
135153
standardOutput = new FileOutputStream("${buildDir}/rr/JSqlParserCC.ebnf")
136-
main="-jar";
154+
main="-jar"
137155
args = [
138156
"$buildDir/rr/convert.war",
139157
"$buildDir/generated/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jj"
140158
]
141159
}
142160

143161
javaexec {
144-
main="-jar";
162+
main="-jar"
145163
args = [
146164
"$buildDir/rr/rr.war",
147165
"-noepsilon",

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
package net.sf.jsqlparser.statement;
1111

12-
public class Block implements Statement {
12+
public class Block extends StatementImpl {
1313

1414
private Statements statements;
1515

@@ -27,8 +27,19 @@ public void accept(StatementVisitor statementVisitor) {
2727
}
2828

2929
@Override
30-
public String toString() {
31-
return "BEGIN\n" + (statements != null ? statements.toString() : "") + "END";
30+
public boolean isBlock() {
31+
return true;
32+
}
33+
34+
@Override
35+
public StatementType getStatementType() {
36+
return StatementType.BLOCK;
37+
}
38+
39+
@Override
40+
public StringBuilder appendTo(StringBuilder builder) {
41+
builder.append("BEGIN\n").append(statements != null ? statements.toString() : "").append("END");
42+
return builder;
3243
}
3344

3445
public Block withStatements(Statements statements) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
*/
1010
package net.sf.jsqlparser.statement;
1111

12-
public class Commit implements Statement {
12+
public class Commit extends DMLStatement {
1313
@Override
1414
public void accept(StatementVisitor statementVisitor) {
1515
statementVisitor.visit(this);
1616
}
17-
17+
1818
@Override
19-
public String toString() {
20-
return "COMMIT";
19+
public StringBuilder appendTo(StringBuilder builder) {
20+
builder.append("COMMIT");
21+
return builder;
2122
}
2223
}

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
/**
1919
* A base for the declaration of function like statements
2020
*/
21-
public abstract class CreateFunctionalStatement implements Statement {
21+
public abstract class CreateFunctionalStatement extends DDLStatement {
2222

2323
private String kind;
2424
private boolean orReplace = false;
@@ -89,11 +89,9 @@ public void accept(StatementVisitor statementVisitor) {
8989
statementVisitor.visit(this);
9090
}
9191

92-
@Override
93-
public String toString() {
94-
return "CREATE "
95-
+ (orReplace?"OR REPLACE ":"")
96-
+ kind + " " + formatDeclaration();
92+
public StringBuilder appendTo(StringBuilder builder) {
93+
builder.append("CREATE ").append(orReplace ? "OR REPLACE " : "").append(kind).append(" ").append(formatDeclaration());
94+
return builder;
9795
}
9896

9997
public CreateFunctionalStatement withFunctionDeclarationParts(List<String> functionDeclarationParts) {

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

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import net.sf.jsqlparser.statement.create.table.ColDataType;
2020
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;
2121

22-
public final class DeclareStatement implements Statement {
22+
public final class DeclareStatement extends StatementImpl {
2323

2424
private UserVariable userVariable = null;
2525
private DeclareType declareType = DeclareType.TYPE;
@@ -47,6 +47,8 @@ public DeclareType getType() {
4747
return getDeclareType();
4848
}
4949

50+
51+
5052
/**
5153
* @return the {@link DeclareType}
5254
*/
@@ -116,39 +118,50 @@ public void setTypeName(String typeName) {
116118
}
117119

118120
@Override
119-
public String toString() {
120-
StringBuilder b = new StringBuilder("DECLARE ");
121+
public boolean isBlock() {
122+
return true;
123+
}
124+
125+
@Override
126+
public StatementType getStatementType() {
127+
return StatementType.BLOCK;
128+
}
129+
130+
@Override
131+
public StringBuilder appendTo(StringBuilder builder) {
132+
builder.append("DECLARE ");
121133
if (declareType == DeclareType.AS) {
122-
b.append(userVariable.toString());
123-
b.append(" AS ").append(typeName);
134+
builder.append(userVariable.toString());
135+
builder.append(" AS ").append(typeName);
124136
} else {
125137
if (declareType == DeclareType.TABLE) {
126-
b.append(userVariable.toString());
127-
b.append(" TABLE (");
138+
builder.append(userVariable.toString());
139+
builder.append(" TABLE (");
128140
for (int i = 0; i < columnDefinitions.size(); i++) {
129141
if (i > 0) {
130-
b.append(", ");
142+
builder.append(", ");
131143
}
132-
b.append(columnDefinitions.get(i).toString());
144+
builder.append(columnDefinitions.get(i).toString());
133145
}
134-
b.append(")");
146+
builder.append(")");
135147
} else {
136148
for (int i = 0; i < typeDefExprList.size(); i++) {
137149
if (i > 0) {
138-
b.append(", ");
150+
builder.append(", ");
139151
}
140152
final TypeDefExpr type = typeDefExprList.get(i);
141153
if (type.userVariable != null) {
142-
b.append(type.userVariable.toString()).append(" ");
154+
builder.append(type.userVariable).append(" ");
143155
}
144-
b.append(type.colDataType.toString());
156+
builder.append(type.colDataType.toString());
145157
if (type.defaultExpr != null) {
146-
b.append(" = ").append(type.defaultExpr.toString());
158+
builder.append(" = ").append(type.defaultExpr);
147159
}
148160
}
149161
}
150162
}
151-
return b.toString();
163+
164+
return builder;
152165
}
153166

154167
@Override

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
import net.sf.jsqlparser.schema.Table;
1313

14-
public class DescribeStatement implements Statement {
14+
public class DescribeStatement extends DDLStatement {
1515

1616
private Table table;
1717

1818
public DescribeStatement() {
19-
// empty constructor
19+
table = null;
2020
}
2121

2222
public DescribeStatement(Table table) {
@@ -31,11 +31,6 @@ public void setTable(Table table) {
3131
this.table = table;
3232
}
3333

34-
@Override
35-
public String toString() {
36-
return "DESCRIBE " + table.getFullyQualifiedName();
37-
}
38-
3934
@Override
4035
public void accept(StatementVisitor statementVisitor) {
4136
statementVisitor.visit(this);
@@ -45,4 +40,10 @@ public DescribeStatement withTable(Table table) {
4540
this.setTable(table);
4641
return this;
4742
}
43+
44+
@Override
45+
public StringBuilder appendTo(StringBuilder builder) {
46+
builder.append("DESCRIBE ").append(table.getFullyQualifiedName());
47+
return builder;
48+
}
4849
}

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/**
1818
* An {@code EXPLAIN} statement
1919
*/
20-
public class ExplainStatement implements Statement {
20+
public class ExplainStatement extends DDLStatement {
2121

2222
private Select select;
2323
private LinkedHashMap<OptionType, Option> options;
@@ -63,21 +63,21 @@ public Option getOption(OptionType optionType) {
6363
}
6464

6565
@Override
66-
public String toString() {
67-
StringBuilder statementBuilder = new StringBuilder("EXPLAIN");
68-
if (options != null) {
69-
statementBuilder.append(" ");
70-
statementBuilder.append(options.values().stream().map(Option::formatOption).collect(Collectors.joining(" ")));
71-
}
72-
73-
statementBuilder.append(" ");
74-
statementBuilder.append(select.toString());
75-
return statementBuilder.toString();
66+
public void accept(StatementVisitor statementVisitor) {
67+
statementVisitor.visit(this);
7668
}
7769

7870
@Override
79-
public void accept(StatementVisitor statementVisitor) {
80-
statementVisitor.visit(this);
71+
public StringBuilder appendTo(StringBuilder builder) {
72+
builder.append("EXPLAIN");
73+
if (options != null) {
74+
builder.append(" ");
75+
builder.append(options.values().stream().map(Option::formatOption).collect(Collectors.joining(" ")));
76+
}
77+
78+
builder.append(" ");
79+
select.appendTo(builder);
80+
return builder;
8181
}
8282

8383
public enum OptionType {

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
1919
*/
20-
public class IfElseStatement implements Statement {
20+
public class IfElseStatement extends StatementImpl {
2121
private final Expression condition;
2222
private final Statement ifStatement;
2323
private Statement elseStatement;
@@ -63,6 +63,16 @@ public boolean isUsingSemicolonForIfStatement() {
6363
return usingSemicolonForIfStatement;
6464
}
6565

66+
@Override
67+
public boolean isBlock() {
68+
return true;
69+
}
70+
71+
@Override
72+
public StatementType getStatementType() {
73+
return StatementType.BLOCK;
74+
}
75+
6676
public StringBuilder appendTo(StringBuilder builder) {
6777
builder.append("IF ").append(condition).append(" ").append(ifStatement)
6878
.append(usingSemicolonForIfStatement ? ";" : "");
@@ -74,11 +84,6 @@ public StringBuilder appendTo(StringBuilder builder) {
7484
return builder;
7585
}
7686

77-
@Override
78-
public String toString() {
79-
return appendTo(new StringBuilder()).toString();
80-
}
81-
8287
@Override
8388
public void accept(StatementVisitor statementVisitor) {
8489
statementVisitor.visit(this);

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @see <a href="https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_9018.htm">Purge</a>
2121
*/
2222

23-
public class PurgeStatement implements Statement {
23+
public class PurgeStatement extends DDLStatement {
2424
private final PurgeObjectType purgeObjectType;
2525
private final Object object;
2626
private String userName;
@@ -80,11 +80,6 @@ public StringBuilder appendTo(StringBuilder builder) {
8080
return builder;
8181
}
8282

83-
@Override
84-
public String toString() {
85-
return appendTo(new StringBuilder()).toString();
86-
}
87-
8883
public String getUserName() {
8984
return userName;
9085
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,19 @@
1010
package net.sf.jsqlparser.statement;
1111

1212

13-
public final class ResetStatement implements Statement {
13+
public final class ResetStatement extends DDLStatement {
1414

15-
private String name = "";
15+
private String name;
1616

1717
public ResetStatement() {
18-
// empty constructor
18+
name = "";
1919
}
2020

2121
public ResetStatement(String name) {
22-
add(name);
22+
this.name = name;
2323
}
2424

25+
@Deprecated
2526
public void add(String name) {
2627
this.name = name;
2728
}
@@ -35,9 +36,9 @@ public void setName(String name) {
3536
}
3637

3738
@Override
38-
public String toString() {
39-
StringBuilder b = new StringBuilder("RESET ").append(name);
40-
return b.toString();
39+
public StringBuilder appendTo(StringBuilder builder) {
40+
builder.append("RESET ").append(name);
41+
return builder;
4142
}
4243

4344
@Override

0 commit comments

Comments
 (0)