All Downloads are FREE. Search and download functionalities are using the official Maven repository.

apoc.cypher.CypherUtils Maven / Gradle / Ivy

There is a newer version: 5.25.1
Show newest version
package apoc.cypher;

import apoc.result.MapResult;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.stream.Stream;
import org.neo4j.graphdb.Transaction;
import org.neo4j.procedure.Name;
import static java.lang.String.format;
import static java.lang.String.join;
import static java.util.stream.Collectors.toList;

public class CypherUtils {
    public static Stream runCypherQuery(Transaction tx, @Name("cypher") String statement, @Name("params") Map params) {
        if (params == null) params = Collections.emptyMap();
        return tx.execute(withParamMapping(statement, params.keySet()), params).stream().map(MapResult::new);
    }

    public static String withParamMapping(String fragment, Collection keys) {
        if (keys.isEmpty()) return fragment;
        String declaration = " WITH " + join(", ", keys.stream().map(s -> format(" $`%s` as `%s` ", s, s)).collect(toList()));
        return declaration + fragment;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy