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);
}
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;
}
Aggregations