use of org.eclipse.persistence.internal.expressions.FunctionExpression in project eclipselink by eclipse-ee4j.
the class SpatialExpressionFactory method getSpatialExpression.
/**
* INTERNAL:
* A utility method to build a SpatialExpression
*
* @param operator the ordinal of the operator
*/
public static Expression getSpatialExpression(int operator, Expression geom1, Object geom2, String params) {
List<Object> vParameters = new Vector<>(2);
vParameters.add(geom2);
// by null prior to passing to Geometry call.
if (params == null || params.trim().equals("")) {
vParameters.add(null);
} else {
vParameters.add(params);
}
ExpressionOperator anOperator = geom1.getOperator(operator);
FunctionExpression expression = new FunctionExpression();
expression.create(geom1, vParameters, anOperator);
Expression finalExpression = expression.equal("TRUE");
return finalExpression;
}
use of org.eclipse.persistence.internal.expressions.FunctionExpression in project eclipselink by eclipse-ee4j.
the class Expression method dateDifference.
/**
* PUBLIC:
* Function, Return the difference between the queried part of a date(i.e. years, days etc.)
* and same part of the given date. The equivalent of the Sybase function DateDiff
* <p>Example:
* <blockquote><pre>
* EclipseLink: employee.get("date").dateDifference("year", new Date(System.currentTimeMillis()))
* Java: NA
* SQL: DATEADD(date, 2, GETDATE)
* </pre></blockquote>
*/
public Expression dateDifference(String datePart, java.util.Date date) {
ExpressionOperator anOperator = getOperator(ExpressionOperator.DateDifference);
FunctionExpression expression = new FunctionExpression();
expression.setBaseExpression(this);
expression.addChild(Expression.fromLiteral(datePart, this));
expression.addChild(Expression.from(date, this));
expression.addChild(this);
expression.setOperator(anOperator);
return expression;
}
use of org.eclipse.persistence.internal.expressions.FunctionExpression in project eclipselink by eclipse-ee4j.
the class ObjectPersistenceRuntimeXMLProject method buildFunctionExpressionDescriptor.
protected ClassDescriptor buildFunctionExpressionDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(FunctionExpression.class);
descriptor.setDefaultRootElement("function-expression");
descriptor.getInheritancePolicy().setParentClass(Expression.class);
// A function's base is always its first child so not persisted,
// Is fixed up to be its first child or new expression builder if no children.
// Child value expressions need their backpointer to their local base set,
// this is not persisted so must be hooked back up after loading.
descriptor.getEventManager().addListener(new DescriptorEventAdapter() {
@Override
public void postBuild(DescriptorEvent event) {
FunctionExpression expression = (FunctionExpression) event.getObject();
for (int index = 0; index < expression.getChildren().size(); index++) {
Expression child = expression.getChildren().get(index);
if (child.isValueExpression()) {
child.setLocalBase(new ExpressionBuilder());
}
}
if (expression.getChildren().size() > 0) {
expression.setBaseExpression(expression.getChildren().get(0));
} else {
expression.setBaseExpression(new ExpressionBuilder());
}
}
});
XMLDirectMapping operatorMapping = new XMLDirectMapping();
operatorMapping.setAttributeName("operator");
ExpressionOperatorConverter operatorConverter = new ExpressionOperatorConverter();
operatorConverter.addConversionValue("like", ExpressionOperator.getOperator(ExpressionOperator.Like));
operatorConverter.addConversionValue("notLike", ExpressionOperator.getOperator(ExpressionOperator.NotLike));
operatorConverter.addConversionValue("not", ExpressionOperator.getOperator(ExpressionOperator.Not));
operatorConverter.addConversionValue("isNull", ExpressionOperator.getOperator(ExpressionOperator.IsNull));
operatorConverter.addConversionValue("notNull", ExpressionOperator.getOperator(ExpressionOperator.NotNull));
operatorConverter.addConversionValue("ascending", ExpressionOperator.getOperator(ExpressionOperator.Ascending));
operatorConverter.addConversionValue("descending", ExpressionOperator.getOperator(ExpressionOperator.Descending));
// These are platform specific so not on operator.
operatorConverter.addConversionValue("upper", new ExpressionOperator(ExpressionOperator.ToUpperCase, new ArrayList<>(0)));
operatorConverter.addConversionValue("lower", new ExpressionOperator(ExpressionOperator.ToLowerCase, new ArrayList<>(0)));
// Aggregate functions
operatorConverter.addConversionValue("count", ExpressionOperator.getOperator(ExpressionOperator.Count));
operatorConverter.addConversionValue("sum", ExpressionOperator.getOperator(ExpressionOperator.Sum));
operatorConverter.addConversionValue("average", ExpressionOperator.getOperator(ExpressionOperator.Average));
operatorConverter.addConversionValue("maximum", ExpressionOperator.getOperator(ExpressionOperator.Maximum));
operatorConverter.addConversionValue("minimum", ExpressionOperator.getOperator(ExpressionOperator.Minimum));
// standardDeviation is platform specific.
operatorConverter.addConversionValue("standardDeviation", new ExpressionOperator(ExpressionOperator.StandardDeviation, new ArrayList<>(0)));
operatorConverter.addConversionValue("variance", new ExpressionOperator(ExpressionOperator.Variance, new ArrayList<>(0)));
operatorConverter.addConversionValue("distinct", ExpressionOperator.getOperator(ExpressionOperator.Distinct));
operatorMapping.setConverter(operatorConverter);
operatorMapping.setXPath("@function");
descriptor.addMapping(operatorMapping);
XMLCompositeCollectionMapping childrenMapping = new XMLCompositeCollectionMapping();
childrenMapping.useCollectionClass(NonSynchronizedVector.class);
childrenMapping.setAttributeName("children");
childrenMapping.setReferenceClass(Expression.class);
childrenMapping.setXPath(getPrimaryNamespaceXPath() + "arguments/" + getPrimaryNamespaceXPath() + "argument");
descriptor.addMapping(childrenMapping);
return descriptor;
}
use of org.eclipse.persistence.internal.expressions.FunctionExpression in project eclipselink by eclipse-ee4j.
the class Expression method decode.
/**
* PUBLIC:
* Function Convert values returned by the query to values given in the decodeableItems Map.
* The equivalent of the Oracle DECODE function.
* Note: This will only work on databases that support Decode with the syntax below.
* <p>Example:
* <blockquote><pre>
* Map decodeTable = new HashMap();
* decodeTable.put("Robert", "Bob");
* decodeTable.put("Susan", "Sue");
*
* EclipseLink: employee.get("name").Decode(decodeTable, "No-Nickname")
* Java: NA
* SQL: DECODE(name, "Robert", "Bob", "Susan", "Sue", "No-Nickname")
* </pre></blockquote>
* @param decodeableItems java.util.Map
* a Map containing the items to be decoded. Keys represent
* the items to match coming from the query. Values represent what
* a key will be changed to.
* @param defaultItem
* the default value that will be used if none of the keys in the
* Map match
*/
public Expression decode(Map decodeableItems, String defaultItem) {
/**
* decode works differently than most of the functionality in the expression framework.
* It takes a variable number of arguments and as a result, the printed strings for
* a decode call have to be built when the number of arguments are known.
* As a result, we do not look up decode in the ExpressionOperator. Instead we build
* the whole operator here. The side effect of this is that decode will not thrown
* an invalid operator exception for any platform. (Even the ones that do not support
* decode)
*/
ExpressionOperator anOperator = new ExpressionOperator();
anOperator.setSelector(ExpressionOperator.Decode);
anOperator.setName("DECODE");
anOperator.setNodeClass(ClassConstants.FunctionExpression_Class);
anOperator.setType(ExpressionOperator.FunctionOperator);
anOperator.bePrefix();
List<String> v = new ArrayList<>(decodeableItems.size() + 1);
v.add("DECODE(");
for (int i = 0; i < ((decodeableItems.size() * 2) + 1); i++) {
v.add(", ");
}
v.add(")");
anOperator.printsAs(v);
FunctionExpression expression = new FunctionExpression();
expression.setBaseExpression(this);
expression.addChild(this);
Iterator iterator = decodeableItems.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
expression.addChild(Expression.from(key, this));
expression.addChild(Expression.from(decodeableItems.get(key), this));
}
expression.addChild(Expression.from(defaultItem, this));
expression.setOperator(anOperator);
return expression;
}
use of org.eclipse.persistence.internal.expressions.FunctionExpression in project eclipselink by eclipse-ee4j.
the class ReportQueryResult method processItemFromMapping.
private Object processItemFromMapping(ReportQuery query, AbstractRecord row, DatabaseMapping mapping, ReportItem item, int itemIndex) {
Object value = null;
// Check for non database (EIS) records to use normal get.
if (row instanceof DatabaseRecord) {
value = row.getValues().get(itemIndex);
} else {
value = row.get(mapping.getField());
}
// Bug 421056: JPA 2.1; section 4.8.5
if (item.getAttributeExpression().isFunctionExpression()) {
FunctionExpression exp = (FunctionExpression) item.getAttributeExpression();
int selector = exp.getOperator().getSelector();
// we want to return null, per the spec, here before the mapping gets to alter the value
if (value == null && ((selector == ExpressionOperator.Maximum) || (selector == ExpressionOperator.Minimum))) {
return value;
}
}
// If the mapping was set on the ReportItem, then use the mapping to convert the value
if (mapping.isAbstractColumnMapping()) {
value = ((AbstractColumnMapping) mapping).getObjectValue(value, query.getSession());
} else if (mapping.isDirectCollectionMapping()) {
value = ((DirectCollectionMapping) mapping).getObjectValue(value, query.getSession());
}
return value;
}
Aggregations