com.jaregu.database.queries.compiling.OptionalHyphenNamedParameterFeature Maven / Gradle / Ivy
Show all versions of queries Show documentation
package com.jaregu.database.queries.compiling;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import com.jaregu.database.queries.building.ParameterBinder;
import com.jaregu.database.queries.compiling.expr.ExpressionParser;
import com.jaregu.database.queries.parsing.ParsedQueryPart;
/**
* Matches parameter line
*
* Example:
*
*
* ...
* AND t.SOME_FIELD = 123 -- value_expression[; conditional_expression]
* ...
*
*
* Expression is something for what
* {@link ExpressionParser#isLikeExpression(String)} is true
*
*
* value_expression will be value for binded SQL parameter
*
* conditional_expression is testable condition when this row will be
* added to SQL, default condition is conditional_expression != null
*/
final class OptionalHyphenNamedParameterFeature extends OptionalNamedParameterFeatureBase {
private final List> _const_comment_ = Arrays.asList(isSqlWithoutNewLine,
isHyphenCommentExpression);
private final List> _question_comment_ = Arrays.asList(isAnonymousVariable,
isHyphenCommentExpression);
private final List> _question_sql_comment_ = Arrays.asList(isAnonymousVariable,
isSqlWithoutNewLine, isHyphenCommentExpression);
private final List> _sql_question_comment_ = Arrays.asList(isSqlWithoutNewLine,
isAnonymousVariable, isHyphenCommentExpression);
private final List> _sql_question_sql_comment_ = Arrays
.asList(isSqlWithoutNewLine, isAnonymousVariable, isSqlWithoutNewLine, isHyphenCommentExpression);
OptionalHyphenNamedParameterFeature(ExpressionParser expressionParser, ParameterBinder parameterBinder) {
super(expressionParser, parameterBinder);
}
@Override
public boolean isCompilable(Source source) {
return isPartsLike(source, _const_comment_) || isPartsLike(source, _question_comment_)
|| isPartsLike(source, _question_sql_comment_) || isPartsLike(source, _sql_question_comment_)
|| isPartsLike(source, _sql_question_sql_comment_);
}
@Override
public Result compile(Source source, Compiler compiler) {
Builder builder;
if (isPartsLike(source, _const_comment_)) {
builder = new Builder(source).before(0, true).after(1).comment(1);
} else if (isPartsLike(source, _question_comment_)) {
builder = new Builder(source).after(1).comment(1);
} else if (isPartsLike(source, _question_sql_comment_)) {
builder = new Builder(source).after(1, 2).comment(2);
} else if (isPartsLike(source, _sql_question_comment_)) {
builder = new Builder(source).before(0).after(2).comment(2);
} else if (isPartsLike(source, _sql_question_sql_comment_)) {
builder = new Builder(source).before(0).after(2, 3).comment(3);
} else {
throw new QueryCompileException("Wrong parts count!");
}
return builder.build();
}
}