redis_graph/
commands.rs

1use crate::types::*;
2use redis::{cmd, ConnectionLike, FromRedisValue, RedisResult, ToRedisArgs};
3
4/// Provides a high level synchronous API to work with Redis graph data types.
5/// The graph command becomes directly available on ConnectionLike types from
6/// the redis crate when you import the GraphCommands trait.
7/// ```rust,no_run
8/// # fn run() -> redis::RedisResult<()> {
9/// use redis::Commands;
10/// use redis_graph::{GraphCommands, GraphResultSet};
11///
12/// let client = redis::Client::open("redis://127.0.0.1/")?;
13/// let mut con = client.get_connection()?;
14///
15/// let res:GraphResultSet = con.graph_query(
16///     "my_graph",
17///     "CREATE (:Rider {name:'Valentino Rossi'})-[:rides]->(:Team {name:'Yamaha'})"
18/// )?;
19///
20/// let res_read_only:GraphResultSet = con.graph_ro_query(
21///     "my_graph",
22///     "MATCH (rider:Rider)-[:rides]->(:Team {name:'Yamaha'}) RETURN rider"
23/// )?;
24/// # Ok(()) }
25/// ```
26///
27pub trait GraphCommands: ConnectionLike + Sized {
28    fn graph_query<K: ToRedisArgs, Q: ToRedisArgs>(
29        &mut self,
30        key: K,
31        query: Q,
32    ) -> RedisResult<GraphResultSet> {
33        cmd("GRAPH.QUERY").arg(key).arg(query).query(self)
34    }
35
36    fn graph_ro_query<K: ToRedisArgs, Q: ToRedisArgs>(
37        &mut self,
38        key: K,
39        query: Q,
40    ) -> RedisResult<GraphResultSet> {
41        cmd("GRAPH.RO_QUERY").arg(key).arg(query).query(self)
42    }
43
44    fn graph_profile<K: ToRedisArgs, Q: ToRedisArgs, RV: FromRedisValue>(
45        &mut self,
46        key: K,
47        query: Q,
48    ) -> RedisResult<RV> {
49        cmd("GRAPH.PROFILE").arg(key).arg(query).query(self)
50    }
51
52    fn graph_delete<K: ToRedisArgs>(&mut self, key: K) -> RedisResult<String> {
53        cmd("GRAPH.DELETE").arg(key).query(self)
54    }
55
56    fn graph_explain<K: ToRedisArgs, Q: ToRedisArgs, RV: FromRedisValue>(
57        &mut self,
58        key: K,
59        query: Q,
60    ) -> RedisResult<RV> {
61        cmd("GRAPH.EXPLAIN").arg(key).arg(query).query(self)
62    }
63
64    fn graph_slowlog<K: ToRedisArgs>(&mut self, key: K) -> RedisResult<Vec<SlowLogEntry>> {
65        cmd("GRAPH.SLOWLOG").arg(key).query(self)
66    }
67
68    fn graph_config_set<K: ToRedisArgs, V: ToRedisArgs>(
69        &mut self,
70        name: K,
71        value: V,
72    ) -> RedisResult<bool> {
73        cmd("GRAPH.CONFIG")
74            .arg("SET")
75            .arg(name)
76            .arg(value)
77            .query(self)
78    }
79
80    fn graph_config_get<K: ToRedisArgs, RV: FromRedisValue>(&mut self, name: K) -> RedisResult<RV> {
81        value_from_pair(&cmd("GRAPH.CONFIG").arg("GET").arg(name).query(self)?)
82    }
83
84    fn graph_config_get_all(&mut self) -> RedisResult<GraphConfig> {
85        cmd("GRAPH.CONFIG").arg("GET").arg("*").query(self)
86    }
87}
88
89impl<T> GraphCommands for T where T: ConnectionLike {}