Search in sources :

Example 6 with JdbcParameter

use of net.sf.jsqlparser.expression.JdbcParameter 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);
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) ArrayList(java.util.ArrayList) StatementExecutionException(herddb.model.StatementExecutionException) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) WhenClause(net.sf.jsqlparser.expression.WhenClause) NullValue(net.sf.jsqlparser.expression.NullValue) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) IntHolder(herddb.utils.IntHolder) List(java.util.List) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) StringValue(net.sf.jsqlparser.expression.StringValue) Between(net.sf.jsqlparser.expression.operators.relational.Between) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) LongValue(net.sf.jsqlparser.expression.LongValue) Map(java.util.Map) AbstractMap(java.util.AbstractMap) NotExpression(net.sf.jsqlparser.expression.NotExpression) Function(net.sf.jsqlparser.expression.Function) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) Expression(net.sf.jsqlparser.expression.Expression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression)

Example 7 with JdbcParameter

use of net.sf.jsqlparser.expression.JdbcParameter in project JSqlParser by JSQLParser.

the class UpdateTest method testUpdate.

@Test
public void testUpdate() throws JSQLParserException {
    String statement = "UPDATE mytable set col1='as', col2=?, col3=565 Where o >= 3";
    Update update = (Update) parserManager.parse(new StringReader(statement));
    assertEquals("mytable", update.getTables().get(0).getName());
    assertEquals(3, update.getColumns().size());
    assertEquals("col1", ((Column) update.getColumns().get(0)).getColumnName());
    assertEquals("col2", ((Column) update.getColumns().get(1)).getColumnName());
    assertEquals("col3", ((Column) update.getColumns().get(2)).getColumnName());
    assertEquals("as", ((StringValue) update.getExpressions().get(0)).getValue());
    assertTrue(update.getExpressions().get(1) instanceof JdbcParameter);
    assertEquals(565, ((LongValue) update.getExpressions().get(2)).getValue());
    assertTrue(update.getWhere() instanceof GreaterThanEquals);
}
Also used : GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) StringReader(java.io.StringReader) Update(net.sf.jsqlparser.statement.update.Update) Test(org.junit.Test)

Example 8 with JdbcParameter

use of net.sf.jsqlparser.expression.JdbcParameter in project JSqlParser by JSQLParser.

the class ReplaceTest method testReplaceSyntax1.

@Test
public void testReplaceSyntax1() throws JSQLParserException {
    String statement = "REPLACE mytable SET col1='as', col2=?, col3=565";
    Replace replace = (Replace) PARSER_MANAGER.parse(new StringReader(statement));
    assertEquals("mytable", replace.getTable().getName());
    assertEquals(3, replace.getColumns().size());
    assertEquals("col1", ((Column) replace.getColumns().get(0)).getColumnName());
    assertEquals("col2", ((Column) replace.getColumns().get(1)).getColumnName());
    assertEquals("col3", ((Column) replace.getColumns().get(2)).getColumnName());
    assertEquals("as", ((StringValue) replace.getExpressions().get(0)).getValue());
    assertTrue(replace.getExpressions().get(1) instanceof JdbcParameter);
    assertEquals(565, ((LongValue) replace.getExpressions().get(2)).getValue());
    assertEquals(statement, "" + replace);
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 9 with JdbcParameter

use of net.sf.jsqlparser.expression.JdbcParameter in project JSqlParser by JSQLParser.

the class ExecuteDeParserTest method shouldDeParseExecute.

@Test
public void shouldDeParseExecute() {
    Execute execute = new Execute();
    String name = "name";
    List<Expression> expressions = new ArrayList<>();
    expressions.add(new JdbcParameter());
    expressions.add(new JdbcParameter());
    execute.withName(name).withExecType(ExecType.EXECUTE).withParenthesis(true).withExprList(new ExpressionList().withExpressions(expressions));
    executeDeParser.deParse(execute);
    String actual = buffer.toString();
    assertEquals("EXECUTE " + name + " (?, ?)", actual);
}
Also used : Execute(net.sf.jsqlparser.statement.execute.Execute) Expression(net.sf.jsqlparser.expression.Expression) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.jupiter.api.Test)

Example 10 with JdbcParameter

use of net.sf.jsqlparser.expression.JdbcParameter in project JSqlParser by JSQLParser.

the class JSQLParserFluentModelTests method testParseAndBuild.

@Test
public void testParseAndBuild() throws JSQLParserException {
    String statement = // 
    "SELECT * FROM tab1 AS t1 " + "JOIN tab2 t2 ON t1.ref = t2.id WHERE (t1.col1 = ? OR t1.col2 = ?) AND t1.col3 IN ('A')";
    Statement parsed = TestUtils.assertSqlCanBeParsedAndDeparsed(statement);
    Table t1 = new Table("tab1").withAlias(new Alias("t1").withUseAs(true));
    Table t2 = new Table("tab2").withAlias(new Alias("t2", false));
    AndExpression where = new AndExpression().withLeftExpression(new Parenthesis(new OrExpression().withLeftExpression(new EqualsTo().withLeftExpression(new Column(asList("t1", "col1"))).withRightExpression(new JdbcParameter().withIndex(1))).withRightExpression(new EqualsTo(new Column(asList("t1", "col2")), new JdbcParameter().withIndex(2))))).withRightExpression(new InExpression().withLeftExpression(new Column(asList("t1", "col3"))).withRightItemsList(new ExpressionList(new StringValue("A"))));
    Select select = new Select().withSelectBody(new PlainSelect().addSelectItems(new AllColumns()).withFromItem(t1).addJoins(new Join().withRightItem(t2).withOnExpression(new EqualsTo(new Column(asList("t1", "ref")), new Column(asList("t2", "id"))))).withWhere(where));
    ExpressionList list = select.getSelectBody(PlainSelect.class).getWhere(AndExpression.class).getRightExpression(InExpression.class).getRightItemsList(ExpressionList.class);
    List<Expression> elist = list.getExpressions();
    list.setExpressions(elist);
    assertDeparse(select, statement);
    assertEqualsObjectTree(parsed, select);
}
Also used : Table(net.sf.jsqlparser.schema.Table) Statement(net.sf.jsqlparser.statement.Statement) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Join(net.sf.jsqlparser.statement.select.Join) AllColumns(net.sf.jsqlparser.statement.select.AllColumns) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Column(net.sf.jsqlparser.schema.Column) XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) Alias(net.sf.jsqlparser.expression.Alias) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) StringValue(net.sf.jsqlparser.expression.StringValue) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.jupiter.api.Test)

Aggregations

JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)15 Expression (net.sf.jsqlparser.expression.Expression)8 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)8 ArrayList (java.util.ArrayList)6 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)6 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)6 StringReader (java.io.StringReader)5 List (java.util.List)5 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)5 StringValue (net.sf.jsqlparser.expression.StringValue)5 StatementExecutionException (herddb.model.StatementExecutionException)4 LongValue (net.sf.jsqlparser.expression.LongValue)4 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)4 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)4 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)4 MultiExpressionList (net.sf.jsqlparser.expression.operators.relational.MultiExpressionList)4 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)4 Map (java.util.Map)3 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)3 DoubleValue (net.sf.jsqlparser.expression.DoubleValue)3