diff --git a/jdbcmapper/pom.xml b/jdbcmapper/pom.xml index b08329b..230bd9a 100644 --- a/jdbcmapper/pom.xml +++ b/jdbcmapper/pom.xml @@ -45,8 +45,6 @@ maven-compiler-plugin - 1.6 - 1.6 true 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 7725e79..7984f90 100644 --- a/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java +++ b/jdbcmapper/src/main/java/com/moparisthebest/jdbc/codegen/CompileTimeResultSetMapper.java @@ -18,6 +18,9 @@ import java.io.IOException; import java.io.Writer; import java.sql.ResultSet; import java.util.*; +//IFJAVA8_START +import java.util.stream.Stream; +//IFJAVA8_END import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorStringNoGenerics; import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass; @@ -41,6 +44,9 @@ public class CompileTimeResultSetMapper { public final Types types; public final TypeMirror collectionType, mapType, mapCollectionType, iteratorType, listIteratorType, finishableType, resultSetType, resultSetIterableType; + //IFJAVA8_START + public final TypeMirror streamType; + //IFJAVA8_END private final boolean java8; public CompileTimeResultSetMapper(final ProcessingEnvironment processingEnv) { @@ -61,6 +67,10 @@ public class CompileTimeResultSetMapper { resultSetType = elements.getTypeElement(ResultSet.class.getCanonicalName()).asType(); resultSetIterableType = types.getDeclaredType(elements.getTypeElement(ResultSetIterable.class.getCanonicalName()), types.getWildcardType(null, null)); + + //IFJAVA8_START + streamType = types.getDeclaredType(elements.getTypeElement(Stream.class.getCanonicalName()), types.getWildcardType(null, null)); + //IFJAVA8_END } public static String getConcreteClassCanonicalName(final TypeMirror returnType, final Class defaultConcreteClass) { @@ -115,7 +125,14 @@ public class CompileTimeResultSetMapper { toListIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner); else toIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner); - } else { + } + //IFJAVA8_START + else if (types.isAssignable(returnTypeMirror, streamType)) { + toStream(w, keys, ((DeclaredType) returnTypeMirror).getTypeArguments().get(0), cal, cleaner, closePs); + return false; + } + //IFJAVA8_END + else { toObject(w, keys, returnTypeMirror, cal, cleaner); } return true; @@ -172,6 +189,32 @@ public class CompileTimeResultSetMapper { w.append(";\n"); } + //IFJAVA8_START + + // being in this method implies java8 is true already, how else could you be compiling code using Stream? so this won't have the checks for lambdas toResultSetIterable does... + private void toStream(final Writer w, final String[] keys, final TypeMirror returnTypeMirror, final String cal, final String cleaner, final boolean closePs) throws IOException, ClassNotFoundException { + + if(closePs) + w.write("\t\t\tfinal PreparedStatement finalPs = ps;\n"); + + w.write("\t\t\treturn com.moparisthebest.jdbc.util.ResultSetIterable.getStream(rs,\n\t\t\t\t\trs.next() ? "); + + w.append("(_rs, _cal) -> {\n"); + + // com.moparisthebest.jdbc.util.ResultSetToObject implementation + writeObject(w, keys, returnTypeMirror, "_rs", cal == null ? null : "_cal"); + w.write("\t\t\t\t\t\treturn "); + clean(w, cleaner).write(";\n"); + // end ResultSetToObject implementation + + w.append("\t\t\t\t\t}\n\t\t\t\t: null, ").append(cal == null ? "null" : cal).append(")"); + if(closePs) + w.append(".onClose(() -> tryClose(finalPs))"); + w.append(";\n"); + } + + //IFJAVA8_END + public void writeCollection(final Writer w, final String[] keys, final String returnTypeString, final String concreteTypeString, final TypeMirror componentTypeMirror, MaxRows maxRows, String cal, final String cleaner) throws IOException, ClassNotFoundException { maxRowInit(w, maxRows).write("\t\t\tfinal "); w.write(returnTypeString); diff --git a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/JdbcMapperTest.java b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/JdbcMapperTest.java index d4ccc90..4e04d53 100644 --- a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/JdbcMapperTest.java +++ b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/JdbcMapperTest.java @@ -9,6 +9,10 @@ import org.junit.Test; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +//IFJAVA8_START +import java.util.stream.Collectors; +import java.util.stream.Stream; +//IFJAVA8_END import static com.moparisthebest.jdbc.QueryMapperTest.fieldPerson1; import static com.moparisthebest.jdbc.QueryMapperTest.getConnection; @@ -65,4 +69,26 @@ public class JdbcMapperTest { rsi.close(); assertArrayEquals(people, fromDb.toArray()); } + + //IFJAVA8_START + + @Test + public void testStream() throws SQLException { + final List fromDb; + try(Stream rsi = dao.getPeopleStream(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());) { + fromDb = rsi.collect(Collectors.toList()); + } + assertArrayEquals(people, fromDb.toArray()); + } + + @Test + public void testStreamCachedPreparedStatement() throws SQLException { + final List fromDb; + try(Stream rsi = dao.getPeopleStreamCachedPreparedStatement(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());) { + fromDb = rsi.collect(Collectors.toList()); + } + assertArrayEquals(people, fromDb.toArray()); + } + + //IFJAVA8_END } 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 62815ab..ba492d7 100644 --- a/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java +++ b/jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java @@ -10,6 +10,9 @@ import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.*; +//IFJAVA8_START +import java.util.stream.Stream; +//IFJAVA8_END /** * Created by mopar on 5/24/17. @@ -144,4 +147,14 @@ public interface PersonDAO extends Closeable { @JdbcMapper.SQL(value = "SELECT person_no, birth_date, last_name, first_name from person WHERE person_no IN ({personNo1},{personNo2},{personNo3}) ORDER BY person_no", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) ResultSetIterable getPeopleResultSetIterableCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException; + + //IFJAVA8_START + + @JdbcMapper.SQL("SELECT person_no, birth_date, last_name, first_name from person WHERE person_no IN ({personNo1},{personNo2},{personNo3}) ORDER BY person_no") + Stream getPeopleStream(long personNo1, long personNo2, long personNo3) throws SQLException; + + @JdbcMapper.SQL(value = "SELECT person_no, birth_date, last_name, first_name from person WHERE person_no IN ({personNo1},{personNo2},{personNo3}) ORDER BY person_no", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) + Stream getPeopleStreamCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException; + + //IFJAVA8_END } diff --git a/presto-sqlparser/genPrestoPersonDAO.sh b/presto-sqlparser/genPrestoPersonDAO.sh new file mode 100755 index 0000000..a77c17a --- /dev/null +++ b/presto-sqlparser/genPrestoPersonDAO.sh @@ -0,0 +1,5 @@ +#!/bin/bash +cd "$(dirname "$0")" + +sed -e 's/PersonDAO extends Closeable/PrestoPersonDAO extends PersonDAO/' -e 's@// , sqlParser = SimpleSQLParser.class@ , sqlParser = PrestoSQLParser.class@' ../jdbcmapper/src/test/java/com/moparisthebest/jdbc/codegen/PersonDAO.java > src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java + diff --git a/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java b/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java index 07ecb70..0c04cbd 100644 --- a/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java +++ b/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAO.java @@ -5,22 +5,26 @@ import com.moparisthebest.jdbc.dto.FieldPerson; import com.moparisthebest.jdbc.dto.Person; import com.moparisthebest.jdbc.util.ResultSetIterable; +import java.io.Closeable; import java.sql.SQLException; import java.sql.Time; import java.sql.Timestamp; import java.util.*; +//IFJAVA8_START +import java.util.stream.Stream; +//IFJAVA8_END /** * Created by mopar on 5/24/17. */ @JdbcMapper.Mapper( // jndiName = "bob", -// cachePreparedStatements = false, - sqlParser = PrestoSQLParser.class +// databaseType = JdbcMapper.DatabaseType.ORACLE + cachePreparedStatements = JdbcMapper.OptionalBool.FALSE + , sqlParser = PrestoSQLParser.class ) public interface PrestoPersonDAO extends PersonDAO { - @JdbcMapper.SQL("UPDATE person SET first_name = {firstName} WHERE last_name = {lastName}") int setFirstName(String firstName, String lastName); @@ -144,4 +148,13 @@ public interface PrestoPersonDAO extends PersonDAO { @JdbcMapper.SQL(value = "SELECT person_no, birth_date, last_name, first_name from person WHERE person_no IN ({personNo1},{personNo2},{personNo3}) ORDER BY person_no", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) ResultSetIterable getPeopleResultSetIterableCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException; + //IFJAVA8_START + + @JdbcMapper.SQL("SELECT person_no, birth_date, last_name, first_name from person WHERE person_no IN ({personNo1},{personNo2},{personNo3}) ORDER BY person_no") + Stream getPeopleStream(long personNo1, long personNo2, long personNo3) throws SQLException; + + @JdbcMapper.SQL(value = "SELECT person_no, birth_date, last_name, first_name from person WHERE person_no IN ({personNo1},{personNo2},{personNo3}) ORDER BY person_no", cachePreparedStatement = JdbcMapper.OptionalBool.TRUE) + Stream getPeopleStreamCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException; + + //IFJAVA8_END } diff --git a/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAOTest.java b/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAOTest.java index ff4641f..601ebd9 100644 --- a/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAOTest.java +++ b/presto-sqlparser/src/test/java/com/moparisthebest/jdbc/codegen/PrestoPersonDAOTest.java @@ -24,8 +24,4 @@ public class PrestoPersonDAOTest extends JdbcMapperTest { public static void tearDown() throws Throwable { tryClose(dao); } - - public PersonDAO getDao() { - return dao; - } }