Add QueryMapper methods insertGetGeneratedKey and insertGetGeneratedKeyType

This commit is contained in:
Travis Burtrum 2018-09-21 00:35:20 -04:00
parent 185b4eb501
commit 10b1806b26
4 changed files with 136 additions and 11 deletions

View File

@ -2,10 +2,7 @@ package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.util.ResultSetIterable; import com.moparisthebest.jdbc.util.ResultSetIterable;
import java.sql.Connection; import java.sql.*;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*; import java.util.*;
//IFJAVA8_START //IFJAVA8_START
import java.util.stream.Stream; import java.util.stream.Stream;
@ -96,7 +93,7 @@ public class CachingQueryMapper extends QueryMapper {
} }
protected PreparedStatement getPreparedStatement(String sql) throws SQLException { protected PreparedStatement getPreparedStatement(String sql) throws SQLException {
return getPreparedStatement(sql, ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY); return getPreparedStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
} }
protected PreparedStatement getPreparedStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { protected PreparedStatement getPreparedStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
@ -110,6 +107,17 @@ public class CachingQueryMapper extends QueryMapper {
return ps; return ps;
} }
protected PreparedStatement getInsertPreparedStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
PreparedStatement ps = cache.get(sql);
if (ps == null) {
//System.out.println("cache miss");
ps = conn.prepareStatement(sql, autoGeneratedKeys);
cache.put(sql, ps);
}
//else System.out.println("cache hit");
return ps;
}
public void clearCache(boolean close) { public void clearCache(boolean close) {
//System.out.println("cache size: "+cache.size()); //System.out.println("cache size: "+cache.size());
for (PreparedStatement ps : cache.values()) for (PreparedStatement ps : cache.values())
@ -139,7 +147,17 @@ public class CachingQueryMapper extends QueryMapper {
return super.executeUpdateSuccess(getPreparedStatement(sql), bindObjects); return super.executeUpdateSuccess(getPreparedStatement(sql), bindObjects);
} }
// these grab ResultSets from the database @Override
public Long insertGetGeneratedKey(String sql, Object... bindObjects) throws SQLException {
return super.insertGetGeneratedKey(getInsertPreparedStatement(sql, Statement.RETURN_GENERATED_KEYS), bindObjects);
}
@Override
public <T> T insertGetGeneratedKeyType(String sql, TypeReference<T> typeReference, Object... bindObjects) throws SQLException {
return super.insertGetGeneratedKeyType(getInsertPreparedStatement(sql, Statement.RETURN_GENERATED_KEYS), typeReference, bindObjects);
}
// these grab ResultSets from the database
@Override @Override
public ResultSet toResultSet(String sql, Object... bindObjects) throws SQLException { public ResultSet toResultSet(String sql, Object... bindObjects) throws SQLException {

View File

@ -153,7 +153,17 @@ public class ListQueryMapper extends QueryMapper {
return delegate.executeUpdateSuccess(ps, bindObjects); return delegate.executeUpdateSuccess(ps, bindObjects);
} }
// these update the database using UpdateableDTOs @Override
public Long insertGetGeneratedKey(PreparedStatement ps, Object... bindObjects) throws SQLException {
return delegate.insertGetGeneratedKey(ps, bindObjects);
}
@Override
public <T> T insertGetGeneratedKeyType(PreparedStatement ps, TypeReference<T> typeReference, Object... bindObjects) throws SQLException {
return delegate.insertGetGeneratedKeyType(ps, typeReference, bindObjects);
}
// these update the database using UpdateableDTOs
@Override @Override
public int updateRows(final UpdateableDTO dto) throws SQLException { public int updateRows(final UpdateableDTO dto) throws SQLException {
@ -282,7 +292,17 @@ public class ListQueryMapper extends QueryMapper {
return delegate.toResultSet(prepareSql(sql, bindObjects), bindObjects); return delegate.toResultSet(prepareSql(sql, bindObjects), bindObjects);
} }
// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh @Override
public Long insertGetGeneratedKey(String sql, Object... bindObjects) throws SQLException {
return delegate.insertGetGeneratedKey(prepareSql(sql, bindObjects), bindObjects);
}
@Override
public <T> T insertGetGeneratedKeyType(String sql, TypeReference<T> typeReference, Object... bindObjects) throws SQLException {
return delegate.insertGetGeneratedKeyType(prepareSql(sql, bindObjects), typeReference, bindObjects);
}
// 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 {

View File

@ -104,6 +104,26 @@ public class NullQueryMapper extends QueryMapper {
return false; return false;
} }
@Override
public Long insertGetGeneratedKey(PreparedStatement ps, Object... bindObjects) throws SQLException {
try {
return delegate.insertGetGeneratedKey(ps, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> T insertGetGeneratedKeyType(PreparedStatement ps, TypeReference<T> typeReference, Object... bindObjects) throws SQLException {
try {
return delegate.insertGetGeneratedKeyType(ps, typeReference, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override @Override
public int executeUpdate(String sql, Object... bindObjects) { public int executeUpdate(String sql, Object... bindObjects) {
try { try {
@ -124,7 +144,27 @@ public class NullQueryMapper extends QueryMapper {
return false; return false;
} }
// these update the database using UpdateableDTOs @Override
public Long insertGetGeneratedKey(String sql, Object... bindObjects) throws SQLException {
try {
return delegate.insertGetGeneratedKey(sql, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> T insertGetGeneratedKeyType(String sql, TypeReference<T> typeReference, Object... bindObjects) throws SQLException {
try {
return delegate.insertGetGeneratedKeyType(sql, typeReference, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
// these update the database using UpdateableDTOs
@Override @Override
public int updateRows(UpdateableDTO dto) { public int updateRows(UpdateableDTO dto) {

View File

@ -3,6 +3,7 @@ package com.moparisthebest.jdbc;
import com.moparisthebest.jdbc.codegen.JdbcMapper; import com.moparisthebest.jdbc.codegen.JdbcMapper;
import com.moparisthebest.jdbc.codegen.JdbcMapperFactory; import com.moparisthebest.jdbc.codegen.JdbcMapperFactory;
import com.moparisthebest.jdbc.util.ResultSetIterable; import com.moparisthebest.jdbc.util.ResultSetIterable;
import com.moparisthebest.jdbc.util.ResultSetUtil;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -265,11 +266,11 @@ public class QueryMapper implements JdbcMapper {
return ps; return ps;
} }
protected PreparedStatement bind(final PreparedStatement ps, final Object... bindObjects) throws SQLException { protected static PreparedStatement bind(final PreparedStatement ps, final Object... bindObjects) throws SQLException {
return bindStatement(ps, bindObjects); return bindStatement(ps, bindObjects);
} }
protected ResultSet bindExecute(final PreparedStatement ps, final Object... bindObjects) throws SQLException { protected static ResultSet bindExecute(final PreparedStatement ps, final Object... bindObjects) throws SQLException {
return bind(ps, bindObjects).executeQuery(); return bind(ps, bindObjects).executeQuery();
} }
@ -283,6 +284,32 @@ public class QueryMapper implements JdbcMapper {
return this.executeUpdate(ps, bindObjects) >= 0; return this.executeUpdate(ps, bindObjects) >= 0;
} }
public Long insertGetGeneratedKey(final PreparedStatement ps, final Object... bindObjects) throws SQLException {
this.executeUpdate(ps, bindObjects);
ResultSet rs = null;
try {
rs = ps.getGeneratedKeys();
if(rs.next())
return ResultSetUtil.getObjectLong(rs, 1);
} finally {
tryClose(rs);
}
return null;
}
public <T> T insertGetGeneratedKeyType(final PreparedStatement ps, final TypeReference<T> typeReference, final Object... bindObjects) throws SQLException {
this.executeUpdate(ps, bindObjects);
ResultSet rs = null;
try {
rs = ps.getGeneratedKeys();
if(rs.next())
return cm.toType(rs, typeReference);
} finally {
tryClose(rs);
}
return null;
}
public int executeUpdate(String sql, final Object... bindObjects) throws SQLException { public int executeUpdate(String sql, final Object... bindObjects) throws SQLException {
PreparedStatement ps = null; PreparedStatement ps = null;
try { try {
@ -303,6 +330,26 @@ public class QueryMapper implements JdbcMapper {
} }
} }
public Long insertGetGeneratedKey(final String sql, final Object... bindObjects) throws SQLException {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
return this.insertGetGeneratedKey(ps, bindObjects);
} finally {
tryClose(ps);
}
}
public <T> T insertGetGeneratedKeyType(final String sql, final TypeReference<T> typeReference, final Object... bindObjects) throws SQLException {
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
return this.insertGetGeneratedKeyType(ps, typeReference, bindObjects);
} finally {
tryClose(ps);
}
}
// these update the database using UpdateableDTOs // these update the database using UpdateableDTOs
public int updateRows(UpdateableDTO dto) throws SQLException { public int updateRows(UpdateableDTO dto) throws SQLException {