-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dynamic Variable Names #46
Comments
Howdy, Im not sure that I fully understand. The node name, the some_var = 'n23'
p.match.node(some_var).where.property('root') == some_var Also, the Let me know if this fixes your issue or if I missed what you were asking |
Thank you prompt response.
Here, n1, n2, ... etc can be of variable number (e.g. for some query it's n1, n2, n3. For some other query n1, n2, n3, n4, n5, My code is
Now, after this I want to add conditions. Conditions are of the form Earlier I was appending these to a list by extending anon class, and was getting
And after this, I call
which completes the generation. This almost works, except that it is now producing queries like |
I was able to get I did manage to get something going based on what you want using the EDGES = [(1, 'REL1', 2), (1, 'REL2', 3), (2, 'REL3', 3)]
node_prefix = 'n'
relation_prefix = 'r'
edges = []
_rid = 0
for _src_id, relation, _dst_id in EDGES:
_rid += 1
src_var = f'{node_prefix}{_src_id}'
dst_var = f'{node_prefix}{_dst_id}'
rel_var = f'{relation_prefix}{_rid}'
edge = __.node(src_var).rel_out(rel_var, relation).node(dst_var)
edges.append(edge)
conditions = []
NAMES = {'first': 'mark', 'you': 'hrshikeshrt'}
_root = 'some root value'
for _var, _name in NAMES.items():
conditions.append(__().raw(_var).property('name') == _root)
p.Match(*edges)
p.WHERE(__.ConditionalAND(*conditions))
p.Return('*')
|
Thank you again! I'm wondering why we needed to do And on an unrelated note, is there a method that would give the final query with bound parameters substituted? (I was also wondering why they are separated out in the first place, but I'm sure there would be usecases for that) |
The bound params are substituted out for a few reasons:
Yes, there is ConditionalOR object. You could chain them together like this I just looked into the |
I have a testing tool that will substitute the variables. I use it for logging. def _query_debug(query, params):
from string import Template
if not params:
return query
temp = Template(query)
fixed = {}
for k, v in params.items():
if isinstance(v, str):
v = "'{}'".format(v) if v else ''
fixed[k] = v or "''"
try:
return temp.substitute(**fixed)
except Exception as e:
return '{} -- {}'.format(query, params) |
Thank you again for your very quick and very helpful responses. Does the point 2 in your comment mean that there's a way of providing the query generated with |
Which library are you using to run queries in python? It should have a way to pass in parameters And to do named params, you could do something like name = Param(name='var_name', value='Unique Name')
p.Where(__.__name__ == name)
str(p) # where name == $var_name |
I plan to use |
py2neo is great. Here is the base query method that accepts params https://py2neo.org/2021.1/workflow.html#py2neo.Graph.run |
Thank you for this package.
I wanted to know if the nodes can be specified by strings
For example, I want to write a query
MATCH (n) WHERE n.root = "Something"
Now, I can do this using
p.Match.node('n').Where.n.property('root') == "Something"
However, I have to do this for many variables for a longer pattern in
MATCH
, and the namen
is generated dynamically,for example it could be n1, n2, n3 etc depending on which iteration of loop it is in. Is it possible to achieve that?
I tried giving
Where.raw('n')
andWhere._('n')
but both insert aSTATEMENT
word in the query.(Apologies if this is a stupid question)
The text was updated successfully, but these errors were encountered: