mirror of
https://github.com/moparisthebest/JdbcMapper
synced 2024-11-24 10:02:14 -05:00
Implement Stream support in jdbcmapper
This commit is contained in:
parent
280e11b713
commit
9c22b36267
@ -45,8 +45,6 @@
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>1.6</source>
|
||||
<target>1.6</target>
|
||||
<debug>true</debug>
|
||||
<!--compilerArgument>-Xlint:unchecked</compilerArgument-->
|
||||
</configuration>
|
||||
|
@ -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);
|
||||
|
@ -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<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
|
||||
}
|
||||
|
@ -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<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
|
||||
}
|
||||
|
5
presto-sqlparser/genPrestoPersonDAO.sh
Executable file
5
presto-sqlparser/genPrestoPersonDAO.sh
Executable 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
|
||||
|
@ -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<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
|
||||
}
|
||||
|
@ -24,8 +24,4 @@ public class PrestoPersonDAOTest extends JdbcMapperTest {
|
||||
public static void tearDown() throws Throwable {
|
||||
tryClose(dao);
|
||||
}
|
||||
|
||||
public PersonDAO getDao() {
|
||||
return dao;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user