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

Added switches for database manipulation to main.py

parent 75ad7eed
No related branches found
No related tags found
No related merge requests found
No preview for this file type
......@@ -11,12 +11,16 @@ class IntegrityError(sqlite.IntegrityError):
 
 
class Database:
def __init__(self):
self.connection = sqlite.connect('database.db')
def __init__(self, path):
self.connection = sqlite.connect(path)
self.checkDb()
self.connection.isolation_level = None # so we don't have to use self.connection.commit()
self.connection.row_factory = sqlite.Row
# self.connection.execute("PRAGMA synchronous = OFF")
 
def checkDb(self):
self.connection.execute("SELECT * FROM groups")
def getGroups(self):
"""
:return: all groups in database
......
......@@ -2,80 +2,82 @@
 
import sqlite3 as sqlite
 
conn = sqlite.connect('database.db')
conn.isolation_level = None # so we don't have to use self.connection.commit()
curs = conn.cursor()
 
curs.execute(
'''
CREATE TABLE Groups (
group_id INTEGER PRIMARY KEY,
name VARCHAR UNIQUE
NOT NULL
);
'''
)
def create_db(path):
conn = sqlite.connect(path)
conn.isolation_level = None # so we don't have to use self.connection.commit()
curs = conn.cursor()
 
curs.execute(
curs.execute(
'''
CREATE TABLE Groups (
group_id INTEGER PRIMARY KEY,
name VARCHAR UNIQUE
NOT NULL
);
'''
CREATE TABLE Member (
member_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR,
group_id INTEGER REFERENCES Groups (group_id) ON DELETE CASCADE,
UNIQUE (
name,
group_id
)
);
'''
)
 
curs.execute(
curs.execute(
'''
CREATE TABLE Member (
member_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR,
group_id INTEGER REFERENCES Groups (group_id) ON DELETE CASCADE,
UNIQUE (
name,
group_id
)
);
'''
CREATE TABLE Distribution (
distribution_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
creation_time DATETIME DEFAULT (CURRENT_TIMESTAMP),
group_id REFERENCES Groups (group_id) ON DELETE CASCADE
);
'''
)
)
 
curs.execute(
curs.execute(
'''
CREATE TABLE Distribution (
distribution_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR NOT NULL,
creation_time DATETIME DEFAULT (CURRENT_TIMESTAMP),
group_id REFERENCES Groups (group_id) ON DELETE CASCADE
);
'''
CREATE TABLE Team (
team_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR,
distribution_id INTEGER REFERENCES Distribution (distribution_id) ON DELETE CASCADE
NOT NULL
);'''
)
)
 
curs.execute(
curs.execute(
'''
CREATE TABLE Team (
team_id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR,
distribution_id INTEGER REFERENCES Distribution (distribution_id) ON DELETE CASCADE
NOT NULL
);'''
)
curs.execute(
'''
CREATE TABLE Member_to_Team (
member_id INTEGER REFERENCES Member (member_id) ON DELETE CASCADE,
team_id INTEGER REFERENCES Team (team_id) ON DELETE CASCADE,
PRIMARY KEY (
team_id,
member_id
)
);
'''
CREATE TABLE Member_to_Team (
member_id INTEGER REFERENCES Member (member_id) ON DELETE CASCADE,
team_id INTEGER REFERENCES Team (team_id) ON DELETE CASCADE,
PRIMARY KEY (
team_id,
member_id
)
);
'''
)
 
curs.execute(
curs.execute(
'''
SELECT t1.member_id member_id,
t2.member_id With_member_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
'''
SELECT t1.member_id member_id,
t2.member_id With_member_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
'''
)
)
#!/usr/bin/env python3
import sqlite3
import sys
import os
import database_create
from argparse import ArgumentParser
from PyQt5 import QtWidgets as Qt
from PyQt5.QtCore import QTranslator
from controllers.mainwindow_controller import MainWindow
......@@ -10,11 +12,39 @@ from globals import Global
 
 
def main():
path = os.path.dirname(os.path.abspath(__file__))
Global.db = Database()
script_path = os.path.dirname(os.path.abspath(__file__))
parser = ArgumentParser()
parser.add_argument(
'-d',
'--database',
default=script_path + '/database.db',
help='use specified file as database file, if not specified the default file (script_path/database.db} is used',
metavar='PATH'
)
parser.add_argument(
'-fc',
'--force-database-create',
action='store_true',
help='force creation of database - program then overwrites file specified in -d or the default one!'
)
args = parser.parse_args()
# clear and create database if -fc was given or create database if given file doesn't exist
if args.force_database_create is True or not os.path.isfile(args.database):
if os.path.isfile(args.database):
overwrite = input("Are you sure, you want to overwrite file '" + args.database + "'?[Y/N]\n")
if overwrite != 'Y':
exit()
os.remove(args.database)
database_create.create_db(args.database)
try:
Global.db = Database(args.database)
except (sqlite3.DatabaseError, sqlite3.OperationalError) as e:
print('The specified database file is invalid.\nTry rerunning with -fc?')
exit()
app = Qt.QApplication(sys.argv)
translator = QTranslator()
translator.load(path + "translations/czech.qm")
translator.load(script_path + "translations/czech.qm")
app.installTranslator(translator)
form = MainWindow()
form.show()
......
File added
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