all/
all.rs

1use apache_age::sync::{AgeClient, Client};
2use apache_age::{AgType, NoTls, Vertex};
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Serialize, Deserialize, Clone)]
6struct Person {
7    pub name: String,
8    pub surname: String,
9}
10
11pub fn main() {
12    // Create client
13    let mut client = Client::connect_age(
14        "host=localhost user=postgres password=passwd port=8081",
15        NoTls,
16    )
17    .unwrap();
18
19    // Create graph
20    client.create_graph("my_apache_graph").unwrap();
21    assert!(client.graph_exists("my_apache_graph").unwrap());
22
23    // Using a simple postgres query to manipulate graph
24    client
25        .simple_query(
26            &("SELECT * FROM cypher('".to_string()
27                + "my_apache_graph"
28                + "', $$ "
29                + "CREATE(n:Person {name: 'T', surname: 'Doe'}) "
30                + "RETURN n "
31                + "$$) AS (v agtype)"),
32        )
33        .unwrap();
34
35    // Using a normal postgres query to operate on graph
36    match client.query(
37        &("SELECT v FROM ag_catalog.cypher('".to_string()
38            + "my_apache_graph"
39            + "', $$ "
40            + "MATCH(n: Person) WHERE n.name='T' "
41            + "RETURN n "
42            + "$$) AS (v ag_catalog.agtype)"),
43        &[],
44    ) {
45        Err(_e) => {}
46        Ok(query) => {
47            for row in query {
48                // Vertex usage
49                let person_vertex: Vertex<Person> = row.get(0);
50                assert_eq!(person_vertex.label(), "Person");
51                assert_eq!(person_vertex.properties().surname, "Doe");
52            }
53        }
54    }
55
56    // Using execute_cypher with some input variables
57    assert!(client
58        .execute_cypher(
59            "my_apache_graph",
60            "CREATE(n: Person {name: $name, surname: $surname})",
61            Some(AgType::<Person>(Person {
62                name: "John".into(),
63                surname: "Doe".into(),
64            })),
65        )
66        .is_ok());
67
68    // Using execute_cypher without some input variables
69    assert!(client
70        .execute_cypher::<()>(
71            "my_apache_graph",
72            "CREATE(n: Person {name: 'Ask', surname: 'Me'})",
73            None,
74        )
75        .is_ok());
76
77    // Using query_cypher without parameters
78    let rows = client
79        .query_cypher::<()>(
80            "my_apache_graph",
81            "
82            MATCH (n: Person) 
83            WHERE n.name = 'Ask' 
84            RETURN {name: n.name, surname: n.surname}
85        ",
86            None,
87        )
88        .unwrap();
89
90    let x: AgType<Person> = rows[0].get(0);
91    assert_eq!(x.0.surname, "Me");
92
93    // Prepared statements
94    let statement = client
95        .prepare_cypher(
96            "my_apache_graph",
97            "MATCH (n: Person) WHERE n.name = 'John' RETURN n",
98            false,
99        )
100        .unwrap();
101
102    let x = client.query(&statement, &[]).unwrap();
103    let john_doe: Vertex<Person> = x[0].get(0);
104    assert_eq!(john_doe.properties().surname, "Doe");
105
106    // Constraints
107    client
108        .required_constraint("my_apache_graph", "Person", "myconstraint", "surname")
109        .unwrap();
110
111    client
112        .unique_index("my_apache_graph", "Person", "myuniq", "name")
113        .unwrap();
114
115    assert!(client
116        .execute_cypher::<()>(
117            "my_apache_graph",
118            "CREATE (p: Person { name: 'No surname' })",
119            None
120        )
121        .is_err());
122
123    assert!(client
124        .execute_cypher::<()>(
125            "my_apache_graph",
126            "CREATE (p: Person { name: 'John', surname: 'Repeated name' })",
127            None
128        )
129        .is_err());
130
131    // Drop / destroy graph
132    client.drop_graph("my_apache_graph").unwrap();
133}