Search in sources :

Example 1 with ASTObjPath

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

the class DataMapUtils method getParameterNames.

private Map<String, String> getParameterNames(Expression expression, Object root) {
    if (expression != null) {
        Map<String, String> types = new HashMap<>();
        String typeName = "";
        List<String> names = new LinkedList<>();
        for (int i = 0; i < expression.getOperandCount(); i++) {
            Object operand = expression.getOperand(i);
            if (operand instanceof Expression) {
                types.putAll(getParameterNames((Expression) operand, root));
            }
            if (operand instanceof ASTObjPath) {
                PathComponent<ObjAttribute, ObjRelationship> component = ((Entity) root).lastPathComponent((ASTObjPath) operand, null);
                ObjAttribute attribute = component.getAttribute();
                if (attribute != null) {
                    typeName = attribute.getType();
                } else {
                    ObjRelationship relationship = component.getRelationship();
                    if (relationship != null) {
                        typeName = relationship.getTargetEntity().getClassName();
                    } else {
                        typeName = "Object";
                    }
                }
            }
            if (operand instanceof ASTList) {
                Object[] values = (Object[]) ((ASTList) operand).getOperand(0);
                for (Object value : values) {
                    if (value instanceof ExpressionParameter) {
                        names.add(((ExpressionParameter) value).getName());
                    }
                }
            }
            if (operand instanceof ExpressionParameter) {
                names.add(((ExpressionParameter) operand).getName());
            }
        }
        for (String name : names) {
            types.put(Util.underscoredToJava(name, false), typeName);
        }
        return types;
    }
    return Collections.emptyMap();
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ObjEntity(org.apache.cayenne.map.ObjEntity) Entity(org.apache.cayenne.map.Entity) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjAttribute(org.apache.cayenne.map.ObjAttribute) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Expression(org.apache.cayenne.exp.Expression) ExpressionParameter(org.apache.cayenne.exp.ExpressionParameter) ASTList(org.apache.cayenne.exp.parser.ASTList)

Example 2 with ASTObjPath

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

the class SelectQueryReturnTypesIT method testSelectBitwiseXor.

@Test
public void testSelectBitwiseXor() throws Exception {
    if (!accessStackAdapter.supportsBitwiseOps()) {
        return;
    }
    createNumericsDataSet();
    // to simplify result checking, do double NOT
    Expression left = new ASTBitwiseXor(new Object[] { new ASTObjPath(ReturnTypesMap1.INTEGER_COLUMN.getName()), new ASTScalar(1) });
    Expression right = new ASTScalar(5);
    Expression equal = new ASTEqual();
    equal.setOperand(0, left);
    equal.setOperand(1, right);
    SelectQuery query = new SelectQuery(ReturnTypesMap1.class);
    query.setQualifier(equal);
    List<ReturnTypesMap1> objects = context.performQuery(query);
    assertEquals(1, objects.size());
    assertEquals(4, objects.get(0).getIntegerColumn().intValue());
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ReturnTypesMap1(org.apache.cayenne.testdo.return_types.ReturnTypesMap1) Expression(org.apache.cayenne.exp.Expression) ASTEqual(org.apache.cayenne.exp.parser.ASTEqual) ASTBitwiseXor(org.apache.cayenne.exp.parser.ASTBitwiseXor) ASTScalar(org.apache.cayenne.exp.parser.ASTScalar) Test(org.junit.Test)

Example 3 with ASTObjPath

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

the class LikeExpressionHelperTest method testEscape_TwoChars_Mix.

@Test
public void testEscape_TwoChars_Mix() {
    PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "ab%c_");
    LikeExpressionHelper.escape(node);
    assertEquals("ab!%c!_", node.getOperand(1));
    assertEquals('!', node.getEscapeChar());
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ASTLike(org.apache.cayenne.exp.parser.ASTLike) PatternMatchNode(org.apache.cayenne.exp.parser.PatternMatchNode) Test(org.junit.Test)

Example 4 with ASTObjPath

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

the class LikeExpressionHelperTest method testEscape_AltEscapeChar2.

@Test
public void testEscape_AltEscapeChar2() {
    PatternMatchNode node = new ASTLike(new ASTObjPath("x"), "a!%c#_");
    LikeExpressionHelper.escape(node);
    assertEquals("a!$%c#$_", node.getOperand(1));
    assertEquals('$', node.getEscapeChar());
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) ASTLike(org.apache.cayenne.exp.parser.ASTLike) PatternMatchNode(org.apache.cayenne.exp.parser.PatternMatchNode) Test(org.junit.Test)

Example 5 with ASTObjPath

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

the class PropertyTest method testCreationWithName.

@Test
public void testCreationWithName() {
    Property<String> p1 = new Property<>("p1", String.class);
    assertEquals(String.class, p1.getType());
    assertEquals("p1", p1.getName());
    assertEquals(new ASTObjPath("p1"), p1.getExpression());
    Property<String> p2 = Property.create("p1", String.class);
    assertEquals(p1, p2);
}
Also used : ASTObjPath(org.apache.cayenne.exp.parser.ASTObjPath) 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