Search in sources :

Example 56 with JdbcException

use of org.seasar.doma.jdbc.JdbcException 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)

Example 57 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class NodePreparedSqlBuilder method handleIterableValueNode.

protected void handleIterableValueNode(ValueNode node, Context p, Iterable<?> values, Class<?> valueClass, Consumer<Scalar<?, ?>> consumer) {
    int index = 0;
    for (Object v : values) {
        if (v == null) {
            SqlLocation location = node.getLocation();
            throw new JdbcException(Message.DOMA2115, location.getSql(), location.getLineNumber(), location.getPosition(), node.getVariableName(), index);
        }
        Supplier<Scalar<?, ?>> supplier = wrap(node.getLocation(), node.getVariableName(), v, v.getClass());
        consumer.accept(supplier.get());
        p.appendRawSql(", ");
        p.appendFormattedSql(", ");
        index++;
    }
    if (index == 0) {
        p.appendRawSql("null");
        p.appendFormattedSql("null");
    } else {
        p.cutBackSqlBuf(2);
        p.cutBackFormattedSqlBuf(2);
    }
}
Also used : SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) JdbcException(org.seasar.doma.jdbc.JdbcException) Scalar(org.seasar.doma.internal.jdbc.scalar.Scalar)

Example 58 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class BuiltinTableIdGenerator method selectId.

protected long selectId(IdGenerationConfig config, PreparedSql sql) {
    JdbcLogger logger = config.getJdbcLogger();
    Connection connection = JdbcUtil.getConnection(config.getDataSource());
    try {
        PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
        try {
            logger.logSql(getClass().getName(), "selectId", sql);
            setupOptions(config, preparedStatement);
            preparedStatement.setString(1, pkColumnValue);
            ResultSet resultSet = preparedStatement.executeQuery();
            if (resultSet.next()) {
                Object result = resultSet.getObject(1);
                if (result instanceof Number) {
                    return ((Number) result).longValue();
                }
            }
            throw new JdbcException(Message.DOMA2017, config.getEntityType().getName());
        } catch (SQLException e) {
            throw new JdbcException(Message.DOMA2018, e, config.getEntityType().getName(), e);
        } finally {
            JdbcUtil.close(preparedStatement, logger);
        }
    } finally {
        JdbcUtil.close(connection, logger);
    }
}
Also used : JdbcLogger(org.seasar.doma.jdbc.JdbcLogger) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 59 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class BuiltinTableIdGenerator method initialize.

@Override
public void initialize() {
    if (qualifiedTableName == null) {
        throw new JdbcException(Message.DOMA2033, "qualifiedTableName");
    }
    if (pkColumnName == null) {
        throw new JdbcException(Message.DOMA2033, "pkColumnName");
    }
    if (pkColumnValue == null) {
        throw new JdbcException(Message.DOMA2033, "pkColumnValue");
    }
    if (valueColumnName == null) {
        throw new JdbcException(Message.DOMA2033, "valueColumnName");
    }
    LongWrapper allocationSizeWrapper = new LongWrapper();
    allocationSizeWrapper.set(allocationSize);
    StringWrapper pkColumnValueWrapper = new StringWrapper();
    pkColumnValueWrapper.set(pkColumnValue);
    updateSql = new PreparedSql(SqlKind.UPDATE, createUpdateRawSql(), createUpdateFormattedSql(), null, Arrays.asList(new BasicInParameter<>(() -> allocationSizeWrapper), new BasicInParameter<>(() -> pkColumnValueWrapper)), SqlLogType.FORMATTED);
    selectSql = new PreparedSql(SqlKind.SELECT, createSelectRawSql(), createSelectFormattedSql(), null, Collections.singletonList(new BasicInParameter<>(() -> pkColumnValueWrapper)), SqlLogType.FORMATTED);
}
Also used : StringWrapper(org.seasar.doma.wrapper.StringWrapper) PreparedSql(org.seasar.doma.jdbc.PreparedSql) LongWrapper(org.seasar.doma.wrapper.LongWrapper) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 60 with JdbcException

use of org.seasar.doma.jdbc.JdbcException in project doma by domaframework.

the class BuiltinTableIdGenerator method updateId.

protected void updateId(IdGenerationConfig config, PreparedSql sql) {
    JdbcLogger logger = config.getJdbcLogger();
    Connection connection = JdbcUtil.getConnection(config.getDataSource());
    try {
        PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
        try {
            logger.logSql(getClass().getName(), "updateId", sql);
            setupOptions(config, preparedStatement);
            preparedStatement.setLong(1, allocationSize);
            preparedStatement.setString(2, pkColumnValue);
            int rows = preparedStatement.executeUpdate();
            if (rows != 1) {
                throw new JdbcException(Message.DOMA2017, config.getEntityType().getName());
            }
        } catch (SQLException e) {
            throw new JdbcException(Message.DOMA2018, e, config.getEntityType().getName(), e);
        } finally {
            JdbcUtil.close(preparedStatement, logger);
        }
    } finally {
        JdbcUtil.close(connection, logger);
    }
}
Also used : JdbcLogger(org.seasar.doma.jdbc.JdbcLogger) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) JdbcException(org.seasar.doma.jdbc.JdbcException)

Aggregations

JdbcException (org.seasar.doma.jdbc.JdbcException)69 Test (org.junit.jupiter.api.Test)35 SqlNode (org.seasar.doma.jdbc.SqlNode)18 SQLException (java.sql.SQLException)15 BigDecimal (java.math.BigDecimal)9 Value (org.seasar.doma.internal.expr.Value)9 ExpressionEvaluator (org.seasar.doma.internal.expr.ExpressionEvaluator)8 SqlLocation (org.seasar.doma.internal.jdbc.sql.node.SqlLocation)7 SelectOptions (org.seasar.doma.jdbc.SelectOptions)7 SqlParser (org.seasar.doma.internal.jdbc.sql.SqlParser)6 Connection (java.sql.Connection)5 MockConfig (org.seasar.doma.internal.jdbc.mock.MockConfig)5 PreparedStatement (java.sql.PreparedStatement)4 Savepoint (java.sql.Savepoint)4 ArrayList (java.util.ArrayList)4 LinkedHashMap (java.util.LinkedHashMap)4 DomaNullPointerException (org.seasar.doma.DomaNullPointerException)4 MockConnection (org.seasar.doma.internal.jdbc.mock.MockConnection)4 MockDataSource (org.seasar.doma.internal.jdbc.mock.MockDataSource)4 IfBlockNode (org.seasar.doma.internal.jdbc.sql.node.IfBlockNode)4