use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.
the class SQLParserExpressionCompiler method compileExpressionInternal.
private static CompiledSQLExpression compileExpressionInternal(Expression expression, OpSchema tableSchema) {
if (expression == null) {
return null;
}
if (expression instanceof JdbcParameter) {
JdbcParameter p = (JdbcParameter) expression;
return new JdbcParameterExpression(p.getIndex() - 1);
} else if (expression instanceof StringValue || expression instanceof LongValue || expression instanceof NullValue || expression instanceof DoubleValue || expression instanceof TimestampValue) {
return JSQLParserPlanner.resolveValueAsCompiledSQLExpression(expression, false);
} else if (expression instanceof net.sf.jsqlparser.schema.Column) {
// mapping a reference to a Column to the index in the schema of the table
net.sf.jsqlparser.schema.Column col = (net.sf.jsqlparser.schema.Column) expression;
String tableAlias = extractTableName(col);
// no fix backtick, handle false/true literals, without backticks
String columnName = col.getColumnName();
if (isBooleanLiteral(col)) {
return new ConstantExpression(Boolean.parseBoolean(columnName.toLowerCase()), ColumnTypes.NOTNULL_BOOLEAN);
}
IntHolder indexInSchema = new IntHolder(-1);
ColumnRef found = findColumnInSchema(tableAlias, columnName, tableSchema, indexInSchema);
if (indexInSchema.value == -1 || found == null) {
String nameInError = tableAlias != null ? tableAlias + "." + columnName : columnName;
throw new StatementExecutionException("Column " + nameInError + " not found in target table (schema " + tableSchema + ")");
}
return new AccessCurrentRowExpression(indexInSchema.value, found.type);
} else if (expression instanceof BinaryExpression) {
return compileBinaryExpression((BinaryExpression) expression, tableSchema);
} else if (expression instanceof IsNullExpression) {
IsNullExpression eq = (IsNullExpression) expression;
CompiledSQLExpression left = compileExpression(eq.getLeftExpression(), tableSchema);
return new CompiledIsNullExpression(eq.isNot(), left);
} else if (expression instanceof NotExpression) {
NotExpression eq = (NotExpression) expression;
CompiledSQLExpression left = compileExpression(eq.getExpression(), tableSchema);
return new CompiledNotExpression(left);
} else if (expression instanceof Parenthesis) {
Parenthesis eq = (Parenthesis) expression;
return compileExpression(eq.getExpression(), tableSchema);
} else if (expression instanceof SignedExpression) {
SignedExpression eq = (SignedExpression) expression;
return new CompiledSignedExpression(eq.getSign(), compileExpression(eq.getExpression(), tableSchema));
} else if (expression instanceof InExpression) {
InExpression eq = (InExpression) expression;
checkSupported(eq.getOldOracleJoinSyntax() == EqualsTo.NO_ORACLE_JOIN);
checkSupported(eq.getOraclePriorPosition() == EqualsTo.NO_ORACLE_PRIOR);
checkSupported(eq.getLeftItemsList() == null);
checkSupported(eq.getMultiExpressionList() == null);
checkSupported(eq.getRightExpression() == null);
CompiledSQLExpression left = compileExpression(eq.getLeftExpression(), tableSchema);
ItemsList rightItemsList = eq.getRightItemsList();
checkSupported(rightItemsList instanceof ExpressionList, "Sub Selects are not supported with jSQLParser");
ExpressionList expressionList = (ExpressionList) rightItemsList;
CompiledSQLExpression[] values = new CompiledSQLExpression[expressionList.getExpressions().size()];
int i = 0;
for (Expression exp : expressionList.getExpressions()) {
values[i++] = compileExpression(exp, tableSchema);
}
return new CompiledInExpression(left, values);
} else if (expression instanceof TimeKeyExpression) {
TimeKeyExpression eq = (TimeKeyExpression) expression;
if (eq.getStringValue().equalsIgnoreCase("CURRENT_TIMESTAMP")) {
return new CompiledFunction(BuiltinFunctions.CURRENT_TIMESTAMP, Collections.emptyList());
}
// fallthru
} else if (expression instanceof Function) {
Function eq = (Function) expression;
checkSupported(eq.getKeep() == null);
checkSupported(eq.getMultipartName() != null && eq.getMultipartName().size() == 1);
checkSupported(eq.getNamedParameters() == null);
checkSupported(eq.getAttribute() == null);
checkSupported(eq.getAttributeName() == null);
List<CompiledSQLExpression> operands = new ArrayList<>();
if (eq.getParameters() != null) {
for (Expression e : eq.getParameters().getExpressions()) {
operands.add(compileExpression(e, tableSchema));
}
}
switch(eq.getName().toUpperCase()) {
case BuiltinFunctions.NAME_LOWERCASE:
return new CompiledFunction(BuiltinFunctions.LOWER, operands);
case BuiltinFunctions.NAME_UPPER:
return new CompiledFunction(BuiltinFunctions.UPPER, operands);
case BuiltinFunctions.NAME_ABS:
return new CompiledFunction(BuiltinFunctions.ABS, operands);
case BuiltinFunctions.NAME_AVG:
return new CompiledFunction(BuiltinFunctions.AVG, operands);
case BuiltinFunctions.NAME_ROUND:
return new CompiledFunction(BuiltinFunctions.ROUND, operands);
case BuiltinFunctions.NAME_EXTRACT:
return new CompiledFunction(BuiltinFunctions.EXTRACT, operands);
case BuiltinFunctions.NAME_FLOOR:
return new CompiledFunction(BuiltinFunctions.FLOOR, operands);
case BuiltinFunctions.NAME_RAND:
return new CompiledFunction(BuiltinFunctions.RAND, operands);
default:
}
// fallthru
} else if (expression instanceof CaseExpression) {
CaseExpression eq = (CaseExpression) expression;
checkSupported(eq.getSwitchExpression() == null);
List<WhenClause> whenClauses = eq.getWhenClauses();
List<Map.Entry<CompiledSQLExpression, CompiledSQLExpression>> cases = new ArrayList<>(whenClauses.size());
for (WhenClause c : whenClauses) {
cases.add(new AbstractMap.SimpleImmutableEntry<>(compileExpression(c.getWhenExpression(), tableSchema), compileExpression(c.getThenExpression(), tableSchema)));
}
CompiledSQLExpression elseExp = eq.getElseExpression() != null ? compileExpression(eq.getElseExpression(), tableSchema) : null;
return new CompiledCaseExpression(cases, elseExp);
} else if (expression instanceof Between) {
Between b = (Between) expression;
boolean not = b.isNot();
CompiledSQLExpression baseValue = compileExpression(b.getLeftExpression(), tableSchema);
CompiledSQLExpression start = compileExpression(b.getBetweenExpressionStart(), tableSchema);
CompiledSQLExpression end = compileExpression(b.getBetweenExpressionEnd(), tableSchema);
CompiledSQLExpression result = new CompiledAndExpression(new CompiledGreaterThanEqualsExpression(baseValue, start), new CompiledMinorThanEqualsExpression(baseValue, end));
if (not) {
return new CompiledNotExpression(result);
} else {
return result;
}
} else if (expression instanceof net.sf.jsqlparser.expression.CastExpression) {
net.sf.jsqlparser.expression.CastExpression b = (net.sf.jsqlparser.expression.CastExpression) expression;
CompiledSQLExpression left = compileExpression(b.getLeftExpression(), tableSchema);
int type = JSQLParserPlanner.sqlDataTypeToColumnType(b.getType());
return new CastExpression(left, type);
}
// }
throw new StatementExecutionException("not implemented expression type " + expression.getClass() + ": " + expression);
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class SelectUtilsTest method testTableAliasIssue311.
@Test
public void testTableAliasIssue311() {
Table table1 = new Table("mytable1");
table1.setAlias(new Alias("tab1"));
Table table2 = new Table("mytable2");
table2.setAlias(new Alias("tab2"));
List<? extends Expression> colunas = Arrays.asList(new Column(table1, "col1"), new Column(table1, "col2"), new Column(table1, "col3"), new Column(table2, "b1"), new Column(table2, "b2"));
Select select = SelectUtils.buildSelectFromTableAndExpressions(table1, colunas.toArray(new Expression[colunas.size()]));
final EqualsTo equalsTo = new EqualsTo();
equalsTo.setLeftExpression(new Column(table1, "col1"));
equalsTo.setRightExpression(new Column(table2, "b1"));
Join addJoin = SelectUtils.addJoin(select, table2, equalsTo);
addJoin.setLeft(true);
assertEquals("SELECT tab1.col1, tab1.col2, tab1.col3, tab2.b1, tab2.b2 FROM mytable1 AS tab1 LEFT JOIN mytable2 AS tab2 ON tab1.col1 = tab2.b1", select.toString());
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class TablesNamesFinderTest method testExpr.
@Test
public void testExpr() throws JSQLParserException {
String sql = "mycol in (select col2 from mytable)";
Expression expr = (Expression) CCJSqlParserUtil.parseCondExpression(sql);
TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();
List<String> tableList = tablesNamesFinder.getTableList(expr);
assertEquals(1, tableList.size());
assertTrue(tableList.contains("mytable"));
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class CNFTest method test3.
/**
* This is the case when we test a more complex tree structure,
* Notice you could see the amount of line to build up the CNF tree.
* You could tell how complicated the CNF could be.
*
* OR
* / \
* ( ) ( )
* | |
* AND OR
* / \ / \
* >= <= ( ) NOT
* / \ / \ | |
* 7.0 8.0 9.0 10.0 AND OR
* / \ / \
* ( ) = != ( )
* | / \ / \ |
* AND 11.0 12.0 13.0 14.0 AND
* / \ / \
* < > = ( )
* / \ / \ / \ |
* 7.0 8.0 9.0 10.0 15.0 16.0 OR
* / \
* = >
* / \ / \
* 17.0 18.0 19.0 20.0
*
* Here is the converted expression tree:
*
* AND
* / \
* AND ( )
* / \ |
* AND ( ) part18
* / \ |
* AND ( ) part17
* / \ |
* AND ( ) part16
* / \ |
* AND ( ) part15
* / \ |
* AND ( ) part14
* / \ |
* AND ( ) part13
* / \ |
* AND ( ) part12
* / \ |
* AND ( ) part11
* / \ |
* AND ( ) part10
* / \ |
* AND ( ) part9
* / \ |
* AND ( ) part8
* / \ |
* AND ( ) part7
* / \ |
* AND ( ) part6
* / \ |
* AND ( ) part5
* / \ |
* AND ( ) part4
* / \ |
* ( ) ( ) part3
* | |
* part1 part2
*
* part1: OR
* / \
* OR NOT
* / \ |
* >= < !=
* / \ / \ / \
* 3.0 4.0 7.0 8.0 13.0 14.0
*
* part2: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 17.0 18.0
* / \ / \ / \
* 3.0 4.0 7.0 8.0 15.0 16.0
*
* part3: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 19.0 20.0
* / \ / \ / \
* 3.0 4.0 7.0 8.0 15.0 16.0
*
* part4: OR
* / \
* OR NOT
* / \ |
* >= < !=
* / \ / \ / \
* 3.0 4.0 9.0 10.0 13.0 14.0
*
* part5: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 17.0 18.0
* / \ / \ / \
* 3.0 4.0 9.0 10.0 15.0 16.0
*
* part6: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 19.0 20.0
* / \ / \ / \
* 3.0 4.0 9.0 10.0 15.0 16.0
*
* part7: OR
* / \
* OR NOT
* / \ |
* >= < !=
* / \ / \ / \
* 3.0 4.0 11.0 12.0 13.0 14.0
*
* part8: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 17.0 18.0
* / \ / \ / \
* 3.0 4.0 11.0 12.0 15.0 16.0
*
* part9: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 19.0 20.0
* / \ / \ / \
* 3.0 4.0 11.0 12.0 15.0 16.0
*
* part10: OR
* / \
* OR NOT
* / \ |
* >= < !=
* / \ / \ / \
* 5.0 6.0 7.0 8.0 13.0 14.0
*
* part11: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 17.0 18.0
* / \ / \ / \
* 5.0 6.0 7.0 8.0 15.0 16.0
*
* part12: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 19.0 20.0
* / \ / \ / \
* 5.0 6.0 7.0 8.0 15.0 16.0
*
* part13: OR
* / \
* OR NOT
* / \ |
* >= < !=
* / \ / \ / \
* 5.0 6.0 9.0 10.0 13.0 14.0
*
* part14: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 17.0 18.0
* / \ / \ / \
* 5.0 6.0 9.0 10.0 15.0 16.0
*
* part15: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 19.0 20.0
* / \ / \ / \
* 5.0 6.0 9.0 10.0 15.0 16.0
*
* part16: OR
* / \
* OR NOT
* / \ |
* >= < !=
* / \ / \ / \
* 5.0 6.0 11.0 12.0 13.0 14.0
*
* part17: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 17.0 18.0
* / \ / \ / \
* 5.0 6.0 11.0 12.0 15.0 16.0
*
* part18: OR
* / \
* OR NOT
* / \ |
* OR NOT =
* / \ | / \
* >= < = 19.0 20.0
* / \ / \ / \
* 5.0 6.0 11.0 12.0 15.0 16.0
*/
@Test
public void test3() throws Exception {
Expression expr = CCJSqlParserUtil.parseCondExpression("(3.0 >= 4.0 AND 5.0 <= 6.0) OR " + "(((7.0 < 8.0 AND 9.0 > 10.0) AND 11.0 = 12.0) OR " + "NOT (13.0 <> 14.0 OR (15.0 = 16.0 AND (17.0 = 18.0 OR 19.0 > 20.0))))");
Expression expected = CCJSqlParserUtil.parseCondExpression("(3.0 >= 4.0 OR 7.0 < 8.0 OR NOT 13.0 <> 14.0) AND " + "(3.0 >= 4.0 OR 7.0 < 8.0 OR NOT 15.0 = 16.0 OR NOT 17.0 = 18.0) AND " + "(3.0 >= 4.0 OR 7.0 < 8.0 OR NOT 15.0 = 16.0 OR NOT 19.0 > 20.0) AND " + "(3.0 >= 4.0 OR 9.0 > 10.0 OR NOT 13.0 <> 14.0) AND " + "(3.0 >= 4.0 OR 9.0 > 10.0 OR NOT 15.0 = 16.0 OR NOT 17.0 = 18.0) AND " + "(3.0 >= 4.0 OR 9.0 > 10.0 OR NOT 15.0 = 16.0 OR NOT 19.0 > 20.0) AND " + "(3.0 >= 4.0 OR 11.0 = 12.0 OR NOT 13.0 <> 14.0) AND " + "(3.0 >= 4.0 OR 11.0 = 12.0 OR NOT 15.0 = 16.0 OR NOT 17.0 = 18.0) AND " + "(3.0 >= 4.0 OR 11.0 = 12.0 OR NOT 15.0 = 16.0 OR NOT 19.0 > 20.0) AND " + "(5.0 <= 6.0 OR 7.0 < 8.0 OR NOT 13.0 <> 14.0) AND " + "(5.0 <= 6.0 OR 7.0 < 8.0 OR NOT 15.0 = 16.0 OR NOT 17.0 = 18.0) AND " + "(5.0 <= 6.0 OR 7.0 < 8.0 OR NOT 15.0 = 16.0 OR NOT 19.0 > 20.0) AND " + "(5.0 <= 6.0 OR 9.0 > 10.0 OR NOT 13.0 <> 14.0) AND " + "(5.0 <= 6.0 OR 9.0 > 10.0 OR NOT 15.0 = 16.0 OR NOT 17.0 = 18.0) AND " + "(5.0 <= 6.0 OR 9.0 > 10.0 OR NOT 15.0 = 16.0 OR NOT 19.0 > 20.0) AND " + "(5.0 <= 6.0 OR 11.0 = 12.0 OR NOT 13.0 <> 14.0) AND " + "(5.0 <= 6.0 OR 11.0 = 12.0 OR NOT 15.0 = 16.0 OR NOT 17.0 = 18.0) AND " + "(5.0 <= 6.0 OR 11.0 = 12.0 OR NOT 15.0 = 16.0 OR NOT 19.0 > 20.0)");
Expression result = CNFConverter.convertToCNF(expr);
assertEquals(expected.toString(), result.toString());
}
use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.
the class ExecuteDeParserTest method shouldDeParseExecute.
@Test
public void shouldDeParseExecute() {
Execute execute = new Execute();
String name = "name";
ExpressionList exprList = new ExpressionList();
List<Expression> expressions = new ArrayList<Expression>();
Expression expression1 = mock(Expression.class);
Expression expression2 = mock(Expression.class);
execute.setName(name);
execute.setExprList(exprList);
exprList.setExpressions(expressions);
expressions.add(expression1);
expressions.add(expression2);
executeDeParser.deParse(execute);
String actual = buffer.toString();
assertTrue(actual.matches("EXECUTE " + name + " .*?, .*"));
}
Aggregations