Implement Stream support in jdbcmapper

This commit is contained in:
Travis Burtrum 2017-06-12 23:48:54 -04:00
parent 280e11b713
commit 9c22b36267
7 changed files with 104 additions and 10 deletions

View File

@ -45,8 +45,6 @@
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>1.6</source>
<target>1.6</target>
<debug>true</debug> <debug>true</debug>
<!--compilerArgument>-Xlint:unchecked</compilerArgument--> <!--compilerArgument>-Xlint:unchecked</compilerArgument-->
</configuration> </configuration>

View File

@ -18,6 +18,9 @@ import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.util.*; 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.typeMirrorStringNoGenerics;
import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass; import static com.moparisthebest.jdbc.codegen.JdbcMapperProcessor.typeMirrorToClass;
@ -41,6 +44,9 @@ public class CompileTimeResultSetMapper {
public final Types types; public final Types types;
public final TypeMirror collectionType, mapType, mapCollectionType, iteratorType, listIteratorType, finishableType, resultSetType, resultSetIterableType; public final TypeMirror collectionType, mapType, mapCollectionType, iteratorType, listIteratorType, finishableType, resultSetType, resultSetIterableType;
//IFJAVA8_START
public final TypeMirror streamType;
//IFJAVA8_END
private final boolean java8; private final boolean java8;
public CompileTimeResultSetMapper(final ProcessingEnvironment processingEnv) { public CompileTimeResultSetMapper(final ProcessingEnvironment processingEnv) {
@ -61,6 +67,10 @@ public class CompileTimeResultSetMapper {
resultSetType = elements.getTypeElement(ResultSet.class.getCanonicalName()).asType(); resultSetType = elements.getTypeElement(ResultSet.class.getCanonicalName()).asType();
resultSetIterableType = types.getDeclaredType(elements.getTypeElement(ResultSetIterable.class.getCanonicalName()), types.getWildcardType(null, null)); 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) { 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); toListIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner);
else else
toIterator(w, keys, typeArguments.get(0), maxRows, cal, cleaner); 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); toObject(w, keys, returnTypeMirror, cal, cleaner);
} }
return true; return true;
@ -172,6 +189,32 @@ public class CompileTimeResultSetMapper {
w.append(";\n"); 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 { 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 "); maxRowInit(w, maxRows).write("\t\t\tfinal ");
w.write(returnTypeString); w.write(returnTypeString);

View File

@ -9,6 +9,10 @@ import org.junit.Test;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; 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.fieldPerson1;
import static com.moparisthebest.jdbc.QueryMapperTest.getConnection; import static com.moparisthebest.jdbc.QueryMapperTest.getConnection;
@ -65,4 +69,26 @@ public class JdbcMapperTest {
rsi.close(); rsi.close();
assertArrayEquals(people, fromDb.toArray()); assertArrayEquals(people, fromDb.toArray());
} }
//IFJAVA8_START
@Test
public void testStream() throws SQLException {
final List<FieldPerson> fromDb;
try(Stream<FieldPerson> 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<FieldPerson> fromDb;
try(Stream<FieldPerson> rsi = dao.getPeopleStreamCachedPreparedStatement(people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo());) {
fromDb = rsi.collect(Collectors.toList());
}
assertArrayEquals(people, fromDb.toArray());
}
//IFJAVA8_END
} }

View File

@ -10,6 +10,9 @@ import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.*; import java.util.*;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END
/** /**
* Created by mopar on 5/24/17. * 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) @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<FieldPerson> getPeopleResultSetIterableCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException; ResultSetIterable<FieldPerson> 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<FieldPerson> 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<FieldPerson> getPeopleStreamCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException;
//IFJAVA8_END
} }

View File

@ -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

View File

@ -5,22 +5,26 @@ import com.moparisthebest.jdbc.dto.FieldPerson;
import com.moparisthebest.jdbc.dto.Person; import com.moparisthebest.jdbc.dto.Person;
import com.moparisthebest.jdbc.util.ResultSetIterable; import com.moparisthebest.jdbc.util.ResultSetIterable;
import java.io.Closeable;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Time; import java.sql.Time;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.util.*; import java.util.*;
//IFJAVA8_START
import java.util.stream.Stream;
//IFJAVA8_END
/** /**
* Created by mopar on 5/24/17. * Created by mopar on 5/24/17.
*/ */
@JdbcMapper.Mapper( @JdbcMapper.Mapper(
// jndiName = "bob", // jndiName = "bob",
// cachePreparedStatements = false, // databaseType = JdbcMapper.DatabaseType.ORACLE
sqlParser = PrestoSQLParser.class cachePreparedStatements = JdbcMapper.OptionalBool.FALSE
, sqlParser = PrestoSQLParser.class
) )
public interface PrestoPersonDAO extends PersonDAO { public interface PrestoPersonDAO extends PersonDAO {
@JdbcMapper.SQL("UPDATE person SET first_name = {firstName} WHERE last_name = {lastName}") @JdbcMapper.SQL("UPDATE person SET first_name = {firstName} WHERE last_name = {lastName}")
int setFirstName(String firstName, String 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) @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<FieldPerson> getPeopleResultSetIterableCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException; ResultSetIterable<FieldPerson> 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<FieldPerson> 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<FieldPerson> getPeopleStreamCachedPreparedStatement(long personNo1, long personNo2, long personNo3) throws SQLException;
//IFJAVA8_END
} }

View File

@ -24,8 +24,4 @@ public class PrestoPersonDAOTest extends JdbcMapperTest {
public static void tearDown() throws Throwable { public static void tearDown() throws Throwable {
tryClose(dao); tryClose(dao);
} }
public PersonDAO getDao() {
return dao;
}
} }