2

The neo4j_run API allows you to enter a map, but I don't know the syntax of the query. In python, this is a simple {x}. I cannot find an example of the neo4j_map syntax.

const char *q = "MATCH (p:Person) WHERE p.age > {age} RETURN p.name AS name, p.age as AGE";
neo4j_run(session, q, my_map)

How should I (a) construct my_map and (b) indicate the fields in the query?

UPDATE: For the first part, this test shows how to construct the map. Copied here for clarity:

START_TEST (invalid_map_value)
{
    neo4j_map_entry_t map_entries[] =
        { { .key = neo4j_string("bernie"), .value = neo4j_int(1) },
          { .key = neo4j_int(1), .value = neo4j_int(2) } };
    neo4j_value_t value = neo4j_map(map_entries, 2);
    ck_assert(neo4j_is_null(value));
    ck_assert_int_eq(errno, NEO4J_INVALID_MAP_KEY_TYPE);
}
END_TEST
Brian Dolan
  • 3,086
  • 2
  • 24
  • 35

1 Answers1

2

The map passed as the third argument to neo4j_run is the parameters for the query. Constructing the map takes an array of entries and its length. If it's just a single entry map, you could pass a pointer to a single neo4j_map_entry_t. For your example:

const char *q = "MATCH (p:Person) WHERE p.age > {age} RETURN p.name AS name, p.age as AGE";
neo4j_map_entry_t map_entry = neo4j_map_entry("age", 28);
neo4j_value_t params = neo4j_map(&map_entry, 1);

neo4j_run(session, q, params);

If there are multiple parameters, build an array of map entries, e.g.:

const char *q = "MATCH (p:Person) WHERE {min_age} < p.age < {max_age} RETURN p.name AS name, p.age as AGE";
neo4j_map_entry_t map_entries[2];
map_entries[0] = neo4j_map_entry("min_age", 28);
map_entries[1] = neo4j_map_entry("max_age", 30);
neo4j_value_t params = neo4j_map(map_entries, 2);

neo4j_run(session, q, params);

You can also build the map_entry_t using an initializer, as in the example test you copied. But it's usually clearer to use the neo4j_map_entry constructor.

Chris Leishman
  • 1,777
  • 13
  • 19
  • Is there an easy way to send in a string array. For instance if the query includes `WHERE p.name IN ["Brian", "Chris"]` ? I cannot find something like a `neo4j_string_array` type. – Brian Dolan Oct 24 '16 at 19:42
  • I had two problems with this. (a) I had to wrap the integer with `neo4j_map_entry("min_age", neo4j_int(28))` and (b) when trying to use a string like `neo4j_map_entry("name", "brian")` or `neo4j_map_entry("name", neo4j_string("brian"))` I got "no matching call for strlen". Any thoughts? – Brian Dolan Oct 24 '16 at 22:31
  • Hey @BrianDolan - I'm unsure why you'd get that. `neo4j_string(s)` is macro that expands to `neo4j_ustring((s), strlen(s))`. But `strlen` is defined in `string.h`, which is included by `neo4j-client.h`. Perhaps you can supply the code, and we can try to reproduce? – Chris Leishman Nov 01 '16 at 15:32
  • 2
    @BrianDolan, you can use [`neo4j_list`](https://cleishm.github.io/libneo4j-client/doc/latest/neo4j-client_8h.html#a93b3e42c87b79fa751c2e85ab0375634) for building a string array. – Chris Leishman Nov 01 '16 at 15:41
  • I found an example in [your github](https://github.com/cleishm/libneo4j-client/blob/master/tests/check_values.c#L259-L313) – Brian Dolan Nov 02 '16 at 20:04
  • Another thing to note, the replacement is viewed as an array, you can do to `WHERE IN {ids}` not, for instance `WHERE IN [{ids}]`. That wasn't clear to me. – Brian Dolan Nov 02 '16 at 21:30