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 java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
import java.util.*;
//IFJAVA8_START
import java.util.stream.Stream;
@ -96,7 +93,7 @@ public class CachingQueryMapper extends QueryMapper {
}
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 {
@ -110,6 +107,17 @@ public class CachingQueryMapper extends QueryMapper {
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) {
//System.out.println("cache size: "+cache.size());
for (PreparedStatement ps : cache.values())
@ -139,7 +147,17 @@ public class CachingQueryMapper extends QueryMapper {
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
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);
}
// 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
public int updateRows(final UpdateableDTO dto) throws SQLException {
@ -282,7 +292,17 @@ public class ListQueryMapper extends QueryMapper {
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
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;
}
@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
public int executeUpdate(String sql, Object... bindObjects) {
try {
@ -124,7 +144,27 @@ public class NullQueryMapper extends QueryMapper {
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
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.JdbcMapperFactory;
import com.moparisthebest.jdbc.util.ResultSetIterable;
import com.moparisthebest.jdbc.util.ResultSetUtil;
import java.io.*;
import java.nio.charset.Charset;
@ -265,11 +266,11 @@ public class QueryMapper implements JdbcMapper {
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);
}
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();
}
@ -283,6 +284,32 @@ public class QueryMapper implements JdbcMapper {
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 {
PreparedStatement ps = null;
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
public int updateRows(UpdateableDTO dto) throws SQLException {