Search in sources :

Example 1 with EvaluationResult

use of org.seasar.doma.internal.expr.EvaluationResult in project doma by domaframework.

the class NodePreparedSqlBuilder method visitEmbeddedVariableNode.

@Override
public Void visitEmbeddedVariableNode(EmbeddedVariableNode node, Context p) {
    SqlLocation location = node.getLocation();
    String name = node.getVariableName();
    EvaluationResult result = p.evaluate(location, name);
    Object value = result.getValue();
    if (value != null) {
        String fragment = value.toString();
        if (fragment.indexOf('\'') > -1) {
            throw new JdbcException(Message.DOMA2116, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName());
        }
        if (fragment.indexOf(';') > -1) {
            throw new JdbcException(Message.DOMA2117, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName());
        }
        if (fragment.contains("--")) {
            throw new JdbcException(Message.DOMA2122, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName());
        }
        if (fragment.contains("/*")) {
            throw new JdbcException(Message.DOMA2123, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName());
        }
        if (!startsWithClauseKeyword(fragment)) {
            p.setAvailable(true);
        }
        p.appendRawSql(fragment);
        p.appendFormattedSql(fragment);
    }
    for (SqlNode child : node.getChildren()) {
        child.accept(this, p);
    }
    return null;
}
Also used : SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) JdbcException(org.seasar.doma.jdbc.JdbcException) EvaluationResult(org.seasar.doma.internal.expr.EvaluationResult) SqlNode(org.seasar.doma.jdbc.SqlNode)

Example 2 with EvaluationResult

use of org.seasar.doma.internal.expr.EvaluationResult in project doma by domaframework.

the class NodePreparedSqlBuilder method handleElseifNode.

protected boolean handleElseifNode(IfBlockNode node, Context p) {
    for (ElseifNode elseifNode : node.getElseifNodes()) {
        SqlLocation location = elseifNode.getLocation();
        String expression = elseifNode.getExpression();
        EvaluationResult elseifResult = p.evaluate(location, expression);
        if (elseifResult.getBooleanValue()) {
            elseifNode.accept(this, p);
            return true;
        }
    }
    return false;
}
Also used : SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) ElseifNode(org.seasar.doma.internal.jdbc.sql.node.ElseifNode) EvaluationResult(org.seasar.doma.internal.expr.EvaluationResult)

Example 3 with EvaluationResult

use of org.seasar.doma.internal.expr.EvaluationResult in project doma by domaframework.

the class NodePreparedSqlBuilder method visitValueNode.

@SuppressWarnings("SameReturnValue")
protected Void visitValueNode(ValueNode node, Context p, Consumer<Scalar<?, ?>> valueHandler) {
    SqlLocation location = node.getLocation();
    String name = node.getVariableName();
    EvaluationResult result = p.evaluate(location, name);
    Object value = result.getValue();
    Class<?> valueClass = result.getValueClass();
    p.setAvailable(true);
    if (node.isWordNodeIgnored()) {
        handleSingleValueNode(node, p, value, valueClass, valueHandler);
    } else if (node.isParensNodeIgnored()) {
        ParensNode parensNode = node.getParensNode();
        OtherNode openedFragmentNode = parensNode.getOpenedFragmentNode();
        openedFragmentNode.accept(this, p);
        if (Iterable.class.isAssignableFrom(valueClass)) {
            handleIterableValueNode(node, p, (Iterable<?>) value, valueClass, valueHandler);
        } else if (valueClass.isArray()) {
            handleIterableValueNode(node, p, Arrays.asList((Object[]) value), valueClass, valueHandler);
        } else {
            throw new JdbcException(Message.DOMA2112, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName(), valueClass);
        }
        OtherNode closedFragmentNode = parensNode.getClosedFragmentNode();
        closedFragmentNode.accept(this, p);
    } else {
        assertUnreachable();
    }
    return null;
}
Also used : ParensNode(org.seasar.doma.internal.jdbc.sql.node.ParensNode) OtherNode(org.seasar.doma.internal.jdbc.sql.node.OtherNode) SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) JdbcException(org.seasar.doma.jdbc.JdbcException) EvaluationResult(org.seasar.doma.internal.expr.EvaluationResult)

Example 4 with EvaluationResult

use of org.seasar.doma.internal.expr.EvaluationResult in project doma by domaframework.

the class NodePreparedSqlBuilder method visitExpandNode.

@Override
public Void visitExpandNode(ExpandNode node, Context p) {
    EvaluationResult evalResult = p.evaluate(node.getLocation(), node.getAlias());
    String alias = evalResult.getValue().toString();
    String prefix = alias.isEmpty() ? "" : alias + ".";
    StringJoiner joiner = new StringJoiner(", ");
    for (String column : columnsExpander.apply(node)) {
        joiner.add(prefix + column);
    }
    String joined = joiner.toString();
    p.appendRawSql(joined);
    p.appendFormattedSql(joined);
    return null;
}
Also used : EvaluationResult(org.seasar.doma.internal.expr.EvaluationResult) StringJoiner(java.util.StringJoiner)

Example 5 with EvaluationResult

use of org.seasar.doma.internal.expr.EvaluationResult in project doma by domaframework.

the class NodePreparedSqlBuilder method handleIfNode.

protected boolean handleIfNode(IfBlockNode node, Context p) {
    IfNode ifNode = node.getIfNode();
    SqlLocation location = ifNode.getLocation();
    String expression = ifNode.getExpression();
    EvaluationResult ifResult = p.evaluate(location, expression);
    if (ifResult.getBooleanValue()) {
        ifNode.accept(this, p);
        return true;
    }
    return false;
}
Also used : SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) IfNode(org.seasar.doma.internal.jdbc.sql.node.IfNode) EvaluationResult(org.seasar.doma.internal.expr.EvaluationResult)

Aggregations

EvaluationResult (org.seasar.doma.internal.expr.EvaluationResult)24 Test (org.junit.jupiter.api.Test)18 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)18 SqlLocation (org.seasar.doma.internal.jdbc.sql.node.SqlLocation)5 JdbcException (org.seasar.doma.jdbc.JdbcException)3 Value (org.seasar.doma.internal.expr.Value)2 SqlNode (org.seasar.doma.jdbc.SqlNode)2 StringJoiner (java.util.StringJoiner)1 ElseifNode (org.seasar.doma.internal.jdbc.sql.node.ElseifNode)1 EndNode (org.seasar.doma.internal.jdbc.sql.node.EndNode)1 ForNode (org.seasar.doma.internal.jdbc.sql.node.ForNode)1 IfNode (org.seasar.doma.internal.jdbc.sql.node.IfNode)1 OtherNode (org.seasar.doma.internal.jdbc.sql.node.OtherNode)1 ParensNode (org.seasar.doma.internal.jdbc.sql.node.ParensNode)1