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

com.kenshoo.jooq.SelectQueryExtender 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.collect.ImmutableList;
import com.kenshoo.pl.entity.*;
import com.kenshoo.pl.entity.UniqueKey;
import org.jooq.*;
import org.jooq.lambda.tuple.Tuple2;
import org.jooq.lambda.tuple.Tuple3;
import org.jooq.lambda.tuple.Tuple4;
import org.jooq.lambda.tuple.Tuple5;

import java.util.Collection;
import java.util.List;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * Provides a convenient (and MySQL-friendly) way of solving the use-case of querying records by N-tuples of identifiers.
 * The simplest case is querying by a list of IDs in which case it could be expressed with a simple IN. However in
 * terms of performance (and logs) it's better to do a join with a temporary table populated with those IDs instead.
 * This class does it automatically. In the more complicated case where the lookup is done by two fields (e.g. profile/affcode),
 * it is impossible to express it with an IN condition and a temp table has to be used in this case.
 *
 * Example of simple usage:
 * 
 * AdCriterias ac = AdCriterias.TABLE;
 * SelectConditionStep<Record2<String, Integer>> query = dslContext.select(ac.affcode, ac.criteria_id)
 *         .from(ac).where(ac.profile_id.eq(profileId));
 * try (QueryExtension<SelectConditionStep<Record2<String, Integer>>> queryExtension =
 *         SelectQueryExtender.of(query).withCondition(ac.affcode).in(affcodes)) {
 *     return queryExtension.getQuery().fetchMap(ac.affcode, ac.criteria_id);
 * }
 * 
* The returned object is a resource and it is crucial to place it inside try/finally block so it could be closed */ public class SelectQueryExtender { public static QueryExtension of(final DSLContext dslContext, Q query, List> conditions) { return new QueryExtensionImpl<>(dslContext, query, conditions); } public static BuilderWith of(final DSLContext dslContext, final Q query) { return new BuilderWith() { @Override public BuilderIn1 withCondition(final Field field) { return values -> new QueryExtensionImpl<>(dslContext, query, ImmutableList.of(new FieldAndValues<>(field, values))); } @Override public BuilderIn2 withCondition(final Field field1, final Field field2) { return values -> new QueryExtensionImpl<>(dslContext, query, ImmutableList.of( new FieldAndValues<>(field1, values.stream().map(Tuple2::v1).collect(toList())), new FieldAndValues<>(field2, values.stream().map(Tuple2::v2).collect(toList())) )); } @Override public BuilderIn3 withCondition(final Field field1, final Field field2, final Field field3) { return values -> new QueryExtensionImpl<>(dslContext, query, ImmutableList.of( new FieldAndValues<>(field1, values.stream().map(Tuple3::v1).collect(toList())), new FieldAndValues<>(field2, values.stream().map(Tuple3::v2).collect(toList())), new FieldAndValues<>(field3, values.stream().map(Tuple3::v3).collect(toList())) )); } @Override public BuilderIn4 withCondition(final Field field1, final Field field2, final Field field3, final Field field4) { return values -> new QueryExtensionImpl<>(dslContext, query, ImmutableList.of( new FieldAndValues<>(field1, values.stream().map(Tuple4::v1).collect(toList())), new FieldAndValues<>(field2, values.stream().map(Tuple4::v2).collect(toList())), new FieldAndValues<>(field3, values.stream().map(Tuple4::v3).collect(toList())), new FieldAndValues<>(field4, values.stream().map(Tuple4::v4).collect(toList())) )); } @Override public BuilderIn5 withCondition(final Field field1, final Field field2, final Field field3, final Field field4, final Field field5) { return values -> new QueryExtensionImpl<>(dslContext, query, ImmutableList.of( new FieldAndValues<>(field1, values.stream().map(Tuple5::v1).collect(toList())), new FieldAndValues<>(field2, values.stream().map(Tuple5::v2).collect(toList())), new FieldAndValues<>(field3, values.stream().map(Tuple5::v3).collect(toList())), new FieldAndValues<>(field4, values.stream().map(Tuple5::v4).collect(toList())), new FieldAndValues<>(field5, values.stream().map(Tuple5::v5).collect(toList())) )); } }; } public interface BuilderWith { BuilderIn1 withCondition(Field field); BuilderIn2 withCondition(Field field1, Field field2); BuilderIn3 withCondition(Field field1, Field field2, Field field3); BuilderIn4 withCondition(Field field1, Field field2, Field field3, Field field4); BuilderIn5 withCondition(Field field1, Field field2, Field field3, Field field4, Field field5); } public interface BuilderIn1 { QueryExtension in(Collection values); } public interface BuilderIn2 { QueryExtension in(Collection> values); } public interface BuilderIn3 { QueryExtension in(Collection> values); } public interface BuilderIn4 { QueryExtension in(Collection> values); } public interface BuilderIn5 { QueryExtension in(Collection> values); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy