forked from rajhrikshit/nl2sql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.py
More file actions
339 lines (263 loc) · 9.63 KB
/
query.py
File metadata and controls
339 lines (263 loc) · 9.63 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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
from .constants import Color
class Select():
def __init__(self):
self.columns = []
def add_column(self, column, column_type):
if [column, column_type] not in self.columns:
self.columns.append([column, column_type])
def get_column(self):
return self.columns
def get_just_column_name(self, column):
if column != str(None):
return column.rsplit('.', 1)[1]
else:
return column
def print_column(self, selected_column):
column = selected_column[0]
column_type = selected_column[1]
if column is None:
if column_type is not None:
if 'COUNT' in column_type:
return Color.BOLD + 'COUNT(' + Color.END + '*' + Color.BOLD + ')' + Color.END
elif 'DISTINCT' in column_type:
return Color.BOLD + 'DISTINCT' + Color.END
else:
return '*'
else:
return '*'
else:
if 'DISTINCT' in column_type:
if 'COUNT' in column_type:
return Color.BOLD + 'COUNT(DISTINCT ' + Color.END + str(column) + Color.BOLD + ')' + Color.END
else:
return Color.BOLD + 'DISTINCT ' + Color.END + str(column)
if 'COUNT' in column_type:
return Color.BOLD + 'COUNT(' + Color.END + str(column) + Color.BOLD + ')' + Color.END
elif 'AVG' in column_type:
return Color.BOLD + 'AVG(' + Color.END + str(column) + Color.BOLD + ')' + Color.END
elif 'SUM' in column_type:
return Color.BOLD + 'SUM(' + Color.END + str(column) + Color.BOLD + ')' + Color.END
elif 'MAX' in column_type:
return Color.BOLD + 'MAX(' + Color.END + str(column) + Color.BOLD + ')' + Color.END
elif 'MIN' in column_type:
return Color.BOLD + 'MIN(' + Color.END + str(column) + Color.BOLD + ')' + Color.END
else:
return str(column)
def __str__(self):
select_string = ''
for i in range(0, len(self.columns)):
if i == (len(self.columns) - 1):
select_string += str(self.print_column(self.columns[i]))
else:
select_string += str(self.print_column(self.columns[i])) + ', '
return Color.BOLD + 'SELECT ' + Color.END + select_string
class From():
table = ''
def __init__(self, table=None):
if table is not None:
self.table = table
else:
self.table = ''
def set_table(self, table):
self.table = table
def get_table(self):
return self.table
def __str__(self):
return '\n' + Color.BOLD + 'FROM ' + Color.END + str(self.table)
class Join():
tables = []
links = []
def __init__(self):
self.tables = []
self.links = []
def add_table(self, table):
if table not in self.tables:
self.tables.append(table)
def set_links(self, links):
self.links = links
def get_tables(self):
return self.tables
def get_links(self):
return self.links
def __str__(self):
if len(self.links) >= 1:
string = ''
for i in range(0, len(self.links)):
string += '\n' + Color.BOLD + 'INNER JOIN ' + Color.END + str(
self.links[i][1][0]) + '\n' + Color.BOLD + 'ON ' + Color.END + str(self.links[i][0][0]) + '.' + str(
self.links[i][0][1]) + ' = ' + str(self.links[i][1][0]) + '.' + str(self.links[i][1][1])
return string
elif len(self.tables) >= 1:
if len(self.tables) == 1:
return '\n' + Color.BOLD + 'NATURAL JOIN ' + Color.END + self.tables[0]
else:
string = '\n' + Color.BOLD + 'NATURAL JOIN ' + Color.END
for i in range(0, len(self.tables)):
if i == (len(self.tables) - 1):
string += str(self.tables[i])
else:
string += str(self.tables[i]) + ', '
return string
else:
return ''
class Condition():
column = ''
column_type = ''
operator = ''
value = ''
def __init__(self, column, column_type, operator, value):
self.column = column
self.column_type = column_type
self.operator = operator
self.value = value
def get_column(self):
return self.column
def get_column_type(self):
return self.column_type
def get_operator(self):
return self.operator
def get_value(self):
return self.value
def get_in_list(self):
return [self.column, self.column_type, self.operator, self.value]
def get_just_column_name(self, column):
if column != str(None):
return column.rsplit('.', 1)[1]
else:
return column
def get_column_with_type_operation(self, column, column_type):
if column_type is None:
return self.column
else:
return Color.BOLD + str(column_type) + '(' + Color.END + self.column + Color.BOLD + ')' + Color.END
def get_pretty_operator(self, operator):
if operator == 'BETWEEN':
return Color.BOLD + 'BETWEEN' + Color.END + ' OOV ' + Color.BOLD + 'AND' + Color.END
else:
return Color.BOLD + operator + Color.END
def __str__(self):
return str(self.get_column_with_type_operation(self.column, self.column_type)) + ' ' + str(
self.get_pretty_operator(self.operator)) + ' ' + str(self.value)
class Where():
conditions = []
def __init__(self, clause=None):
if clause is not None:
self.conditions.append([None, clause])
else:
self.conditions = []
def add_condition(self, junction, clause):
self.conditions.append([junction, clause])
def get_conditions(self):
return self.conditions
def __str__(self):
string = ''
if len(self.conditions) >= 1:
for i in range(0, len(self.conditions)):
if i == 0:
string += '\n' + Color.BOLD + 'WHERE' + Color.END + ' ' + str(self.conditions[i][1])
else:
string += '\n' + Color.BOLD + str(self.conditions[i][0]) + Color.END + ' ' + str(
self.conditions[i][1])
return string
else:
return string
class GroupBy():
column = None
def __init__(self, column=None):
if column is not None:
self.column = column
else:
self.column = None
def set_column(self, column):
self.column = column
def get_column(self):
return self.column
def get_just_column_name(self, column):
if column != str(None):
return column.rsplit('.', 1)[1]
else:
return column
def __str__(self):
if self.column is not None:
return '\n' + Color.BOLD + 'GROUP BY ' + Color.END + str(self.column)
else:
return ''
class OrderBy():
columns = []
def __init__(self):
self.columns = []
def add_column(self, column, order):
if [column, order] not in self.columns:
self.columns.append([column, order])
def get_columns(self):
return self.columns
def __str__(self):
if self.columns != []:
string = Color.BOLD + 'ORDER BY ' + Color.END
for i in range(0, len(self.columns)):
if i == (len(self.columns) - 1):
string += self.columns[i][0] + ' ' + Color.BOLD + self.columns[i][1] + Color.END
else:
string += self.columns[i][0] + ' ' + Color.BOLD + self.columns[i][1] + Color.END + ', '
return '\n' + string
else:
return ''
class Query():
select = None
_from = None
join = None
where = None
group_by = None
order_by = None
def __init__(self, select=None, _from=None, join=None, where=None, group_by=None, order_by=None):
if select is not None:
self.select = select
else:
self.select = None
if _from is not None:
self._from = _from
else:
self._from = None
if join is not None:
self.join = join
else:
self.join = None
if where is not None:
self.where = where
else:
self.where = None
if group_by is not None:
self.group_by = group_by
else:
self.group_by = None
if order_by is not None:
self.order_by = order_by
else:
self.order_by = None
def set_select(self, select):
self.select = select
def get_select(self):
return self.select
def set_from(self, _from):
self._from = _from
def get_from(self):
return self._from
def set_join(self, join):
self.join = join
def get_join(self):
return self.join
def set_where(self, where):
self.where = where
def get_where(self):
return self.where
def set_group_by(self, group_by):
self.group_by = group_by
def get_group_by(self):
return self.group_by
def set_order_by(self, order_by):
self.order_by = order_by
def get_order_by(self):
return self.order_by
def __str__(self):
return '\n' + str(self.select) + str(self._from) + str(self.join) + str(self.where) + str(self.group_by) + str(
self.order_by) + ';\n'