Browse Source

Support @RunInTransaction for methods returning void

dependabot/maven/junit-junit-4.13.1
Travis Burtrum 4 years ago
parent
commit
9e89131a5b
  1. 22
      common/src/main/java/com/moparisthebest/jdbc/QueryRunner.java
  2. 15
      jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java
  3. 1
      readme.md
  4. 5
      test/src/main/java/com/moparisthebest/jdbc/codegen/PersonDAO.java
  5. 5
      test/src/main/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java

22
common/src/main/java/com/moparisthebest/jdbc/QueryRunner.java

@ -252,6 +252,28 @@ public class QueryRunner<T extends JdbcMapper> { @@ -252,6 +252,28 @@ public class QueryRunner<T extends JdbcMapper> {
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 {
long getDelay(int attempt);

15
jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/JdbcMapperProcessor.java

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

1
readme.md

@ -570,7 +570,6 @@ TODO @@ -570,7 +570,6 @@ TODO
----
* DOCUMENTATION!!!!!
* @RunInTransaction void support
* 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
* CompilingResultSetMapper fails on inner class like 'public static class Bla {'

5
test/src/main/java/com/moparisthebest/jdbc/codegen/PersonDAO.java

@ -282,6 +282,11 @@ public interface PersonDAO extends JdbcMapper { @@ -282,6 +282,11 @@ public interface PersonDAO extends JdbcMapper {
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
// test blob

5
test/src/main/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java

@ -282,6 +282,11 @@ public interface PrestoPersonDAO extends PersonDAO { @@ -282,6 +282,11 @@ public interface PrestoPersonDAO extends PersonDAO {
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
// test blob

Loading…
Cancel
Save