org.yop.orm.evaluation.IdIn Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of orm Show documentation
Show all versions of orm Show documentation
YOP-orm is a lightweight ORM wannabe. Hit and run, no session, no bytecode generation, SQL-like syntax.
Strong constraints on the Database schema.
Java 8 is required.
Ω≡{Ⓐ}
The newest version!
package org.yop.orm.evaluation;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import org.yop.orm.model.Yopable;
import org.yop.orm.query.Context;
import org.yop.orm.sql.Config;
import org.yop.orm.sql.SQLPart;
import org.yop.orm.util.ORMUtil;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
/**
* ID restriction on several values. Can be easier to write than OR...
*/
public class IdIn implements Evaluation {
public static final String VALUES = "values";
/** ID value restriction */
private final Collection values = new HashSet<>();
private IdIn() {}
/**
* Default constructor : gimme all the values you have !
* @param values the ID value restriction. Can be empty. Must not be null.
*/
public IdIn(Collection extends Comparable> values) {
this();
this.values.addAll(values);
}
/**
* {@inheritDoc}
*
* Simply build a SQL portion : '[Context][ID column] IN (?,?...)' and fill the parameters.
*/
@Override
public CharSequence toSQL(Context context, Config config) {
if(this.values.isEmpty()) {
return "";
}
String idColumn = ORMUtil.getIdColumn(context, config);
Field idField = ORMUtil.getIdField(context.getTarget());
List values = this.values
.stream()
.map(value -> SQLPart.parameter(idColumn + "=" + value, value, idField, config))
.collect(Collectors.toList());
return config.getDialect().in(idColumn, values);
}
@Override
public JsonElement toJSON(Context context) {
JsonObject json = Evaluation.super.toJSON(context).getAsJsonObject();
json.add(VALUES, new JsonArray());
for (Comparable value : this.values) {
if(value instanceof Number) {
json.get(VALUES).getAsJsonArray().add((Number) value);
} else if(value instanceof Boolean) {
json.get(VALUES).getAsJsonArray().add((Boolean) value);
} else {
json.get(VALUES).getAsJsonArray().add(String.valueOf(value));
}
}
return json;
}
@Override
public void fromJSON(Context context, JsonElement element, Config config) {
Evaluation.super.toJSON(context);
JsonArray values = element.getAsJsonObject().get(VALUES).getAsJsonArray();
Gson gson = new Gson();
for (JsonElement value : values) {
this.values.add((Comparable) gson.fromJson(value, ORMUtil.getIdField(context.getTarget()).getType()));
}
element.getAsJsonObject().get(VALUES).getAsJsonArray().forEach(e -> this.values.add(e.getAsLong()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy