Search in sources :

Example 11 with JdbcException

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

the class AutoDeleteTest method testNoId.

@Test
public void testNoId(Config config) throws Exception {
    NoIdDao dao = new NoIdDaoImpl(config);
    NoId entity = new NoId();
    entity.setValue1(1);
    entity.setValue2(2);
    try {
        dao.delete(entity);
        fail();
    } catch (JdbcException expected) {
        assertEquals(Message.DOMA2022, expected.getMessageResource());
    }
}
Also used : NoIdDao(org.seasar.doma.it.dao.NoIdDao) NoId(org.seasar.doma.it.entity.NoId) NoIdDaoImpl(org.seasar.doma.it.dao.NoIdDaoImpl) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 12 with JdbcException

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

the class AutoUpdateTest method testNoId.

@Test
public void testNoId(Config config) throws Exception {
    NoIdDao dao = new NoIdDaoImpl(config);
    NoId entity = new NoId();
    entity.setValue1(1);
    entity.setValue2(2);
    try {
        dao.update(entity);
        fail();
    } catch (JdbcException expected) {
        assertEquals(Message.DOMA2022, expected.getMessageResource());
    }
}
Also used : NoIdDao(org.seasar.doma.it.dao.NoIdDao) NoId(org.seasar.doma.it.entity.NoId) NoIdDaoImpl(org.seasar.doma.it.dao.NoIdDaoImpl) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 13 with JdbcException

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

the class SqlFileSelectForUpdateTest method testUnsupported.

@Test
@Run(unless = { Dbms.H2, Dbms.POSTGRESQL, Dbms.ORACLE, Dbms.MYSQL, Dbms.DB2, Dbms.SQLSERVER })
public void testUnsupported(Config config) throws Exception {
    EmployeeDao dao = new EmployeeDaoImpl(config);
    try {
        dao.selectById(1, SelectOptions.get().forUpdate());
        fail();
    } catch (JdbcException expected) {
        assertEquals(Message.DOMA2023, expected.getMessageResource());
    }
}
Also used : EmployeeDao(org.seasar.doma.it.dao.EmployeeDao) JdbcException(org.seasar.doma.jdbc.JdbcException) EmployeeDaoImpl(org.seasar.doma.it.dao.EmployeeDaoImpl) Test(org.junit.jupiter.api.Test) Run(org.seasar.doma.it.Run)

Example 14 with JdbcException

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

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

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