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;
@ -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,6 +147,16 @@ public class CachingQueryMapper extends QueryMapper {
return super.executeUpdateSuccess(getPreparedStatement(sql), bindObjects); return super.executeUpdateSuccess(getPreparedStatement(sql), bindObjects);
} }
@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 // these grab ResultSets from the database
@Override @Override

View File

@ -153,6 +153,16 @@ public class ListQueryMapper extends QueryMapper {
return delegate.executeUpdateSuccess(ps, bindObjects); return delegate.executeUpdateSuccess(ps, bindObjects);
} }
@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 // these update the database using UpdateableDTOs
@Override @Override
@ -282,6 +292,16 @@ public class ListQueryMapper extends QueryMapper {
return delegate.toResultSet(prepareSql(sql, bindObjects), bindObjects); return delegate.toResultSet(prepareSql(sql, bindObjects), bindObjects);
} }
@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 // DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh
@Override @Override

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,6 +144,26 @@ public class NullQueryMapper extends QueryMapper {
return false; return false;
} }
@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 // these update the database using UpdateableDTOs
@Override @Override

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 {