org.babyfish.jimmer.sql.fetcher.impl.FetcherContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jimmer-sql Show documentation
Show all versions of jimmer-sql Show documentation
A revolutionary ORM framework for both java and kotlin
package org.babyfish.jimmer.sql.fetcher.impl;
import org.babyfish.jimmer.meta.*;
import org.babyfish.jimmer.runtime.DraftSpi;
import org.babyfish.jimmer.sql.fetcher.Fetcher;
import org.babyfish.jimmer.sql.fetcher.Field;
import org.babyfish.jimmer.sql.fetcher.RecursionStrategy;
import org.babyfish.jimmer.sql.runtime.JSqlClientImplementor;
import org.jetbrains.annotations.Nullable;
import java.sql.Connection;
import java.util.*;
import java.util.function.BiConsumer;
class FetcherContext {
private static final ThreadLocal FETCHER_CONTEXT_LOCAL = new ThreadLocal<>();
private JSqlClientImplementor sqlClient;
private Connection con;
private FetchingCache cache = new FetchingCache();
private Map taskMap = new LinkedHashMap<>();
public static void using(
JSqlClientImplementor sqlClient,
Connection con,
BiConsumer block
) {
FetcherContext ctx = FETCHER_CONTEXT_LOCAL.get();
if (ctx != null) {
block.accept(ctx, false);
} else {
ctx = new FetcherContext(sqlClient, con);
FETCHER_CONTEXT_LOCAL.set(ctx);
try {
block.accept(ctx, true);
} finally {
FETCHER_CONTEXT_LOCAL.remove();
}
}
}
private FetcherContext(JSqlClientImplementor sqlClient, Connection con) {
this.sqlClient = sqlClient;
this.con = con;
}
@SuppressWarnings("unchecked")
private void add(FetchPath path, Fetcher> fetcher, DraftSpi draft) {
FetcherImplementor> fetcherImplementor = (FetcherImplementor>) fetcher;
setVisibility(draft, (FetcherImplementor>) fetcher);
for (Field field : fetcherImplementor.__unresolvedFieldMap().values()) {
if (!field.isSimpleField() ||
(!field.isRawId() &&
sqlClient.getFilters().getFilter(field.getProp().getTargetType()) != null)
) {
RecursionStrategy> recursionStrategy = field.getRecursionStrategy();
if (recursionStrategy != null &&
!((RecursionStrategy