com.kenshoo.jooq.IdsListImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
package com.kenshoo.jooq;
import com.google.common.base.Preconditions;
import org.jooq.BatchBindStep;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.SelectConnectByStep;
import org.jooq.SelectJoinStep;
import org.jooq.SelectWhereStep;
import org.jooq.UpdateConditionStep;
import org.jooq.UpdateWhereStep;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* An abstraction of a list of IDs designed to hide the decision whether IN clause or in-memory temporary
* table should be used. Because of that it is very important to use this class as a resource,
* in a try-with-resources statement.
*/
abstract class IdsListImpl implements IdsList {
private final DSLContext dslContext;
private final List ids = new ArrayList<>();
private final IdsTempTable idsTempTable;
private TempTableResource tempTableResource;
public IdsListImpl(DSLContext dslContext, IdsTempTable idsTempTable) {
this.dslContext = dslContext;
this.idsTempTable = idsTempTable;
}
public IdsListImpl(DSLContext dslContext, IdsTempTable idsTempTable, Collection ids) {
this(dslContext, idsTempTable);
this.ids.addAll(ids);
}
@Override
public void add(T id) {
ids.add(id);
}
@Override
public void addAll(Collection ids) {
this.ids.addAll(ids);
}
@Override
public boolean isEmpty() {
return ids.isEmpty();
}
@Override
public Iterator iterator() {
return ids.iterator();
}
@Override
public , FT> S imposeOnQuery(S query, Field idField) {
//noinspection unchecked
return (S) _imposeOnQuery(query, idField);
}
private Object _imposeOnQuery(Object object, Field idField) {
if (shouldUseTempTable()) {
Preconditions.checkArgument(object instanceof SelectJoinStep, "Expected " + SelectJoinStep.class.getName() + " but got " + object.getClass().getName());
populateTempTable();
//noinspection ConstantConditions,unchecked
SelectJoinStep query = (SelectJoinStep) object;
// The idField parameter supports a different type from for the case when we join INT temp ids with BIGINT idField
// The cast below in this case is actually a no-op in jOOQ (SQL is not changed) but it makes the compiler happy
return query.join(idsTempTable).on(idsTempTable.id.cast(idField.getDataType()).eq(idField));
} else {
Preconditions.checkArgument(object instanceof SelectWhereStep, "Expected " + SelectWhereStep.class.getName() + " but got " + object.getClass().getName());
//noinspection unchecked,ConstantConditions
SelectWhereStep query = (SelectWhereStep) object;
return query.where(idField.in(ids));
}
}
@Override
public , FT> UpdateConditionStep imposeOnUpdate(S update, Field idField) {
return update.where(idField.in(ids));
}
private void populateTempTable() {
if (tempTableResource != null) {
return;
}
tempTableResource = TempTableHelper.tempInMemoryTable(dslContext, idsTempTable, new TablePopulator() {
@Override
public void populate(BatchBindStep batchBindStep) {
for (T id : ids) {
batchBindStep.bind(id);
}
}
});
}
@Override
public void close() {
if (tempTableResource != null) {
tempTableResource.close();
}
}
private boolean shouldUseTempTable() {
return ids.size() > 10;
}
public List getIds() {
return ids;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy