public Coalesce<T> value(Expression<? extends T> expr) { List args = null; if (queryExpr != null) { args = ((InvokeExpression)queryExpr).getArguments(); } args.add(expr); queryExpr = new InvokeExpression(null, "COALESCE", args); return this; }
private String getAliasIfMapExpression(InvokeExpression expr, Set<String> mappingAliases) { if ("mapValue".equals(expr.getOperation()) || "mapKey".equals(expr.getOperation()) || "mapEntry".equals(expr.getOperation())) { String alias = expr.getLeft().getSymbol().getQualifiedName(); mappingAliases.add(alias); return alias; } else { return getAlias(expr.getLeft(), mappingAliases); } } }
private void handleContainsOperation(InvokeExpression invokeExpr, QueryData qd) { Expression param = (Expression) invokeExpr.getArguments().get(0); param.bind(getSymbolTable()); if (invokeExpr.getLeft() instanceof PrimaryExpression) { PrimaryExpression left = (PrimaryExpression) invokeExpr.getLeft(); // Make sure that the left expression is a collection List<String> tuples = getTuples(left, qd.compilation.getCandidateAlias()); if (tuples.size() == 1) { // Handle case of simple field name AbstractMemberMetaData mmd = qd.acmd.getMetaDataForMember(tuples.get(0)); if (mmd != null && !Collection.class.isAssignableFrom(mmd.getType())) { throw new UnsupportedDatastoreFeatureException("The 'contains' method is only for use with a Collection expression"); } } // treat contains as equality since that's how the low-level api does checks on multi-value properties. addLeftPrimaryExpression(left, Expression.OP_EQ, param, qd); } else if (invokeExpr.getLeft() instanceof ParameterExpression && param instanceof PrimaryExpression) { ParameterExpression pe = (ParameterExpression) invokeExpr.getLeft(); addLeftPrimaryExpression((PrimaryExpression) param, Expression.OP_EQ, pe, qd); } else { throw newUnsupportedQueryMethodException(invokeExpr); } }
private boolean appendInvokeExpression(ExpressionBuilderHelper h, InvokeExpression invocation, ExecutionContext executionContext, boolean not) { boolean done = false; String oper = invocation.getOperation(); if ("matches".equals(oper)) { if (not) h.sb.append("NOT "); appendExpression(h, invocation.getLeft(), executionContext); h.sb.append(" like "); appendExpression(h, invocation.getArguments().get(0), executionContext); } else if ("toLowerCase".equals(oper)) { appendExpression(h, invocation.getLeft(), executionContext); } else if ("COUNT".equals(oper)) { h.sb.append(String.format(" %s()", oper)); appendContainsExpression(h, invocation, not); } else if ("size".equals(oper)) { boolean isEmpty = invocation.getParent().getOperator() == Expression.OP_EQ; appendExpression(h, invocation.getLeft(), executionContext); for (Expression e : invocation.getArguments()) { if (pos++ > 0) { h.sb.append(", "); appendExpression(h, invocation.getRight(), executionContext);
protected Object processInvokeExpression(InvokeExpression expr) Expression invokedExpr = expr.getLeft(); String method = expr.getOperation(); SubstringFilter filter; if (invokedExpr instanceof PrimaryExpression) Expression param = expr.getArguments().get(0); String value = QueryUtils.getStringValueForExpression(param, parameters); filter = new SubstringFilter(attribute); Expression param = expr.getArguments().get(0); String value = QueryUtils.getStringValueForExpression(param, parameters); filter = new SubstringFilter(attribute);
private UnsupportedDatastoreFeatureException newUnsupportedQueryMethodException( InvokeExpression invocation) { throw new UnsupportedDatastoreFeatureException( "Unsupported method <" + invocation.getOperation() + "> while parsing expression: " + invocation); }
String oper = ev.getOperation(); if ("mapKey".equals(oper) || "mapValue".equals(oper) || "mapEntry".equals(oper)) { String alias = expr.getLeft().getSymbol().getQualifiedName(); return value; return getDataForExpression(ffms, sObject, ev.getArguments().get(0), fieldName, value.toString()); } else if ("SUM".equals(oper)) { Object value = sObject.getField(fieldName); ExpressionMetaData exprMetaData = getExpressionMetaData(ev.getArguments().get(0)); AbstractMemberMetaData mmd = exprMetaData.getMemberMetaData(); if (mmd != null) { return ((Double) value).longValue(); } else if (mmd.getType() == BigInteger.class || mmd.getType() == BigDecimal.class) { return getDataForExpression(ffms, sObject, ev.getArguments().get(0), fieldName, value.toString());
private TupleName getMappedExpression(ExpressionBuilderHelper h, InvokeExpression expr) { String alias = expr.getLeft().getSymbol().getQualifiedName(); TupleName fieldName = null; for (Map.Entry<TupleName, String> ent : h.relatedJoinAliases.entrySet()) { if (ent.getValue().equals(alias)) { fieldName = ent.getKey(); break; } } if (fieldName == null) { throw new NucleusDataStoreException("Cannot find field name for alias: " + alias); } return fieldName; }
public NumericExpression<Integer> getMonth() { org.datanucleus.query.expression.Expression invokeExpr = new InvokeExpression(queryExpr, "getMonth", null); return new NumericExpressionImpl<Integer>(invokeExpr); }
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) { Expression argExpr = expr.getArguments().get(0); if (argExpr instanceof PrimaryExpression) { PrimaryExpression primExpr = (PrimaryExpression)argExpr; Object value = eval.getValueForPrimaryExpression(primExpr); return JDOHelper.getObjectId(value); } else if (argExpr instanceof ParameterExpression) { ParameterExpression paramExpr = (ParameterExpression)argExpr; Object value = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr); return JDOHelper.getObjectId(value); } else { throw new NucleusException("Dont currently support JDOHelper.getObjectId with arg of type " + argExpr.getClass().getName()); } } }
private void appendContainsExpression(ExpressionBuilderHelper h, InvokeExpression expr, boolean not) { PrimaryExpression pe = (PrimaryExpression) expr.getLeft(); TupleName fieldName = new TupleName(pe.getTuples()); AbstractMemberMetaData ammd = h.acmd.getMetaDataForMember(fieldName.getShortName()); if (ammd != null && (ammd.getMap() != null || ammd.getCollection() != null)) { processJoinExpression(pe, null, h, h.compilation, false, false, not); if (expr.getArguments().size() > 0) { String name = hints != null ? (String) hints.get(QueryHints.MEMBER_OF_FIELD) : null; h.sb.append(" where "); appendExpression(h, new PrimaryExpression(new TupleName(name != null ? name : "name").getTuple()), ec); h.sb.append(" = "); appendExpression(h, expr.getArguments().get(0), ec); } h.sb.append(")"); } else { // This is simply picklist values appendExpression(h, expr.getLeft(), ec); if (expr.getArguments().size() > 0) { h.sb.append(not ? " excludes(" : " includes("); Literal l = (Literal) expr.getArguments().get(0); h.sb.append(new MultiPicklistFormatter(l.getLiteral().toString()).getFormattedString()); h.sb.append(")"); } else { h.sb.append(not ? " = " : " != ").append("null"); } } }
private boolean appendInvokeExpression(ExpressionBuilderHelper h, InvokeExpression invocation, ExecutionContext executionContext, boolean not) { boolean done = false; String oper = invocation.getOperation(); if ("matches".equals(oper)) { if (not) h.sb.append("NOT "); appendExpression(h, invocation.getLeft(), executionContext); h.sb.append(" like "); appendExpression(h, invocation.getArguments().get(0), executionContext); } else if ("toLowerCase".equals(oper)) { appendExpression(h, invocation.getLeft(), executionContext); } else if ("COUNT".equals(oper)) { h.sb.append(String.format(" %s()", oper)); appendContainsExpression(h, invocation, not); } else if ("size".equals(oper)) { boolean isEmpty = invocation.getParent().getOperator() == Expression.OP_EQ; appendExpression(h, invocation.getLeft(), executionContext); for (Expression e : invocation.getArguments()) { if (pos++ > 0) { h.sb.append(", "); appendExpression(h, invocation.getRight(), executionContext);
private boolean isAggregate(Expression expr) { if (expr == null) return false; if (expr instanceof InvokeExpression) { String methodName = ((InvokeExpression) expr).getOperation(); return AGGREGATE_METHODS.contains(methodName); } else { return isAggregate(expr.getRight()) || isAggregate(expr.getLeft()); } }
if (invokeExpr.getOperation().equals("contains") && invokeExpr.getArguments().size() == 1) { handleContainsOperation(invokeExpr, qd); }else if (invokeExpr.getOperation().equals("startsWith") && invokeExpr.getArguments().size() == 1) { handleStartsWithOperation(invokeExpr, qd); } else if (invokeExpr.getOperation().equals("matches")) { handleMatchesOperation(invokeExpr, qd); } else {
private TupleName getMappedExpression(ExpressionBuilderHelper h, InvokeExpression expr) { String alias = expr.getLeft().getSymbol().getQualifiedName(); TupleName fieldName = null; for (Map.Entry<TupleName, String> ent : h.relatedJoinAliases.entrySet()) { if (ent.getValue().equals(alias)) { fieldName = ent.getKey(); break; } } if (fieldName == null) { throw new NucleusDataStoreException("Cannot find field name for alias: " + alias); } return fieldName; }
public NumericExpression<Integer> size() { org.datanucleus.query.expression.Expression invokeExpr = new InvokeExpression(queryExpr, "size", null); return new NumericExpressionImpl<Integer>(invokeExpr); } }
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) { Expression argExpr = expr.getArguments().get(0); if (argExpr instanceof PrimaryExpression) { PrimaryExpression primExpr = (PrimaryExpression)argExpr; Object value = eval.getValueForPrimaryExpression(primExpr); return JDOHelper.getVersion(value); } else if (argExpr instanceof ParameterExpression) { ParameterExpression paramExpr = (ParameterExpression)argExpr; Object value = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr); return JDOHelper.getVersion(value); } else { throw new NucleusException("Dont currently support JDOHelper.getVersion with arg of type " + argExpr.getClass().getName()); } } }
private void appendContainsExpression(ExpressionBuilderHelper h, InvokeExpression expr, boolean not) { PrimaryExpression pe = (PrimaryExpression) expr.getLeft(); TupleName fieldName = new TupleName(pe.getTuples()); AbstractMemberMetaData ammd = h.acmd.getMetaDataForMember(fieldName.getShortName()); if (ammd != null && (ammd.getMap() != null || ammd.getCollection() != null)) { processJoinExpression(pe, null, h, h.compilation, false, false, not); if (expr.getArguments().size() > 0) { String name = hints != null ? (String) hints.get(QueryHints.MEMBER_OF_FIELD) : null; h.sb.append(" where "); appendExpression(h, new PrimaryExpression(new TupleName(name != null ? name : "name").getTuple()), ec); h.sb.append(" = "); appendExpression(h, expr.getArguments().get(0), ec); } h.sb.append(")"); } else { // This is simply picklist values appendExpression(h, expr.getLeft(), ec); if (expr.getArguments().size() > 0) { h.sb.append(not ? " excludes(" : " includes("); Literal l = (Literal) expr.getArguments().get(0); h.sb.append(new MultiPicklistFormatter(l.getLiteral().toString()).getFormattedString()); h.sb.append(")"); } else { h.sb.append(not ? " = " : " != ").append("null"); } } }
private String getAliasIfMapExpression(InvokeExpression expr, Set<String> mappingAliases) { if ("mapValue".equals(expr.getOperation()) || "mapKey".equals(expr.getOperation()) || "mapEntry".equals(expr.getOperation())) { String alias = expr.getLeft().getSymbol().getQualifiedName(); mappingAliases.add(alias); return alias; } else { return getAlias(expr.getLeft(), mappingAliases); } } }
private boolean isAggregate(Expression expr) { if (expr == null) return false; if (expr instanceof InvokeExpression) { String methodName = ((InvokeExpression) expr).getOperation(); return AGGREGATE_METHODS.contains(methodName); } else { return isAggregate(expr.getRight()) || isAggregate(expr.getLeft()); } }