org.babyfish.jimmer.sql.ast.impl.ExampleImpl 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
The newest version!
package org.babyfish.jimmer.sql.ast.impl;
import org.babyfish.jimmer.lang.NewChain;
import org.babyfish.jimmer.meta.*;
import org.babyfish.jimmer.runtime.ImmutableSpi;
import org.babyfish.jimmer.sql.JoinType;
import org.babyfish.jimmer.sql.ast.Expression;
import org.babyfish.jimmer.sql.ast.LikeMode;
import org.babyfish.jimmer.sql.ast.Predicate;
import org.babyfish.jimmer.sql.ast.StringExpression;
import org.babyfish.jimmer.sql.ast.impl.query.MutableRootQueryImpl;
import org.babyfish.jimmer.sql.ast.query.Example;
import org.babyfish.jimmer.sql.ast.table.Table;
import org.jetbrains.annotations.Nullable;
import java.util.*;
public class ExampleImpl implements Example {
private final static Predicate[] EMPTY_PREDICATES = new Predicate[0];
private final ImmutableSpi spi;
private final MatchMode matchMode;
private final LikeMode likeMode;
private final boolean trim;
private final Map propDataMap;
public ExampleImpl(E obj) {
if (!(obj instanceof ImmutableSpi)) {
throw new IllegalArgumentException("example must be immutable instance");
}
this.spi = (ImmutableSpi) obj;
this.matchMode = MatchMode.NOT_EMPTY;
this.likeMode = LikeMode.EXACT;
this.trim = false;
this.propDataMap = new HashMap<>();
}
private ExampleImpl(ExampleImpl base, PropData data) {
Map newPropDataMap = new HashMap<>(base.propDataMap);
newPropDataMap.put(data.prop, data);
this.spi = base.spi;
this.matchMode = base.matchMode;
this.likeMode = base.likeMode;
this.trim = base.trim;
this.propDataMap = newPropDataMap;
}
private ExampleImpl(ExampleImpl base, MatchMode matchMode) {
this.spi = base.spi;
this.matchMode = matchMode;
this.likeMode = base.likeMode;
this.trim = base.trim;
this.propDataMap = base.propDataMap;
}
private ExampleImpl(ExampleImpl base, boolean trim) {
this.spi = base.spi;
this.matchMode = base.matchMode;
this.likeMode = base.likeMode;
this.trim = trim;
this.propDataMap = base.propDataMap;
}
@Override
public Example match(MatchMode mode) {
if (this.matchMode == mode) {
return this;
}
return new ExampleImpl<>(this, mode);
}
@Override
public Example match(TypedProp prop, MatchMode matchMode) {
PropData data = propDataMap.get(prop.unwrap());
if (data != null && data.matchMode == matchMode) {
return this;
}
data = new PropData(
prop.unwrap(),
matchMode,
data != null && data.trim,
data != null && data.ignoreZero,
data != null && data.likeInsensitive,
data != null ? data.likeMode : LikeMode.EXACT
);
return new ExampleImpl<>(this, data);
}
@Override
public Example trim() {
if (this.trim) {
return this;
}
return new ExampleImpl<>(this, true);
}
@Override
public Example trim(TypedProp.Scalar prop) {
PropData data = propDataMap.get(prop.unwrap());
if (data != null && data.trim) {
return this;
}
data = new PropData(
prop.unwrap(),
data != null ? data.matchMode : null,
true,
data != null && data.ignoreZero,
data != null && data.likeInsensitive,
data != null ? data.likeMode : LikeMode.EXACT
);
return new ExampleImpl<>(this, data);
}
@Override
public Example ignoreZero(TypedProp.Scalar prop) {
PropData data = propDataMap.get(prop.unwrap());
if (data != null && data.ignoreZero) {
return this;
}
data = new PropData(
prop.unwrap(),
data != null ? data.matchMode : null,
data != null && data.trim,
true,
data != null && data.likeInsensitive,
data != null ? data.likeMode : LikeMode.EXACT
);
return new ExampleImpl<>(this, data);
}
@NewChain
@Override
public ExampleImpl like(TypedProp.Scalar prop, LikeMode likeMode) {
PropData data = propDataMap.get(prop.unwrap());
if (data != null && !data.likeInsensitive && data.likeMode == likeMode) {
return this;
}
data = new PropData(
prop.unwrap(),
data != null ? data.matchMode : null,
data != null && data.trim,
data != null && data.ignoreZero,
false,
likeMode
);
return new ExampleImpl<>(this, data);
}
@NewChain
@Override
public ExampleImpl ilike(TypedProp.Scalar prop, LikeMode likeMode) {
PropData data = propDataMap.get(prop.unwrap());
if (data != null && data.likeInsensitive && data.likeMode == likeMode) {
return this;
}
data = new PropData(
prop.unwrap(),
data != null ? data.matchMode : null,
data != null && data.trim,
data != null && data.ignoreZero,
true,
likeMode
);
return new ExampleImpl<>(this, data);
}
ImmutableType type() {
return spi.__type();
}
public Predicate toPredicate(Table> table) {
List predicates = new ArrayList<>();
for (ImmutableProp prop : spi.__type().getProps().values()) {
if (spi.__isLoaded(prop.getId()) && prop.isColumnDefinition()) {
Object value = spi.__get(prop.getId());
if (value != null && prop.isReference(TargetLevel.PERSISTENT)) {
PropId targetIdPropId = prop.getTargetType().getIdProp().getId();
if (!((ImmutableSpi)value).__isLoaded(targetIdPropId)) {
continue;
}
value = ((ImmutableSpi)value).__get(targetIdPropId);
}
Expression