-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataBaseConnection.py
96 lines (80 loc) · 3.3 KB
/
DataBaseConnection.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
import psycopg2
from datetime import datetime
import re
import xml.etree.ElementTree as ET
from PyQt5.QtCore import Qt
class DataBaseConnection:
def __init__(self, dbname, user, password):
try:
print("Connecting to database...")
self.connection = psycopg2.connect("dbname =" + dbname + " user = " + user + " password = " + password)
print("Connection to the database has been established.")
except(Exception, psycopg2.Error) as errorMsg:
print("A database-related error occured: ", errorMsg)
def insertXmlFile(self, filename: str, description):
cursor = self.connection.cursor()
f = open(filename, "r")
xmlFile = f.read()
cursor.execute(
"INSERT INTO tblUppaal(createDate, description, xmlfile, \"fileName\")"
"VALUES(%s, %s, %s, %s) RETURNING modelID;",
(str(datetime.now()), description, xmlFile, filename.split('/')[-1]))
modelID = cursor.fetchall()[0][0]
xmlFile = xmlFile.replace('\n', '')
pattern = "<queries>(.*)</queries>"
chopped = re.search(pattern, xmlFile)
if chopped:
xmlFile = "<queries> " + chopped.group(1) + " </queries>"
tree = ET.ElementTree(ET.fromstring(xmlFile))
root = tree.getroot()
for query in root.findall('query'):
cursor.execute(
"INSERT INTO tblQuery(modelID, query, description)"
"VALUES(%s, %s, %s)",
(modelID, query[0].text, query[1].text))
self.connection.commit()
cursor.close()
def selectUppaalQueries(self, modelID):
cursor = self.connection.cursor()
cursor.execute(
"SELECT tblquery.query, tblquery.description, tblquery.result, tblquery.queryid FROM tblquery "
"WHERE tblquery.modelID={0}".format(str(modelID)))
rec = cursor.fetchall()
cursor.close()
return rec
def setQueryState(self, queryID, state):
cursor = self.connection.cursor()
stateFormatted = 'null'
if state == Qt.CheckState.Unchecked:
stateFormatted = '0'
elif state == Qt.CheckState.Checked:
stateFormatted = '1'
cursor.execute(
"update tblquery set result = {0}::bit(1) where queryid={1}".format(stateFormatted, str(queryID)))
self.connection.commit()
cursor.close()
pass
def selectAllModelIDInfo(self):
cursor = self.connection.cursor()
cursor.execute(
"SELECT modelID, \"fileName\", createDate, description FROM tblUppaal")
rec = cursor.fetchall()
cursor.close()
return rec
def selectUppaalModelInfo(self, modelID):
cursor = self.connection.cursor()
cursor.execute(
"SELECT modelID, \"fileName\", createDate, description, xmlFile FROM tblUppaal "
"WHERE modelId = %s",
(str(modelID)))
rec = cursor.fetchall()
cursor.close()
return rec[0]
def selectUppaalModelXml(self, modelID):
cursor = self.connection.cursor()
cursor.execute(
"SELECT xmlFile FROM tblUppaal "
"WHERE modelId = {0}".format(str(modelID)))
rec = cursor.fetchall()
cursor.close()
return rec[0][0]