Search in sources :

Example 46 with JdbcException

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

the class NodePreparedSqlBuilderTest method testEmbeddedVariable_containsSemicolon.

@Test
public void testEmbeddedVariable_containsSemicolon() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000)));
    evaluator.add("orderBy", new Value(String.class, "aaa;bbb"));
    String testSql = "select * from aaa where ename = /*name*/'aaa' and sal = /*salary*/-2000 /*#orderBy*/";
    SqlParser parser = new SqlParser(testSql);
    SqlNode sqlNode = parser.parse();
    try {
        new NodePreparedSqlBuilder(config, SqlKind.SELECT, "dummyPath", evaluator, SqlLogType.FORMATTED).build(sqlNode, Function.identity());
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(Message.DOMA2117, expected.getMessageResource());
    }
}
Also used : Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) JdbcException(org.seasar.doma.jdbc.JdbcException) BigDecimal(java.math.BigDecimal) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 47 with JdbcException

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

the class StandardDialectTest method testTransformSelectSqlNode_forUpdateWait_alias.

@Test
public void testTransformSelectSqlNode_forUpdateWait_alias() {
    StandardDialect dialect = new StandardDialectStab();
    SqlParser parser = new SqlParser("select * from emp order by emp.id");
    SqlNode sqlNode = parser.parse();
    SelectOptions options = SelectOptions.get().forUpdateWait(1, "emp");
    try {
        dialect.transformSelectSqlNode(sqlNode, options);
        fail();
    } catch (JdbcException ex) {
        System.out.println(ex.getMessage());
        assertEquals("DOMA2081", ex.getMessageResource().getCode());
    }
}
Also used : SelectOptions(org.seasar.doma.jdbc.SelectOptions) SqlParser(org.seasar.doma.internal.jdbc.sql.SqlParser) JdbcException(org.seasar.doma.jdbc.JdbcException) SqlNode(org.seasar.doma.jdbc.SqlNode) Test(org.junit.jupiter.api.Test)

Example 48 with JdbcException

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

the class KeepAliveLocalTransactionTest method testBegin_failedToBegin.

@Test
public void testBegin_failedToBegin() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getKeepAliveLocalTransaction(jdbcLogger);
    try {
        transaction.begin();
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 49 with JdbcException

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

the class LocalTransactionTest method testBeginAndGetConnection_failedToSetAutoCommit.

@Test
public void testBeginAndGetConnection_failedToSetAutoCommit() {
    final SQLException exception = new SQLException();
    MockConnection connection = new MockConnection() {

        @Override
        public void setAutoCommit(boolean autoCommit) throws SQLException {
            throw exception;
        }
    };
    LocalTransactionDataSource dataSource = new LocalTransactionDataSource(new MockDataSource(connection));
    LocalTransaction transaction = dataSource.getLocalTransaction(jdbcLogger);
    try {
        transaction.begin();
        dataSource.getConnection();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(exception, expected.getCause());
    }
}
Also used : SQLException(java.sql.SQLException) MockDataSource(org.seasar.doma.internal.jdbc.mock.MockDataSource) MockConnection(org.seasar.doma.internal.jdbc.mock.MockConnection) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 50 with JdbcException

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

the class SqlParser method validate.

protected void validate() {
    if (isAfterValueNode()) {
        ValueNode valueNode = pop();
        throw new JdbcException(Message.DOMA2110, sql, tokenizer.getLineNumber(), tokenizer.getPosition(), valueNode.getText());
    }
    if (isInIfBlockNode()) {
        removeNodesTo(IfBlockNode.class);
        IfBlockNode ifBlockNode = pop();
        SqlLocation location = ifBlockNode.getIfNode().getLocation();
        throw new JdbcException(Message.DOMA2133, sql, location.getLineNumber(), location.getPosition());
    }
    if (isInForBlockNode()) {
        removeNodesTo(ForBlockNode.class);
        ForBlockNode forBlockNode = pop();
        SqlLocation location = forBlockNode.getForNode().getLocation();
        throw new JdbcException(Message.DOMA2134, sql, location.getLineNumber(), location.getPosition());
    }
}
Also used : ForBlockNode(org.seasar.doma.internal.jdbc.sql.node.ForBlockNode) ValueNode(org.seasar.doma.internal.jdbc.sql.node.ValueNode) IfBlockNode(org.seasar.doma.internal.jdbc.sql.node.IfBlockNode) SqlLocation(org.seasar.doma.internal.jdbc.sql.node.SqlLocation) 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