Skip to content
Snippets Groups Projects
Commit f2c5f991 authored by Jefferson Quesado's avatar Jefferson Quesado
Browse files

Fazendo usar o recurso no `PstmtNullSafe`

A ideia é corrigir o comportamento do retorno do `executeBatch`
usando esses recursos
parent 0f55decd
No related branches found
No related tags found
1 merge request!3Usando ResourcePool
Pipeline #195500405 passed
......@@ -25,17 +25,19 @@ import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import br.com.softsite.pstmtnullsafe.SQLRuntimeException;
import br.com.softsite.pstmtnullsafe.function.SQLConsumer;
import br.com.softsite.pstmtnullsafe.function.SQLFunction;
import br.com.softsite.pstmtnullsafe.function.SQLTriConsumer;
import br.com.softsite.pstmtnullsafe.resource.ResourceWrapper;
import br.com.softsite.pstmtnullsafe.resource.SingletonPool;
import br.com.softsite.toolbox.indirection.IntIndirection;
public class PstmtNullSafe implements PreparedStatement {
......@@ -44,14 +46,12 @@ public class PstmtNullSafe implements PreparedStatement {
private final String rawQuery;
private final PstmtNullSafeConnection conn;
private final SQLFunction<String, PreparedStatement> ultimatePreparer;
private final List<String> parts;
private final int questCount;
private final PreparedStatement allNonNullQuery;
private final SingletonPool<Set<Integer>, PreparedStatement> poolPstmt;
private PreparedStatement lastQuery;
private HashMap<Set<Integer>, PreparedStatement> allQueries = new HashMap<>();
private HashMap<Set<Integer>, Integer> batchCount = new HashMap<>();
private HashMap<String, SQLConsumer<PreparedStatement>> lazyPreparer = new HashMap<>();
private HashMap<Integer, Object> lazySettable = new HashMap<>();
private boolean closed = false;
......@@ -68,13 +68,12 @@ public class PstmtNullSafe implements PreparedStatement {
private PstmtNullSafe(String rawQuery, List<String> parts, SQLFunction<String, PreparedStatement> ultimatePreparer, PstmtNullSafeConnection conn) throws SQLException {
this.conn = conn;
this.rawQuery = rawQuery;
this.ultimatePreparer = ultimatePreparer;
this.parts = Collections.unmodifiableList(new ArrayList<>(parts));
this.questCount = parts.size() - 1;
allNonNullQuery = ultimatePreparer.apply(rawQuery);
addQuery(allNonNullQuery, new HashSet<>());
poolPstmt = new SingletonPool<>(ultimatePreparer.toFunction().compose(this::createQueryString));
allNonNullQuery = poolPstmt.getResource(Collections.emptySet()).getWrapped();
}
private String createQueryString(Set<Integer> nulos) {
......@@ -99,21 +98,8 @@ public class PstmtNullSafe implements PreparedStatement {
private void addQueryIfNotExists() throws SQLException {
Set<Integer> nulos = getNulosId();
PreparedStatement pstmt = allQueries.get(nulos);
if (pstmt != null) {
setLastQuery(pstmt);
return;
}
addQuery(createQueryString(nulos), nulos);
}
private void addQuery(String rawQuery, Set<Integer> nulos) throws SQLException {
addQuery(ultimatePreparer.apply(rawQuery), nulos);
}
private void addQuery(PreparedStatement query, Set<Integer> nulos) {
allQueries.put(Collections.unmodifiableSet(nulos), query);
setLastQuery(query);
PreparedStatement pstmt = poolPstmt.getResource(nulos).getWrapped();
setLastQuery(pstmt);
}
private void setLastQuery(PreparedStatement query) {
......@@ -179,7 +165,7 @@ public class PstmtNullSafe implements PreparedStatement {
@Override
public void close() throws SQLException {
allRun(PreparedStatement::close);
allRun(PreparedStatement::close, poolPstmt.getAllResources().stream().map(ResourceWrapper::getWrapped));
closed = true;
}
......@@ -301,23 +287,16 @@ public class PstmtNullSafe implements PreparedStatement {
@Override
public int[] executeBatch() throws SQLException {
try {
return allQueries.entrySet().stream()
.filter(es -> hasBatch(es.getKey()))
.map(es -> es.getValue())
return poolPstmt.getAllocatedResources().stream()
.map(ResourceWrapper::getWrapped)
.map(SQLFunction.staticToFunction(PreparedStatement::executeBatch))
.flatMapToInt(batchResult -> IntStream.of(batchResult))
.toArray();
} finally {
batchCount.clear();
poolPstmt.releaseAll();
}
}
private boolean hasBatch(Set<Integer> conteudo) {
Integer thisBatch = batchCount.get(conteudo);
return thisBatch != null && thisBatch > 0;
}
@Override
public Connection getConnection() throws SQLException {
return conn;
......@@ -548,7 +527,6 @@ public class PstmtNullSafe implements PreparedStatement {
@Override
public void addBatch() throws SQLException {
addQueryIfNotExists();
batchCount.compute(getNulosId(), (k, v) -> (v == null)? 1: v + 1);
lastQuery.addBatch();
}
......@@ -717,8 +695,13 @@ public class PstmtNullSafe implements PreparedStatement {
}
private void allRun(SQLConsumer<PreparedStatement> consumer) throws SQLException {
allRun(consumer, poolPstmt.getAllocatedResources().stream().map(ResourceWrapper::getWrapped));
}
private void allRun(SQLConsumer<PreparedStatement> consumer, Stream<PreparedStatement> stmts) throws SQLException {
SQLException sqle = null;
for (PreparedStatement pstmt: allQueries.values()) {
Iterable<PreparedStatement> it = () -> stmts.iterator();
for (PreparedStatement pstmt: it) {
try {
consumer.accept(pstmt);
} catch (SQLException e) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment