-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries.py
210 lines (166 loc) · 5.53 KB
/
queries.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from connect import create_connection, database
def get_tasks_by_user(conn, user_id):
sql = '''
SELECT * FROM tasks WHERE user_id = ?;
'''
cur = conn.cursor()
cur.execute(sql, (user_id,))
return cur.fetchall()
def get_tasks_by_status(conn, status_name):
sql = '''
SELECT * FROM tasks WHERE status_id = (SELECT id FROM status WHERE name = ?);
'''
cur = conn.cursor()
cur.execute(sql, (status_name,))
return cur.fetchall()
def update_task_status(conn, task_id, new_status_name):
sql = '''
UPDATE tasks SET status_id = (SELECT id FROM status WHERE name = ?) WHERE id = ?;
'''
cur = conn.cursor()
cur.execute(sql, (new_status_name, task_id))
conn.commit()
def get_users_without_tasks(conn):
sql = '''
SELECT * FROM users WHERE id NOT IN (SELECT user_id FROM tasks);
'''
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
def add_task_for_user(conn, title, description, status_name, user_id):
sql = '''
INSERT INTO tasks (title, description, status_id, user_id)
VALUES (?, ?, (SELECT id FROM status WHERE name = ?), ?);
'''
cur = conn.cursor()
cur.execute(sql, (title, description, status_name, user_id))
conn.commit()
def get_uncompleted_tasks(conn):
sql = '''
SELECT * FROM tasks WHERE status_id != (SELECT id FROM status WHERE name = 'completed');
'''
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
def delete_task(conn, task_id):
sql = '''
DELETE FROM tasks WHERE id = ?;
'''
cur = conn.cursor()
cur.execute(sql, (task_id,))
conn.commit()
def find_users_by_email(conn, email_pattern):
sql = '''
SELECT * FROM users WHERE email LIKE ?;
'''
cur = conn.cursor()
cur.execute(sql, (email_pattern,))
return cur.fetchall()
def update_user_name(conn, user_id, new_name):
sql = '''
UPDATE users SET fullname = ? WHERE id = ?;
'''
cur = conn.cursor()
cur.execute(sql, (new_name, user_id))
conn.commit()
def count_tasks_by_status(conn):
sql = '''
SELECT s.name, COUNT(t.id)
FROM tasks t
JOIN status s ON t.status_id = s.id
GROUP BY s.name;
'''
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
def get_tasks_by_user_email_domain(conn, domain):
sql = '''
SELECT t.*
FROM tasks t
JOIN users u ON t.user_id = u.id
WHERE u.email LIKE ?;
'''
cur = conn.cursor()
cur.execute(sql, ('%' + domain,))
return cur.fetchall()
def get_tasks_without_description(conn):
sql = '''
SELECT * FROM tasks WHERE description IS NULL OR description = '';
'''
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
def get_users_and_tasks_in_progress(conn):
sql = '''
SELECT u.fullname, t.title
FROM tasks t
JOIN users u ON t.user_id = u.id
JOIN status s ON t.status_id = s.id
WHERE s.name = 'in progress';
'''
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
def get_users_and_task_count(conn):
sql = '''
SELECT u.fullname, COUNT(t.id) as task_count
FROM users u
LEFT JOIN tasks t ON u.id = t.user_id
GROUP BY u.fullname;
'''
cur = conn.cursor()
cur.execute(sql)
return cur.fetchall()
if __name__ == '__main__':
with create_connection(database) as conn:
if conn is not None:
tasks = get_tasks_by_user(conn, 1)
print("Tasks for user_id=1:")
for task in tasks:
print(task)
tasks = get_tasks_by_status(conn, 'new')
print("Tasks with status 'new':")
for task in tasks:
print(task)
update_task_status(conn, 1, 'in progress')
print("Updated task status for task_id=1 to 'in progress'.")
users = get_users_without_tasks(conn)
print("Users without tasks:")
for user in users:
print(user)
add_task_for_user(conn, 'New Task', 'This is a new task', 'new', 1)
print("Added new task for user_id=1.")
tasks = get_uncompleted_tasks(conn)
print("Uncompleted tasks:")
for task in tasks:
print(task)
delete_task(conn, 1)
print("Deleted task with task_id=1.")
users = find_users_by_email(conn, '%@example.com')
print("Users with email pattern %@example.com:")
for user in users:
print(user)
update_user_name(conn, 1, 'New Name')
print("Updated name for user_id=1.")
status_count = count_tasks_by_status(conn)
print("Task count by status:")
for status in status_count:
print(status)
tasks = get_tasks_by_user_email_domain(conn, 'example.com')
print("Tasks for users with email domain example.com:")
for task in tasks:
print(task)
tasks = get_tasks_without_description(conn)
print("Tasks without description:")
for task in tasks:
print(task)
users_tasks = get_users_and_tasks_in_progress(conn)
print("Users and tasks in progress:")
for user_task in users_tasks:
print(user_task)
user_task_count = get_users_and_task_count(conn)
print("Users and their task count:")
for user in user_task_count:
print(user)
else:
print("Error! cannot create the database connection.")