2023-10-08 21:46:31 +02:00
|
|
|
import sqlite3
|
|
|
|
|
|
|
|
|
|
class Session:
|
|
|
|
|
|
|
|
|
|
def __init__(self, db_agent):
|
|
|
|
|
self.user = -1
|
|
|
|
|
self.db_agent = db_agent
|
|
|
|
|
self.resultstring = 'no results yet'
|
|
|
|
|
|
|
|
|
|
def set_user(self, db_index):
|
|
|
|
|
self.user = db_index
|
|
|
|
|
return 'User set'
|
|
|
|
|
|
|
|
|
|
def get_active_user(self):
|
|
|
|
|
sql_command = f'''
|
2023-10-11 06:46:21 +02:00
|
|
|
SELECT first_name, Nachname
|
2023-10-08 21:46:31 +02:00
|
|
|
FROM Spieler_in
|
|
|
|
|
WHERE id = {self.user}
|
|
|
|
|
'''
|
|
|
|
|
active_user = ''
|
|
|
|
|
try:
|
|
|
|
|
first_name, name = self.db_agent.execute(sql_command).fetchone()
|
|
|
|
|
active_user = f'{first_name} {name}'
|
|
|
|
|
except:
|
|
|
|
|
active_user = 'No active user'
|
|
|
|
|
return active_user
|
|
|
|
|
|
|
|
|
|
def invoke_command(self, command, arguments):
|
|
|
|
|
self.resultstring = ''
|
|
|
|
|
if command == 'a':
|
|
|
|
|
self.resultstring += self.set_user(arguments[0])
|
|
|
|
|
elif command == 'n':
|
|
|
|
|
self.create_new_user(arguments[0], arguments[1])
|
|
|
|
|
self.resultstring += f'Created new user {arguments[0]} {arguments[1]}.'
|
|
|
|
|
elif command == 's':
|
|
|
|
|
self.resultstring += self.show_works()
|
|
|
|
|
elif command == 'su':
|
|
|
|
|
self.resultstring += self.show_users()
|
|
|
|
|
elif command == 'q':
|
|
|
|
|
self.resultstring += 'Bye-bye'
|
|
|
|
|
else:
|
|
|
|
|
self.resultstring += 'Could not understand command.'
|
|
|
|
|
|
|
|
|
|
def result(self):
|
|
|
|
|
return self.resultstring
|
|
|
|
|
|
|
|
|
|
def create_new_user(self, first_name, name):
|
|
|
|
|
sql_command = f'''
|
2023-10-11 06:46:21 +02:00
|
|
|
INSERT INTO Spieler_in (first_name, Nachname)
|
2023-10-08 21:46:31 +02:00
|
|
|
VALUES ("{first_name}", "{name}");
|
|
|
|
|
'''
|
|
|
|
|
self.db_agent.execute(sql_command)
|
|
|
|
|
|
|
|
|
|
def show_users(self):
|
|
|
|
|
sql_command = '''
|
|
|
|
|
SELECT * FROM Spieler_in;
|
|
|
|
|
'''
|
|
|
|
|
result_list = self.db_agent.execute(sql_command).fetchall()
|
|
|
|
|
fun_resultstring = ''
|
|
|
|
|
for item in result_list:
|
|
|
|
|
fun_resultstring += f'{item[0]}: {item[1]} {item[2]}\n'
|
|
|
|
|
return fun_resultstring
|
|
|
|
|
|
|
|
|
|
def show_works(self):
|
|
|
|
|
sql_command = '''
|
|
|
|
|
SELECT id
|
2023-10-13 06:42:37 +02:00
|
|
|
FROM work
|
2023-10-08 21:46:31 +02:00
|
|
|
'''
|
|
|
|
|
list_of_work_ids = self.db_agent.execute(sql_command).fetchall()
|
|
|
|
|
# GET WORKS OUT!
|
|
|
|
|
return ''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# End session
|
|
|
|
|
|
|
|
|
|
class Werk_unter_id:
|
|
|
|
|
# con = sqlite3.connect('repertoire.db')
|
|
|
|
|
# reader = con.cursor()
|
|
|
|
|
|
|
|
|
|
def __init__(self, werk_id, db_agent):
|
|
|
|
|
self.values = dict()
|
|
|
|
|
self.reader = db_agent
|
|
|
|
|
|
|
|
|
|
sql_wui = f"""
|
|
|
|
|
SELECT *
|
2023-10-13 06:42:37 +02:00
|
|
|
FROM work
|
2023-10-08 21:46:31 +02:00
|
|
|
WHERE id = {werk_id}
|
|
|
|
|
"""
|
|
|
|
|
werk_list_val = self.reader.execute(sql_wui).fetchone()
|
2023-10-18 06:41:51 +02:00
|
|
|
werk_list_att = ('id', 'comp_id', 'year', 'opus', 'collection', 'main_key', 'title', 'mov_title', 'alias', 'work_directory', 'wd_number')
|
2023-10-08 21:46:31 +02:00
|
|
|
for tup in zip(werk_list_att, werk_list_val):
|
|
|
|
|
self.values[tup[0]] = tup[1]
|
|
|
|
|
|
|
|
|
|
sql_comp = f"""
|
2023-10-11 06:54:39 +02:00
|
|
|
SELECT first_name, name
|
2023-10-11 06:40:09 +02:00
|
|
|
FROM composer
|
2023-10-13 06:46:19 +02:00
|
|
|
WHERE id = {self.values['comp_id']}
|
2023-10-08 21:46:31 +02:00
|
|
|
"""
|
|
|
|
|
werk_comp = self.reader.execute(sql_comp).fetchone()
|
2023-10-11 06:46:21 +02:00
|
|
|
self.values['first_name'] = werk_comp[0]
|
2023-10-11 06:54:39 +02:00
|
|
|
self.values['name'] = werk_comp[1]
|
2023-10-08 21:46:31 +02:00
|
|
|
|
|
|
|
|
sql_suw = f"""
|
|
|
|
|
SELECT *
|
|
|
|
|
FROM Satz
|
|
|
|
|
WHERE Werk_id = {werk_id}
|
|
|
|
|
"""
|
|
|
|
|
saetze = dict()
|
|
|
|
|
nummern = set()
|
|
|
|
|
suw_liste = self.reader.execute(sql_suw).fetchall()
|
|
|
|
|
# 0 Werk_id, 1 Lfd_Satznummer, 2 Nummer, 3 Bezeichnung, 4 Tonart, 5 Aufnahme
|
|
|
|
|
for satz in suw_liste:
|
|
|
|
|
saetze[satz[1]] = satz[2], satz[3], satz[4]
|
|
|
|
|
if not satz[2] is None:
|
|
|
|
|
nummern.add(satz[2])
|
|
|
|
|
self.values['Sätze'] = saetze
|
|
|
|
|
if len(nummern) == 1:
|
|
|
|
|
self.sätze_unter_nummer = True
|
|
|
|
|
self.values['Nummer'] = nummern.pop()
|
|
|
|
|
else:
|
|
|
|
|
self.sätze_unter_nummer = False
|
|
|
|
|
|
|
|
|
|
def sammlung(self):
|
2023-10-13 06:53:02 +02:00
|
|
|
sammlung = self.values['collection']
|
2023-10-08 21:46:31 +02:00
|
|
|
if not sammlung:
|
|
|
|
|
return 'ohne'
|
|
|
|
|
else:
|
|
|
|
|
return sammlung
|
|
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
|
ret_str = ''
|
|
|
|
|
for key, value in self.values.items():
|
|
|
|
|
if not value is None:
|
|
|
|
|
ret_str += f'{key}: {value}\n'
|
|
|
|
|
return ret_str
|
|
|
|
|
|
|
|
|
|
# End Werk_unter_id
|
|
|
|
|
|
|
|
|
|
def parse_user_input(user_in):
|
|
|
|
|
split_user_in = user_in.split()
|
|
|
|
|
command = split_user_in[0]
|
|
|
|
|
if command in ('a', 'n', 'q', 's', 'su'):
|
|
|
|
|
arguments = split_user_in[1:]
|
|
|
|
|
return (command, arguments)
|
|
|
|
|
else:
|
|
|
|
|
return (None, None)
|
|
|
|
|
|
|
|
|
|
def command_line_loop(session):
|
|
|
|
|
user_in = ''
|
|
|
|
|
while not user_in == 'q':
|
|
|
|
|
user_in = input(f'Piano-Repertoire: {session.get_active_user()} >>> ')
|
|
|
|
|
command, arguments = parse_user_input(user_in)
|
|
|
|
|
session.invoke_command(command, arguments)
|
|
|
|
|
# print(f'command: {command}, arguments: {arguments}')
|
|
|
|
|
print(session.result())
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
con = sqlite3.connect('repertoire.db')
|
|
|
|
|
db_agent = con.cursor()
|
|
|
|
|
session = Session(db_agent)
|
|
|
|
|
command_line_loop(session)
|
|
|
|
|
con.commit()
|
|
|
|
|
print('changes committed.')
|
|
|
|
|
con.close()
|
|
|
|
|
print('db-connection closed. Bye-bye!')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
main()
|