Search in sources :

Example 26 with JdbcException

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

the class AutoBatchInsertTest method testIdNotAssigned.

@Test
public void testIdNotAssigned(Config config) throws Exception {
    DepartmentDao dao = new DepartmentDaoImpl(config);
    Department department = new Department();
    department.setDepartmentNo(99);
    department.setDepartmentName("hoge");
    Department department2 = new Department();
    department2.setDepartmentNo(98);
    department2.setDepartmentName("hoge");
    try {
        dao.insert(Arrays.asList(department, department2));
        fail();
    } catch (JdbcException expected) {
        assertEquals(Message.DOMA2020, expected.getMessageResource());
    }
}
Also used : DepartmentDaoImpl(org.seasar.doma.it.dao.DepartmentDaoImpl) CompKeyDepartmentDaoImpl(org.seasar.doma.it.dao.CompKeyDepartmentDaoImpl) Department(org.seasar.doma.it.entity.Department) CompKeyDepartment(org.seasar.doma.it.entity.CompKeyDepartment) DepartmentDao(org.seasar.doma.it.dao.DepartmentDao) CompKeyDepartmentDao(org.seasar.doma.it.dao.CompKeyDepartmentDao) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 27 with JdbcException

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

the class MssqlPagingTransformer method visitSelectStatementNode.

@Override
public SqlNode visitSelectStatementNode(SelectStatementNode node, Void p) {
    if (processed) {
        return node;
    }
    processed = true;
    if (!forceOffsetFetch && offset <= 0) {
        return super.appendTopNode(node);
    }
    OrderByClauseNode originalOrderBy = node.getOrderByClauseNode();
    if (originalOrderBy == null) {
        throw new JdbcException(Message.DOMA2201);
    }
    OrderByClauseNode orderBy = new OrderByClauseNode(originalOrderBy.getWordNode());
    for (SqlNode child : originalOrderBy.getChildren()) {
        orderBy.appendNode(child);
    }
    String offset = this.offset <= 0 ? "0" : String.valueOf(this.offset);
    orderBy.appendNode(new FragmentNode(" offset "));
    orderBy.appendNode(new FragmentNode(offset));
    orderBy.appendNode(new FragmentNode(" rows"));
    if (this.limit > 0) {
        orderBy.appendNode(new FragmentNode(" fetch next "));
        orderBy.appendNode(new FragmentNode(String.valueOf(this.limit)));
        orderBy.appendNode(new FragmentNode(" rows only"));
    }
    SelectStatementNode result = new SelectStatementNode();
    result.setSelectClauseNode(node.getSelectClauseNode());
    result.setFromClauseNode(node.getFromClauseNode());
    result.setWhereClauseNode(node.getWhereClauseNode());
    result.setGroupByClauseNode(node.getGroupByClauseNode());
    result.setHavingClauseNode(node.getHavingClauseNode());
    result.setOrderByClauseNode(orderBy);
    result.setForUpdateClauseNode(node.getForUpdateClauseNode());
    result.setOptionClauseNode(node.getOptionClauseNode());
    return result;
}
Also used : SelectStatementNode(org.seasar.doma.internal.jdbc.sql.node.SelectStatementNode) FragmentNode(org.seasar.doma.internal.jdbc.sql.node.FragmentNode) JdbcException(org.seasar.doma.jdbc.JdbcException) OrderByClauseNode(org.seasar.doma.internal.jdbc.sql.node.OrderByClauseNode) SqlNode(org.seasar.doma.jdbc.SqlNode)

Example 28 with JdbcException

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

the class LocalTransaction method commit.

/**
 * Commits this transaction.
 *
 * @throws TransactionNotYetBegunException if this transaction is not yet begun
 * @throws JdbcException if a JDBC related error occurs
 */
public void commit() {
    LocalTransactionContext context = localTxContextHolder.get();
    if (!isActiveInternal(context)) {
        throw new TransactionNotYetBegunException(Message.DOMA2046);
    }
    if (context.hasConnection()) {
        LocalTransactionConnection connection = context.getConnection();
        try {
            connection.commit();
            jdbcLogger.logTransactionCommitted(className, "commit", context.getId());
        } catch (SQLException e) {
            rollbackInternal("commit");
            throw new JdbcException(Message.DOMA2043, e, e);
        } finally {
            end("commit");
        }
    } else {
        end("commit");
    }
}
Also used : SQLException(java.sql.SQLException) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 29 with JdbcException

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

the class LocalTransaction method releaseSavepoint.

/**
 * Removes the specified save point and subsequent save points from this transaction.
 *
 * @param savepointName the name of the save point
 * @throws DomaNullPointerException if the {@code savepointName} is {@code null}
 * @throws TransactionNotYetBegunException if this transaction is not yet begun
 * @throws JdbcException if a JDBC related error occurs
 */
public void releaseSavepoint(String savepointName) {
    if (savepointName == null) {
        rollbackInternal("releaseSavepoint");
        throw new DomaNullPointerException("savepointName");
    }
    LocalTransactionContext context = localTxContextHolder.get();
    if (!isActiveInternal(context)) {
        throw new TransactionNotYetBegunException(Message.DOMA2061, savepointName);
    }
    String id = context.getId();
    Savepoint savepoint = context.releaseAndGetSavepoint(savepointName);
    if (savepoint == null) {
        rollbackInternal("releaseSavepoint");
        throw new SavepointNotFoundException(savepointName);
    }
    LocalTransactionConnection connection = context.getConnection();
    try {
        connection.releaseSavepoint(savepoint);
    } catch (SQLException e) {
        rollbackInternal("releaseSavepoint");
        throw new JdbcException(Message.DOMA2060, e, savepointName, e);
    }
    jdbcLogger.logTransactionSavepointReleased(className, "releaseSavepoint", id, savepointName);
}
Also used : SQLException(java.sql.SQLException) DomaNullPointerException(org.seasar.doma.DomaNullPointerException) Savepoint(java.sql.Savepoint) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 30 with JdbcException

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

the class StandardDialect method transformSelectSqlNode.

@Override
public SqlNode transformSelectSqlNode(SqlNode sqlNode, SelectOptions options) {
    if (sqlNode == null) {
        throw new DomaNullPointerException("sqlNode");
    }
    if (options == null) {
        throw new DomaNullPointerException("options");
    }
    SqlNode transformed = sqlNode;
    if (SelectOptionsAccessor.isCount(options)) {
        transformed = toCountCalculatingSqlNode(sqlNode);
    }
    long offset = SelectOptionsAccessor.getOffset(options);
    long limit = SelectOptionsAccessor.getLimit(options);
    if (offset >= 0 || limit >= 0) {
        transformed = toPagingSqlNode(transformed, offset, limit);
    }
    SelectForUpdateType forUpdateType = SelectOptionsAccessor.getForUpdateType(options);
    if (forUpdateType != null) {
        String[] aliases = SelectOptionsAccessor.getAliases(options);
        if (!supportsSelectForUpdate(forUpdateType, false)) {
            switch(forUpdateType) {
                case NORMAL:
                    throw new JdbcException(Message.DOMA2023, getName());
                case WAIT:
                    throw new JdbcException(Message.DOMA2079, getName());
                case NOWAIT:
                    throw new JdbcException(Message.DOMA2080, getName());
                default:
                    AssertionUtil.assertUnreachable();
            }
        }
        if (aliases.length > 0) {
            if (!supportsSelectForUpdate(forUpdateType, true)) {
                switch(forUpdateType) {
                    case NORMAL:
                        throw new JdbcException(Message.DOMA2024, getName());
                    case WAIT:
                        throw new JdbcException(Message.DOMA2081, getName());
                    case NOWAIT:
                        throw new JdbcException(Message.DOMA2082, getName());
                    default:
                        AssertionUtil.assertUnreachable();
                }
            }
        }
        int waitSeconds = SelectOptionsAccessor.getWaitSeconds(options);
        transformed = toForUpdateSqlNode(transformed, forUpdateType, waitSeconds, aliases);
    }
    return transformed;
}
Also used : DomaNullPointerException(org.seasar.doma.DomaNullPointerException) SelectForUpdateType(org.seasar.doma.jdbc.SelectForUpdateType) JdbcException(org.seasar.doma.jdbc.JdbcException) JdbcMappingHint(org.seasar.doma.jdbc.JdbcMappingHint) SqlNode(org.seasar.doma.jdbc.SqlNode)

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