Search in sources :

Example 1 with Element

use of org.vertexium.Element in project vertexium by visallo.

the class ExpressionExecutor method executeArrayAccess.

private Object executeArrayAccess(VertexiumCypherQueryContext ctx, CypherArrayAccess arrayAccess, ExpressionScope scope) {
    Object array = ctx.getExpressionExecutor().executeExpression(ctx, arrayAccess.getArrayExpression(), scope);
    if (array == null) {
        return null;
    }
    Object indexObj = ctx.getExpressionExecutor().executeExpression(ctx, arrayAccess.getIndexExpression(), scope);
    if (array instanceof Element) {
        Element element = (Element) array;
        if (indexObj instanceof String) {
            String propertyName = (String) indexObj;
            return element.getPropertyValue(propertyName);
        }
        throw new VertexiumCypherTypeErrorException("expected string property name, found " + indexObj.getClass().getName());
    }
    if (array instanceof Map) {
        Map map = (Map) array;
        if (indexObj instanceof String) {
            String propertyName = (String) indexObj;
            return map.get(propertyName);
        }
        throw new VertexiumCypherTypeErrorException("MapElementAccessByNonString: expected string, found " + indexObj.getClass().getName());
    }
    if (array instanceof List || array instanceof CypherListLiteral) {
        if (indexObj instanceof Long) {
            indexObj = ((Long) indexObj).intValue();
        }
        if (indexObj instanceof Integer) {
            int index = (int) indexObj;
            if (array instanceof CypherListLiteral) {
                return ((CypherListLiteral) array).get(index);
            } else if (array instanceof List) {
                return ((List) array).get(index);
            }
        }
        throw new VertexiumCypherTypeErrorException("ListElementAccessByNonInteger: expected integer, found " + indexObj.getClass().getName());
    }
    if (array instanceof Stream) {
        if (indexObj instanceof Long) {
            indexObj = ((Long) indexObj).intValue();
        }
        if (indexObj instanceof Integer) {
            int index = (int) indexObj;
            return ((Stream<?>) array).skip(index).findFirst().get();
        }
        throw new VertexiumCypherTypeErrorException("ListElementAccessByNonInteger: expected integer, found " + indexObj.getClass().getName());
    }
    throw new VertexiumCypherTypeErrorException("InvalidElementAccess: unexpected object access, found object " + array.getClass().getName() + ": " + array + ", index " + indexObj.getClass().getName() + ": " + indexObj);
}
Also used : VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Element(org.vertexium.Element) Stream(java.util.stream.Stream)

Example 2 with Element

use of org.vertexium.Element in project vertexium by visallo.

the class SetClauseExecutor method executeSetVariable.

private void executeSetVariable(VertexiumCypherQueryContext ctx, CypherSetVariable setItem, VertexiumCypherScope.Item item) {
    Object left = ctx.getExpressionExecutor().executeExpression(ctx, setItem.getLeft(), item);
    VertexiumCypherTypeErrorException.assertType(left, Element.class, null);
    if (left == null) {
        return;
    }
    Object values = ctx.getExpressionExecutor().executeExpression(ctx, setItem.getRight(), item);
    CypherSetItem.Op op = setItem.getOp();
    if (values instanceof Stream) {
        values = ((Stream<?>) values).collect(Collectors.toList());
    }
    if (values instanceof List && ((List) values).size() == 1) {
        values = ((List) values).get(0);
    }
    VertexiumCypherTypeErrorException.assertType(values, Map.class, Element.class);
    if (values instanceof Element) {
        values = ctx.getElementPropertiesAsMap((Element) values);
    }
    Element element = (Element) left;
    // noinspection unchecked
    Map<String, ?> valuesMap = (Map<String, ?>) values;
    ExistingElementMutation<Element> m = element.prepareMutation();
    if (op == CypherSetItem.Op.EQUAL) {
        for (Property property : element.getProperties()) {
            if (ctx.isLabelProperty(property)) {
                continue;
            }
            if (!valuesMap.containsKey(property.getName())) {
                ctx.removeProperty(m, property.getName());
            }
        }
    }
    for (Map.Entry<String, ?> valuesMapEntry : valuesMap.entrySet()) {
        String propertyName = valuesMapEntry.getKey();
        Object value = valuesMapEntry.getValue();
        if (value instanceof CypherLiteral) {
            value = ((CypherLiteral) value).getValue();
        }
        if (value == null) {
            ctx.removeProperty(m, propertyName);
        } else {
            ctx.setProperty(m, propertyName, value);
        }
    }
    ctx.saveElement(m);
}
Also used : Element(org.vertexium.Element) Stream(java.util.stream.Stream) List(java.util.List) Map(java.util.Map) Property(org.vertexium.Property)

Example 3 with Element

use of org.vertexium.Element in project vertexium by visallo.

the class SetClauseExecutor method executeSetProperty.

private void executeSetProperty(VertexiumCypherQueryContext ctx, CypherSetProperty setItem, VertexiumCypherScope.Item item) {
    Object left = ctx.getExpressionExecutor().executeExpression(ctx, setItem.getLeft().getAtom(), item);
    VertexiumCypherTypeErrorException.assertType(left, Element.class, null);
    if (left == null) {
        return;
    }
    String propertyName = setItem.getLeft().getProperty();
    Object value = ctx.getExpressionExecutor().executeExpression(ctx, setItem.getRight(), item);
    Element element = (Element) left;
    ExistingElementMutation<Element> m = element.prepareMutation();
    switch(setItem.getOp()) {
        case EQUAL:
            if (value == null) {
                ctx.removeProperty(m, propertyName);
            } else {
                ctx.setProperty(m, propertyName, value);
            }
            break;
        default:
            throw new VertexiumCypherNotImplemented("" + setItem);
    }
    ctx.saveElement(m);
}
Also used : VertexiumCypherNotImplemented(org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented) Element(org.vertexium.Element)

Example 4 with Element

use of org.vertexium.Element in project vertexium by visallo.

the class RemoveClauseExecutor method executeRemoveProperty.

private void executeRemoveProperty(VertexiumCypherQueryContext ctx, CypherRemovePropertyExpressionItem removeItem, VertexiumCypherScope.Item item) {
    CypherAstBase propertyExpression = removeItem.getPropertyExpression();
    if (propertyExpression instanceof CypherLookup) {
        CypherLookup cypherLookup = (CypherLookup) propertyExpression;
        Object elementObj = ctx.getExpressionExecutor().executeExpression(ctx, cypherLookup.getAtom(), item);
        if (elementObj == null) {
            return;
        }
        if (elementObj instanceof Element) {
            Element element = (Element) elementObj;
            ExistingElementMutation<Element> m = element.prepareMutation();
            ctx.removeProperty(m, cypherLookup.getProperty());
            ctx.saveElement(m);
            return;
        }
        throw new VertexiumCypherTypeErrorException(elementObj, Element.class, null);
    }
    throw new VertexiumCypherTypeErrorException(propertyExpression, CypherLookup.class);
}
Also used : VertexiumCypherTypeErrorException(org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException) Element(org.vertexium.Element)

Aggregations

Element (org.vertexium.Element)4 Stream (java.util.stream.Stream)2 VertexiumCypherTypeErrorException (org.vertexium.cypher.exceptions.VertexiumCypherTypeErrorException)2 List (java.util.List)1 Map (java.util.Map)1 Property (org.vertexium.Property)1 VertexiumCypherNotImplemented (org.vertexium.cypher.exceptions.VertexiumCypherNotImplemented)1