Skip to content
Snippets Groups Projects
Commit b6fb5142 authored by Jakub Štercl's avatar Jakub Štercl
Browse files

updated create_database script, PRAGMA synchronous is now on and distribution...

updated create_database script, PRAGMA synchronous is now on and distribution is being saved in one transaction
parent ce074cd9
No related branches found
No related tags found
No related merge requests found
...@@ -11,7 +11,7 @@ class Database: ...@@ -11,7 +11,7 @@ class Database:
self.connection = sqlite.connect('database.db') self.connection = sqlite.connect('database.db')
self.connection.isolation_level = None # so we don't have to use self.connection.commit() self.connection.isolation_level = None # so we don't have to use self.connection.commit()
self.connection.row_factory = sqlite.Row self.connection.row_factory = sqlite.Row
self.connection.execute("PRAGMA synchronous = OFF") # self.connection.execute("PRAGMA synchronous = OFF")
   
def getGroups(self): def getGroups(self):
""" """
...@@ -124,25 +124,30 @@ class Database: ...@@ -124,25 +124,30 @@ class Database:
curs = self.connection.execute('''INSERT INTO distribution (name, group_id) VALUES (?, ?)''', (name, group_id,)) curs = self.connection.execute('''INSERT INTO distribution (name, group_id) VALUES (?, ?)''', (name, group_id,))
return curs.lastrowid return curs.lastrowid
   
def createTeam(self, name, distribution_id): def createTeam(self, name, distribution_id, curs=None):
""" """
create new team create new team
:param team_id: :param team_id:
:param distribution_id: :param distribution_id:
:param name: :param name:
:param curs: cursor to operate on
:return: id of the newly created team :return: id of the newly created team
""" """
curs = self.connection.execute('''INSERT INTO team (name, distribution_id) VALUES (?, ?)''', if curs is None:
curs = self.connection.cursor()
curs.execute('''INSERT INTO team (name, distribution_id) VALUES (?, ?)''',
(name, distribution_id,)) (name, distribution_id,))
return curs.lastrowid return curs.lastrowid
   
def addMembersToTeam(self, team_id, members): def addMembersToTeam(self, team_id, members, curs=None):
""" """
add members to team add members to team
:param team_id: :param team_id:
:param members: iterable of Member :param members: iterable of Member
:param curs: cursor to operate on
""" """
curs = self.connection.cursor() if curs is None:
curs = self.connection.cursor()
for member in members: for member in members:
curs.execute('''INSERT INTO member_to_team (member_id, team_id) VALUES (?, ?)''', (member.id_, team_id,)) curs.execute('''INSERT INTO member_to_team (member_id, team_id) VALUES (?, ?)''', (member.id_, team_id,))
   
...@@ -216,3 +221,19 @@ class Database: ...@@ -216,3 +221,19 @@ class Database:
:param member_id: :param member_id:
""" """
curs = self.connection.execute('''UPDATE member SET group = NULL WHERE member_id = ?''',(member_id,)) curs = self.connection.execute('''UPDATE member SET group = NULL WHERE member_id = ?''',(member_id,))
def beginTransaction(self):
"""
starts transaction
:return: curs with started transaction
NOTE: whoever uses this has the responsibility of calling endTransaction providing the returned cursor
(after finishing his operations)
"""
return self.connection.execute("begin")
def endTransaction(self, curs):
"""
end transaction on given cursor
:param curs:
"""
curs.execute("commit")
...@@ -7,21 +7,44 @@ conn.isolation_level = None # so we don't have to use self.connection.commit() ...@@ -7,21 +7,44 @@ conn.isolation_level = None # so we don't have to use self.connection.commit()
curs = conn.cursor() curs = conn.cursor()
   
curs.execute( curs.execute(
'''CREATE TABLE Groups ( '''
CREATE TABLE Groups (
group_id INTEGER PRIMARY KEY, group_id INTEGER PRIMARY KEY,
name VARCHAR UNIQUE name VARCHAR UNIQUE
NOT NULL NOT NULL
);''' );
'''
) )
   
curs.execute( curs.execute(
'''CREATE TABLE Member ( '''
CREATE TABLE Member (
member_id INTEGER PRIMARY KEY AUTOINCREMENT, member_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR, name VARCHAR,
surname VARCHAR, surname VARCHAR,
group_id INTEGER REFERENCES Groups (group_id) group_id INTEGER REFERENCES Groups (group_id) ON DELETE CASCADE
NOT NULL );
);''' '''
)
curs.execute(
'''
CREATE VIEW Member_history_view AS
SELECT t1.member_id member_id,
t2.member_id With_member_id,
name,
surname,
group_id,
count( * ) times_together
FROM Member_to_Team t1
JOIN
Member_to_Team t2 ON t1.team_id = t2.team_id AND
t1.member_id != t2.member_id
JOIN
Member ON t2.member_id = Member.member_id
GROUP BY t1.member_id,
t2.member_id;
'''
) )
   
curs.execute( curs.execute(
...@@ -29,8 +52,10 @@ curs.execute( ...@@ -29,8 +52,10 @@ curs.execute(
CREATE TABLE Distribution ( CREATE TABLE Distribution (
distribution_id INTEGER PRIMARY KEY AUTOINCREMENT, distribution_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL, name VARCHAR NOT NULL,
creation_time DATETIME DEFAULT (CURRENT_TIMESTAMP) creation_time DATETIME DEFAULT (CURRENT_TIMESTAMP),
);''' group_id REFERENCES Groups (group_id) ON DELETE CASCADE
);
'''
) )
   
curs.execute( curs.execute(
...@@ -38,20 +63,21 @@ curs.execute( ...@@ -38,20 +63,21 @@ curs.execute(
CREATE TABLE Team ( CREATE TABLE Team (
team_id INTEGER PRIMARY KEY AUTOINCREMENT, team_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR, name VARCHAR,
distribution_id INTEGER REFERENCES Distribution (distribution_id) distribution_id INTEGER REFERENCES Distribution (distribution_id) ON DELETE CASCADE
NOT NULL NOT NULL
);''' );'''
) )
   
curs.execute( curs.execute(
''' '''
CREATE TABLE Member_to_Team ( CREATE TABLE Member_to_Team (
member_id INTEGER REFERENCES Member (member_id), member_id INTEGER REFERENCES Member (member_id) ON DELETE CASCADE,
team_id INTEGER REFERENCES Team (team_id), team_id INTEGER REFERENCES Team (team_id) ON DELETE CASCADE,
PRIMARY KEY ( PRIMARY KEY (
team_id, team_id,
member_id member_id
) )
);''' );
'''
) )
   
...@@ -11,8 +11,10 @@ class Distribution: ...@@ -11,8 +11,10 @@ class Distribution:
   
def saveToDb(self): def saveToDb(self):
self.id_ = Global.db.createDistribution(self.name, self.group_id) self.id_ = Global.db.createDistribution(self.name, self.group_id)
curs = Global.db.beginTransaction()
for team in self.teams: for team in self.teams:
start = timer() start = timer()
team_id = Global.db.createTeam(team.name, self.id_) team_id = Global.db.createTeam(team.name, self.id_, curs=curs)
print(timer() - start) print(timer() - start)
Global.db.addMembersToTeam(team_id, team.members) Global.db.addMembersToTeam(team_id, team.members, curs=curs)
Global.db.endTransaction(curs)
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.2.1, 2017-04-11T17:09:40. --> <!-- Written by QtCreator 4.2.1, 2017-04-11T23:28:24. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment