-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
291 lines (232 loc) · 8.26 KB
/
helper.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
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
import csv
import re
from class_table import *
import operator
def getDB():
table_list = []
with open('metadata.txt', 'r') as f:
check = 0
attr = []
table_name = ''
file_name = ''
for line in f:
line = line.strip()
if line == '<end_table>':
table = []
with open(file_name, 'rb') as csvfile:
content = csv.reader(csvfile, delimiter = ',')
for row in content:
table.append(row)
table_list.append(Table(table_name, attr, table))
attr = []
check = 0
elif check == 0:
check = 1
elif check == 1:
table_name = line
file_name = table_name + '.csv'
check = 2
else:
attr.append(line)
return table_list
########################################################################
def ValidateTablename(from_query, table_list):
for f in from_query:
found = 0
for table in table_list:
if f == table.name:
found = 1
break
if found == 0:
return "Error: table does not exist"
return True
##############################################################################
def GetTablesRows(from_query, table_list):
X = []
Y = []
for t in table_list:
if from_query[0] == t.name:
X = t.table
if len(from_query) > 1 and from_query[1] == t.name:
Y = t.table
return X, Y
###############################################################################
def Join(X, Y):
joinTable = []
'''for row1 in X:
for row2 in Y:
joinTable.append(row1 + row2)
'''
for row2 in Y:
for row1 in X:
joinTable.append(row1 + row2)
return joinTable
##############################################################################
def GetOperator(where_query):
#Get the operator in where and parse it
operator = ''
if 'AND' in where_query:
index = where_query.index('AND')
operator = 'AND'
elif 'OR' in where_query:
index = where_query.index('OR')
operator = 'OR'
else:
index = -1
if operator != '':
q1 = ''.join(where_query[0:index])
q2 = ''.join(where_query[index + 1:])
conditions = [q1, q2] #query
else:
q = ''.join(where_query)
conditions = [q]
return operator, conditions
############################################################################
def getHash(from_query, require_tables):
i = 0
dictionary = {}
for table in require_tables:
for attr in table.attr:
dictionary[table.name + '.' + attr] = i
i = i + 1
if len(require_tables) == 1:
i = 0
for attr in require_tables[0].attr:
dictionary[attr] = i
i = i + 1
return dictionary
else:
temp = []
for attr1 in require_tables[0].attr:
for attr2 in require_tables[1].attr:
if attr1 == attr2:
temp.append(attr1)
if temp == []:
return dictionary
i = 0
for attr1 in require_tables[0].attr:
if attr1 not in temp:
dictionary[attr1] = i;
i = i + 1
j = 0
for attr2 in require_tables[1].attr:
if attr2 not in temp:
dictionary[attr2] = len(require_tables[0].attr) + j
j = j + 1
return dictionary
##########################################################################
def SingleCondition(conditions, attr_dictionary, ops_dictionary, joinTable):
"If single condition in where"
symbols = ['=', '>', '<', '>=', '<=']
operator = ''
for sym in symbols:
if sym in conditions[0]:
operator = sym
conditions = re.findall(r'[^(%s)\s]+' %operator , conditions[0])
if operator == '':
return "Error: Valid Operator does not exist", []
'''
if i != 1:
#There can be only be only two operand
return "Error: Invalid Syntax in condition", []
'''
table = []
for row in joinTable:
if conditions[0] in attr_dictionary:
i = attr_dictionary[conditions[0]]
else:
return "Error: Attribute does not exist", []
#Either it's a column name or a number
if conditions[1] in attr_dictionary: #check if it's a column name
j = attr_dictionary[conditions[1]]
if ops_dictionary[operator](int(row[i]), int(row[j])):
table.append(row)
else: #check if it's a number
if conditions[1].isdigit():
j = conditions[1]
if ops_dictionary[operator](int(row[i]), int(j)):
table.append(row)
else:
#Found characters other than numeric
return "Error: Invalid operand", []
return True, table
#################################################################################
def DoubleCondition(conditions, attr_dictionary, ops_dictionary, joinTable, operator):
'''For two conditions in where joined by AND or OR'''
symbols = ['=', '>', '<', '>=', '<=']
op1 = ''
op2 = ''
for sym in symbols:
if sym in conditions[0]:
op1 = sym
cond1 = conditions[0].split(op1)
if sym in conditions[1]:
op2 = sym
cond2 = conditions[1].split(op2)
if op1 == '' or op2 == '':
#No operator found, INVALID
return "Error: Valid Operator does not exist", []
'''if i != 1 or j != 1:
#There can be only be only two operand
return "Error: Invalid Syntax in condition", []
'''
table = []
for row in joinTable:
if cond1[0] in attr_dictionary and cond2[0] in attr_dictionary:
i1 = attr_dictionary[cond1[0]]
i3 = attr_dictionary[cond2[0]]
else:
return "Error: Attribute does not exist", []
if cond1[1] in attr_dictionary:
i2 = attr_dictionary[cond1[1]]
val1 = ops_dictionary[op1](int(row[i1]), int(row[i2]))
elif cond1[1].isdigit():
i2 = cond1[1]
val1 = ops_dictionary[op1](int(row[i1]), int(i2))
else:
return "Error: Invalid operand", []
if cond2[1] in attr_dictionary:
i4 = attr_dictionary[cond2[1]]
val2 = ops_dictionary[op2](int(row[i3]), int(row[i4]))
elif cond2[1].isdigit():
i4 = cond2[1]
val2 = ops_dictionary[op2](int(row[i3]), int(i4))
else:
return "Error: Invalid operand", []
if operator == 'AND':
if val1 and val2:
table.append(row)
else:
if val1 or val2:
table.append(row)
return True, table
###################################################################################
def printTable(finalTable, select_query, require_tables):
for q in select_query:
if 'distinct' in q or 'DISTINCT' in q or 'Distinct' in q:
print select_query[0]
for element in finalTable:
print element
return
temp = []
if '*' in select_query:
if len(require_tables) > 1:
for i in range(len(require_tables[0].attr)):
require_tables[0].attr[i] = require_tables[0].name + '.' + require_tables[0].attr[i]
for i in range(len(require_tables[1].attr)):
require_tables[1].attr[i] = require_tables[1].name + '.' + require_tables[1].attr[i]
temp = require_tables[0].attr + require_tables[1].attr
if len(require_tables) == 1:
for i in range(len(require_tables[0].attr)):
require_tables[0].attr[i] = require_tables[0].name + '.' + require_tables[0].attr[i]
temp = require_tables[0].attr
else :
for q in select_query:
temp.append(q)
print(" ".join([element for element in temp]))
for row in finalTable:
#print row
if isinstance(row,list):
print (" ".join([element for element in row]))
else:
print row