Search in sources :

Example 11 with LongValue

use of net.sf.jsqlparser.expression.LongValue 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);
}
Also used : Arrays(java.util.Arrays) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) BigDecimal(java.math.BigDecimal) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) RexNode(org.apache.calcite.rex.RexNode) Map(java.util.Map) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) IsNullExpression(net.sf.jsqlparser.expression.operators.relational.IsNullExpression) RexLiteral(org.apache.calcite.rex.RexLiteral) NullValue(net.sf.jsqlparser.expression.NullValue) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) Expression(net.sf.jsqlparser.expression.Expression) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) RexInputRef(org.apache.calcite.rex.RexInputRef) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) List(java.util.List) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) RexDynamicParam(org.apache.calcite.rex.RexDynamicParam) BinaryExpressionBuilder(herddb.sql.expressions.CompiledSQLExpression.BinaryExpressionBuilder) RexCorrelVariable(org.apache.calcite.rex.RexCorrelVariable) CalcitePlanner(herddb.sql.CalcitePlanner) RexCall(org.apache.calcite.rex.RexCall) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) RexFieldAccess(org.apache.calcite.rex.RexFieldAccess) BuiltinFunctions(herddb.sql.functions.BuiltinFunctions) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) CURRENT_TIMESTAMP(herddb.sql.functions.BuiltinFunctions.CURRENT_TIMESTAMP) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) ArrayList(java.util.ArrayList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Function(net.sf.jsqlparser.expression.Function) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SqlOperator(org.apache.calcite.sql.SqlOperator) SqlTypeName(org.apache.calcite.sql.type.SqlTypeName) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) LongValue(net.sf.jsqlparser.expression.LongValue) AbstractMap(java.util.AbstractMap) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) Between(net.sf.jsqlparser.expression.operators.relational.Between) Collections(java.util.Collections) StringValue(net.sf.jsqlparser.expression.StringValue) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) Multiplication(net.sf.jsqlparser.expression.operators.arithmetic.Multiplication) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) StatementExecutionException(herddb.model.StatementExecutionException) CaseExpression(net.sf.jsqlparser.expression.CaseExpression) NullValue(net.sf.jsqlparser.expression.NullValue) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) StringValue(net.sf.jsqlparser.expression.StringValue) Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) 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) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) MinorThanEquals(net.sf.jsqlparser.expression.operators.relational.MinorThanEquals) SqlCastFunction(org.apache.calcite.sql.fun.SqlCastFunction) Function(net.sf.jsqlparser.expression.Function) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) Division(net.sf.jsqlparser.expression.operators.arithmetic.Division) MinorThan(net.sf.jsqlparser.expression.operators.relational.MinorThan) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) NotEqualsTo(net.sf.jsqlparser.expression.operators.relational.NotEqualsTo) GreaterThanEquals(net.sf.jsqlparser.expression.operators.relational.GreaterThanEquals) LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Subtraction(net.sf.jsqlparser.expression.operators.arithmetic.Subtraction) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) SignedExpression(net.sf.jsqlparser.expression.SignedExpression)

Example 12 with LongValue

use of net.sf.jsqlparser.expression.LongValue in project spanner-jdbc by olavloite.

the class AbstractSpannerExpressionVisitorAdapter method visit.

@Override
public void visit(SignedExpression value) {
    Expression underlyingValue = value.getExpression();
    if (underlyingValue instanceof DoubleValue) {
        DoubleValue doubleValue = (DoubleValue) underlyingValue;
        doubleValue.setValue(value.getSign() == '-' ? -doubleValue.getValue() : doubleValue.getValue());
        visit(doubleValue);
    } else if (underlyingValue instanceof LongValue) {
        LongValue longValue = (LongValue) underlyingValue;
        longValue.setValue(value.getSign() == '-' ? -longValue.getValue() : longValue.getValue());
        visit(longValue);
    } else {
        super.visit(value);
    }
}
Also used : SignedExpression(net.sf.jsqlparser.expression.SignedExpression) Expression(net.sf.jsqlparser.expression.Expression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) LongValue(net.sf.jsqlparser.expression.LongValue)

Example 13 with LongValue

use of net.sf.jsqlparser.expression.LongValue in project new-cloud by xie-summer.

the class MybatisPlusConfig method paginationInterceptor.

/**
 * mybatis-plus分页插件<br>
 * 文档:http://mp.baomidou.com<br>
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    // 开启 PageHelper 的支持
    paginationInterceptor.setLocalPage(true);
    /*
     * 【测试多租户】 SQL 解析处理拦截器<br>
     * 这里固定写成住户 1 实际情况你可以从cookie读取,因此数据看不到 【 麻花藤 】 这条记录( 注意观察 SQL )<br>
     */
    List<ISqlParser> sqlParserList = new ArrayList<>();
    TenantSqlParser tenantSqlParser = new TenantSqlParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId() {
            return new LongValue(1L);
        }

        @Override
        public String getTenantIdColumn() {
            return "tenant_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            /*
            if ("user".equals(tableName)) {
                return true;
            }*/
            return false;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    // });
    return paginationInterceptor;
}
Also used : TenantHandler(com.baomidou.mybatisplus.plugins.parser.tenant.TenantHandler) ISqlParser(com.baomidou.mybatisplus.plugins.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) LongValue(net.sf.jsqlparser.expression.LongValue) PaginationInterceptor(com.baomidou.mybatisplus.plugins.PaginationInterceptor) TenantSqlParser(com.baomidou.mybatisplus.plugins.parser.tenant.TenantSqlParser) Bean(org.springframework.context.annotation.Bean)

Example 14 with LongValue

use of net.sf.jsqlparser.expression.LongValue in project albedo by somowhere.

the class DataScopeInterceptor method beforeQuery.

@SneakyThrows
@Override
public void beforeQuery(Executor executor, MappedStatement mappedStatement, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) {
    if (SqlCommandType.SELECT == mappedStatement.getSqlCommandType() && StatementType.CALLABLE != mappedStatement.getStatementType()) {
        // 查找参数中包含DataScope类型的参数
        DataScope dataScope = findDataScopeObject(parameter);
        if (dataScope != null && !dataScope.isAll()) {
            String scopeName = dataScope.getScopeName(), creatorName = dataScope.getCreatorName();
            Long userId = dataScope.getUserId();
            Set<Long> deptIds = dataScope.getDeptIds();
            String originalSql = boundSql.getSql();
            Select selectStatement = (Select) CCJSqlParserUtil.parse(originalSql);
            SelectBody selectBody = selectStatement.getSelectBody();
            if (selectBody instanceof PlainSelect) {
                PlainSelect plainSelect = (PlainSelect) selectBody;
                Expression expression = null;
                Alias alias = plainSelect.getFromItem().getAlias();
                String aliaName = "";
                if (alias != null && StringUtil.isNotEmpty(alias.getName())) {
                    aliaName = alias.getName() + StrPool.DOT;
                }
                if (StringUtil.isNotBlank(scopeName) && CollectionUtil.isNotEmpty(deptIds)) {
                    ItemsList itemsList = new ExpressionList(deptIds.stream().map(deptId -> new LongValue(deptId)).collect(Collectors.toList()));
                    expression = new InExpression(new Column(aliaName + scopeName), itemsList);
                } else if (StringUtil.isNotEmpty(creatorName) && dataScope.isSelf()) {
                    EqualsTo equalsTo = new EqualsTo();
                    equalsTo.setLeftExpression(new Column(aliaName + creatorName));
                    equalsTo.setRightExpression(new LongValue(userId));
                    expression = equalsTo;
                }
                if (expression != null) {
                    AndExpression andExpression = new AndExpression(plainSelect.getWhere(), expression);
                    plainSelect.setWhere(andExpression);
                    PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
                    mpBoundSql.sql(plainSelect.toString());
                }
            } else {
                // todo: don't known how to resole
                log.warn("can not parse " + selectBody);
            }
        }
    }
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) PluginUtils(com.baomidou.mybatisplus.core.toolkit.PluginUtils) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) Alias(net.sf.jsqlparser.expression.Alias) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) LongValue(net.sf.jsqlparser.expression.LongValue) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) SneakyThrows(lombok.SneakyThrows)

Example 15 with LongValue

use of net.sf.jsqlparser.expression.LongValue 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)

Aggregations

LongValue (net.sf.jsqlparser.expression.LongValue)42 Expression (net.sf.jsqlparser.expression.Expression)24 Column (net.sf.jsqlparser.schema.Column)16 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)13 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)11 ArrayList (java.util.ArrayList)10 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)9 Table (net.sf.jsqlparser.schema.Table)9 Select (net.sf.jsqlparser.statement.select.Select)8 List (java.util.List)7 StringValue (net.sf.jsqlparser.expression.StringValue)7 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)7 Limit (net.sf.jsqlparser.statement.select.Limit)7 ISqlParser (com.baomidou.mybatisplus.core.parser.ISqlParser)6 TenantHandler (com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler)6 TenantSqlParser (com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser)6 Function (net.sf.jsqlparser.expression.Function)6 EqualsTo (net.sf.jsqlparser.expression.operators.relational.EqualsTo)6 Test (org.junit.jupiter.api.Test)5 Bean (org.springframework.context.annotation.Bean)5