forked from www-Ye/ChatDocuFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_operater.py
76 lines (57 loc) · 2.47 KB
/
db_operater.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import pandas as pd
import sqlite3
from py2neo import Graph, Node, Relationship
from py2neo import NodeMatcher, RelationshipMatcher
import numpy as np
class Neo4j_DB:
def __init__(self, user, password):
## Connect to the Neo4j database by entering the user password.
self.graph = Graph('http://localhost:7474', auth=(user, password))
# self.graph.delete_all()
self.node_matcher = NodeMatcher(self.graph)
self.relationship_matcher = RelationshipMatcher(self.graph)
def execute_cypher(self, cypher):
return self.graph.run(cypher)
def create_relation(self, node1, node2, re_type):
relation = self.get_relation(node1, node2, re_type).first()
if relation is None:
relation = Relationship(node1, re_type, node2)
self.graph.create(relation)
def get_relation(self, node1, node2, re_type):
return self.relationship_matcher.match((node1, node2), r_type=re_type)
def delete_relation(self, node1, node2, re_type):
relation = self.get_relation(node1, node2, re_type).first()
self.graph.separate(relation)
def get_nodes(self, node_type, name=None, page_id=None):
if name is None:
nodes = self.node_matcher.match(node_type)
else:
if page_id is None:
nodes = self.node_matcher.match(node_type, name=name)
else:
nodes = self.node_matcher.match(node_type, name=name, page_id=page_id)
return nodes
def create_node(self, node_type, name, embedding=None, summary=None, page_id=None, tags=None, text=None):
# node = node_matcher.match(e_type).where(node_id=node_id).first()
node = self.get_nodes(node_type, name, page_id).first()
if node is None:
node = Node(node_type, name=name, embedding=embedding, summary=summary, page_id=page_id, tags=tags, text=text)
self.graph.create(node)
return node
def delete_node(self, node_type, name):
nodes = self.get_nodes(node_type, name)
for node in nodes:
self.graph.delete(node)
class Sqlite_DB:
def __init__(self, db_name):
self.db_name = db_name
def create_table(self):
pass
def open(self):
# Open database connection.
self.conn = sqlite3.connect(self.db_name)
self.cursor = self.conn.cursor()
def close(self):
# Close database connection.
self.cursor.close()
self.conn.close()