Search in sources :

Example 1 with ForNode

use of org.seasar.doma.internal.jdbc.sql.node.ForNode in project doma by domaframework.

the class SqlParser method parseForBlockComment.

protected void parseForBlockComment() {
    ForBlockNode forBlockNode = new ForBlockNode();
    appendNode(forBlockNode);
    push(forBlockNode);
    String expr = tokenType.extract(token);
    int pos = expr.indexOf(":");
    if (pos == -1) {
        throw new JdbcException(Message.DOMA2124, sql, tokenizer.getLineNumber(), tokenizer.getPosition());
    }
    String identifier = expr.substring(0, pos).trim();
    if (identifier.isEmpty()) {
        throw new JdbcException(Message.DOMA2125, sql, tokenizer.getLineNumber(), tokenizer.getPosition());
    }
    String expression = expr.substring(pos + 1).trim();
    if (expression.isEmpty()) {
        throw new JdbcException(Message.DOMA2126, sql, tokenizer.getLineNumber(), tokenizer.getPosition());
    }
    ForNode forNode = new ForNode(getLocation(), identifier, expression, token);
    forBlockNode.setForNode(forNode);
    push(forNode);
}
Also used : ForNode(org.seasar.doma.internal.jdbc.sql.node.ForNode) ForBlockNode(org.seasar.doma.internal.jdbc.sql.node.ForBlockNode) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 2 with ForNode

use of org.seasar.doma.internal.jdbc.sql.node.ForNode in project doma by domaframework.

the class NodePreparedSqlBuilder method visitForBlockNode.

@Override
public Void visitForBlockNode(ForBlockNode node, Context p) {
    ForNode forNode = node.getForNode();
    SqlLocation location = forNode.getLocation();
    EvaluationResult expressionResult = p.evaluate(location, forNode.getExpression());
    Object expressionValue = expressionResult.getValue();
    Class<?> expressionValueClass = expressionResult.getValueClass();
    Iterable<?> iterable;
    if (Iterable.class.isAssignableFrom(expressionValueClass)) {
        iterable = (Iterable<?>) expressionValue;
    } else if (expressionValueClass.isArray()) {
        iterable = Arrays.asList((Object[]) expressionValue);
    } else {
        throw new JdbcException(Message.DOMA2129, location.getSql(), location.getLineNumber(), location.getPosition(), forNode.getExpression(), expressionValueClass);
    }
    String identifier = forNode.getIdentifier();
    Value originalIdentifierValue = p.removeValue(identifier);
    String hasNextVariable = identifier + ForBlockNode.HAS_NEXT_SUFFIX;
    Value originalHasNextValue = p.removeValue(hasNextVariable);
    String indexVariable = identifier + ForBlockNode.INDEX_SUFFIX;
    Value originalIndexValue = p.removeValue(indexVariable);
    int index = 0;
    for (Iterator<?> it = iterable.iterator(); it.hasNext(); ) {
        Object each = it.next();
        Value value = each == null ? new Value(void.class, null) : new Value(each.getClass(), each);
        p.putValue(identifier, value);
        p.putValue(hasNextVariable, new Value(boolean.class, it.hasNext()));
        p.putValue(indexVariable, new Value(int.class, index));
        for (SqlNode child : forNode.getChildren()) {
            child.accept(this, p);
        }
        index++;
    }
    if (originalIdentifierValue == null) {
        p.removeValue(identifier);
    } else {
        p.putValue(identifier, originalIdentifierValue);
    }
    if (originalHasNextValue == null) {
        p.removeValue(hasNextVariable);
    } else {
        p.putValue(hasNextVariable, originalHasNextValue);
    }
    if (originalIndexValue == null) {
        p.removeValue(indexVariable);
    } else {
        p.putValue(indexVariable, originalIndexValue);
    }
    EndNode endNode = node.getEndNode();
    endNode.accept(this, p);
    return null;
}
Also used : ForNode(org.seasar.doma.internal.jdbc.sql.node.ForNode) JdbcException(org.seasar.doma.jdbc.JdbcException) EvaluationResult(org.seasar.doma.internal.expr.EvaluationResult) EndNode(org.seasar.doma.internal.jdbc.sql.node.EndNode) Value(org.seasar.doma.internal.expr.Value) SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) SqlNode(org.seasar.doma.jdbc.SqlNode)

Aggregations

ForNode (org.seasar.doma.internal.jdbc.sql.node.ForNode)2 JdbcException (org.seasar.doma.jdbc.JdbcException)2 EvaluationResult (org.seasar.doma.internal.expr.EvaluationResult)1 Value (org.seasar.doma.internal.expr.Value)1 EndNode (org.seasar.doma.internal.jdbc.sql.node.EndNode)1 ForBlockNode (org.seasar.doma.internal.jdbc.sql.node.ForBlockNode)1 SqlLocation (org.seasar.doma.internal.jdbc.sql.node.SqlLocation)1 SqlNode (org.seasar.doma.jdbc.SqlNode)1