Search in sources :

Example 41 with JdbcException

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

the class LocalTransaction method setSavepoint.

/**
 * Creates a save point with the specified name.
 *
 * @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 SavepointAlreadyExistsException if the save point already exists
 * @throws JdbcException if a JDBC related error occurs
 */
public void setSavepoint(String savepointName) {
    if (savepointName == null) {
        rollbackInternal("setSavepoint");
        throw new DomaNullPointerException("savepointName");
    }
    LocalTransactionContext context = localTxContextHolder.get();
    if (!isActiveInternal(context)) {
        throw new TransactionNotYetBegunException(Message.DOMA2053, savepointName);
    }
    String id = context.getId();
    Savepoint savepoint = context.getSavepoint(savepointName);
    if (savepoint != null) {
        rollbackInternal("setSavepoint");
        throw new SavepointAlreadyExistsException(savepointName);
    }
    LocalTransactionConnection connection = context.getConnection();
    try {
        savepoint = connection.setSavepoint(savepointName);
    } catch (SQLException e) {
        rollbackInternal("setSavepoint");
        throw new JdbcException(Message.DOMA2051, e, savepointName, e);
    }
    context.addSavepoint(savepointName, savepoint);
    jdbcLogger.logTransactionSavepointCreated(className, "setSavepoint", id, savepointName);
}
Also used : SQLException(java.sql.SQLException) DomaNullPointerException(org.seasar.doma.DomaNullPointerException) Savepoint(java.sql.Savepoint) JdbcException(org.seasar.doma.jdbc.JdbcException)

Example 42 with JdbcException

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

the class NodePreparedSqlBuilderTest method testEmbeddedVariable_containsSingleQuote.

@Test
public void testEmbeddedVariable_containsSingleQuote() {
    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'"));
    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.DOMA2116, 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 43 with JdbcException

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

the class NodePreparedSqlBuilderTest method testLiteralVariable_containsSingleQuote.

@Test
public void testLiteralVariable_containsSingleQuote() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hog'e"));
    evaluator.add("salary", new Value(BigDecimal.class, new BigDecimal(10000)));
    String testSql = "select * from aaa where ename = /*^name*/'aaa' and sal = /*salary*/-2000";
    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.DOMA2224, 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 44 with JdbcException

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

the class NodePreparedSqlBuilderTest method testEmbeddedVariable_blockComment.

@Test
public void testEmbeddedVariable_blockComment() {
    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.DOMA2123, 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 45 with JdbcException

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

the class SqlParserTest method testLiteralVariable_endsWithLiteralVariableComment.

@Test
public void testLiteralVariable_endsWithLiteralVariableComment() {
    ExpressionEvaluator evaluator = new ExpressionEvaluator();
    evaluator.add("name", new Value(String.class, "hoge"));
    String testSql = "select * from aaa where ename = /*^name*/";
    SqlParser parser = new SqlParser(testSql);
    try {
        parser.parse();
        fail();
    } catch (JdbcException expected) {
        System.out.println(expected.getMessage());
        assertEquals(Message.DOMA2110, expected.getMessageResource());
    }
}
Also used : Value(org.seasar.doma.internal.expr.Value) ExpressionEvaluator(org.seasar.doma.internal.expr.ExpressionEvaluator) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

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