From a378e640ae5d424f2e1e0ee98998eeb5eb7b53e5 Mon Sep 17 00:00:00 2001 From: moparisthebest Date: Thu, 11 Oct 2018 00:33:52 -0400 Subject: [PATCH] Fix ListQueryMapper --- .../moparisthebest/jdbc/ListQueryMapper.java | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/querymapper/src/main/java/com/moparisthebest/jdbc/ListQueryMapper.java b/querymapper/src/main/java/com/moparisthebest/jdbc/ListQueryMapper.java index 700c254..838a0d8 100644 --- a/querymapper/src/main/java/com/moparisthebest/jdbc/ListQueryMapper.java +++ b/querymapper/src/main/java/com/moparisthebest/jdbc/ListQueryMapper.java @@ -58,16 +58,24 @@ public class ListQueryMapper extends QueryMapper { } protected final QueryMapper delegate; + protected final boolean closeDelegate; protected final InList inList; public static final String inListReplace = "{inList}"; private ListQueryMapper(Connection conn, String jndiName, Factory factory, QueryMapper delegate, ResultSetMapper cm, InList inList) { this.inList = inList.instance(conn); - this.delegate = delegate == null ? new QueryMapper(conn, jndiName, factory, cm) : + this.closeDelegate = delegate == null; + this.delegate = this.closeDelegate ? new QueryMapper(conn, jndiName, factory, cm) : (delegate instanceof ListQueryMapper ? ((ListQueryMapper)delegate).delegate : delegate); } + public ListQueryMapper(InList inList, QueryMapper delegate, boolean closeDelegate) { + this.delegate = delegate; + this.closeDelegate = closeDelegate; + this.inList = inList; + } + public ListQueryMapper(QueryMapper delegate, InList inList) { this(null, null, null, delegate, null, inList); } @@ -124,13 +132,20 @@ public class ListQueryMapper extends QueryMapper { this(factory, cm, defaultInList); } - // todo: get rid of wrap, cause, how do you know to close it or not? :'( - public static ListQueryMapper wrap(final QueryMapper qm){ - return qm instanceof ListQueryMapper ? (ListQueryMapper)qm : new ListQueryMapper(qm); + public static ListQueryMapper of(final Factory qmFactory, final InList inList) throws SQLException { + return new ListQueryMapper(inList, qmFactory.create(), true); } - public static ListQueryMapper wrap(final QueryMapper qm, final InList inList){ - return qm instanceof ListQueryMapper && ((ListQueryMapper)qm).inList == inList ? (ListQueryMapper)qm : new ListQueryMapper(qm, inList); + public static ListQueryMapper of(final QueryMapper qm, final InList inList) { + return new ListQueryMapper(inList, qm, false); + } + + public static ListQueryMapper of(final Factory qmFactory) throws SQLException { + return of(qmFactory, defaultInList); + } + + public static ListQueryMapper of(final QueryMapper qm) { + return of(qm, defaultInList); } public InList.InListObject inList(final String columnName, final Collection values) throws SQLException { @@ -218,7 +233,8 @@ public class ListQueryMapper extends QueryMapper { @Override public void close() { - delegate.close(); + if(closeDelegate) + delegate.close(); } // and these are standard @@ -312,7 +328,7 @@ public class ListQueryMapper extends QueryMapper { return delegate.insertGetGeneratedKeyType(prepareSql(sql, bindObjects), psf, typeReference, bindObjects); } -// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh + // DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh @Override public T toObject(PreparedStatement ps, Class componentType, final Object... bindObjects) throws SQLException {