Search in sources :

Example 1 with SqlLocation

use of org.seasar.doma.internal.jdbc.sql.node.SqlLocation 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 SqlLocation

use of org.seasar.doma.internal.jdbc.sql.node.SqlLocation 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 SqlLocation

use of org.seasar.doma.internal.jdbc.sql.node.SqlLocation 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 SqlLocation

use of org.seasar.doma.internal.jdbc.sql.node.SqlLocation 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)

Example 5 with SqlLocation

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

the class SqlParser method validateParensClosed.

protected void validateParensClosed() {
    if (isInParensNode()) {
        removeNodesTo(ParensNode.class);
        ParensNode parensNode = pop();
        SqlLocation location = parensNode.getLocation();
        throw new JdbcException(Message.DOMA2135, sql, location.getLineNumber(), location.getPosition());
    }
}
Also used : ParensNode(org.seasar.doma.internal.jdbc.sql.node.ParensNode) SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) JdbcException(org.seasar.doma.jdbc.JdbcException)

Aggregations

SqlLocation (org.seasar.doma.internal.jdbc.sql.node.SqlLocation)16 JdbcException (org.seasar.doma.jdbc.JdbcException)7 AptException (org.seasar.doma.internal.apt.AptException)6 EvaluationResult (org.seasar.doma.internal.expr.EvaluationResult)5 TypeDeclaration (org.seasar.doma.internal.apt.decl.TypeDeclaration)4 SqlNode (org.seasar.doma.jdbc.SqlNode)3 Value (org.seasar.doma.internal.expr.Value)2 ParensNode (org.seasar.doma.internal.jdbc.sql.node.ParensNode)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ArrayType (javax.lang.model.type.ArrayType)1 DeclaredType (javax.lang.model.type.DeclaredType)1 TypeMirror (javax.lang.model.type.TypeMirror)1 FetchType (org.seasar.doma.FetchType)1 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)1 BasicSingleResultHandler (org.seasar.doma.internal.jdbc.command.BasicSingleResultHandler)1