Skip to content

Commit 10208aa

Browse files
authored
Improve project documentation examples (#23)
1 parent 0bb3433 commit 10208aa

File tree

3 files changed

+52
-160
lines changed

3 files changed

+52
-160
lines changed

README.md

Lines changed: 17 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
`rsmgclient` is a Memgraph database adapter for Rust programming language.
66

7-
rsmgclient module is the current implementation of the adapter. It is implemented in C as a wrapper
7+
rsmgclient crate is the current implementation of the adapter. It is implemented as a wrapper
88
around [mgclient](https://github.com/memgraph/mgclient), the official Memgraph client library.
99

1010
## Prerequisites
1111

1212
### Installation
1313

14-
`rsmgclient` is a C wrapper around the [mgclient](https://github.com/memgraph/mgclient) Memgraph
14+
`rsmgclient` is a wrapper around the [mgclient](https://github.com/memgraph/mgclient) Memgraph
1515
client library. To install it from sources you will need:
1616
- [Rust](https://doc.rust-lang.org/cargo/getting-started/installation.html) - 1.42.0 or above
1717
- A C compiler supporting C11 standard
@@ -47,47 +47,25 @@ Online documentation can be found on [docs.rs pages](https://docs.rs/rsmgclient/
4747

4848
## Code sample
4949

50-
Here is an example of an interactive session showing some of the basic commands:
50+
Here is an example showing some of the basic commands:
5151

5252
```rust
5353
use rsmgclient::{ConnectParams, Connection};
5454

55+
let connect_params = ConnectParams {
56+
host: Some(String::from("localhost")),
57+
..Default::default()
58+
};
59+
let mut connection = Connection::connect(&connect_params)?;
5560

56-
fn main(){
57-
// Parameters for connecting to database.
58-
let connect_params = ConnectParams {
59-
host: Some(String::from("localhost")),
60-
..Default::default()
61-
};
62-
63-
// Make a connection to the database.
64-
let mut connection = match Connection::connect(&connect_params) {
65-
Ok(c) => c,
66-
Err(err) => panic!("{}", err)
67-
};
68-
69-
// Execute a query.
70-
let query = "CREATE (u:User {name: 'Alice'})-[:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, m";
71-
match connection.execute(query, None) {
72-
Ok(columns) => println!("Columns: {}", columns.join(", ")),
73-
Err(err) => panic!("{}", err)
74-
};
75-
76-
// Fetch all query results.
77-
match connection.fetchall() {
78-
Ok(records) => {
79-
for value in &records[0].values {
80-
println!("{}", value);
81-
}
82-
},
83-
Err(err) => panic!("{}", err)
84-
};
85-
86-
87-
// Commit any pending transaction to the database.
88-
match connection.commit() {
89-
Ok(()) => {},
90-
Err(err) => panic!("{}", err)
91-
};
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);
9268
}
69+
70+
connection.commit()?;
9371
```

src/connection/mod.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ use std::vec::IntoIter;
3131
/// Connecting to localhost database, running on default port 7687.
3232
/// ```
3333
/// use rsmgclient::{ConnectParams, Connection};
34+
/// # use rsmgclient::{MgError};
35+
/// # fn connect() -> Result<(), MgError> {
3436
///
3537
/// let connect_params = ConnectParams {
3638
/// host: Some(String::from("localhost")),
3739
/// ..Default::default()
3840
/// };
3941
///
40-
/// let mut connection = match Connection::connect(&connect_params) {
41-
/// Ok(c) => c,
42-
/// Err(err) => panic!("{}", err)
43-
/// };
42+
/// let mut connection = Connection::connect(&connect_params)?;
43+
/// # Ok(()) }
4444
/// ```
4545
pub struct ConnectParams {
4646
/// Port number to connect to at the server host. Default port is 7687.
@@ -117,36 +117,25 @@ pub enum SSLMode {
117117
///
118118
/// ```
119119
/// use rsmgclient::{ConnectParams, Connection};
120+
/// # use rsmgclient::{MgError};
121+
/// # fn execute_query() -> Result<(), MgError> {
120122
///
121123
/// let connect_params = ConnectParams {
122124
/// host: Some(String::from("localhost")),
123125
/// ..Default::default()
124126
/// };
127+
/// let mut connection = Connection::connect(&connect_params)?;
125128
///
126-
/// let mut connection = match Connection::connect(&connect_params) {
127-
/// Ok(c) => c,
128-
/// Err(err) => panic!("{}", err)
129-
/// };
129+
/// let query = "CREATE (u:User {name: 'Alice'})-[l:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, l, m";
130+
/// connection.execute(query, None)?;
130131
///
131-
/// let query = "CREATE (u:User {name: 'Alice'})-[:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, m";
132-
/// match connection.execute(query, None) {
133-
/// Ok(columns) => println!("Columns: {}", columns.join(", ")),
134-
/// Err(err) => panic!("{}", err)
135-
/// };
132+
/// let records = connection.fetchall()?;
133+
/// for value in &records[0].values {
134+
/// println!("{}", value);
135+
/// }
136136
///
137-
/// match connection.fetchall() {
138-
/// Ok(records) => {
139-
/// for value in &records[0].values {
140-
/// println!("{}", value);
141-
/// }
142-
/// },
143-
/// Err(err) => panic!("{}", err)
144-
/// };
145-
///
146-
/// match connection.commit() {
147-
/// Ok(()) => {},
148-
/// Err(err) => panic!("{}", err)
149-
/// };
137+
/// connection.commit()?;
138+
/// # Ok(()) }
150139
/// ```
151140
pub struct Connection {
152141
mg_session: *mut bindings::mg_session,
@@ -290,16 +279,16 @@ impl Connection {
290279
///
291280
/// ```
292281
/// use rsmgclient::{ConnectParams, Connection};
282+
/// # use rsmgclient::{MgError};
283+
/// # fn connect() -> Result<(), MgError> {
293284
///
294285
/// let connect_params = ConnectParams {
295286
/// host: Some(String::from("localhost")),
296287
/// ..Default::default()
297288
/// };
298289
///
299-
/// let connection = match Connection::connect(&connect_params) {
300-
/// Ok(c) => c,
301-
/// Err(err) => panic!("{}", err)
302-
/// };
290+
/// let mut connection = Connection::connect(&connect_params)?;
291+
/// # Ok(()) }
303292
/// ```
304293
pub fn connect(param_struct: &ConnectParams) -> Result<Connection, MgError> {
305294
let mg_session_params = unsafe { bindings::mg_session_params_make() };

src/main.rs

Lines changed: 16 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -12,106 +12,31 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use rsmgclient::{ConnectParams, Connection, QueryParam};
16-
use std::collections::HashMap;
15+
use rsmgclient::{ConnectParams, Connection, MgError};
1716

18-
fn main() {
19-
let connect_prms = ConnectParams {
17+
fn execute_query() -> Result<(), MgError> {
18+
let connect_params = ConnectParams {
2019
host: Some(String::from("localhost")),
21-
lazy: true,
22-
autocommit: false,
2320
..Default::default()
2421
};
22+
let mut connection = Connection::connect(&connect_params)?;
2523

26-
let mut connection = match Connection::connect(&connect_prms) {
27-
Ok(c) => c,
28-
Err(err) => panic!("{}", err),
29-
};
30-
31-
let mut params: HashMap<String, QueryParam> = HashMap::new();
32-
params.insert(
33-
String::from("name"),
34-
QueryParam::String(String::from("Alice")),
35-
);
36-
37-
let query = String::from(
38-
"CREATE (u:User {name: 'Alice'})-[:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, m",
39-
);
40-
let columns = match connection.execute(&query, Some(&params)) {
41-
Ok(x) => x,
42-
Err(err) => panic!("Query failed: {}", err),
43-
};
44-
24+
let query =
25+
"CREATE (u:User {name: 'Alice'})-[l:Likes]->(m:Software {name: 'Memgraph'}) RETURN u, l, m";
26+
let columns = connection.execute(query, None)?;
4527
println!("Columns: {}", columns.join(", "));
4628

47-
loop {
48-
match connection.fetchone() {
49-
Ok(res) => match res {
50-
Some(x) => {
51-
println!("Number of rows: 1");
52-
print!("Row: ");
53-
for val in &x.values {
54-
print!("val: {} ", val);
55-
}
56-
println!();
57-
}
58-
None => break,
59-
},
60-
Err(err) => panic!("Fetch failed: {}", err),
61-
}
62-
}
63-
64-
let summary = connection.summary().unwrap();
65-
for (key, val) in summary {
66-
println!("{}: {}", key, val);
29+
let records = connection.fetchall()?;
30+
for value in &records[0].values {
31+
println!("{}", value);
6732
}
6833

69-
match connection.execute(&query, Some(&params)) {
70-
Ok(_x) => {}
71-
Err(err) => panic!("Query failed: {}", err),
72-
};
73-
74-
loop {
75-
let size = 3;
76-
match connection.fetchmany(Some(size)) {
77-
Ok(res) => {
78-
println!("Number of rows: {}", res.len());
79-
for record in &res {
80-
print!("Row: ");
81-
for val in &record.values {
82-
print!("val: {} ", val);
83-
}
84-
println!();
85-
}
86-
if res.len() != size as usize {
87-
break;
88-
}
89-
}
90-
Err(err) => panic!("Fetch failed: {}", err),
91-
}
92-
}
93-
94-
match connection.execute(&query, Some(&params)) {
95-
Ok(_x) => {}
96-
Err(err) => panic!("{}", err),
97-
}
98-
99-
match connection.fetchall() {
100-
Ok(records) => {
101-
println!("Number of rows: {}", records.len());
102-
for record in records {
103-
print!("Row: ");
104-
for val in &record.values {
105-
print!("val: {} ", val);
106-
}
107-
println!();
108-
}
109-
}
110-
Err(err) => panic!("Fetching failed: {}", err),
111-
}
34+
connection.commit()?;
35+
Ok(())
36+
}
11237

113-
match connection.commit() {
114-
Ok(_) => {}
115-
Err(err) => panic!("Fetching failed: {}", err),
38+
fn main() {
39+
if let Err(error) = execute_query() {
40+
panic!("{}", error)
11641
}
11742
}

0 commit comments

Comments
 (0)