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

org.babyfish.jimmer.sql.fetcher.impl.FieldConfigImpl Maven / Gradle / Ivy

The newest version!
package org.babyfish.jimmer.sql.fetcher.impl;

import org.babyfish.jimmer.meta.EmbeddedLevel;
import org.babyfish.jimmer.meta.ImmutableProp;
import org.babyfish.jimmer.meta.TargetLevel;
import org.babyfish.jimmer.sql.ast.table.Table;
import org.babyfish.jimmer.sql.fetcher.Fetcher;
import org.babyfish.jimmer.sql.fetcher.FieldFilter;
import org.babyfish.jimmer.sql.fetcher.RecursionStrategy;
import org.babyfish.jimmer.sql.fetcher.RecursiveListFieldConfig;

class FieldConfigImpl> implements RecursiveListFieldConfig {

    private ImmutableProp prop;

    private FetcherImpl childFetcher;

    private FieldFilter filter;

    private int batchSize;

    private int limit = Integer.MAX_VALUE;

    private int offset = 0;

    private RecursionStrategy recursionStrategy;

    FieldConfigImpl(ImmutableProp prop, FetcherImpl childFetcher) {
        if (childFetcher != null && !prop.isAssociation(TargetLevel.ENTITY) && !prop.isEmbedded(EmbeddedLevel.BOTH)) {
            throw new IllegalArgumentException(
                    "Child fetcher cannot be specified because'" +
                            prop +
                            "' is neither entity association nor embeddable"
            );
        }
        this.prop = prop;
        this.childFetcher = childFetcher;
    }

    @Override
    public RecursiveListFieldConfig filter(FieldFilter filter) {
        if (filter != null && prop.isReference(TargetLevel.PERSISTENT) && !prop.isNullable()) {
            throw new IllegalArgumentException(
                    "Cannot set filter for non-null one-to-one/many-to-one property \"" + prop + "\""
            );
        }
        this.filter = filter;
        return this;
    }

    @Override
    public RecursiveListFieldConfig batch(int size) {
        if (size < 0) {
            throw new IllegalArgumentException("batchSize cannot be less than 0");
        }
        batchSize = size;
        return this;
    }

    @Override
    public RecursiveListFieldConfig limit(int limit, int offset) {
        if (!prop.isReferenceList(TargetLevel.PERSISTENT)) {
            throw new IllegalArgumentException(
                    "Cannot set limit because current property \"" +
                            prop +
                            "\" is not list property"
            );
        }
        if (limit < 0) {
            throw new IllegalArgumentException("'limit' can not be less than 0");
        }
        if (offset < 0) {
            throw new IllegalArgumentException("'offset' can not be less than 0");
        }
        if (limit > Integer.MAX_VALUE - offset) {
            throw new IllegalArgumentException("'limit' > Integer.MAX_VALUE - offset");
        }
        this.limit = limit;
        this.offset = offset;
        return this;
    }

    @Override
    public RecursiveListFieldConfig depth(int depth) {
        return recursive(DefaultRecursionStrategy.of(depth));
    }

    @Override
    public RecursiveListFieldConfig recursive(RecursionStrategy strategy) {
        if (!prop.getDeclaringType().getJavaClass().isAssignableFrom(prop.getTargetType().getJavaClass())) {
            throw new IllegalArgumentException(
                    "Cannot set recursive strategy because current property \"" +
                            prop +
                            "\" is not recursive property"
            );
        }
        this.recursionStrategy = strategy;
        return this;
    }

    ImmutableProp getProp() {
        return prop;
    }

    FetcherImpl getChildFetcher() {
        return childFetcher;
    }

    FieldFilter getFilter() {
        return filter;
    }

    int getBatchSize() {
        return batchSize;
    }

    int getLimit() {
        return limit;
    }

    int getOffset() {
        return offset;
    }

    RecursionStrategy getRecursionStrategy() {
        return recursionStrategy;
    }

    void setRecursiveTarget(FetcherImpl fetcher) {
        if (this.childFetcher != null) {
            throw new IllegalStateException("childFetcher has already been set");
        }
        if (!prop.isAssociation(TargetLevel.ENTITY) ||
                !prop.getDeclaringType().isEntity() ||
                !prop.getDeclaringType().isAssignableFrom(prop.getTargetType())
        ) {
            throw new IllegalStateException("current property is not recursive");
        }
        this.childFetcher = fetcher;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy