-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModel.py
127 lines (108 loc) · 3.63 KB
/
Model.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import psycopg2
from psycopg2 import sql
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import QueryResult
from DataGenerator import DataGenerator
from config import DATABASE_NAME, DATABASE_LOGIN, DATABASE_PASSWORD
from static import CREATE_TABLES
class SQL:
"""
This class represents an interface for communicating between user and the database.
"""
conn = None
def __init__(self):
# connect to postgres
self.connect_to_server()
self.conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
# create database if it does not exist
if not self.db_exist():
self.create_or_reset_db()
else:
self.connect_to_db()
print('Connected successfully')
def process_query(self, query):
"""
Takes a query (string) and returns the result from the database.
:param query: string query
:return: result of the query
"""
# getting cursor
cur = self.conn.cursor()
# initialize a QueryResult instance
result = QueryResult.QueryResult()
# try to execute the query and fetch the result
try:
cur.execute(query)
result.column_names = [column.name for column in cur.description]
result.values = cur.fetchall()
except Exception as e:
result.is_error = True
result.exception = e
return result
def populate_database(self):
"""
Populate database with random data.
"""
self.create_or_reset_db()
cur = self.conn.cursor()
# generate queries
queries = DataGenerator().generate()
for query in queries:
# insert each query
cur.execute(query)
cur.close()
print("Populated successfully.")
def generate_data(self):
"""
WARNING! DEPRECATED METHOD!
Use populate_database instead.
Populate database with random data.
"""
self.create_or_reset_db()
cur = self.conn.cursor()
queries = DataGenerator().generate()
for x in queries:
# f = open('insertion.sql', 'w', encoding='utf-8')
# f.write(x)
# f.close()
cur.execute(x)
print("Committed successfully.")
cur.close()
def db_exist(self):
cursor = self.conn.cursor()
cursor.execute("SELECT 1 FROM pg_catalog.pg_database WHERE datname = %s;", [DATABASE_NAME])
return cursor.fetchone()
def connect_to_db(self):
"""
Connect to the database.
:return:
"""
self.conn = psycopg2.connect(
database=DATABASE_NAME,
user=DATABASE_LOGIN,
password=DATABASE_PASSWORD
)
self.conn.autocommit = True
def connect_to_server(self):
"""
Connect to postgres.
"""
self.conn = psycopg2.connect(
user=DATABASE_LOGIN,
password=DATABASE_PASSWORD
)
self.conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
self.conn.autocommit = True
def create_or_reset_db(self):
"""
Creates or resets the database.
"""
self.connect_to_server()
cursor = self.conn.cursor()
# recreate the database
cursor.execute(sql.SQL("DROP DATABASE IF EXISTS {};").format(sql.Identifier(DATABASE_NAME)))
cursor.execute(sql.SQL("CREATE DATABASE {};").format(sql.Identifier(DATABASE_NAME)))
# reconnect to the database and create tables there
self.connect_to_db()
cursor = self.conn.cursor()
cursor.execute(CREATE_TABLES)