thingdom/node-neo4j

Using params in regex passes through to neo4j

richardwillars opened this issue ยท 5 comments

Using the following query:
MATCH (f:Foo) WHERE f.name =~ '.*{name}.*' RETURN f LIMIT 100

Which when executed with

var fields = { name: 'bar' };
db.cypher({ query: query, params: fields })

Throws up an error:

{ [neo4j.DatabaseError: [Neo.DatabaseError.Statement.ExecutionFailure] java.util.regex.PatternSyntaxException: Illegal repetition near index 1
.{name}.

It looks like it's passing the parameter name with the curly braces through the library to neo4j. Should it scan for parameters and inject them into the query like it does with strings etc? If not, what is the best practice for building regular expressions based on user input?

Hey @richardwillars, I'm not 100% sure (cc @jexp?), but I think params are all-or-nothing, so if you want to use a regex value, it has to be the param.

So try:

MATCH (f:Foo) WHERE f.name =~ {name} RETURN f LIMIT 100
var fields = { name: '.*bar.*' };
db.cypher({ query: query, params: fields })

Damn, that was too easy! Works fine and is quite logical really.. probably just needs adding to the documentation more than anything.

Thanks

Great. Okay, will keep it in mind for node-neo4j documentation, but cc @jexp for Cypher docs too. =)

jexp commented

You can also use

a.name =~ '.'+{name}+'.'

Von meinem iPhone gesendet

Am 20.08.2015 um 17:47 schrieb Aseem Kishore notifications@github.com:

Great. Okay, will keep it in mind for node-neo4j documentation, but cc @jexp for Cypher docs too. =)

โ€”
Reply to this email directly or view it on GitHub.

Please add this to the documentation.