Search in sources :

Example 6 with ASTObjPath

use of org.apache.cayenne.exp.parser.ASTObjPath in project cayenne by apache.

the class ObjEntityIT method testLastPathComponent.

@Test
public void testLastPathComponent() {
    ObjEntity artistE = runtime.getDataDomain().getEntityResolver().getObjEntity("Artist");
    Map<String, String> aliases = new HashMap<>();
    aliases.put("a", "paintingArray.toGallery");
    PathComponent<ObjAttribute, ObjRelationship> lastAttribute = artistE.lastPathComponent(ExpressionFactory.exp("paintingArray.paintingTitle"), aliases);
    assertTrue(lastAttribute.getAttribute() != null);
    assertEquals("paintingTitle", lastAttribute.getAttribute().getName());
    PathComponent<ObjAttribute, ObjRelationship> lastRelationship = artistE.lastPathComponent(ExpressionFactory.exp("paintingArray.toGallery"), aliases);
    assertTrue(lastRelationship.getRelationship() != null);
    assertEquals("toGallery", lastRelationship.getRelationship().getName());
    PathComponent<ObjAttribute, ObjRelationship> lastLeftJoinRelationship = artistE.lastPathComponent(new ASTObjPath("paintingArray+.toGallery+"), aliases);
    assertTrue(lastLeftJoinRelationship.getRelationship() != null);
    assertEquals("toGallery", lastLeftJoinRelationship.getRelationship().getName());
    PathComponent<ObjAttribute, ObjRelationship> lastAliasedRelationship = artistE.lastPathComponent(new ASTObjPath("a"), aliases);
    assertTrue(lastAliasedRelationship.getRelationship() != null);
    assertEquals("toGallery", lastAliasedRelationship.getRelationship().getName());
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 7 with ASTObjPath

use of org.apache.cayenne.exp.parser.ASTObjPath in project cayenne by apache.

the class ExpressionFactory method matchAnyExp.

/**
 * Creates an expression that matches any of the key-values pairs in the
 * <code>map</code>.
 * <p>
 * For each pair <code>pairType</code> operator is used to build a binary
 * expression. Key is considered to be a OBJ_PATH expression. OR is used to
 * join pair binary expressions.
 */
public static Expression matchAnyExp(Map<String, ?> map, int pairType) {
    List<Expression> pairs = new ArrayList<>(map.size());
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        Expression exp = expressionOfType(pairType);
        exp.setOperand(0, new ASTObjPath(entry.getKey()));
        exp.setOperand(1, wrapPathOperand(entry.getValue()));
        pairs.add(exp);
    }
    return joinExp(Expression.OR, pairs);
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 8 with ASTObjPath

use of org.apache.cayenne.exp.parser.ASTObjPath in project cayenne by apache.

the class ExpressionFactory method matchAllExp.

/**
 * @since 3.0
 */
public static Expression matchAllExp(String path, Object... values) {
    if (values == null) {
        throw new NullPointerException("Null values collection");
    }
    if (values.length == 0) {
        return new ASTTrue();
    }
    int split = path.indexOf(SPLIT_SEPARATOR);
    List<Expression> matches = new ArrayList<>(values.length);
    if (split >= 0 && split < path.length() - 1) {
        int splitEnd = path.indexOf(Entity.PATH_SEPARATOR, split + 1);
        String beforeSplit = split > 0 ? path.substring(0, split) + "." : "";
        String afterSplit = splitEnd > 0 ? "." + path.substring(splitEnd + 1) : "";
        String aliasBase = "split" + autoAliasId++ + "_";
        String splitChunk = splitEnd > 0 ? path.substring(split + 1, splitEnd) : path.substring(split + 1);
        // fix the path - replace split with dot if it's in the middle, or
        // strip it if
        // it's in the beginning
        path = split == 0 ? path.substring(1) : path.replace(SPLIT_SEPARATOR, '.');
        int i = 0;
        for (Object value : values) {
            String alias = aliasBase + i;
            String aliasedPath = beforeSplit + alias + afterSplit;
            i++;
            ASTPath pathExp = new ASTObjPath(aliasedPath);
            pathExp.setPathAliases(Collections.singletonMap(alias, splitChunk));
            matches.add(new ASTEqual(pathExp, value));
        }
    } else {
        for (Object value : values) {
            matches.add(new ASTEqual(new ASTObjPath(path), value));
        }
    }
    return joinExp(Expression.AND, matches);
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ArrayList(java.util.ArrayList) ASTEqual(org.apache.cayenne.exp.parser.ASTEqual) ASTFullObject(org.apache.cayenne.exp.parser.ASTFullObject) ASTPath(org.apache.cayenne.exp.parser.ASTPath) ASTTrue(org.apache.cayenne.exp.parser.ASTTrue)

Example 9 with ASTObjPath

use of org.apache.cayenne.exp.parser.ASTObjPath in project cayenne by apache.

the class ExpressionFactory method matchAllExp.

/**
 * Creates an expression that matches all key-values pairs in
 * <code>map</code>.
 * <p>
 * For each pair <code>pairType</code> operator is used to build a binary
 * expression. Key is considered to be a OBJ_PATH expression. AND is used to
 * join pair binary expressions.
 */
public static Expression matchAllExp(Map<String, ?> map, int pairType) {
    List<Expression> pairs = new ArrayList<>(map.size());
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        Expression exp = expressionOfType(pairType);
        exp.setOperand(0, new ASTObjPath(entry.getKey()));
        exp.setOperand(1, wrapPathOperand(entry.getValue()));
        pairs.add(exp);
    }
    return joinExp(Expression.AND, pairs);
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 10 with ASTObjPath

use of org.apache.cayenne.exp.parser.ASTObjPath in project cayenne by apache.

the class SelectQueryReturnTypesIT method testSelectBitwiseNot.

@Test
public void testSelectBitwiseNot() throws Exception {
    if (!accessStackAdapter.supportsBitwiseOps()) {
        return;
    }
    createNumericsDataSet();
    // to simplify result checking, do double NOT
    Expression left = new ASTBitwiseNot(new ASTBitwiseNot(new ASTObjPath(ReturnTypesMap1.INTEGER_COLUMN.getName())));
    Expression right = new ASTScalar(2);
    Expression greater = new ASTGreater();
    greater.setOperand(0, left);
    greater.setOperand(1, right);
    SelectQuery query = new SelectQuery(ReturnTypesMap1.class);
    query.setQualifier(greater);
    List<ReturnTypesMap1> objects = context.performQuery(query);
    assertEquals(2, objects.size());
}
Also used : ASTGreater(org.apache.cayenne.exp.parser.ASTGreater) ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ASTBitwiseNot(org.apache.cayenne.exp.parser.ASTBitwiseNot) ReturnTypesMap1(org.apache.cayenne.testdo.return_types.ReturnTypesMap1) Expression(org.apache.cayenne.exp.Expression) ASTScalar(org.apache.cayenne.exp.parser.ASTScalar) Test(org.junit.Test)

Aggregations

ASTObjPath (org.apache.cayenne.exp.parser.ASTObjPath)18 Test (org.junit.Test)13 ASTLike (org.apache.cayenne.exp.parser.ASTLike)6 PatternMatchNode (org.apache.cayenne.exp.parser.PatternMatchNode)6 Expression (org.apache.cayenne.exp.Expression)5 ArrayList (java.util.ArrayList)4 ASTEqual (org.apache.cayenne.exp.parser.ASTEqual)4 ASTScalar (org.apache.cayenne.exp.parser.ASTScalar)4 ReturnTypesMap1 (org.apache.cayenne.testdo.return_types.ReturnTypesMap1)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ObjAttribute (org.apache.cayenne.map.ObjAttribute)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 ExpressionException (org.apache.cayenne.exp.ExpressionException)1 ExpressionParameter (org.apache.cayenne.exp.ExpressionParameter)1 ASTBitwiseAnd (org.apache.cayenne.exp.parser.ASTBitwiseAnd)1