use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.
the class SQLPlanner method validateColumnConstaintExpressionToExpression.
private Expression validateColumnConstaintExpressionToExpression(Expression testExpression, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
Expression result = null;
if (expressionType.isAssignableFrom(testExpression.getClass())) {
BinaryExpression e = (BinaryExpression) testExpression;
if (e.getLeftExpression() instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e.getLeftExpression();
boolean okAlias = true;
if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(tableAlias)) {
okAlias = false;
}
if (okAlias && columnName.equalsIgnoreCase(c.getColumnName()) && SQLRecordPredicate.isConstant(e.getRightExpression())) {
return e;
}
} else if (e.getLeftExpression() instanceof AndExpression) {
result = findConstraintExpressionOnColumn(e.getLeftExpression(), columnName, tableAlias, expressionType);
if (result != null) {
return result;
}
} else if (e.getRightExpression() instanceof AndExpression) {
result = findConstraintExpressionOnColumn(e.getRightExpression(), columnName, tableAlias, expressionType);
if (result != null) {
return result;
}
}
}
return result;
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.
the class SQLExpressionCompiler method compileExpression.
// this method never returns NULL
public static CompiledSQLExpression compileExpression(String validatedTableAlias, Expression exp) {
if (exp instanceof BinaryExpression) {
CompiledSQLExpression compiled = compileSpecialBinaryExpression(validatedTableAlias, exp);
if (compiled != null) {
return compiled;
}
}
if (exp instanceof net.sf.jsqlparser.schema.Column) {
return compileColumnExpression(validatedTableAlias, exp);
} else if (exp instanceof StringValue) {
return new ConstantExpression(RawString.of(((StringValue) exp).getValue()));
} else if (exp instanceof LongValue) {
try {
return new ConstantExpression(((LongValue) exp).getValue());
} catch (NumberFormatException largeNumber) {
return new ConstantExpression(Double.valueOf(((LongValue) exp).getStringValue()));
}
} else if (exp instanceof DoubleValue) {
return new ConstantExpression(((DoubleValue) exp).getValue());
} else if (exp instanceof TimestampValue) {
return new ConstantExpression(((TimestampValue) exp).getValue());
} else if (exp instanceof NullValue) {
return new ConstantExpression(null);
} else if (exp instanceof TimeKeyExpression) {
TimeKeyExpression ext = (TimeKeyExpression) exp;
if (CURRENT_TIMESTAMP.equalsIgnoreCase(ext.getStringValue())) {
return new ConstantExpression(new java.sql.Timestamp(System.currentTimeMillis()));
} else {
throw new StatementExecutionException("unhandled expression " + exp);
}
} else if (exp instanceof JdbcParameter) {
int index = ((JdbcParameter) exp).getIndex() - 1;
return new JdbcParameterExpression(index);
} else if (exp instanceof AndExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAndExpression(a, b, c));
} else if (exp instanceof OrExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledOrExpression(a, b, c));
} else if (exp instanceof Function) {
return CompiledFunction.create((Function) exp, validatedTableAlias);
} else if (exp instanceof Addition) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledAddExpression(a, b, c));
} else if (exp instanceof Subtraction) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledSubtractExpression(a, b, c));
} else if (exp instanceof Multiplication) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMultiplyExpression(a, b, c));
} else if (exp instanceof Division) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledDivideExpression(a, b, c));
} else if (exp instanceof Parenthesis) {
Parenthesis p = (Parenthesis) exp;
CompiledSQLExpression inner = compileExpression(validatedTableAlias, p.getExpression());
return new CompiledParenthesisExpression(p.isNot(), inner);
} else if (exp instanceof EqualsTo) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledEqualsExpression(a, b, c));
} else if (exp instanceof NotEqualsTo) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledNotEqualsExpression(a, b, c));
} else if (exp instanceof MinorThan) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenExpression(a, b, c));
} else if (exp instanceof MinorThanEquals) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledMinorThenEqualsExpression(a, b, c));
} else if (exp instanceof GreaterThan) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenExpression(a, b, c));
} else if (exp instanceof GreaterThanEquals) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledGreaterThenEqualsExpression(a, b, c));
} else if (exp instanceof LikeExpression) {
return tryCompileBinaryExpression(validatedTableAlias, (BinaryExpression) exp, (a, b, c) -> new CompiledLikeExpression(a, b, c));
} else if (exp instanceof Between) {
return CompiledBetweenExpression.create(validatedTableAlias, (Between) exp);
} else if (exp instanceof SignedExpression) {
SignedExpression s = (SignedExpression) exp;
CompiledSQLExpression inner = compileExpression(validatedTableAlias, s.getExpression());
return new CompiledSignedExpression(s.getSign(), inner);
} else if (exp instanceof InExpression) {
InExpression in = (InExpression) exp;
return CompiledInExpression.create(in, validatedTableAlias);
} else if (exp instanceof IsNullExpression) {
IsNullExpression i = (IsNullExpression) exp;
CompiledSQLExpression left = compileExpression(validatedTableAlias, i.getLeftExpression());
return new CompiledIsNullExpression(i.isNot(), left);
} else if (exp instanceof CaseExpression) {
return CompiledCaseExpression.create(validatedTableAlias, (CaseExpression) exp);
}
throw new StatementExecutionException("unsupported operand " + exp.getClass() + ", expression is " + exp);
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.
the class CloneHelper method changeBack.
/**
* This helper method is used to change the multiple expression into
* the binary form, respectively and return the root of the expression tree.
* @param isMultiOr variable tells whether the expression is or.
* @param exp the expression that needs to be converted.
* @return the root of the expression tree.
*/
public Expression changeBack(Boolean isMultiOr, Expression exp) {
if (!(exp instanceof MultipleExpression)) {
return exp;
}
MultipleExpression changed = (MultipleExpression) exp;
Expression result = changed.getChild(0);
for (int i = 1; i < changed.size(); i++) {
Expression left = result;
Expression right = changed.getChild(i);
if (isMultiOr) {
result = new OrExpression(left, right);
} else {
result = new AndExpression(left, right);
}
}
if (isMultiOr) {
return new Parenthesis(result);
}
return result;
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.
the class CloneHelper method modify.
/**
* This method is used for changing the logical structure of the tree.
* The main method is to convert and operator and or operator to let
* them have multiple children (reflected in MultipleExpression.java
* from the conditional package). Note if the value from the conditional
* operator has a not attached to it we need to append an not operator
* ahead of it since the not operator needed to be pushed down during
* the second step. Also, I will leave out all the parenthesis expression
* which is connected to the conditional operator.
* @param express the expression that will be modified
* @return the modified expression.
*/
public Expression modify(Expression express) {
if (express instanceof NotExpression) {
return new NotExpression(modify(((NotExpression) express).getExpression()));
}
if (express instanceof Parenthesis) {
Parenthesis parenthesis = (Parenthesis) express;
Expression result = modify(parenthesis.getExpression());
if (parenthesis.isNot()) {
return new NotExpression(result);
}
return result;
}
if (express instanceof AndExpression) {
AndExpression and = (AndExpression) express;
List<Expression> list = new ArrayList<Expression>();
list.add(modify(and.getLeftExpression()));
list.add(modify(and.getRightExpression()));
MultiAndExpression result = new MultiAndExpression(list);
if (and.isNot()) {
return new NotExpression(result);
}
return result;
}
if (express instanceof OrExpression) {
OrExpression or = (OrExpression) express;
List<Expression> list = new ArrayList<Expression>();
list.add(modify(or.getLeftExpression()));
list.add(modify(or.getRightExpression()));
MultiOrExpression result = new MultiOrExpression(list);
if (or.isNot()) {
return new NotExpression(result);
}
return result;
}
if (express instanceof BinaryExpression) {
BinaryExpression binary = (BinaryExpression) express;
if (binary.isNot()) {
binary.removeNot();
return new NotExpression(modify(binary));
}
}
/* at this stage, there is no need to modify, just simply return. */
return express;
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.
the class AdaptersTest method testAdapters.
/**
* Test extracting JDBC named parameters using adapters
*/
@Test
public void testAdapters() throws JSQLParserException {
String sql = "SELECT * FROM MYTABLE WHERE COLUMN_A = :paramA AND COLUMN_B <> :paramB";
Statement stmnt = CCJSqlParserUtil.parse(sql);
final Stack<Pair<String, String>> params = new Stack<Pair<String, String>>();
stmnt.accept(new StatementVisitorAdapter() {
@Override
public void visit(Select select) {
select.getSelectBody().accept(new SelectVisitorAdapter() {
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.getWhere().accept(new ExpressionVisitorAdapter() {
@Override
protected void visitBinaryExpression(BinaryExpression expr) {
if (!(expr instanceof AndExpression)) {
params.push(new Pair<String, String>(null, null));
}
super.visitBinaryExpression(expr);
}
@Override
public void visit(Column column) {
params.push(new Pair<String, String>(column.getColumnName(), params.pop().getRight()));
}
@Override
public void visit(JdbcNamedParameter parameter) {
params.push(new Pair<String, String>(params.pop().getLeft(), parameter.getName()));
}
});
}
});
}
});
assertEquals(2, params.size());
Pair<String, String> param2 = params.pop();
assertEquals("COLUMN_B", param2.getLeft());
assertEquals("paramB", param2.getRight());
Pair<String, String> param1 = params.pop();
assertEquals("COLUMN_A", param1.getLeft());
assertEquals("paramA", param1.getRight());
}
Aggregations