You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@age.apache.org by GitBox <gi...@apache.org> on 2022/04/26 04:40:02 UTC

[GitHub] [incubator-age] lnoir opened a new issue, #210: Dynamic queries

lnoir opened a new issue, #210:
URL: https://github.com/apache/incubator-age/issues/210

   I came across [this question](https://github.com/apache/incubator-age/issues/113) which has an example for my scenario, however I'm having trouble with it.
   
   If I have a vertex I want to create or modify, how do I create a dynamic query in the case that:
   * there are a predefined set of modifiable properties
   * it isn't known _which_ properties will be present at the time the query is run
   * at least one property will be present
   
   Below are a couple of contrived but concrete examples of queries modifying different properties on the same vertex. Assume the vertex with `user_id: 1` already exists.
   
   ### Example 1
   Query updating a user's favourite things:
   ```
   SELECT * FROM ag_catalog.cypher('userdata', $$
       MATCH (f:favs {user_id: 1})
       SET f.food = $food, f.drink = $drink, f.movie = $movie
       RETURN f
   $$) as (f ag_catalog.agtype);
   ```
   Data:
   
   `{food: 'pizza', drink: 'strawberry milkshake', movie: 'Donne Darko'}`
   
   ### Example 2
   Same vertex, but different properties modified:
   ```
   SELECT * FROM ag_catalog.cypher('userdata', $$
       MATCH (f:favs {user_id: 1})
       SET f.food = $food, f.music = $music
       RETURN f
   $$) as (f ag_catalog.agtype);
   ```
   Data:
   
   `{food: 'calzone', music: 'The Lark Ascending'}`
   
   From my experimentation with prepared statements, I've found I can create and modify vertices, but the properties must be known beforehand. I'd like to be able to build dynamic insert queries and update queries where the properties aren't known until run time.
   
   I want to avoid having to write multiple prepared statements to do essentially the same thing. Is it possible?
   
   
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@age.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-age] lnoir commented on issue #210: Dynamic queries

Posted by GitBox <gi...@apache.org>.
lnoir commented on issue #210:
URL: https://github.com/apache/incubator-age/issues/210#issuecomment-1109420302

   Hi @JoshInnis, thanks for your quick response and help. I figured out the problem and realised the correct approach. :+1: 
   
   It wasn't clear to me how I should construct and execute the queries. Reading the documentation, named parameters (`$param_name` syntax) _seemed_ to be restricted to prepared statements. Coupled with reading elsewhere that AGE doesn't support `$n` syntax in the cypher query itself, I was a little confused.
   
   I'm using NodeJS with the [pg](https://github.com/brianc/node-postgres) library. I'll leave a working example here for anyone else who might find it useful. This works:
   
   ```
   ...
   
   const id = 'e4510317-67dc-4002-b28d-347699c8f8ca';
   const query = ` 
       SELECT * FROM ag_catalog.cypher('minmail', $$
       MATCH (u:User {_id: $userId})
       RETURN u
   `;
   const params = {userId: id};
   const json = JSON.stringify(params);
   const values = [json];
   pool.query(query, values).then(...).catch(...);
   
   ...
   ```
   
   Of course, I aim to use prepared statements too, but just didn't want to have dozens of prepared statements that were essentially just slight variations on the same thing.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@age.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-age] JoshInnis commented on issue #210: Dynamic queries

Posted by GitBox <gi...@apache.org>.
JoshInnis commented on issue #210:
URL: https://github.com/apache/incubator-age/issues/210#issuecomment-1109366211

   Hello,
   
    There has been a solution proposed to allow a map to be passed to the cypher function. For example, something similar to:
   ```
   SELECT * FROM ag_catalog.cypher('userdata', $$
       CREATE (f $props)
       RETURN f
   $$, $1) as (f ag_catalog.agtype);
   ```
   
   That solution has not been implemented yet. 
   
   However, currently AGE fully supports dynamic sql, so you could construct a query in real time and run it, though can be performance issues in situations and we would need to know more about your situation to know.  You can check out an example of this here: https://github.com/JoshInnis/AGE-Machine-Learning/blob/main/graph-creation/complete_graph.sql . That is creating a dynamic number of nodes and edges, not properties, but might help you out.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@age.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [incubator-age] lnoir closed issue #210: Dynamic queries

Posted by GitBox <gi...@apache.org>.
lnoir closed issue #210: Dynamic queries
URL: https://github.com/apache/incubator-age/issues/210


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@age.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org