This commit is contained in:
juan.marchionatto 2023-11-15 10:42:59 -05:00
parent 0bbedb8f41
commit 1139c1e5a2
1 changed files with 11 additions and 20 deletions

View File

@ -5,7 +5,6 @@ import com.google.common.collect.Iterators;
import com.google.common.collect.UnmodifiableIterator;
import org.hibernate.dialect.Oracle12cDialect;
import java.security.InvalidParameterException;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
@ -19,12 +18,10 @@ public class SqlQueryUtil {
private final HibernatePropertiesProvider myHibernatePropertiesProvider;
public SqlQueryUtil(HibernatePropertiesProvider theHibernatePropertiesProvider) {
myHibernatePropertiesProvider = theHibernatePropertiesProvider;
}
/**
* Intended to partition 'in' clause parameter lists for Oracle, which doesn't accept more than 1,000 parameters
*
@ -37,58 +34,52 @@ public class SqlQueryUtil {
* or x.getid in (7) )
*/
public String buildInList(String theInClause, Collection<Long> theInElements, int maxListSize) {
if (theInElements.isEmpty()) {
if (theInElements.isEmpty()) {
return " 1=2 -- replaced empty 'in' parameter list: " + theInClause + " in () " + NL;
}
StringBuilder sb = new StringBuilder();
UnmodifiableIterator<List<Long>> iter = Iterators.partition(theInElements.iterator(), maxListSize);
while(iter.hasNext()) {
while (iter.hasNext()) {
List<Long> subList = iter.next();
sb.append( sb.length() == 0 ? " ( " : " or " )
.append(theInClause)
.append(" in (")
.append( getCsvString(subList) )
.append(")");
sb.append(sb.length() == 0 ? " ( " : " or ")
.append(theInClause)
.append(" in (")
.append(getCsvString(subList))
.append(")");
}
sb.append(" ) ");
return sb.toString();
}
/**
* Returns a split 'in' clause only if the data source is Oracle and there are more than 1,000 parameters
* and a not split clause otherwise.
*/
public <T> String buildInListIfNeeded(String theInClause, Collection<Long> theInElements) {
if (theInElements.isEmpty()) {
if (theInElements.isEmpty()) {
return " 1=2 -- replaced empty 'in' parameter list: " + theInClause + " in () " + NL;
}
if ( ! isOracleDialect() || theInElements.size() <= ORACLE_MAX_IN_PARAM_ENTRIES) {
return " " + theInClause + " in (" + getCsvString(theInElements) + ") ";
if (!isOracleDialect() || theInElements.size() <= ORACLE_MAX_IN_PARAM_ENTRIES) {
return " " + theInClause + " in (" + getCsvString(theInElements) + ") ";
}
return buildInList(theInClause, theInElements, ORACLE_MAX_IN_PARAM_ENTRIES);
}
/**
* Return input collection elements as a CSV string
*/
private String getCsvString(Collection<Long> theInElements) {
return theInElements.stream()
.map(String::valueOf)
.collect(Collectors.joining(", "));
return theInElements.stream().map(String::valueOf).collect(Collectors.joining(", "));
}
private boolean isOracleDialect() {
return myHibernatePropertiesProvider.getDialect().getClass().equals(Oracle12cDialect.class);
}
private static final String NL = System.getProperty("line.separator");
}