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);
}
}
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());
}
}
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());
}
}
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();
}
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();
}
Aggregations