Skip to content

Commit

Permalink
* an extensible validation visitor framework (ValidationCapability and
Browse files Browse the repository at this point in the history
its subtypes FeatureSetValidation, DatabaseMetaDataValidation,
AllowedTypesValidation)
* FeatureConfiguration/Feature/FeatureSet class

new packages
* net.sf.jsqlparser.util.validation
* net.sf.jsqlparser.parser.feature

------

history:
* fresh clone from JSQLParser/JSqlParser (master) -> master.validate
* (Merge the work on this issue done in 'github.validate' into
master.validate)

JSQLParser#1005
  • Loading branch information
gitmotte committed Aug 26, 2020
2 parents 9e26b76 + 225db94 commit 1c58c6e
Show file tree
Hide file tree
Showing 71 changed files with 6,224 additions and 78 deletions.
41 changes: 6 additions & 35 deletions src/main/java/net/sf/jsqlparser/JSQLParserException.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,50 +12,21 @@
public class JSQLParserException extends Exception {

private static final long serialVersionUID = -1099039459759769980L;
private Throwable cause = null;

public JSQLParserException() {
super();
}

public JSQLParserException(String arg0) {
super(arg0);
public JSQLParserException(String message, Throwable cause) {
super(message, cause);
}

public JSQLParserException(Throwable arg0) {
this.cause = arg0;
public JSQLParserException(String message) {
super(message);
}

public JSQLParserException(String arg0, Throwable arg1) {
super(arg0);
this.cause = arg1;
public JSQLParserException(Throwable cause) {
super(cause == null ? null : cause.getMessage(), cause);
}

@Override
public synchronized Throwable getCause() {
return cause;
}

@Override
public void printStackTrace() {
printStackTrace(System.err);
}

@Override
public void printStackTrace(java.io.PrintWriter pw) {
super.printStackTrace(pw);
if (cause != null) {
pw.println("Caused by:");
cause.printStackTrace(pw);
}
}

@Override
public void printStackTrace(java.io.PrintStream ps) {
super.printStackTrace(ps);
if (cause != null) {
ps.println("Caused by:");
cause.printStackTrace(ps);
}
}
}
49 changes: 49 additions & 0 deletions src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2020 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.parser;

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

import net.sf.jsqlparser.parser.feature.Feature;
import net.sf.jsqlparser.parser.feature.FeatureConfiguration;

public abstract class AbstractJSqlParser<P> {

protected int jdbcParameterIndex = 0;
protected boolean errorRecovery = false;
protected List<ParseException> parseErrors = new ArrayList<>();

public P withSquareBracketQuotation(boolean allowSquareBracketQuotation) {
return withFeature(Feature.allowSquareBracketQuotation, allowSquareBracketQuotation);
}

public P withFeature(Feature f, boolean enabled) {
getConfiguration().setValue(f, enabled);
return me();
}

public abstract FeatureConfiguration getConfiguration();

public abstract P me();

public boolean getAsBoolean(Feature f) {
return getConfiguration().getAsBoolean(f);
}

public void setErrorRecovery(boolean errorRecovery) {
this.errorRecovery = errorRecovery;
}

public List<ParseException> getParseErrors() {
return parseErrors;
}

}
89 changes: 63 additions & 26 deletions src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
package net.sf.jsqlparser.parser;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.function.Consumer;
Expand All @@ -29,11 +30,7 @@ private CCJSqlParserUtil() {

public static Statement parse(Reader statementReader) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StreamProvider(statementReader));
try {
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
return parseStatement(parser);
}

public static Statement parse(String sql) throws JSQLParserException {
Expand All @@ -42,32 +39,40 @@ public static Statement parse(String sql) throws JSQLParserException {

/**
* Parses an sql statement while allowing via consumer to configure the used parser before.
*
*
* For instance to activate SQLServer bracket quotation on could use:
*
* {@code
*
* {@code
* CCJSqlParserUtil.parse("select * from [mytable]", parser -> parser.withSquareBracketQuotation(true));
* }
*
*
* @param sql
* @param consumer
* @return
* @throws JSQLParserException
* @throws JSQLParserException
*/
public static Statement parse(String sql, Consumer<CCJSqlParser> consumer) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(sql));
CCJSqlParser parser = newParser(sql);
if (consumer != null) {
consumer.accept(parser);
}
try {
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
return parseStatement(parser);
}

public static CCJSqlParser newParser(String sql) {
return new CCJSqlParser(new StringProvider(sql));
}

public static CCJSqlParser newParser(InputStream is) throws IOException {
return new CCJSqlParser(new StreamProvider(is));
}

public static CCJSqlParser newParser(InputStream is, String encoding) throws IOException {
return new CCJSqlParser(new StreamProvider(is, encoding));
}

public static Node parseAST(String sql) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(sql));
CCJSqlParser parser = newParser(sql);
try {
parser.Statement();
return parser.jjtree.rootNode();
Expand All @@ -78,7 +83,7 @@ public static Node parseAST(String sql) throws JSQLParserException {

public static Statement parse(InputStream is) throws JSQLParserException {
try {
CCJSqlParser parser = new CCJSqlParser(new StreamProvider(is));
CCJSqlParser parser = newParser(is);
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
Expand All @@ -87,7 +92,7 @@ public static Statement parse(InputStream is) throws JSQLParserException {

public static Statement parse(InputStream is, String encoding) throws JSQLParserException {
try {
CCJSqlParser parser = new CCJSqlParser(new StreamProvider(is, encoding));
CCJSqlParser parser = newParser(is, encoding);
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
Expand All @@ -99,7 +104,7 @@ public static Expression parseExpression(String expression) throws JSQLParserExc
}

public static Expression parseExpression(String expression, boolean allowPartialParse) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(expression));
CCJSqlParser parser = newParser(expression);
try {
Expression expr = parser.SimpleExpression();
if (!allowPartialParse && parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
Expand All @@ -113,20 +118,28 @@ public static Expression parseExpression(String expression, boolean allowPartial
}
}

/**
* Parse an conditional expression. This is the expression after a where clause.
* Partial parsing is enabled.
*
* @param condExpr
* @return the expression parsed
* @see #parseCondExpression(String, boolean)
*/
public static Expression parseCondExpression(String condExpr) throws JSQLParserException {
return parseCondExpression(condExpr, true);
}

/**
* Parse an conditional expression. This is the expression after a where
* clause.
* Parse an conditional expression. This is the expression after a where clause.
*
* @param condExpr
* @param allowPartialParse false: needs the whole string to be processed.
* @return
* @return the expression parsed
* @see #parseCondExpression(String)
*/
public static Expression parseCondExpression(String condExpr, boolean allowPartialParse) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(condExpr));
CCJSqlParser parser = newParser(condExpr);
try {
Expression expr = parser.Expression();
if (!allowPartialParse && parser.getNextToken().kind != CCJSqlParserTokenManager.EOF) {
Expand All @@ -140,11 +153,35 @@ public static Expression parseCondExpression(String condExpr, boolean allowParti
}
}

/**
* @param parser
* @return the statement parsed
* @throws JSQLParserException
*/
public static Statement parseStatement(CCJSqlParser parser) throws JSQLParserException {
try {
return parser.Statement();
} catch (Exception ex) {
throw new JSQLParserException(ex);
}
}

/**
* Parse a statement list.
*
* @return the statements parsed
*/
public static Statements parseStatements(String sqls) throws JSQLParserException {
CCJSqlParser parser = new CCJSqlParser(new StringProvider(sqls));
CCJSqlParser parser = newParser(sqls);
return parseStatements(parser);
}

/**
* @param parser
* @return the statements parsed
* @throws JSQLParserException
*/
public static Statements parseStatements(CCJSqlParser parser) throws JSQLParserException {
try {
return parser.Statements();
} catch (Exception ex) {
Expand All @@ -154,7 +191,7 @@ public static Statements parseStatements(String sqls) throws JSQLParserException

public static void streamStatements(StatementListener listener, InputStream is, String encoding) throws JSQLParserException {
try {
CCJSqlParser parser = new CCJSqlParser(new StreamProvider(is, encoding));
CCJSqlParser parser = newParser(is, encoding);
while (true) {
Statement stmt = parser.SingleStatement();
listener.accept(stmt);
Expand Down
Loading

0 comments on commit 1c58c6e

Please sign in to comment.