Support @RunInTransaction for methods returning void

This commit is contained in:
Travis Burtrum 2019-07-21 00:35:09 -04:00
parent b62dc1912a
commit 9e89131a5b
5 changed files with 46 additions and 2 deletions

View File

@ -252,6 +252,28 @@ public class QueryRunner<T extends JdbcMapper> {
E run(T dao) throws SQLException; E run(T dao) throws SQLException;
} }
public static <T> Runner<T, Void> voidToRunner(final VoidRunner<T> query) {
//IFJAVA8_START
return dao -> {
query.run(dao);
return null;
};
//IFJAVA8_END
/*IFJAVA6_START
return new Runner<T, Void>() {
@Override
public Void run(final T dao) throws SQLException {
query.run(dao);
return null;
}
};
IFJAVA6_END*/
}
public static interface VoidRunner<T> {
void run(T dao) throws SQLException;
}
public static interface DelayStrategy { public static interface DelayStrategy {
long getDelay(int attempt); long getDelay(int attempt);

View File

@ -766,6 +766,7 @@ public class JdbcMapperProcessor extends AbstractProcessor {
private void outputRunInTransaction(final ExecutableElement eeMethod, final Writer w) throws IOException { private void outputRunInTransaction(final ExecutableElement eeMethod, final Writer w) throws IOException {
w.write("\n\t@Override\n\tpublic "); w.write("\n\t@Override\n\tpublic ");
final String returnType = eeMethod.getReturnType().toString(); final String returnType = eeMethod.getReturnType().toString();
final boolean isVoid = returnType.equals("void");
w.write(returnType); w.write(returnType);
w.write(" "); w.write(" ");
w.write(eeMethod.getSimpleName().toString()); w.write(eeMethod.getSimpleName().toString());
@ -809,12 +810,20 @@ public class JdbcMapperProcessor extends AbstractProcessor {
final boolean thisDaoImplementsJdbcMapper = types.isAssignable(thisDao.asType(), jdbcMapperType); final boolean thisDaoImplementsJdbcMapper = types.isAssignable(thisDao.asType(), jdbcMapperType);
final String thisDaoName = thisDao.getSimpleName().toString(); final String thisDaoName = thisDao.getSimpleName().toString();
w.write("\t\treturn com.moparisthebest.jdbc.QueryRunner.run"); w.write("\t\t");
if(!isVoid) {
w.write("return ");
}
w.write("com.moparisthebest.jdbc.QueryRunner.run");
if(thisDaoImplementsJdbcMapper) if(thisDaoImplementsJdbcMapper)
w.write("InTransaction(this, "); w.write("InTransaction(this, ");
else else
w.write("ConnectionInTransaction(this.conn, "); w.write("ConnectionInTransaction(this.conn, ");
if(isVoid) {
w.append("com.moparisthebest.jdbc.QueryRunner.voidToRunner(");
}
if(!java8) { if(!java8) {
final String tType = thisDaoImplementsJdbcMapper ? thisDaoName : "Connection"; final String tType = thisDaoImplementsJdbcMapper ? thisDaoName : "Connection";
w.append("new com.moparisthebest.jdbc.QueryRunner.Runner<").append(tType).append(", ").append(returnType).append(">() {\n" + w.append("new com.moparisthebest.jdbc.QueryRunner.Runner<").append(tType).append(", ").append(returnType).append(">() {\n" +
@ -844,6 +853,10 @@ public class JdbcMapperProcessor extends AbstractProcessor {
w.append(";\n\t\t\t}\n" + w.append(";\n\t\t\t}\n" +
"\t\t}"); "\t\t}");
if(isVoid) {
w.append(")");
}
w.write(");\n"); w.write(");\n");
w.write("\t}\n"); w.write("\t}\n");
} }

View File

@ -570,7 +570,6 @@ TODO
---- ----
* DOCUMENTATION!!!!! * DOCUMENTATION!!!!!
* @RunInTransaction void support
* QueryMapper mapping errors should be clearer, especially if a .finish(ResultSet) throws an error * QueryMapper mapping errors should be clearer, especially if a .finish(ResultSet) throws an error
* check QueryMapper/ResultSetMapper closing of ResultSets, it doesn't look guaranteed * check QueryMapper/ResultSetMapper closing of ResultSets, it doesn't look guaranteed
* CompilingResultSetMapper fails on inner class like 'public static class Bla {' * CompilingResultSetMapper fails on inner class like 'public static class Bla {'

View File

@ -282,6 +282,11 @@ public interface PersonDAO extends JdbcMapper {
return getPerson(getPersonNo(lastName)); return getPerson(getPersonNo(lastName));
} }
@JdbcMapper.RunInTransaction
default void update(final String lastName) throws SQLException {
// actually do nothing, this is for testing @RunInTransaction void support
}
//IFJAVA8_END //IFJAVA8_END
// test blob // test blob

View File

@ -282,6 +282,11 @@ public interface PrestoPersonDAO extends PersonDAO {
return getPerson(getPersonNo(lastName)); return getPerson(getPersonNo(lastName));
} }
@JdbcMapper.RunInTransaction
default void update(final String lastName) throws SQLException {
// actually do nothing, this is for testing @RunInTransaction void support
}
//IFJAVA8_END //IFJAVA8_END
// test blob // test blob