Search in sources :

Example 61 with JdbcException

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

the class AbstractIdGenerator method getGeneratedValue.

/**
 * Executes the SQL and gets a generated identity.
 *
 * @param config the configuration for identity generation
 * @param sql the SQL to get the generated identityL
 * @return the generated identity
 * @throws JdbcException if a JDBC related error occurs
 */
protected long getGeneratedValue(IdGenerationConfig config, Sql<?> sql) {
    JdbcLogger logger = config.getJdbcLogger();
    Connection connection = JdbcUtil.getConnection(config.getDataSource());
    try {
        PreparedStatement preparedStatement = JdbcUtil.prepareStatement(connection, sql);
        try {
            logger.logSql(getClass().getName(), "getGeneratedId", sql);
            setupOptions(config, preparedStatement);
            ResultSet resultSet = preparedStatement.executeQuery();
            return getGeneratedValue(config, resultSet);
        } 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 62 with JdbcException

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

the class GeneratedIdPropertyType method validateGenerationStrategy.

public void validateGenerationStrategy(IdGenerationConfig config) {
    Dialect dialect = config.getDialect();
    GenerationType generationType = idGenerator.getGenerationType();
    if (!isGenerationTypeSupported(generationType, dialect)) {
        EntityType<?> entityType = config.getEntityType();
        throw new JdbcException(Message.DOMA2021, entityType.getName(), name, generationType.name(), dialect.getName());
    }
}
Also used : Dialect(org.seasar.doma.jdbc.dialect.Dialect) JdbcException(org.seasar.doma.jdbc.JdbcException) GenerationType(org.seasar.doma.GenerationType)

Example 63 with JdbcException

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

the class SelectBuilderTest method testLiteral_singleQuoteIncluded.

@Test
public void testLiteral_singleQuoteIncluded() {
    SelectBuilder builder = SelectBuilder.newInstance(new MockConfig());
    builder.sql("select");
    builder.sql("id").sql(",");
    builder.sql("name").sql(",");
    builder.sql("salary");
    builder.sql("from Emp");
    builder.sql("where");
    builder.sql("code = ").literal(String.class, "a'aa");
    builder.sql("and");
    builder.sql("age > ").param(int.class, 20);
    try {
        builder.getSql();
    } catch (JdbcException e) {
        assertEquals(Message.DOMA2224, e.getMessageResource());
    }
}
Also used : MockConfig(org.seasar.doma.internal.jdbc.mock.MockConfig) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 64 with JdbcException

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

the class BatchInsertExecutorTest method testChangeType.

@Test
public void testChangeType() {
    SqlBatchInsertQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("insert into Emp");
    builder.sql("(name, salary)");
    builder.sql("values (");
    builder.param(String.class, "SMITH").sql(", ");
    builder.param(int.class, 100).sql(")");
    builder = builder.fixSql();
    builder.sql("insert into Emp");
    builder.sql("(name, salary)");
    builder.sql("values (");
    try {
        builder.param(int.class, 10).sql(", ");
    } catch (JdbcException e) {
        assertEquals(Message.DOMA2229, e.getMessageResource());
        return;
    }
    fail();
}
Also used : SqlBatchInsertQuery(org.seasar.doma.jdbc.query.SqlBatchInsertQuery) JdbcException(org.seasar.doma.jdbc.JdbcException) Test(org.junit.jupiter.api.Test)

Example 65 with JdbcException

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

the class BatchUpdateExecutorTest method testParamToLiteral.

@Test
public void testParamToLiteral() {
    SqlBatchUpdateQuery query = mockQuery();
    BatchBuilder builder = BatchBuilder.newInstance(query);
    builder.sql("update Emp");
    builder.sql("set");
    builder.sql("name = ").param(String.class, "SMITH").sql(",");
    builder.sql("salary = ").param(BigDecimal.class, new BigDecimal("1000"));
    builder.sql("where");
    builder.sql("ID = ").param(int.class, 10);
    builder = builder.fixSql();
    builder.sql("update Emp");
    builder.sql("set");
    builder = builder.sql("name = ");
    try {
        builder.literal(String.class, "ALLEN");
    } catch (JdbcException e) {
        assertEquals(Message.DOMA2230, e.getMessageResource());
        return;
    }
    fail();
}
Also used : SqlBatchUpdateQuery(org.seasar.doma.jdbc.query.SqlBatchUpdateQuery) JdbcException(org.seasar.doma.jdbc.JdbcException) BigDecimal(java.math.BigDecimal) 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