Implement toStream() in querymapper
This commit is contained in:
parent
c902da5126
commit
280e11b713
|
@ -24,4 +24,20 @@
|
||||||
<build>
|
<build>
|
||||||
<finalName>${project.artifactId}</finalName>
|
<finalName>${project.artifactId}</finalName>
|
||||||
</build>
|
</build>
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>jdk16</id>
|
||||||
|
<activation>
|
||||||
|
<jdk>[1.6,1.8)</jdk>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.google.code.maven-replacer-plugin</groupId>
|
||||||
|
<artifactId>replacer</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -57,4 +57,18 @@ public class TryClose { // tries to close certain object types
|
||||||
// ignore...
|
// ignore...
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
public static void tryClose(AutoCloseable obj) {
|
||||||
|
if (obj == null)
|
||||||
|
return;
|
||||||
|
try {
|
||||||
|
obj.close();
|
||||||
|
} catch (Throwable e) {
|
||||||
|
// ignore...
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,11 @@ import java.sql.SQLException;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.NoSuchElementException;
|
import java.util.NoSuchElementException;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.Spliterators;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
||||||
|
|
||||||
|
@ -42,19 +47,49 @@ public class ResultSetIterable<T> implements Iterable<T>, Iterator<T>, Closeable
|
||||||
* ResultSetIterable<T> rsi = ResultSetIterable.getResultSetIterable(rs, rs.next() ? complicatedBuildResultSetToObject(rs) : null, cal);
|
* ResultSetIterable<T> rsi = ResultSetIterable.getResultSetIterable(rs, rs.next() ? complicatedBuildResultSetToObject(rs) : null, cal);
|
||||||
* <p>
|
* <p>
|
||||||
* This way you can avoid building or sending in a ResultSetToObject all together if there are no rows, therefore if rsto
|
* This way you can avoid building or sending in a ResultSetToObject all together if there are no rows, therefore if rsto
|
||||||
* sent into this is null, it returns an EMPTY_RESULT_SET_ITERABLE
|
* sent into this is null, it returns an EMPTY_RESULT_SET_ITERABLE and closes rs immediately
|
||||||
* <p>
|
* <p>
|
||||||
* This assumes rs.next() was called once before sent into this function
|
* This assumes rs.next() was called once before sent into this function
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public static <T> ResultSetIterable<T> getResultSetIterable(final ResultSet rs, final ResultSetToObject<T> rsto, final Calendar cal) {
|
public static <T> ResultSetIterable<T> getResultSetIterable(final ResultSet rs, final ResultSetToObject<T> rsto, final Calendar cal) {
|
||||||
if (rsto == null)
|
if (rsto == null) {
|
||||||
|
tryClose(rs); // have to do this here...
|
||||||
return (ResultSetIterable<T>) EMPTY_RESULT_SET_ITERABLE;
|
return (ResultSetIterable<T>) EMPTY_RESULT_SET_ITERABLE;
|
||||||
|
}
|
||||||
final ResultSetIterable<T> ret = new ResultSetIterable<T>(rs, rsto, cal);
|
final ResultSetIterable<T> ret = new ResultSetIterable<T>(rs, rsto, cal);
|
||||||
ret.calledNext = true;
|
ret.calledNext = true;
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a convenience method meant to be called like this, where rs is a ResultSet
|
||||||
|
* <p>
|
||||||
|
* Stream<T> rsi = ResultSetIterable.getStream(rs, rs.next() ? complicatedBuildResultSetToObject(rs) : null, cal);
|
||||||
|
* <p>
|
||||||
|
* This way you can avoid building or sending in a ResultSetToObject all together if there are no rows, therefore if rsto
|
||||||
|
* sent into this is null, it returns an empty Stream, rs might be closed before returning the empty Stream or on .close() to the Stream
|
||||||
|
* <p>
|
||||||
|
* The stream returned MUST be closed in a try-with-resources or finally because the ResultSet is held open until then
|
||||||
|
* <p>
|
||||||
|
* This assumes rs.next() was called once before sent into this function
|
||||||
|
*/
|
||||||
|
//IFJAVA8_START
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public static <T> Stream<T> getStream(final ResultSet rs, final ResultSetToObject<T> rsto, final Calendar cal) {
|
||||||
|
final Stream<T> ret;
|
||||||
|
if (rsto == null) {
|
||||||
|
// todo: static object for this? don't forget to close resultset if so
|
||||||
|
return (Stream<T>) StreamSupport.stream(Spliterators.emptySpliterator(), false);
|
||||||
|
} else {
|
||||||
|
final ResultSetIterable<T> rsi = new ResultSetIterable<T>(rs, rsto, cal);
|
||||||
|
rsi.calledNext = true;
|
||||||
|
ret = StreamSupport.stream(rsi.spliterator(), false);
|
||||||
|
}
|
||||||
|
return ret.onClose(() -> tryClose(rs));
|
||||||
|
}
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
private final ResultSet rs;
|
private final ResultSet rs;
|
||||||
private final Calendar cal;
|
private final Calendar cal;
|
||||||
private final ResultSetToObject<T> rsto;
|
private final ResultSetToObject<T> rsto;
|
||||||
|
|
1
pom.xml
1
pom.xml
|
@ -203,6 +203,7 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<includes>
|
<includes>
|
||||||
<include>*/src/main/java/**/*</include>
|
<include>*/src/main/java/**/*</include>
|
||||||
|
<include>*/src/test/java/**/*</include>
|
||||||
</includes>
|
</includes>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>target/**</exclude>
|
<exclude>target/**</exclude>
|
||||||
|
|
|
@ -22,18 +22,22 @@ function finishFile(){
|
||||||
result="$(prepareFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java")"
|
result="$(prepareFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java")"
|
||||||
|
|
||||||
# single object types
|
# single object types
|
||||||
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | egrep '(toObject|toSingleMap|toResultSetIterable|toResultSetIterableMap)\(' | grep ', Calendar cal)' | while read method
|
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | egrep '(toObject|toSingleMap|toResultSetIterable|toStream)\(' | grep ', Calendar cal)' | while read method
|
||||||
do
|
do
|
||||||
#echo "method: $method"
|
#echo "method: $method"
|
||||||
method_name=$(echo $method | egrep -o '[^ ]+\(')
|
method_name=$(echo $method | egrep -o '[^ ]+\(')
|
||||||
echo "ResultSetMapper.$method_name)"
|
echo "ResultSetMapper.$method_name)"
|
||||||
|
|
||||||
|
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_START\n' >> "$result"
|
||||||
|
|
||||||
cat >> "$result" <<EOF
|
cat >> "$result" <<EOF
|
||||||
$(echo $method | sed "s/, Calendar cal)/)/")
|
$(echo $method | sed "s/, Calendar cal)/)/")
|
||||||
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
|
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
|
||||||
}
|
}
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_END\n' >> "$result"
|
||||||
done
|
done
|
||||||
|
|
||||||
#everything else
|
#everything else
|
||||||
|
@ -67,6 +71,8 @@ do
|
||||||
method_name=$(echo $method | egrep -o '[^ ]+\(')
|
method_name=$(echo $method | egrep -o '[^ ]+\(')
|
||||||
echo "QueryMapper.$method_name)"
|
echo "QueryMapper.$method_name)"
|
||||||
|
|
||||||
|
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_START\n' | tee -a "$query" "$caching_query" "$null_query" "$list_query" >/dev/null
|
||||||
|
|
||||||
# QueryMapper.java
|
# QueryMapper.java
|
||||||
cat >> "$query" <<EOF
|
cat >> "$query" <<EOF
|
||||||
$(echo $method | sed -e 's/ResultSet rs/PreparedStatement ps/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
$(echo $method | sed -e 's/ResultSet rs/PreparedStatement ps/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
||||||
|
@ -76,7 +82,7 @@ do
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
# handle this specially in QueryMapper because we need it to hold open PreparedStatement until the ResultSetIterable is closed
|
# handle this specially in QueryMapper because we need it to hold open PreparedStatement until the ResultSetIterable is closed
|
||||||
if [ "$method_name" != 'toResultSetIterable(' -a "$method_name" != 'toResultSetIterableMap(' ]; then
|
if [ "$method_name" != 'toResultSetIterable(' -a "$method_name" != 'toStream(' ]; then
|
||||||
|
|
||||||
cat >> "$query" <<EOF
|
cat >> "$query" <<EOF
|
||||||
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
|
||||||
|
@ -91,7 +97,7 @@ EOF
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
fi # end special case toResultSetIterable
|
fi # end special case toResultSetIterable/toStream
|
||||||
|
|
||||||
# CachingQueryMapper.java
|
# CachingQueryMapper.java
|
||||||
cat >> "$caching_query" <<EOF
|
cat >> "$caching_query" <<EOF
|
||||||
|
@ -133,6 +139,9 @@ EOF
|
||||||
}
|
}
|
||||||
|
|
||||||
EOF
|
EOF
|
||||||
|
|
||||||
|
[ "$method_name" == 'toStream(' ] && echo -e '\t//IFJAVA8_END\n' | tee -a "$query" "$caching_query" "$null_query" "$list_query" >/dev/null
|
||||||
|
|
||||||
done
|
done
|
||||||
|
|
||||||
finishFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java"
|
finishFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java"
|
||||||
|
|
|
@ -50,4 +50,20 @@
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>jdk16</id>
|
||||||
|
<activation>
|
||||||
|
<jdk>[1.6,1.8)</jdk>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>com.google.code.maven-replacer-plugin</groupId>
|
||||||
|
<artifactId>replacer</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -7,6 +7,9 @@ import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
||||||
|
|
||||||
|
@ -145,10 +148,28 @@ public class CachingQueryMapper extends QueryMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return super.toResultSetIterableMap(getPreparedStatement(sql), componentType, mapValType, bindObjects);
|
return super.toResultSetIterable(getPreparedStatement(sql), componentType, mapValType, bindObjects);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Stream<T> toStream(String sql, Class<T> componentType, final Object... bindObjects) throws SQLException {
|
||||||
|
return super.toStream(getPreparedStatement(sql), componentType, bindObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
|
return super.toStream(getPreparedStatement(sql), componentType, mapValType, bindObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return super.toSingleMap(getPreparedStatement(sql), componentType, mapValType, bindObjects);
|
return super.toSingleMap(getPreparedStatement(sql), componentType, mapValType, bindObjects);
|
||||||
|
|
|
@ -8,6 +8,9 @@ import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
public class ListQueryMapper extends QueryMapper {
|
public class ListQueryMapper extends QueryMapper {
|
||||||
|
|
||||||
|
@ -253,15 +256,43 @@ public class ListQueryMapper extends QueryMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return delegate.toResultSetIterableMap(ps, componentType, mapValType, bindObjects);
|
return delegate.toResultSetIterable(ps, componentType, mapValType, bindObjects);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return delegate.toResultSetIterableMap(prepareSql(sql, bindObjects), componentType, mapValType, bindObjects);
|
return delegate.toResultSetIterable(prepareSql(sql, bindObjects), componentType, mapValType, bindObjects);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Stream<T> toStream(PreparedStatement ps, Class<T> componentType, final Object... bindObjects) throws SQLException {
|
||||||
|
return delegate.toStream(ps, componentType, bindObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Stream<T> toStream(String sql, Class<T> componentType, final Object... bindObjects) throws SQLException {
|
||||||
|
return delegate.toStream(prepareSql(sql, bindObjects), componentType, bindObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
|
return delegate.toStream(ps, componentType, mapValType, bindObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
|
return delegate.toStream(prepareSql(sql, bindObjects), componentType, mapValType, bindObjects);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return delegate.toSingleMap(ps, componentType, mapValType, bindObjects);
|
return delegate.toSingleMap(ps, componentType, mapValType, bindObjects);
|
||||||
|
|
|
@ -7,6 +7,9 @@ import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
public class NullQueryMapper extends QueryMapper {
|
public class NullQueryMapper extends QueryMapper {
|
||||||
|
|
||||||
|
@ -280,9 +283,9 @@ public class NullQueryMapper extends QueryMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(PreparedStatement query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(PreparedStatement query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
||||||
try {
|
try {
|
||||||
return delegate.toResultSetIterableMap(query, componentType, mapValType, bindObjects);
|
return delegate.toResultSetIterable(query, componentType, mapValType, bindObjects);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
if (verbose) e.printStackTrace();
|
if (verbose) e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
@ -290,15 +293,63 @@ public class NullQueryMapper extends QueryMapper {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(String query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(String query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
||||||
try {
|
try {
|
||||||
return delegate.toResultSetIterableMap(query, componentType, mapValType, bindObjects);
|
return delegate.toResultSetIterable(query, componentType, mapValType, bindObjects);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
if (verbose) e.printStackTrace();
|
if (verbose) e.printStackTrace();
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Stream<T> toStream(PreparedStatement query, Class<T> componentType, final Object... bindObjects) {
|
||||||
|
try {
|
||||||
|
return delegate.toStream(query, componentType, bindObjects);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (verbose) e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> Stream<T> toStream(String query, Class<T> componentType, final Object... bindObjects) {
|
||||||
|
try {
|
||||||
|
return delegate.toStream(query, componentType, bindObjects);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (verbose) e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(PreparedStatement query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
||||||
|
try {
|
||||||
|
return delegate.toStream(query, componentType, mapValType, bindObjects);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (verbose) e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(String query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
||||||
|
try {
|
||||||
|
return delegate.toStream(query, componentType, mapValType, bindObjects);
|
||||||
|
} catch (Throwable e) {
|
||||||
|
if (verbose) e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
|
||||||
try {
|
try {
|
||||||
|
|
|
@ -9,6 +9,9 @@ import java.io.*;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
||||||
|
|
||||||
|
@ -362,7 +365,7 @@ public class QueryMapper implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
boolean error = true;
|
boolean error = true;
|
||||||
PreparedStatement ps = null;
|
PreparedStatement ps = null;
|
||||||
ResultSet rs = null;
|
ResultSet rs = null;
|
||||||
|
@ -370,7 +373,7 @@ public class QueryMapper implements Closeable {
|
||||||
try {
|
try {
|
||||||
ps = conn.prepareStatement(sql);
|
ps = conn.prepareStatement(sql);
|
||||||
rs = this.toResultSet(ps, bindObjects);
|
rs = this.toResultSet(ps, bindObjects);
|
||||||
ret = cm.toResultSetIterableMap(rs, componentType, mapValType).setPreparedStatementToClose(ps);
|
ret = cm.toResultSetIterable(rs, componentType, mapValType).setPreparedStatementToClose(ps);
|
||||||
error = false;
|
error = false;
|
||||||
return ret;
|
return ret;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -382,6 +385,50 @@ public class QueryMapper implements Closeable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
public <T> Stream<T> toStream(String sql, Class<T> componentType, final Object... bindObjects) throws SQLException {
|
||||||
|
boolean error = true;
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
Stream<T> ret = null;
|
||||||
|
try {
|
||||||
|
ps = conn.prepareStatement(sql);
|
||||||
|
rs = this.toResultSet(ps, bindObjects);
|
||||||
|
final PreparedStatement finalPs = ps;
|
||||||
|
ret = cm.toStream(rs, componentType).onClose(() -> tryClose(finalPs));
|
||||||
|
error = false;
|
||||||
|
return ret;
|
||||||
|
} finally {
|
||||||
|
if (error) {
|
||||||
|
tryClose(ret);
|
||||||
|
tryClose(rs);
|
||||||
|
tryClose(ps);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
|
boolean error = true;
|
||||||
|
PreparedStatement ps = null;
|
||||||
|
ResultSet rs = null;
|
||||||
|
Stream<Map<String, V>> ret = null;
|
||||||
|
try {
|
||||||
|
ps = conn.prepareStatement(sql);
|
||||||
|
rs = this.toResultSet(ps, bindObjects);
|
||||||
|
final PreparedStatement finalPs = ps;
|
||||||
|
ret = cm.toStream(rs, componentType, mapValType).onClose(() -> tryClose(finalPs));
|
||||||
|
error = false;
|
||||||
|
return ret;
|
||||||
|
} finally {
|
||||||
|
if (error) {
|
||||||
|
tryClose(ret);
|
||||||
|
tryClose(rs);
|
||||||
|
tryClose(ps);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
// these are standard getters
|
// these are standard getters
|
||||||
|
|
||||||
public ResultSetMapper getCustomResultSetMapper() {
|
public ResultSetMapper getCustomResultSetMapper() {
|
||||||
|
@ -412,10 +459,26 @@ public class QueryMapper implements Closeable {
|
||||||
return cm.toResultSetIterable(bindExecute(ps, bindObjects), componentType);
|
return cm.toResultSetIterable(bindExecute(ps, bindObjects), componentType);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return cm.toResultSetIterableMap(bindExecute(ps, bindObjects), componentType, mapValType);
|
return cm.toResultSetIterable(bindExecute(ps, bindObjects), componentType, mapValType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
public <T> Stream<T> toStream(PreparedStatement ps, Class<T> componentType, final Object... bindObjects) throws SQLException {
|
||||||
|
return cm.toStream(bindExecute(ps, bindObjects), componentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
|
return cm.toStream(bindExecute(ps, bindObjects), componentType, mapValType);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement ps, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
|
||||||
return cm.toSingleMap(bindExecute(ps, bindObjects), componentType, mapValType);
|
return cm.toSingleMap(bindExecute(ps, bindObjects), componentType, mapValType);
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,6 +29,9 @@ import java.sql.ResultSetMetaData;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Default ResultSetMapper implementation for Objects.
|
* Default ResultSetMapper implementation for Objects.
|
||||||
|
@ -141,6 +144,20 @@ public class ResultSetMapper implements RowMapperProvider {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
protected <T> Stream<T> privToStream(ResultSet rs, Class<T> componentType, Calendar cal, Class<?> mapValType) {
|
||||||
|
try {
|
||||||
|
return ResultSetIterable.getStream(rs,
|
||||||
|
rs.next() ? getRowMapper(rs, componentType, cal, mapValType, null).getResultSetToObject() : null,
|
||||||
|
cal);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new MapperException("cannot create Stream", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
protected <T extends Collection<E>, E> T privToCollection(ResultSet rs, final Class<T> collectionType, Class<E> componentType, int arrayMaxLength, Calendar cal, Class<?> mapValType) {
|
protected <T extends Collection<E>, E> T privToCollection(ResultSet rs, final Class<T> collectionType, Class<E> componentType, int arrayMaxLength, Calendar cal, Class<?> mapValType) {
|
||||||
return privToCollection(rs, instantiateClass(collectionType, ArrayList.class), componentType, arrayMaxLength, cal, mapValType);
|
return privToCollection(rs, instantiateClass(collectionType, ArrayList.class), componentType, arrayMaxLength, cal, mapValType);
|
||||||
}
|
}
|
||||||
|
@ -455,10 +472,23 @@ public class ResultSetMapper implements RowMapperProvider {
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(ResultSet rs, Class<T> componentType, Class<V> mapValType, Calendar cal) {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(ResultSet rs, Class<T> componentType, Class<V> mapValType, Calendar cal) {
|
||||||
return (ResultSetIterable<Map<String, V>>)privToResultSetIterable(rs, componentType, cal, mapValType);
|
return (ResultSetIterable<Map<String, V>>)privToResultSetIterable(rs, componentType, cal, mapValType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
public <T> Stream<T> toStream(ResultSet rs, Class<T> componentType, Calendar cal) {
|
||||||
|
return privToStream(rs, componentType, cal, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(ResultSet rs, Class<T> componentType, Class<V> mapValType, Calendar cal) {
|
||||||
|
return (Stream<Map<String, V>>)privToStream(rs, componentType, cal, mapValType);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
public <T extends Collection<E>, E> T toCollection(ResultSet rs, final Class<T> collectionType, Class<E> componentType, int arrayMaxLength, Calendar cal) {
|
public <T extends Collection<E>, E> T toCollection(ResultSet rs, final Class<T> collectionType, Class<E> componentType, int arrayMaxLength, Calendar cal) {
|
||||||
return privToCollection(rs, collectionType, componentType, arrayMaxLength, cal, null);
|
return privToCollection(rs, collectionType, componentType, arrayMaxLength, cal, null);
|
||||||
}
|
}
|
||||||
|
@ -633,10 +663,26 @@ public class ResultSetMapper implements RowMapperProvider {
|
||||||
return this.toResultSetIterable(rs, componentType, cal);
|
return this.toResultSetIterable(rs, componentType, cal);
|
||||||
}
|
}
|
||||||
|
|
||||||
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterableMap(ResultSet rs, Class<T> componentType, Class<V> mapValType) {
|
public <T extends Map<String, V>, V> ResultSetIterable<Map<String, V>> toResultSetIterable(ResultSet rs, Class<T> componentType, Class<V> mapValType) {
|
||||||
return this.toResultSetIterableMap(rs, componentType, mapValType, cal);
|
return this.toResultSetIterable(rs, componentType, mapValType, cal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
public <T> Stream<T> toStream(ResultSet rs, Class<T> componentType) {
|
||||||
|
return this.toStream(rs, componentType, cal);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
|
//IFJAVA8_START
|
||||||
|
|
||||||
|
public <T extends Map<String, V>, V> Stream<Map<String, V>> toStream(ResultSet rs, Class<T> componentType, Class<V> mapValType) {
|
||||||
|
return this.toStream(rs, componentType, mapValType, cal);
|
||||||
|
}
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(ResultSet rs, Class<T> componentType, Class<V> mapValType) {
|
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(ResultSet rs, Class<T> componentType, Class<V> mapValType) {
|
||||||
return this.toSingleMap(rs, componentType, mapValType, cal);
|
return this.toSingleMap(rs, componentType, mapValType, cal);
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,10 @@ import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
import java.sql.DriverManager;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
//IFJAVA8_START
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
//IFJAVA8_END
|
||||||
|
|
||||||
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
import static com.moparisthebest.jdbc.TryClose.tryClose;
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
@ -406,4 +410,19 @@ public class QueryMapperTest {
|
||||||
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 = qm.toStream("SELECT * from person WHERE person_no IN (?,?,?) ORDER BY person_no",
|
||||||
|
FieldPerson.class, people[0].getPersonNo(), people[1].getPersonNo(), people[2].getPersonNo())) {
|
||||||
|
fromDb = rsi.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
assertArrayEquals(people, fromDb.toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//IFJAVA8_END
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue