use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.
the class ExistingRelationshipStatementBuilder method build.
@Override
public Statement build() {
final Map<String, Object> parameters = new HashMap<>();
final StringBuilder queryBuilder = new StringBuilder();
Edge firstEdge = edges.iterator().next();
if (edges.size() > 0) {
queryBuilder.append("UNWIND $rows AS row MATCH ()-[r]->() WHERE ID(r) = row.relId ");
if (firstEdge.hasVersionProperty()) {
queryBuilder.append(OptimisticLockingUtils.getFragmentForExistingNodesAndRelationships(firstEdge, "r", dirtyEdges ? 1 : 0));
}
queryBuilder.append(firstEdge.createPropertyRemovalFragment("r"));
queryBuilder.append("SET r += row.props ");
queryBuilder.append("RETURN ID(r) as ref, ID(r) as id, $type as type");
List<Map> rows = new ArrayList<>();
for (Edge edge : edges) {
Map<String, Object> rowMap = new HashMap<>();
rowMap.put("relId", edge.getId());
Map<String, Object> props = new HashMap<>();
for (Property property : edge.getPropertyList()) {
if (!property.equals(edge.getVersion())) {
props.put((String) property.getKey(), property.getValue());
}
}
rowMap.put("props", props);
if (edge.hasVersionProperty()) {
Property version = edge.getVersion();
rowMap.put((String) version.getKey(), version.getValue());
}
rows.add(rowMap);
}
parameters.put("rows", rows);
parameters.put("type", "rel");
if (firstEdge.hasVersionProperty()) {
OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), new String[] { firstEdge.getType() }, firstEdge.getVersion().getKey());
return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
}
}
return statementFactory.statement(queryBuilder.toString(), parameters);
}
use of org.neo4j.ogm.request.OptimisticLockingConfig in project neo4j-ogm by neo4j.
the class NewNodeStatementBuilder method build.
@Override
public Statement build() {
final Map<String, Object> parameters = new HashMap<>();
final StringBuilder queryBuilder = new StringBuilder();
if (newNodes != null && newNodes.size() > 0) {
Node firstNode = newNodes.iterator().next();
queryBuilder.append("UNWIND $rows as row ");
boolean hasPrimaryIndex = firstNode.getPrimaryIndex() != null;
if (hasPrimaryIndex) {
queryBuilder.append("MERGE (n");
} else {
queryBuilder.append("CREATE (n");
}
for (String label : firstNode.getLabels()) {
queryBuilder.append(":`").append(label).append("`");
}
if (hasPrimaryIndex) {
String propertiesToMergeOn = Arrays.stream(firstNode.getPrimaryIndex().split(PROPERTY_SEPARATOR)).map(p -> p + ": row.props." + p).collect(joining(",", "{", "}"));
queryBuilder.append(propertiesToMergeOn);
}
// Closing MERGE or CREATE
queryBuilder.append(") ");
if (firstNode.hasVersionProperty() && hasPrimaryIndex) {
queryBuilder.append(getFragmentForNewOrExistingNodes(firstNode, "n"));
}
queryBuilder.append("SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, $type as type");
List<Map> rows = newNodes.stream().map(node -> node.toRow("nodeRef")).collect(toList());
parameters.put("type", "node");
parameters.put("rows", rows);
if (firstNode.hasVersionProperty()) {
OptimisticLockingConfig olConfig = new OptimisticLockingConfig(rows.size(), firstNode.getLabels(), firstNode.getVersion().getKey());
return statementFactory.statement(queryBuilder.toString(), parameters, olConfig);
}
}
return statementFactory.statement(queryBuilder.toString(), parameters);
}
Aggregations