forked from jeremiahnlin/inventory_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequests.py
More file actions
321 lines (251 loc) · 10.1 KB
/
requests.py
File metadata and controls
321 lines (251 loc) · 10.1 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
class MySQLRequest:
"""
Parent class for all MySQL request classes.
Provides a common structure for generating MySQL queries and descriptions.
"""
def __init__(self, table_name):
self.table_name = table_name
def get_query(self):
"""
Returns the MySQL query as a string.
Must be implemented by child classes.
"""
raise NotImplementedError("Child classes must implement the `get_query` method.")
def get_description(self):
"""
Returns a description of what the action does.
Must be implemented by child classes.
"""
raise NotImplementedError("Child classes must implement the `get_description` method.")
# hello world
class PullData(MySQLRequest):
"""
Pulls all data from a table using a query
"""
def __init__(self):
super.__init__(table_name)
def get_query(self):
return f"SELECT * FROM {self.table_name};"
def get_description(self):
return "Retrieves all the data from" + f"{self.table_name} table"
class InsertData(MySQLRequest):
"""
Inserts a single row of data into a table.
"""
def __init__(self, table_name, columns, values):
super().__init__(table_name)
self.columns = columns
self.values = values
def get_query(self):
return f"INSERT INTO {self.table_name} ({', '.join(self.columns)}) VALUES ({', '.join(self.values)});"
def get_description(self):
return f"Inserts a single row of data into {self.tablename} table with the given column {self.column} and value {self.values}"
class InsertMultipleRows(MySQLRequest):
"""
Inserts multiple rows of data into a table.
"""
def __init__(self, table_name, columns, values_list):
super().__init__(table_name)
self.columns = columns
self.values_list = values_list
def get_query(self):
values_placeholders = ', '.join(['(' + ', '.join(['%s'] * len(self.columns)) + ')'] * len(self.values_list))
return f"""
INSERT INTO {self.table_name} ({', '.join(self.columns)})
VALUES {values_placeholders};
"""
def get_description(self):
return f"Inserts multiple rows into the '{self.table_name}' table with the specified columns and values."
class UpdateData(MySQLRequest):
"""
Updates existing data in a table based on a condition.
"""
def __init__(self, table_name, set_values, condition):
super().__init__(table_name)
self.set_values = set_values
self.condition = condition
def get_query(self):
set_clause = ', '.join([f"{col} = %s" for col in self.set_values.keys()])
return f"""
UPDATE {self.table_name}
SET {set_clause}
WHERE {self.condition};
"""
def get_description(self):
return f"Updates rows in the '{self.table_name}' table where the condition '{self.condition}' is met."
class DeleteData(MySQLRequest):
"""
Deletes rows from a table based on a condition.
"""
def __init__(self, table_name, condition):
super().__init__(table_name)
self.condition = condition
def get_query(self):
return f"""
DELETE FROM {self.table_name}
WHERE {self.condition};
"""
def get_description(self):
return f"Deletes rows from the '{self.table_name}' table where the condition '{self.condition}' is met."
class SelectAllData(MySQLRequest):
"""
Selects all rows and columns from a table.
"""
def get_query(self):
return f"""
SELECT * FROM {self.table_name};
"""
def get_description(self):
return f"Selects all rows and columns from the '{self.table_name}' table."
class SelectSpecificColumns(MySQLRequest):
"""
Selects specific columns from a table.
"""
def __init__(self, table_name, columns):
super().__init__(table_name)
self.columns = columns
def get_query(self):
return f"""
SELECT {', '.join(self.columns)} FROM {self.table_name};
"""
def get_description(self):
return f"Selects the columns {', '.join(self.columns)} from the '{self.table_name}' table."
class FilterDataWithWhere(MySQLRequest):
"""
Selects rows from a table based on a condition.
"""
def __init__(self, table_name, condition,column=None):
super().__init__(table_name)
self.condition = condition
self.column = column or ["*"]
def get_query(self):
where = " AND ".join(self.condition)
columns = ", ".join(self.column)
return f"""
SELECT {columns} FROM {self.table_name}
WHERE {where};
"""
def get_description(self):
return f"Selects {columns} from the '{self.table_name}' table where the condition '{self.condition}' is met."
class SortDataWithOrderBy(MySQLRequest):
"""
Selects rows from a table and sorts them by a column.
"""
def __init__(self, table_name, order_by_column, order='ASC'):
super().__init__(table_name)
self.order_by_column = order_by_column
self.order = order
def get_query(self):
return f"""
SELECT * FROM {self.table_name}
ORDER BY {self.order_by_column} {self.order};
"""
def get_description(self):
return f"Selects rows from the '{self.table_name}' table and sorts them by '{self.order_by_column}' in {self.order} order."
class LimitResults(MySQLRequest):
"""
Limits the number of rows returned by a query.
"""
def __init__(self, table_name, limit):
super().__init__(table_name)
self.limit = limit
def get_query(self):
return f"""
SELECT * FROM {self.table_name}
LIMIT {self.limit};
"""
def get_description(self):
return f"Limits the results from the '{self.table_name}' table to {self.limit} rows."
class PaginateResults(MySQLRequest):
"""
Paginates results by specifying an offset and limit.
"""
def __init__(self, table_name, offset, limit):
super().__init__(table_name)
self.offset = offset
self.limit = limit
def get_query(self):
return f"""
SELECT * FROM {self.table_name}
LIMIT {self.offset}, {self.limit};
"""
def get_description(self):
return f"Paginates results from the '{self.table_name}' table with an offset of {self.offset} and a limit of {self.limit}."
class DistinctValues(MySQLRequest):
"""
Selects distinct values from a column.
"""
def __init__(self, table_name, column_name):
super().__init__(table_name)
self.column_name = column_name
def get_query(self):
return f"""
SELECT DISTINCT {self.column_name} FROM {self.table_name};
"""
def get_description(self):
return f"Selects distinct values from the '{self.column_name}' column in the '{self.table_name}' table."
class AggregateFunctions(MySQLRequest):
"""
Performs aggregate functions (COUNT, SUM, AVG, MIN, MAX) on a column.
"""
def __init__(self, table_name, function, column_name):
super().__init__(table_name)
self.function = function
self.column_name = column_name
def get_query(self):
return f"""
SELECT {self.function}({self.column_name}) FROM {self.table_name};
"""
def get_description(self):
return f"Performs the {self.function} function on the '{self.column_name}' column in the '{self.table_name}' table."
class GroupDataWithGroupBy(MySQLRequest):
"""
Groups rows by a column and applies an aggregate function.
"""
def __init__(self, table_name, group_by_column, aggregate_function, aggregate_column):
super().__init__(table_name)
self.group_by_column = group_by_column
self.aggregate_function = aggregate_function
self.aggregate_column = aggregate_column
def get_query(self):
return f"""
SELECT {self.group_by_column}, {self.aggregate_function}({self.aggregate_column})
FROM {self.table_name}
GROUP BY {self.group_by_column};
"""
def get_description(self):
return f"Groups rows by '{self.group_by_column}' and applies the {self.aggregate_function} function to the '{self.aggregate_column}' column in the '{self.table_name}' table."
class FilterGroupsWithHaving(MySQLRequest):
"""
Filters groups based on a condition after using GROUP BY.
"""
def __init__(self, table_name, group_by_column, aggregate_function, aggregate_column, having_condition):
super().__init__(table_name)
self.group_by_column = group_by_column
self.aggregate_function = aggregate_function
self.aggregate_column = aggregate_column
self.having_condition = having_condition
def get_query(self):
return f"""
SELECT {self.group_by_column}, {self.aggregate_function}({self.aggregate_column})
FROM {self.table_name}
GROUP BY {self.group_by_column}
HAVING {self.having_condition};
"""
def get_description(self):
return f"Groups rows by '{self.group_by_column}', applies the {self.aggregate_function} function to the '{self.aggregate_column}' column, and filters groups using the condition '{self.having_condition}' in the '{self.table_name}' table."
class Subquery(MySQLRequest):
"""
Uses a subquery to filter or retrieve data.
"""
def __init__(self, table_name, column_name, subquery):
super().__init__(table_name)
self.column_name = column_name
self.subquery = subquery
def get_query(self):
return f"""
SELECT * FROM {self.table_name}
WHERE {self.column_name} = ({self.subquery});
"""
def get_description(self):
return f"Uses a subquery to filter rows in the '{self.table_name}' table where '{self.column_name}' matches the result of the subquery."