-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryProcessor.py
More file actions
647 lines (586 loc) · 27.8 KB
/
Copy pathQueryProcessor.py
File metadata and controls
647 lines (586 loc) · 27.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
import os
import json
import shutil
from termcolor import colored
# User_defined Classes
import DataType
import Presentation
import LockingMechanismChecker as l
class QueryProcessor:
def __init__(self):
self.databaseName = ""
self.databaseDir = ""
self.transaction = False
self.locker = l.LockingMechanismChecker()
# ==========================common methods used below===========================================
def checkIfFilePathExist(self, dirName):
if os.path.exists(dirName):
return True
else:
return False
def getDataFromTable(self, tableName):
if (self.databaseDir == ""):
raise Exception("Select Database First")
tableDir = self.databaseDir + tableName + "/"
if self.checkIfFilePathExist(tableDir):
tableDataFilePath = tableDir + tableName + "_data.json"
tableData = ""
with open(tableDataFilePath) as file:
tableData = json.load(file)
return (tableData)
else:
raise Exception(tableName + " Table does not Exist")
def getValueType(self, data):
dataType = ["str", "int", "float"]
if (data == "str"):
return "string"
elif (data == "int"):
return 1
elif (data == "float"):
return 1.1
def getDataInRequiredFormat(self, value):
try:
data = int(value)
return data
except ValueError as e:
data = float(value)
return data
def saveDataToTable(self, tableName, tableData):
tableDataFilePath = self.databaseDir + tableName + "/" + tableName + "_data.json"
with open(tableDataFilePath, 'w') as f:
f.truncate()
json.dump(tableData, f)
# ============================== commands not afftected by transaction =========================================
def useDb(self, DBName):
dbDir = "AllDatabase/" + DBName
if (self.checkIfFilePathExist(dbDir)):
self.databaseName = DBName
self.databaseDir = "AllDatabase/" + self.databaseName + "/"
print(self.databaseName, " Selected")
else:
raise Exception("Database " + DBName + " Does Not Exist")
def createDB(self, DBName):
dbDir = "AllDatabase/" + DBName
if (self.checkIfFilePathExist(dbDir)):
raise Exception("Database already Exist")
else:
print(DBName, " Created")
os.makedirs(dbDir)
self.useDb(DBName)
def dropDb(self, databaseName):
databaseDir = "AllDatabase/" + databaseName
if (self.checkIfFilePathExist(databaseDir)):
shutil.rmtree(databaseDir)
print(databaseName + " Database droped")
else:
raise Exception("Database does not exist " + databaseName)
def createTable(self, tableName, columnDict, primaryKeyList, foreignKeyList=[]):
if (self.databaseName == ""):
raise Exception("Select Database first")
else:
tableName = tableName.rstrip().lstrip()
tableDir = self.databaseDir + tableName + "/"
tableMetadatFilePath = tableDir + tableName + "_metadata.json"
tableDataFilePath = tableDir + tableName + "_data.json"
if self.checkIfFilePathExist(tableDir):
raise Exception("table already Exist")
else:
os.makedirs(tableDir)
tabelMatadata = dict()
tabelMatadata["table_name"] = tableName
column = dict()
for key in columnDict.keys():
columnInputType = DataType.getPythonBasedDataType(columnDict[key])
column[key] = columnInputType
tabelMatadata["columns"] = column
tabelMatadata["primary_key"] = primaryKeyList
tabelMatadata["foreign_key"] = foreignKeyList
tabelMatadata = json.dumps(tabelMatadata)
with open(tableMetadatFilePath, 'w') as f:
f.truncate()
f.write(tabelMatadata)
with open(tableDataFilePath, 'w') as f:
f.truncate()
f.write("[]")
print(tableName + " Table Created")
def dropTable(self, tableName):
tableDir = self.databaseDir + tableName + "/"
if (self.checkIfFilePathExist(tableDir)):
shutil.rmtree(tableDir)
print(tableName + " table dropped")
else:
raise Exception("Wrong Table Name")
def saveToSQLFile(self,folderPath,dbName,queryList,message=""):
if (self.checkIfFilePathExist(folderPath)):
pass
else:
os.makedirs("sql")
dumpLocation = "sql/" + dbName + ".txt"
with open(dumpLocation, 'w') as f:
f.truncate()
f.write(message)
f.write(queryList)
print("SQL Dump Created at location: " + dumpLocation)
def dump(self, dbName):
dbDir = "AllDatabase/" + dbName
if (self.checkIfFilePathExist(dbDir)):
databaseDir = "AllDatabase/" + dbName + "/"
tableList = os.listdir("AllDatabase/" + dbName)
queryList = ""
for table in tableList:
if table == "lock.json":
continue
queryList = queryList + "\n"
queryList = queryList + "-- Table structure for table `" +table +"`"+"\n"
query = "CREATE TABLE " + table + " ( "
valueQuery = "INSERT INTO "+ table + " ( "
tableDir = databaseDir + table + "/"
tableDataFilePath = tableDir + table + "_metadata.json"
tableData = tableDir + table + "_data.json"
with open(tableDataFilePath) as file:
metaData = json.load(file)
with open(tableData) as file:
tableData = json.load(file)
for key in metaData["columns"]:
query = query + key + " " + metaData["columns"][key] + ","
valueQuery = valueQuery + key + ","
query = query.rstrip(query[-1])
valueQuery = valueQuery.rstrip(valueQuery[-1])
query = query + " );"
valueQuery = valueQuery + " ) VALUES ( "
queryList = queryList + query + "\n\n"
queryList = queryList + "-- Dumping data for table `" + table +"`"+"\n"
for data in tableData:
insertQuery = valueQuery
for value in data.values():
insertQuery = insertQuery + str(value) + ","
insertQuery = insertQuery.rstrip(insertQuery[-1])
insertQuery = insertQuery + " );"
queryList = queryList + insertQuery+ "\n"
sqlFolder = "sql"
self.saveToSQLFile(sqlFolder,dbName,queryList)
else:
raise Exception("Database " + dbName + " Does Not Exist")
# ================================== Transaction COMMANDS ====================================================
def startTransaction(self, DbName, tableName):
if(self.locker.checkLock(tableName,DbName)):
raise Exception ("Other User have lock on this table")
else:
self.locker.addLock(tableName, DbName)
self.transaction = True
self.tableName = tableName
self.databaseName = DbName
self.tableDataList = list()
self.savePointDict = dict() # { 'name': index, }
self.useDb(DbName)
self.tableDataPath = "AllDatabase/" + DbName + tableName + "_data.json"
self.tableDataList.append(self.getDataFromTable(tableName))
print("transaction started")
def setSavePoint(self, name):
self.savePointDict[name] = len(self.tableDataList) - 1
print("Save point set")
def rollback(self, savePoint=""):
if savePoint == "":
self.tableDataList = self.tableDataList[:1]
print("rolled back to orignal file")
self.locker.removeLock(self.tableName, self.databaseName)
self.transaction=False
elif savePoint in self.savePointDict.keys():
# rollback to save point
while len(self.tableDataList) > self.savePointDict[savePoint] + 1:
self.tableDataList.pop()
print("Rolled back to :" + savePoint)
else:
raise Exception("No such savepoint present")
def commit(self):
tableData = self.tableDataList[-1]
tableName = self.tableName
self.saveDataToTable(tableName, tableData)
print("Data Saved to db")
self.transaction = False
self.locker.removeLock(self.tableName , self.databaseName)
def isTransaction(self):
return self.transaction
# ========================================query that dont use Transactions====================================
# ============================== commands afftected by transaction =========================================
def selectQuery(self, tableName, columnListToDisplay=[], condition={}):
if self.isTransaction():
if (tableName == self.tableName):
tableData = self.tableDataList[-1].copy()
else:
raise Exception(
"transaction have lock on table " + self.tableName + " Commit or rollback to start new transaction")
else:
tableData = self.getDataFromTable(tableName)
if (len(tableData) == 0):
print(tableName + " Table is Empty")
return
else:
if (len(condition) > 0):
data = list()
for row in tableData:
colToCheck = condition["columnName"]
operator = condition["operator"]
valueToCheck = condition["value"]
if (colToCheck in row.keys()):
if (operator == "="):
if (str(row[colToCheck]) == (valueToCheck)):
data.append(row)
elif (operator == ">"):
if (str(row[colToCheck]) > (valueToCheck)):
data.append(row)
elif (operator == "<"):
if (str(row[colToCheck]) < (valueToCheck)):
data.append(row)
elif (operator == ">="):
if (str(row[colToCheck]) >= (valueToCheck)):
data.append(row)
elif (operator == "<="):
if (str(row[colToCheck]) <= (valueToCheck)):
data.append(row)
elif (operator == "!="):
if (str(row[colToCheck]) != (valueToCheck)):
data.append(row)
'''
tableHeader = dict()
for key in tableData[0].keys():
tableHeader[key] = ""
data.insert(0, tableHeader)
'''
tableData = data
if ("*" not in (columnListToDisplay)):
data = list()
for row in tableData:
selectedCols = dict()
for col in columnListToDisplay:
selectedCols[col] = row[col]
data.append(selectedCols)
'''
tableHeader = dict()
for key in data[0].keys():
tableHeader[key] = ""
data.insert(0, tableHeader)
'''
tableData = data
# Display to user
Presentation.displayJSONListData(tableData)
def deleteQuery(self, tableName, condition=[]):
if self.isTransaction():
if (tableName == self.tableName):
tableData = self.tableDataList[-1].copy()
else:
raise Exception(
"transaction have lock on table " + self.tableName + " Commit or rollback to start new transaction")
else:
tableData = self.getDataFromTable(tableName)
if(self.locker.checkLock(tableName,self.databaseName)):
raise Exception("This table is locked by other user")
else:
self.locker.addLock(tableName,self.databaseName)
if (len(tableData) == 0):
print(tableName + " Table is Empty")
self.locker.removeLock(tableName, self.databaseName)
return
if (len(condition) > 0):
dataLength = len(tableData)
data = tableData[:]
i = 0
while i < dataLength:
row = data[i]
colToCheck = condition["columnName"]
operator = condition["operator"]
valueToCheck = condition["value"]
if (colToCheck in row.keys()):
if (operator == "="):
if (str(row[colToCheck]) == (valueToCheck)):
data.remove(row)
i = i - 1
dataLength = dataLength - 1
elif (operator == ">"):
if (str(row[colToCheck]) > (valueToCheck)):
data.remove(row)
i = i - 1
dataLength = dataLength - 1
elif (operator == "<"):
if (str(row[colToCheck]) < (valueToCheck)):
data.remove(row)
i = i - 1
dataLength = dataLength - 1
elif (operator == ">="):
if (str(row[colToCheck]) >= (valueToCheck)):
data.remove(row)
i = i - 1
dataLength = dataLength - 1
elif (operator == "<="):
if (str(row[colToCheck]) <= (valueToCheck)):
data.remove(row)
i = i - 1
dataLength = dataLength - 1
elif (operator == "!="):
if (str(row[colToCheck]) != (valueToCheck)):
data.remove(row)
i = i - 1
dataLength = dataLength - 1
i = i + 1
tableData = data
if self.isTransaction():
self.tableDataList.append(tableData)
else:
tableDir = self.databaseDir + tableName + "/"
tableDataFilePath = tableDir + tableName + "_data.json"
with open(tableDataFilePath, 'w') as f:
f.truncate()
if (len(condition) > 0):
json.dump(tableData, f)
else:
f.write("[]")
self.locker.removeLock(tableName,self.databaseName)
def insertQuery(self, tableName, valueList, colList=[]):
if self.isTransaction():
if (tableName == self.tableName):
tableData = self.tableDataList[-1].copy()
else:
raise Exception(
"transaction have lock on table " + self.tableName + " Commit or rollback to start new transaction")
else:
tableData = self.getDataFromTable(tableName)
if (self.locker.checkLock(tableName, self.databaseName)):
raise Exception("This table is locked by other user")
else:
self.locker.addLock(tableName, self.databaseName)
tableDir = self.databaseDir + tableName + "/"
tableDataFilePath = tableDir + tableName + "_metadata.json"
metaData = ""
with open(tableDataFilePath) as file:
metaData = json.load(file)
metaData = metaData["columns"]
if (len(colList) == 0):
if (len(valueList) == len(metaData)):
i = 0
dataRow = dict()
for key in metaData.keys():
if type(self.getValueType(metaData[key])) != type("str"):
try:
dataInNeededFormat = self.getDataInRequiredFormat(valueList[i])
except:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(
str(key) + " should be of type " + str(type(self.getValueType(metaData[key]))))
if (type(dataInNeededFormat) == type(self.getValueType(metaData[key]))):
dataRow[key] = dataInNeededFormat
else:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(
str(key) + " should be of type " + str(type(self.getValueType(metaData[key]))))
else:
# its in str format so just put it in
dataRow[key] = valueList[i]
i = i + 1
tableData.append(dataRow)
print("data added")
else:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(tableName + " have " + str(len(metaData)) + " columns but " + str(
len(valueList)) + " values was given")
else:
i = 0
dataRow = dict()
for col in colList:
col = col.lstrip().rstrip()
if col in metaData.keys():
if type(self.getValueType(metaData[col])) != type("str"):
try:
dataInNeededFormat = self.getDataInRequiredFormat(valueList[i])
except:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(
str(col) + " should be of type " + str(type(self.getValueType(metaData[col]))))
if (type(dataInNeededFormat) == type(self.getValueType(metaData[col]))):
dataRow[col] = dataInNeededFormat
else:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(
str(col) + " should be of type " + str(type(self.getValueType(metaData[col]))))
else:
dataRow[col] = valueList[i]
i = i + 1
for key in metaData:
if key in dataRow.keys():
pass
else:
dataRow[key] = ""
tableData.append(dataRow)
print("data added")
if self.isTransaction():
self.tableDataList.append(tableData)
else:
tableDir = self.databaseDir + tableName + "/"
tableDataFilePath = tableDir + tableName + "_data.json"
with open(tableDataFilePath, 'w') as f:
f.truncate()
json.dump(tableData, f)
self.locker.removeLock(tableName, self.databaseName)
return metaData
# method made just of Update query
def updateRow(self, tableName,tableData, i, colList, metaData):
for key in colList.keys():
if key in tableData[i].keys():
if type(self.getValueType(metaData[key])) != type("str"):
try:
dataInNeededFormat = self.getDataInRequiredFormat(colList[key])
except:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(str(key) + " should be of type " + str(type(self.getValueType(metaData[key]))))
if (type(dataInNeededFormat) == type(self.getValueType(metaData[key]))):
if (type(colList[key]) == type(self.getValueType(metaData[key]))):
tableData[i][key] = dataInNeededFormat
else:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(str(key) + " should be of type " + str(type(self.getValueType(metaData[key]))))
else:
# its in str format so just put it in
tableData[i][key] = colList[key]
else:
self.locker.removeLock(tableName, self.databaseName)
raise Exception(key + " not present in table")
def updateQuery(self, tableName, colList, condition):
if self.isTransaction():
if (tableName == self.tableName):
tableData = self.tableDataList[-1].copy()
else:
raise Exception(
"transaction have lock on table " + self.tableName + " Commit or rollback to start new transaction")
else:
tableData = self.getDataFromTable(tableName)
if (self.locker.checkLock(tableName, self.databaseName)):
raise Exception("This table is locked by other user")
else:
self.locker.addLock(tableName, self.databaseName)
tableDir = self.databaseDir + tableName + "/"
tableDataFilePath = tableDir + tableName + "_metadata.json"
metaData = ""
with open(tableDataFilePath) as file:
metaData = json.load(file)
metaData = metaData["columns"]
if (len(tableData) == 0):
self.locker.removeLock(tableName, self.databaseName)
print(tableName + " Table is Empty")
return
else:
if (len(condition) > 0):
dataLength = len(tableData)
i = 0
while i < dataLength:
row = tableData[i]
colToCheck = condition["columnName"]
operator = condition["operator"]
valueToCheck = condition["value"]
if (colToCheck in row.keys()):
if operator == "=":
if (str(row[colToCheck]) == (valueToCheck)):
self.updateRow(tableData, i, tableName,colList, metaData)
elif operator == ">":
if (str(row[colToCheck]) > (valueToCheck)):
self.updateRow(tableData, i, tableName,colList, metaData)
elif (operator == "<"):
if (str(row[colToCheck]) < (valueToCheck)):
self.updateRow(tableData, i, tableName,colList, metaData)
elif (operator == ">="):
if (str(row[colToCheck]) >= (valueToCheck)):
self.updateRow(tableData, i, tableName,colList, metaData)
elif (operator == "<="):
if (str(row[colToCheck]) <= (valueToCheck)):
self.updateRow(tableData, i, tableName,colList, metaData)
elif (operator == "!="):
if (str(row[colToCheck]) != (valueToCheck)):
self.updateRow(tableData, i,tableName, colList, metaData)
i = i + 1
if self.isTransaction():
self.tableDataList.append(tableData)
else:
tableDir = self.databaseDir + tableName + "/"
tableDataFilePath = tableDir + tableName + "_data.json"
with open(tableDataFilePath, 'w') as f:
f.truncate()
json.dump(tableData, f)
self.locker.removeLock(tableName, self.databaseName)
print("Table Updated")
def describeTable(self, tableName):
if self.isTransaction():
if (tableName == self.tableName):
tableData = self.tableDataList[-1].copy()
else:
raise Exception(
"transaction have lock on table " + self.tableName + " Commit or rollback to start new transaction")
else:
tableDir = self.databaseDir + tableName + "/"
if(not self.checkIfFilePathExist(tableDir)):
raise Exception ("Table Name is not Valid")
tableDataFilePath = tableDir + tableName + "_metadata.json"
metaData = ""
with open(tableDataFilePath) as file:
metaData = json.load(file)
print(colored("\n#####################################################", 'green'))
print(colored("\n STRUCTURE FOR TABLE: " + tableName, 'green'))
print("{:<30}".format("Column Name"), end=' ')
print("{:<30}".format("Data Type"), end=' ')
print()
for key in metaData["columns"]:
print("{:<30}".format(key), end=' ')
print("{:<30}".format(metaData["columns"][key]), end=' ')
print()
print(colored("\n#####################################################", 'green'))
def describeDb(self, dbName):
print(colored("\n#####################################################", 'green'))
print(colored("\n Tables in Databse: " + dbName, 'green'))
self.databaseDir = "AllDatabase/" + dbName + "/"
tableList = os.listdir("AllDatabase/" + dbName)
for table in tableList:
if table == "lock.json":
continue
self.describeTable(table)
def getCardinality(self, tableDir,table_name, column):
tableDataFilePath = tableDir + table_name + "_data.json"
with open(tableDataFilePath) as file:
data = json.load(file)
check = list()
for row in data:
if row[column] in check:
return "N"
else:
check.append(row[column])
return "1"
def showErd(self, dbName):
databaseDir = "AllDatabase/" + dbName
if (self.checkIfFilePathExist(databaseDir)):
print("================= ER Diagram for ",dbName," is below ==============")
print("{:<15}".format('Table-1'), "{:<15}".format('Column'), "{:<15}".format('------->'),
"{:<15}".format('Table-2'), "{:<15}".format('Column'))
databaseDir = databaseDir + "/"
tableList = os.listdir("AllDatabase/" + dbName)
for table in tableList:
if table == "lock.json":
continue
tableDir = databaseDir + table + "/"
tableDataFilePath = tableDir + table + "_metadata.json"
metaData = ""
with open(tableDataFilePath) as file:
metaData = json.load(file)
if (len(metaData["foreign_key"]) <= 0):
break
for data in metaData["foreign_key"]:
foreignTable = list(data)[1]
foreignTableDir = databaseDir + foreignTable + "/"
if (self.checkIfFilePathExist(foreignTableDir)):
print("{:<15}".format(metaData['table_name']), end=' ')
print("{:<15}".format(data["column"]), end=' ')
print("{:<15}".format('---('+
self.getCardinality(tableDir,metaData['table_name'],data["column"])+
"-"+self.getCardinality(databaseDir+foreignTable+"/",foreignTable,data[foreignTable])
+')---->'), end=' ')
print("{:<15}".format(foreignTable), end=' ')
print("{:<15}".format(data[foreignTable]), end=' ')
print()
else:
raise Exception(foreignTable, " does not exist in database")
print()