-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
58 lines (50 loc) · 2.43 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import psycopg2
import age
GRAPH_NAME = "test"
conn = psycopg2.connect(host="0.0.0.0", port="5432", dbname="test", user="rrr", password="")
print(conn.status)
age.setUpAge(conn, GRAPH_NAME)
with conn.cursor() as cursor:
try :
cursor.execute("""SELECT * from cypher(%s, $$ CREATE (n:Person {name: 'Joe', title: 'Developer'}) $$) as (v agtype); """, (GRAPH_NAME,) )
cursor.execute("""SELECT * from cypher(%s, $$ CREATE (n:Person {name: 'Smith', title: 'Developer'}) $$) as (v agtype); """, (GRAPH_NAME,))
cursor.execute("""SELECT * from cypher(%s, $$
CREATE (n:Person {name: 'Tom', title: 'Manager'})
RETURN n
$$) as (v agtype); """, (GRAPH_NAME,))
for row in cursor:
print("CREATED::", row[0])
cursor.execute("""SELECT * from cypher(%s, $$
MATCH (a:Person {name: 'Joe'}), (b:Person {name: 'Smith'}) CREATE (a)-[r:workWith {weight: 5}]->(b)
$$) as (v agtype); """, (GRAPH_NAME,))
cursor.execute("""SELECT * from cypher(%s, $$
MATCH (a:Person {name: 'Smith'}), (b:Person {name: 'Tom'}) CREATE (a)-[r:workWith {weight: 3}]->(b)
$$) as (v agtype); """, (GRAPH_NAME,))
# When data inserted or updated, You must commit.
conn.commit()
except Exception as ex:
print(type(ex), ex)
# if exception occurs, you must rollback all transaction.
conn.rollback()
with conn.cursor() as cursor:
try:
print("------- [Select Vertices] --------")
cursor.execute("""SELECT * from cypher(%s, $$ MATCH (n) RETURN n $$) as (v agtype); """, (GRAPH_NAME,))
for row in cursor:
vertex = row[0]
print(vertex.id, vertex.label, vertex["name"], vertex["title"])
print("-->", vertex)
print(type(cursor))
print("------- [Select Paths] --------")
cursor.execute("""SELECT * from cypher(%s, $$ MATCH p=()-[]->() RETURN p LIMIT 10 $$) as (v agtype); """, (GRAPH_NAME,))
for row in cursor:
path = row[0]
v1 = path[0]
e1 = path[1]
v2 = path[2]
print(v1.gtype , v1["name"], e1.gtype , e1.label, e1["weight"], v2.gtype , v2["name"])
print("-->", path)
except Exception as ex:
print(type(ex), ex)
# if exception occurs, you must rollback even though just retrieving.
conn.rollback()