Fix ListQueryMapper

This commit is contained in:
Travis Burtrum 2018-10-11 00:33:52 -04:00
parent 1a161d7214
commit a378e640ae
1 changed files with 24 additions and 8 deletions

View File

@ -58,16 +58,24 @@ public class ListQueryMapper extends QueryMapper {
} }
protected final QueryMapper delegate; protected final QueryMapper delegate;
protected final boolean closeDelegate;
protected final InList inList; protected final InList inList;
public static final String inListReplace = "{inList}"; public static final String inListReplace = "{inList}";
private ListQueryMapper(Connection conn, String jndiName, Factory<Connection> factory, QueryMapper delegate, ResultSetMapper cm, InList inList) { private ListQueryMapper(Connection conn, String jndiName, Factory<Connection> factory, QueryMapper delegate, ResultSetMapper cm, InList inList) {
this.inList = inList.instance(conn); 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); (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) { public ListQueryMapper(QueryMapper delegate, InList inList) {
this(null, null, null, delegate, null, inList); this(null, null, null, delegate, null, inList);
} }
@ -124,13 +132,20 @@ public class ListQueryMapper extends QueryMapper {
this(factory, cm, defaultInList); this(factory, cm, defaultInList);
} }
// todo: get rid of wrap, cause, how do you know to close it or not? :'( public static ListQueryMapper of(final Factory<QueryMapper> qmFactory, final InList inList) throws SQLException {
public static ListQueryMapper wrap(final QueryMapper qm){ return new ListQueryMapper(inList, qmFactory.create(), true);
return qm instanceof ListQueryMapper ? (ListQueryMapper)qm : new ListQueryMapper(qm);
} }
public static ListQueryMapper wrap(final QueryMapper qm, final InList inList){ public static ListQueryMapper of(final QueryMapper qm, final InList inList) {
return qm instanceof ListQueryMapper && ((ListQueryMapper)qm).inList == inList ? (ListQueryMapper)qm : new ListQueryMapper(qm, inList); return new ListQueryMapper(inList, qm, false);
}
public static ListQueryMapper of(final Factory<QueryMapper> qmFactory) throws SQLException {
return of(qmFactory, defaultInList);
}
public static ListQueryMapper of(final QueryMapper qm) {
return of(qm, defaultInList);
} }
public <T> InList.InListObject inList(final String columnName, final Collection<T> values) throws SQLException { public <T> InList.InListObject inList(final String columnName, final Collection<T> values) throws SQLException {
@ -218,6 +233,7 @@ public class ListQueryMapper extends QueryMapper {
@Override @Override
public void close() { public void close() {
if(closeDelegate)
delegate.close(); delegate.close();
} }
@ -312,7 +328,7 @@ public class ListQueryMapper extends QueryMapper {
return delegate.insertGetGeneratedKeyType(prepareSql(sql, bindObjects), psf, typeReference, bindObjects); 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 @Override
public <T> T toObject(PreparedStatement ps, Class<T> componentType, final Object... bindObjects) throws SQLException { public <T> T toObject(PreparedStatement ps, Class<T> componentType, final Object... bindObjects) throws SQLException {