Giter Site home page Giter Site logo

variables as parameters about hasbolt HOT 2 CLOSED

zmactep avatar zmactep commented on June 1, 2024
variables as parameters

from hasbolt.

Comments (2)

zmactep avatar zmactep commented on June 1, 2024

Hi, @Tshimanga!

The question is more about Cypher statement system then the driver itself. So, let us try to understand how this things works. There are no any string interpolation or something like this. Driver itself follow the protocol and do not provide any ORM or template system.

Each query is just a constant string, that is sent to the server. So we can run simple queries, that are presented by Text datatype, like "MATCH (n:Person {name: "Carl"}) RETURN n". So, this query will run just the same operation each time it is sent.

But of course we want some dynamic queries, where we can change values as we need:

  • "MATCH (n:Person {name: "Carl"}) RETURN n"
  • "MATCH (n:Person {name: "John"}) RETURN n"
  • etc
    To make this possible we may try to construct a new Text request each time we send a request, like:
makeCypher :: String -> Text
makeCypher name = T.pack $ ""MATCH (n:Person {name: \"" ++ name ++ "\"}) RETURN n"

But this way is ugly in many cases. First, we have to construct this string and it is unpleasant itself. And second, neo4j server cannot cache such queries, as it see them as different statements (that is why usage of string interpolation is a bad idea here). To fix this problem we can use query parameters.

Neo4j lets us to send a statement and parameters separately. So it will cache our statements and just change some values in them. Parameters are presented as a map with string keys. So, we can place some {hole}s inside our statement and the name of the hole will we a key in such map. The system is pretty reach, as it let us use not only primitive types, but also maps and lists. Here is some examples:

let name = T "Carl"
let paramsMap = M (fromList [("name", name)])
queryP "MATCH (n:Person) WHERE n.name = {name} RETURN n" (fromList [("name", name)])
queryP "MATCH (n:Person {params}) RETURN n" (fromList [("params", paramsMap)])

I hope, it has clarified the situation a bit.

from hasbolt.

Tshimanga avatar Tshimanga commented on June 1, 2024

Thanks @zmactep

from hasbolt.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.