-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.py
More file actions
162 lines (129 loc) · 4.64 KB
/
select.py
File metadata and controls
162 lines (129 loc) · 4.64 KB
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
from connect import create_connection
def select_task_by_user(cursor,user_id):
cursor.execute("""
select t.* from tasks as t
where user_id = %s;"""
, (user_id,))
return cursor.fetchall()
def select_task_by_status_name(cursor,status_name):
cursor.execute("""
select t.*
from tasks as t
join status as s on t.status_id=s.id
where s.name = %s;"""
, (status_name,))
return cursor.fetchall()
def update_task_status(cursor,task_id,status_name):
sql = """
update tasks
set status_id = (select id from status where name=%s)
where id=%s;
"""
cursor.execute(sql,(status_name,task_id))
def select_users_without_task(cursor):
cursor.execute("""
select u.*
from users as u
where u.id not in (select user_id from tasks);
""")
return cursor.fetchall()
def add_task_to_user(cursor,task):
cursor.execute("""
insert into tasks(title,description,status_id,user_id)
values (%s, %s, (select id from status where name=%s),%s);
""",task)
def select_task_not_in_status(cursor,status_name):
cursor.execute("""
select t.*
from tasks as t
join status as s on t.status_id=s.id
where s.name <> %s;"""
, (status_name,))
return cursor.fetchall()
def remove_task(cursor,task_id):
cursor.execute("""
delete from tasks where id = %s;""",(task_id,))
def select_user_by_email(cursor,email):
cursor.execute("""
select u.*
from users as u
where u.email like %s;"""
, (email,))
return cursor.fetchall()
def update_user_name(cursor,user_id,fullname):
cursor.execute("""
update users
set fullname=%s
where id = %s;""",(fullname,user_id))
def select_tasks_count(cursor):
cursor.execute("""
select count(t.*),s.name
from tasks as t
join status as s on t.status_id=s.id
group by s.id;""")
return cursor.fetchall()
def select_tasks_with_email(cursor,email):
cursor.execute("""
select t.*
from tasks as t
join users as s on t.user_id=s.id
where s.email like %s;""",
(email,))
return cursor.fetchall()
def select_tasks_without_description(cursor):
cursor.execute("""
select t.*
from tasks as t
where t.description is null
or t.description = '';""")
return cursor.fetchall()
def select_task_and_users_with_status(cursor,status_name):
cursor.execute("""
select u.*,t.*
from tasks as t
inner join users as u on u.id = t.user_id
inner join status as s on t.status_id=s.id
where s.name = %s;"""
, (status_name,))
return cursor.fetchall()
def select_users_task_count(cursor):
cursor.execute("""
select u.*,count(t.*)
from users as u
left join tasks as t on u.id = t.user_id
group by u.id;""")
return cursor.fetchall()
def main():
with create_connection() as connection:
with connection.cursor() as cursor:
print("Tasks of user 2:")
print(select_task_by_user(cursor,2))
print("Tasks with status New:")
print(select_task_by_status_name(cursor,"New"))
print("Update status of task 2 to 'In Progress':")
update_task_status(cursor,2, "In Progress")
connection.commit()
print("Users without tasks:")
print(select_users_without_task(cursor))
update_user_name(cursor,1,"Smith Sam")
connection.commit()
add_task_to_user(cursor,("Fix slack app","Fix bug #12378596","New",3))
connection.commit()
print("tasks not done:")
print(select_task_not_in_status(cursor,"Done"))
remove_task(cursor,1)
connection.commit()
print("Users in domain example.net")
print(select_user_by_email(cursor,"%example.net"))
print("Count of tasks by status")
print(select_tasks_count(cursor))
print("Tasks for users with domain 'example.com':")
print(select_tasks_with_email(cursor,"%example.com"))
print('select_tasks_without_description:')
print(select_tasks_without_description(cursor))
print("Users and tasks with status 'In Progress'")
print(select_task_and_users_with_status(cursor,"in Progress"))
print("Users with task count")
print(select_users_task_count(cursor))
if __name__ == '__main__':
main()