Search in sources :

Example 1 with SimpleNode

use of org.apache.cayenne.ejbql.parser.SimpleNode in project cayenne by apache.

the class EJBQLPathAnaliserTranslator method processParameter.

private void processParameter(String boundName, EJBQLExpression expression) {
    Object object = context.getBoundParameter(boundName);
    Map<?, ?> map = null;
    if (object instanceof Persistent) {
        map = ((Persistent) object).getObjectId().getIdSnapshot();
    } else if (object instanceof ObjectId) {
        map = ((ObjectId) object).getIdSnapshot();
    } else if (object instanceof Map) {
        map = (Map<?, ?>) object;
    }
    if (map != null) {
        if (map.size() == 1) {
            context.rebindParameter(boundName, map.values().iterator().next());
        } else {
            addMultiColumnOperand(EJBQLMultiColumnOperand.getObjectOperand(context, map));
            return;
        }
    }
    if (object != null) {
        context.append(" #bind($").append(boundName).append(")");
    } else {
        String type = null;
        Node parent = ((SimpleNode) expression).jjtGetParent();
        context.pushMarker("@processParameter", true);
        EJBQLPathAnaliserTranslator translator = new EJBQLPathAnaliserTranslator(context);
        parent.visit(translator);
        translator.visitPath(parent, parent.getChildrenCount());
        String id = translator.idPath;
        if (id != null) {
            ClassDescriptor descriptor = context.getEntityDescriptor(id);
            if (descriptor == null) {
                throw new EJBQLException("Unmapped id variable: " + id);
            }
            String pathChunk = translator.lastPathComponent;
            PropertyDescriptor property = descriptor.getProperty(pathChunk);
            if (property instanceof AttributeProperty) {
                String atrType = ((AttributeProperty) property).getAttribute().getType();
                type = TypesMapping.getSqlNameByType(TypesMapping.getSqlTypeByJava(atrType));
            }
        }
        context.popMarker();
        if (type == null) {
            type = "VARCHAR";
        }
        // this is a hack to prevent execptions on DB's like Derby for
        // expressions
        // "X = NULL". The 'VARCHAR' parameter is totally bogus, but seems
        // to work on
        // all tested DB's... Also note what JPA spec, chapter 4.11 says:
        // "Comparison
        // or arithmetic operations with a NULL value always yield an
        // unknown value."
        // TODO: andrus 6/28/2007 Ideally we should track the type of the
        // current
        // expression to provide a meaningful type.
        context.append(" #bind($").append(boundName).append(" '" + type + "')");
    }
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ObjectId(org.apache.cayenne.ObjectId) SimpleNode(org.apache.cayenne.ejbql.parser.SimpleNode) Node(org.apache.cayenne.ejbql.parser.Node) AggregateConditionNode(org.apache.cayenne.ejbql.parser.AggregateConditionNode) EJBQLException(org.apache.cayenne.ejbql.EJBQLException) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) SimpleNode(org.apache.cayenne.ejbql.parser.SimpleNode) Map(java.util.Map)

Aggregations

Map (java.util.Map)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 EJBQLException (org.apache.cayenne.ejbql.EJBQLException)1 AggregateConditionNode (org.apache.cayenne.ejbql.parser.AggregateConditionNode)1 Node (org.apache.cayenne.ejbql.parser.Node)1 SimpleNode (org.apache.cayenne.ejbql.parser.SimpleNode)1 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)1