Skip to content

Statement Classification QUERY, DML, DDL, BLOCK, UNPARSED #1301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
*.yml~
/nbproject/

/gradle
/.gradle
32 changes: 27 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ compileJavacc {
java {
withSourcesJar()
withJavadocJar()

spotbugs
pmd
}

jacoco {
Expand All @@ -62,16 +65,34 @@ jacocoTestReport {
csv.required = false
html.outputLocation = layout.buildDirectory.dir('reports/jacoco')
}

afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
"**/*Adapter.class",
"**/SimpleCharStream.class",
])
}))
}
}

jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = 0.840
minimum = 0.845
}
}
}

afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it, exclude: [
"**/*Adapter.class",
"**/SimpleCharStream.class",
])
}))
}
}

spotbugsMain {
Expand All @@ -95,8 +116,9 @@ spotbugs {
pmd {
consoleOutput = false
toolVersion = "6.36.0"

sourceSets = [sourceSets.main]

// run only over the main sources for now, but not over the tests
sourceSets = [sourceSets.main /*, sourceSets.test */]

// clear the ruleset in order to use configured rules only, although we should apply the Prio 1 rules eventually
ruleSets = []
Expand Down Expand Up @@ -133,15 +155,15 @@ task renderRR() {

javaexec {
standardOutput = new FileOutputStream("${buildDir}/rr/JSqlParserCC.ebnf")
main="-jar";
main="-jar"
args = [
"$buildDir/rr/convert.war",
"$buildDir/generated/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jj"
]
}

javaexec {
main="-jar";
main="-jar"
args = [
"$buildDir/rr/rr.war",
"-noepsilon",
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/net/sf/jsqlparser/statement/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
package net.sf.jsqlparser.statement;

public class Block implements Statement {
public class Block extends StatementImpl {

private Statements statements;

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

@Override
public String toString() {
return "BEGIN\n" + (statements != null ? statements.toString() : "") + "END";
public boolean isBlock() {
return true;
}

@Override
public StatementType getStatementType() {
return StatementType.BLOCK;
}

@Override
public StringBuilder appendTo(StringBuilder builder) {
builder.append("BEGIN\n").append(statements != null ? statements.toString() : "").append("END");
return builder;
}

public Block withStatements(Statements statements) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/net/sf/jsqlparser/statement/Commit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
*/
package net.sf.jsqlparser.statement;

public class Commit implements Statement {
public class Commit extends DMLStatement {
@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

@Override
public String toString() {
return "COMMIT";
public StringBuilder appendTo(StringBuilder builder) {
builder.append("COMMIT");
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
/**
* A base for the declaration of function like statements
*/
public abstract class CreateFunctionalStatement implements Statement {
public abstract class CreateFunctionalStatement extends DDLStatement {

private String kind;
private boolean orReplace = false;
Expand Down Expand Up @@ -89,11 +89,9 @@ public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

@Override
public String toString() {
return "CREATE "
+ (orReplace?"OR REPLACE ":"")
+ kind + " " + formatDeclaration();
public StringBuilder appendTo(StringBuilder builder) {
builder.append("CREATE ").append(orReplace ? "OR REPLACE " : "").append(kind).append(" ").append(formatDeclaration());
return builder;
}

public CreateFunctionalStatement withFunctionDeclarationParts(List<String> functionDeclarationParts) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/DDLStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2021 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
/*
* To change this license header, choose License Headers in Project Properties. To change this
* template file, choose Tools | Templates and open the template in the editor.
*/

package net.sf.jsqlparser.statement;

/**
*
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
*/
public abstract class DDLStatement extends StatementImpl {
@Override
public boolean isDDL() {
return true;
}

@Override
public StatementType getStatementType() {
return StatementType.DDL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this type method? Your have your grouping by the class.

}

public abstract StringBuilder appendTo(StringBuilder builder);
}
31 changes: 31 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/DMLStatement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2021 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
/*
* To change this license header, choose License Headers in Project Properties. To change this
* template file, choose Tools | Templates and open the template in the editor.
*/

package net.sf.jsqlparser.statement;

/**
*
* @author <a href="mailto:andreas@manticore-projects.com">Andreas Reichel</a>
*/
public abstract class DMLStatement extends StatementImpl {
@Override
public boolean isDML() {
return true;
}

@Override
public StatementType getStatementType() {
return StatementType.DML;
}
}
43 changes: 28 additions & 15 deletions src/main/java/net/sf/jsqlparser/statement/DeclareStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import net.sf.jsqlparser.statement.create.table.ColDataType;
import net.sf.jsqlparser.statement.create.table.ColumnDefinition;

public final class DeclareStatement implements Statement {
public final class DeclareStatement extends StatementImpl {

private UserVariable userVariable = null;
private DeclareType declareType = DeclareType.TYPE;
Expand Down Expand Up @@ -47,6 +47,8 @@ public DeclareType getType() {
return getDeclareType();
}



/**
* @return the {@link DeclareType}
*/
Expand Down Expand Up @@ -116,39 +118,50 @@ public void setTypeName(String typeName) {
}

@Override
public String toString() {
StringBuilder b = new StringBuilder("DECLARE ");
public boolean isBlock() {
return true;
}

@Override
public StatementType getStatementType() {
return StatementType.BLOCK;
}

@Override
public StringBuilder appendTo(StringBuilder builder) {
builder.append("DECLARE ");
if (declareType == DeclareType.AS) {
b.append(userVariable.toString());
b.append(" AS ").append(typeName);
builder.append(userVariable.toString());
builder.append(" AS ").append(typeName);
} else {
if (declareType == DeclareType.TABLE) {
b.append(userVariable.toString());
b.append(" TABLE (");
builder.append(userVariable.toString());
builder.append(" TABLE (");
for (int i = 0; i < columnDefinitions.size(); i++) {
if (i > 0) {
b.append(", ");
builder.append(", ");
}
b.append(columnDefinitions.get(i).toString());
builder.append(columnDefinitions.get(i).toString());
}
b.append(")");
builder.append(")");
} else {
for (int i = 0; i < typeDefExprList.size(); i++) {
if (i > 0) {
b.append(", ");
builder.append(", ");
}
final TypeDefExpr type = typeDefExprList.get(i);
if (type.userVariable != null) {
b.append(type.userVariable.toString()).append(" ");
builder.append(type.userVariable).append(" ");
}
b.append(type.colDataType.toString());
builder.append(type.colDataType.toString());
if (type.defaultExpr != null) {
b.append(" = ").append(type.defaultExpr.toString());
builder.append(" = ").append(type.defaultExpr);
}
}
}
}
return b.toString();

return builder;
}

@Override
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/sf/jsqlparser/statement/DescribeStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

import net.sf.jsqlparser.schema.Table;

public class DescribeStatement implements Statement {
public class DescribeStatement extends DDLStatement {

private Table table;

public DescribeStatement() {
// empty constructor
table = null;
}

public DescribeStatement(Table table) {
Expand All @@ -31,11 +31,6 @@ 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);
Expand All @@ -45,4 +40,10 @@ public DescribeStatement withTable(Table table) {
this.setTable(table);
return this;
}

@Override
public StringBuilder appendTo(StringBuilder builder) {
builder.append("DESCRIBE ").append(table.getFullyQualifiedName());
return builder;
}
}
26 changes: 13 additions & 13 deletions src/main/java/net/sf/jsqlparser/statement/ExplainStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
/**
* An {@code EXPLAIN} statement
*/
public class ExplainStatement implements Statement {
public class ExplainStatement extends DDLStatement {

private Select select;
private LinkedHashMap<OptionType, Option> options;
Expand Down Expand Up @@ -63,21 +63,21 @@ public Option getOption(OptionType optionType) {
}

@Override
public String toString() {
StringBuilder statementBuilder = new StringBuilder("EXPLAIN");
if (options != null) {
statementBuilder.append(" ");
statementBuilder.append(options.values().stream().map(Option::formatOption).collect(Collectors.joining(" ")));
}

statementBuilder.append(" ");
statementBuilder.append(select.toString());
return statementBuilder.toString();
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
}

@Override
public void accept(StatementVisitor statementVisitor) {
statementVisitor.visit(this);
public StringBuilder appendTo(StringBuilder builder) {
builder.append("EXPLAIN");
if (options != null) {
builder.append(" ");
builder.append(options.values().stream().map(Option::formatOption).collect(Collectors.joining(" ")));
}

builder.append(" ");
select.appendTo(builder);
return builder;
}

public enum OptionType {
Expand Down
Loading