Skip to content

Commit

Permalink
Add methods for db label relationship and property
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Jun 13, 2022
1 parent 12abfb8 commit 56c1cf5
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
43 changes: 41 additions & 2 deletions src/aio.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use redis::{aio::ConnectionLike, RedisFuture, cmd};
use redis::{aio::ConnectionLike, RedisFuture, cmd, RedisResult};

use crate::{types::GraphQuery, FromGraphValue, GraphResponse};
use crate::{types::GraphQuery, FromGraphValue, GraphResponse, query};



Expand All @@ -25,6 +25,45 @@ pub trait AsyncGraphCommands: ConnectionLike + Send + Sized {
})
}

fn graph_query_void<'a, Q>(
&'a mut self,
graph: &'a str,
query: Q,
) -> RedisFuture<GraphResponse<()>>
where
Q: Into<GraphQuery> + Send + 'a,
{
Box::pin(async move {
let query = query.into();
cmd(query.read_type())
.arg(graph)
.arg(query.construct_query())
.arg("--compact")
.query_async(self)
.await
})
}

fn labels<'a>(&'a mut self, graph: &'a str) -> RedisFuture<'a, Vec<String>> {
Box::pin(async move {
let data: Vec<Vec<String>> = self.graph_query(graph, query!("CALL db.labels()")).await?.data;
Ok(data.into_iter().map(|mut vec| vec.remove(0)).collect())
})
}

fn property_keys<'a>(&'a mut self, graph: &'a str) -> RedisFuture<'a, Vec<String>> {
Box::pin(async move {
let data: Vec<Vec<String>> = self.graph_query(graph, query!("CALL db.propertyKeys()")).await?.data;
Ok(data.into_iter().map(|mut vec| vec.remove(0)).collect())
})
}

fn relationship_types<'a>(&'a mut self, graph: &'a str) -> RedisFuture<'a, Vec<String>> {
Box::pin(async move {
let data: Vec<Vec<String>> = self.graph_query(graph, query!("CALL db.relationshipTypes()")).await?.data;
Ok(data.into_iter().map(|mut vec| vec.remove(0)).collect())
})
}
}

impl<T> AsyncGraphCommands for T where T: Send + ConnectionLike {}
30 changes: 29 additions & 1 deletion src/sync.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{GraphResponse, FromGraphValue, GraphQuery};
use crate::{GraphResponse, FromGraphValue, GraphQuery, query};
use redis::{cmd, ConnectionLike, RedisResult};

pub trait GraphCommands: ConnectionLike + Sized {
Expand All @@ -14,6 +14,34 @@ pub trait GraphCommands: ConnectionLike + Sized {
.arg("--compact")
.query(self)
}

fn graph_query_void<Q>(
&mut self,
graph: &str,
query: Q,
) -> RedisResult<GraphResponse<()>> where Q: Into<GraphQuery> {
let query = query.into();
cmd(query.read_type())
.arg(graph)
.arg(query.construct_query())
.arg("--compact")
.query(self)
}

fn labels(&mut self, graph: &str) -> RedisResult<Vec<String>> {
let data: Vec<Vec<String>> = self.graph_query(graph, query!("CALL db.labels()"))?.data;
Ok(data.into_iter().map(|mut vec| vec.remove(0)).collect())
}

fn property_keys(&mut self, graph: &str) -> RedisResult<Vec<String>> {
let data: Vec<Vec<String>> = self.graph_query(graph, query!("CALL db.propertyKeys()"))?.data;
Ok(data.into_iter().map(|mut vec| vec.remove(0)).collect())
}

fn relationship_types(&mut self, graph: &str) -> RedisResult<Vec<String>> {
let data: Vec<Vec<String>> = self.graph_query(graph, query!("CALL db.relationshipTypes()"))?.data;
Ok(data.into_iter().map(|mut vec| vec.remove(0)).collect())
}
}

impl<T> GraphCommands for T where T: ConnectionLike {}
Expand Down
10 changes: 9 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
query, GeoPoint, GraphCommands, GraphMap, GraphPath, GraphQuery, GraphValue,
PropertyAccess,
PropertyAccess
};

use paste::paste;
Expand Down Expand Up @@ -213,3 +213,11 @@ fn test_parse_graphtypes() {
)
.unwrap();
}

#[test]
fn test_mappings() {
let con = &mut sync_con();
con.labels("test").unwrap();
con.relationship_types("test").unwrap();
con.property_keys("test").unwrap();
}

0 comments on commit 56c1cf5

Please sign in to comment.