-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
94 lines (83 loc) · 3.39 KB
/
database.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from dotenv import load_dotenv
import os
from psycopg2 import connect, OperationalError
class DatabaseManager:
def __init__(self):
load_dotenv()
self.connection = None
self.create_connection()
def create_connection(self):
try:
connection = connect(os.getenv('DATABASE_URL'), sslmode='require')
self.connection = connection
print("Connection to PostgreSQL DB successful")
except OperationalError as e:
print(f"The error '{e}' occurred")
def close_connection(self, debug=True):
if self.connection is not None:
self.connection.close()
self.connection = None
if debug:
print("Database connection closed.")
def execute_query(self, query):
if self.connection is not None:
self.connection.autocommit = True
cursor = self.connection.cursor()
try:
cursor.execute(query)
print("Query executed successfully")
except OperationalError as e:
print(f"The error '{e}' occurred")
finally:
cursor.close()
def get_corpus_count(self):
if self.connection is not None:
cursor = self.connection.cursor()
try:
cursor.execute("SELECT COUNT(*) FROM corpuses")
count = cursor.fetchone()
return count[0]
except OperationalError as e:
print(f"The error '{e}' occurred")
def get_entity_count(self):
# get the unique count of wiki_ids in the corpuses table
# row -> data (column) -> entities (key) (type: list of objects) -> wiki_id (key) (type: int)
if self.connection is not None:
cursor = self.connection.cursor()
try:
cursor.execute("""
SELECT COUNT(entities->>'wiki_id')
FROM corpuses, jsonb_array_elements(data->'entities') AS entities;
""")
count = cursor.fetchone()
return count[0]
except OperationalError as e:
print(f"The error '{e}' occurred")
def get_entity_count_unique(self):
# get the unique count of wiki_ids in the corpuses table
# row -> data (column) -> entities (key) (type: list of objects) -> wiki_id (key) (type: int)
if self.connection is not None:
cursor = self.connection.cursor()
try:
cursor.execute("""
SELECT COUNT(DISTINCT entities->>'wiki_id')
FROM corpuses, jsonb_array_elements(data->'entities') AS entities;
""")
count = cursor.fetchone()
return count[0]
except OperationalError as e:
print(f"The error '{e}' occurred")
def get_latest_100_platforms(self):
if self.connection is not None:
cursor = self.connection.cursor()
try:
cursor.execute("""
SELECT data->>'platform' AS platform
FROM corpuses
ORDER BY id DESC
LIMIT 100
""")
platforms = cursor.fetchall()
return [platform[0] for platform in platforms]
except OperationalError as e:
print(f"The error '{e}' occurred")