All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.kenshoo.jooq.IdsListImpl Maven / Gradle / Ivy

Go to download

A Java persistence layer based on JOOQ for high performance and business flow support.

There is a newer version: 0.1.121-jooq-3.16.3
Show newest version
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 > 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;
            return query.join(idsTempTable).on(idsTempTable.id.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 > 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