From 220a26c3348113b114fad866451354dad37ee7eb Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Mon, 19 Jun 2017 23:50:59 -0400 Subject: [PATCH] Add support for returning ResultSet to jdbcmapper --- .../jdbc/codegen/CompileTimeResultSetMapper.java | 14 +++++++++++++- .../com/moparisthebest/jdbc/codegen/PersonDAO.java | 13 +++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java index 60ee68b..d551d13 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java @@ -132,7 +132,10 @@ public class CompileTimeResultSetMapper { return false; } //IFJAVA8_END - else { + else if(types.isAssignable(returnTypeMirror, resultSetType)) { + toResultSet(w, closePs); + return false; + } else { toObject(w, keys, returnTypeMirror, cal, cleaner, reflectionFields); } return true; @@ -154,6 +157,15 @@ public class CompileTimeResultSetMapper { getRowMapper(keys, returnTypeMirror, resultSetName, cal, null, null, reflectionFields).gen(w, returnTypeMirror.toString()); } + private void toResultSet(final Writer w, final boolean closePs) throws IOException { + w.append("\t\t\treturn "); + if(closePs) + w.append("new com.moparisthebest.jdbc.StatementClosingResultSet(rs, ps)"); + else + w.append("rs"); + w.append(";\n"); + } + public void toObject(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final ReflectionFields reflectionFields) throws IOException, ClassNotFoundException { w.write("\t\t\tif(rs.next()) {\n"); writeObject(w, keys, returnTypeMirror, cal, reflectionFields); diff --git a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java index 79d0dde..b45e9e6 100644 --- a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java +++ b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java @@ -8,6 +8,7 @@ import com.moparisthebest.jdbc.dto.Person; import com.moparisthebest.jdbc.util.ResultSetIterable; import java.io.Closeable; +import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; @@ -29,6 +30,12 @@ import java.time.*; ) public interface PersonDAO extends Closeable { + @JdbcMapper.SQL("CREATE TABLE person (person_no NUMERIC, first_name VARCHAR(40), last_name VARCHAR(40), birth_date TIMESTAMP)") + void createTablePerson(); + + @JdbcMapper.SQL("INSERT INTO person (person_no, birth_date, last_name, first_name) VALUES ({personNo}, {birthDate}, {firstName}, {lastName})") + int insertPerson(long personNo, Date birthDate, String firstName, String lastName); + @JdbcMapper.SQL("UPDATE person SET first_name = {firstName} WHERE last_name = {lastName}") int setFirstName(String firstName, String lastName); @@ -41,6 +48,12 @@ public interface PersonDAO extends Closeable { @JdbcMapper.SQL("UPDATE person SET first_name = {firstName} WHERE person_no = {personNo}") void setFirstNameBlob(@JdbcMapper.Blob String firstName, long personNo) throws SQLException; + @JdbcMapper.SQL("SELECT first_name, last_name FROM person WHERE last_name = {lastName}") + ResultSet getPeopleResultSet(String lastName) throws SQLException; + + @JdbcMapper.SQL(value = "SELECT first_name, last_name FROM person WHERE last_name = {lastName}", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) + ResultSet getPeopleResultSetCached(String lastName) throws SQLException; + @JdbcMapper.SQL("SELECT first_name FROM person WHERE person_no = {personNo}") String getFirstName(long personNo) throws SQLException;