-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_management.py
66 lines (60 loc) · 2.46 KB
/
database_management.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
import sqlite3
class DatabaseHandler:
def __init__(self, db_file='database.sqlite'):
"""Initialize the database connection."""
self.__conn = sqlite3.connect(db_file)
self.__cursor = self.__conn.cursor()
self.__create_table()
def __create_table(self):
"""Create a table for employees."""
self.__cursor.execute('''
CREATE TABLE IF NOT EXISTS employees (
id TEXT PRIMARY KEY,
name TEXT,
type TEXT,
monthly_salary REAL,
hours_worked REAL,
hourly_rate REAL,
bonus REAL,
deduction REAL
)
''')
self.__conn.commit()
def add_employee(self, employee, FullTimeEmployee):
"""Add an employee to the database."""
emp_data = {
'id': employee.getId(),
'name': employee.getName(),
'type': 'FullTime' if isinstance(employee, FullTimeEmployee) else 'PartTime',
'monthly_salary': getattr(employee, '_FullTimeEmployee__monthlySalary', None),
'hours_worked': getattr(employee, '_PartTimeEmployee__hoursWorked', None),
'hourly_rate': getattr(employee, '_PartTimeEmployee__hourlyRate', None),
'bonus': employee.calculateBonus(),
'deduction': employee.calculateDeductions()
}
sql = '''
INSERT INTO employees (id, name, type, monthly_salary, hours_worked, hourly_rate, bonus, deduction)
VALUES (:id, :name, :type, :monthly_salary, :hours_worked, :hourly_rate, :bonus, :deduction)
ON CONFLICT(id) DO UPDATE SET
name = excluded.name,
type = excluded.type,
monthly_salary = excluded.monthly_salary,
hours_worked = excluded.hours_worked,
hourly_rate = excluded.hourly_rate,
bonus = excluded.bonus,
deduction = excluded.deduction
'''
self.__cursor.execute(sql, emp_data)
self.__conn.commit()
def remove_employee(self, emp_id):
"""Remove an employee from the database."""
self.__cursor.execute('DELETE FROM employees WHERE id = ?', (emp_id,))
self.__conn.commit()
def get_employees(self):
"""Fetch all employees from the database."""
self.__cursor.execute('SELECT * FROM employees')
return self.__cursor.fetchall()
def __del__(self):
"""Close the database connection."""
self.__cursor.close()
self.__conn.close()