|
4 | 4 |
|
5 | 5 | `rsmgclient` is a Memgraph database adapter for Rust programming language. |
6 | 6 |
|
7 | | -rsmgclient crate is the current implementation of the adapter. It is implemented as a wrapper |
8 | | -around [mgclient](https://github.com/memgraph/mgclient), the official Memgraph client library. |
| 7 | +rsmgclient crate is the current implementation of the adapter. It is |
| 8 | +implemented as a wrapper around |
| 9 | +[mgclient](https://github.com/memgraph/mgclient), the official Memgraph client |
| 10 | +library. |
9 | 11 |
|
10 | 12 | ## Prerequisites |
11 | 13 |
|
12 | 14 | ### Installation |
13 | 15 |
|
14 | | -`rsmgclient` is a wrapper around the [mgclient](https://github.com/memgraph/mgclient) Memgraph |
15 | | -client library. To install it from sources you will need: |
16 | | - - [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html) - 1.42.0 or above |
17 | | - - A C compiler supporting C11 standard |
18 | | - - [mgclient](https://github.com/memgraph/mgclient) has to be installed because `rsmgclient` statically links `mgclient` |
19 | | - - [Memgraph](https://docs.memgraph.com/memgraph/quick-start) |
| 16 | +`rsmgclient` is a wrapper around the |
| 17 | +[mgclient](https://github.com/memgraph/mgclient) Memgraph client library. To |
| 18 | +install it from sources you will need: |
20 | 19 |
|
21 | | -Once prerequisites are met, if you want to use it as library for your own Rust project, you can |
22 | | -install rsmgclient using `cargo` to download it from crates.io: |
23 | | -``` |
24 | | -$ cargo install rsmgclient |
| 20 | +- [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html) |
| 21 | + 1.42.0 or above |
| 22 | +- A C compiler supporting C11 standard |
| 23 | +- [Memgraph](https://docs.memgraph.com/memgraph/quick-start) |
| 24 | + |
| 25 | +Once prerequisites are met, if you want to use it as library for your own Rust |
| 26 | +project, you can install rsmgclient using `cargo` to download it from |
| 27 | +crates.io: |
| 28 | + |
| 29 | +```bash |
| 30 | +cargo install rsmgclient |
25 | 31 | ``` |
26 | 32 |
|
27 | 33 | ### Building from source |
28 | 34 |
|
29 | | -To use `rsmgclient` for contributing or just looking closely how it is made, you will need: |
30 | | - - Cloned [rsmgclient](https://github.com/memgraph/rsmgclient) repository |
31 | | - - [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html) - 1.42.0-nightly or above |
32 | | - - A C compiler supporting C11 standard |
33 | | - - [mgclient](https://github.com/memgraph/mgclient) |
34 | | - - [Memgraph](https://docs.memgraph.com/memgraph/quick-start) |
| 35 | +To use `rsmgclient` for contributing or just looking closely how it is made, |
| 36 | +you will need: |
35 | 37 |
|
36 | | -Once rsmgclient is installed, you will need to build it and then you can run the test suite to verify |
37 | | -it is working correctly. |
| 38 | +- Cloned [rsmgclient](https://github.com/memgraph/rsmgclient) repository |
| 39 | +- [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html) |
| 40 | + 1.42.0-nightly or above |
| 41 | +- A C compiler supporting C11 standard |
| 42 | +- [Memgraph](https://docs.memgraph.com/memgraph/quick-start) |
38 | 43 |
|
39 | | -``` |
40 | | -$ cargo build |
41 | | -$ cargo test |
| 44 | +Once rsmgclient is cloned, you will need to build it and then you can run |
| 45 | +the test suite to verify it is working correctly. |
| 46 | + |
| 47 | +```bash |
| 48 | +cargo build |
| 49 | +cargo test |
42 | 50 | ``` |
43 | 51 |
|
44 | 52 | ## Documentation |
45 | 53 |
|
46 | | -Online documentation can be found on [docs.rs pages](https://docs.rs/rsmgclient/). |
| 54 | +Online documentation can be found on [docs.rs |
| 55 | +pages](https://docs.rs/rsmgclient/). |
47 | 56 |
|
48 | 57 | ## Code sample |
49 | 58 |
|
50 | 59 | Here is an example showing some of the basic commands: |
51 | 60 |
|
52 | 61 | ```rust |
53 | | -use rsmgclient::{ConnectParams, Connection}; |
54 | | - |
55 | | -let connect_params = ConnectParams { |
56 | | - host: Some(String::from("localhost")), |
57 | | - ..Default::default() |
58 | | -}; |
59 | | -let mut connection = Connection::connect(&connect_params)?; |
60 | | - |
61 | | -let query = "CREATE (u:User {name: 'Alice'})-[l:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, l, m"; |
62 | | -let columns = connection.execute(query, None)?; |
63 | | -println!("Columns: {}", columns.join(", ")); |
64 | | - |
65 | | -let records = connection.fetchall()?; |
66 | | -for value in &records[0].values { |
67 | | - println!("{}", value); |
| 62 | +use rsmgclient::{ConnectParams, Connection, MgError, Value}; |
| 63 | + |
| 64 | +fn execute_query() -> Result<(), MgError> { |
| 65 | + // Connect to Memgraph. |
| 66 | + let connect_params = ConnectParams { |
| 67 | + host: Some(String::from("localhost")), |
| 68 | + ..Default::default() |
| 69 | + }; |
| 70 | + let mut connection = Connection::connect(&connect_params)?; |
| 71 | + |
| 72 | + // Create simple graph. |
| 73 | + connection.execute_without_results( |
| 74 | + "CREATE (p1:Person {name: 'Alice'})-[l1:Likes]->(m:Software {name: 'Memgraph'}) \ |
| 75 | + CREATE (p2:Person {name: 'John'})-[l2:Likes]->(m);", |
| 76 | + )?; |
| 77 | + |
| 78 | + // Fetch the graph. |
| 79 | + let columns = connection.execute("MATCH (n)-[r]->(m) RETURN n, r, m;", None)?; |
| 80 | + println!("Columns: {}", columns.join(", ")); |
| 81 | + for record in connection.fetchall()? { |
| 82 | + for value in record.values { |
| 83 | + match value { |
| 84 | + Value::Node(node) => print!("{}", node), |
| 85 | + Value::Relationship(edge) => print!("-{}-", edge), |
| 86 | + value => print!("{}", value), |
| 87 | + } |
| 88 | + } |
| 89 | + println!(); |
| 90 | + } |
| 91 | + connection.commit()?; |
| 92 | + |
| 93 | + Ok(()) |
68 | 94 | } |
69 | 95 |
|
70 | | -connection.commit()?; |
| 96 | +fn main() { |
| 97 | + if let Err(error) = execute_query() { |
| 98 | + panic!("{}", error) |
| 99 | + } |
| 100 | +} |
71 | 101 | ``` |
0 commit comments