-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheSqlite.py
937 lines (636 loc) · 28.9 KB
/
eSqlite.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
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
# from . import SED
import SED
import sqlite3 as sq
from tabulate import tabulate
# some of the global methods that will not be accesed by normal user of this module
class ESQLiteGlobalMethods:
# function to tell if the string has the passed subString
@classmethod
def isSubString(cls , string, subString):
string = str(string)
subString = str(subString)
lengthOfSubString = len(subString)
try:
for i, j in enumerate(string):
if(j == subString[0]):
if(subString == string[i:i+lengthOfSubString]):
return True
else:
pass
return False
except Exception:
return False
# main class
class SQLiteConnect:
def __init__(self):
# to tell if the security is on in data base or not
self.security = False
# object for the SED module
self.objSecurity = SED.ED()
# connObj to the data base
self.connObj = None
# dataBase name
self.dataBaseName = None
# to add to table name when table is secured
self.tableNameAdd = "secured_usingSED"
# name of the password storing table
self.passwordStorerTable = "password storer table , 0b242ba11ab4a144a48cd25e88b98d161a7ba1c68ad65646cb9207f66aee1a64"
# table name which the module is currently working on
self.tableName = None
# function to enable to database encryption and to set password as well
def setPassword(self , password , pin = 123456):
"""return None if the password not present in data base before (first time use)
return True if the password and pin are setted correctly
return False if the authentication failed"""
self.objSecurity.setPassword_Pin(password , pin)
# storing the current table name to restore it later
tempTableName = self.tableName
# content list for generating the password table
contentList = [["PASS" , "TEXT" , 1]]
tempSecurity = self.security
try:
# exception will be raised if the table already exist or some problem occur
# security needs to turned on to add self.tableNameAdd to tableName while creating table
self.security = True
# creating table
self.createTable(self.passwordStorerTable , contentList , True)
valuesList = [self.objSecurity.returnPassForStoring()]
self.insertIntoTable(valuesList , forPass=True)
# restoring data
self.tableName = tempTableName
self.security = tempSecurity
return
except Exception:
# security needs to be off so that self.tbaleNameAdd is not added
self.security = False
# getting the current password stored in table
data = self.returnDataOfKey(0 , self.passwordStorerTable + " " + self.tableNameAdd)
data = data[0]
data = data[1]
self.security = tempSecurity
self.tableName = tempTableName
if(self.objSecurity.authenticatePassword(data , password , pin)):
return True
else:
return False
# function to encryt the things
def encrypter(self , string):
return self.objSecurity.encrypter(str(string))
# function to decrypt the things
def decrypter(self , string):
return self.objSecurity.decrypter(str(string))
# function for toggling the security status
def setSecurityStatus(self , status = True):
self.security = status
# function to set the database name
def setDatabase(self, dataBaseName):
self.connObj = sq.connect(dataBaseName , check_same_thread=False)
self.dataBaseName = dataBaseName
# function to get the database name
def getDatabase(self):
return self.dataBaseName
#TODO: return the name + path
# function to create the table
def createTable(self, tableName , contentList , raiseException = False):
"""table parameters -
data types - INT TEXT REAL
content List = [ [nameOfCol , dataBaseType , 0 for NULL or 1 for not NULL] , similar more objects ]
raise exception is False so that if the table already exist it does not raise any exception
but if you think their is some other problem then you make this true to see what is causing he problem"""
# storing teh table name for further use
self.tableName = str(tableName)
# a secure tag will be added after the table name
if(self.security and (not(ESQLiteGlobalMethods.isSubString(tableName , self.tableNameAdd)))):
tableName = tableName + " " + self.tableNameAdd
# generating the query string
string = "CREATE TABLE " + "'" + tableName + "'" + " ("
# adding ID
string = string + "ID INT PRIMARY KEY NOT NULL,"
# adding rest of the content list
for i in contentList:
# adding the col name
string = string + "'" + str(i[0]) + "'" + " "
# adding col data type
if(i[1] == "INT"):
string = string + "INT" + " "
elif(i[1] == "REAL"):
string = string + "REAL" + " "
else:
string = string + "TEXT" + " "
# adding null or not null
try:
if(int(i[2]) == 1):
string = string + "NOT NULL,"
else:
string = string + ","
except IndexError:
string = string + ","
string = string[:-1] + ");"
if(raiseException):
self.connObj.execute(string)
else:
try:
self.connObj.execute(string)
except Exception:
pass
def getOperableTableName(self , tableName):
# checking the module as some stored table name or not
if(tableName == None):
if(self.tableName == None):
raise Exception("either pass a table name or create that table using createTable function")
else:
tableName = self.tableName
# self.tableNameAdd will be added so that secured table can be identified during password change
if(self.security):
# need to be only added once
if(not(ESQLiteGlobalMethods.isSubString(tableName , self.tableNameAdd))):
tableName = tableName + " " + self.tableNameAdd
return tableName
# function to insert data into table
def insertIntoTable(self, valuesList , keyPass = None , tableName = None , forPass = False , commit = True):
tableName = self.getOperableTableName(tableName)
# getting the key value if not passed
if(keyPass == None):
key = self.returnLastKey()
if(key == None):
key = 0
else:
key = key + 1
else:
key = int(keyPass)
# generating query string
string = "INSERT INTO " + "'" + tableName + "'" + " ("
# column list in the table
colList = []
# datatypes of the cols in the table
dataTypeList = []
# getting the table info
cor = self.connObj.execute("PRAGMA table_info(" + "'" + tableName + "'" + ")")
# generating col list and datatype list
for i in cor:
colList.append(i[1])
dataTypeList.append(i[2])
# adding cols to query
for i in colList:
string = string + "'" + i + "'" + ","
string = string[:-1] + " ) VALUES (" + str(key) + ","
tempCount = 1
# adding values to query
for i in valuesList:
# if the encryption is on then it will be always text
# also while insert data into the password table , it should not be encrypted further
if(self.security and (not(forPass))):
string = string + "'" + self.encrypter(i) + "'" + ","
else:
# checking the col data is of INT or REAL
if((dataTypeList[tempCount] == "INT") or (dataTypeList[tempCount] == "REAL")):
string = string + str(i) + ","
else:
string = string + "'" + i + "'" + ","
tempCount += 1
string = string[:-1] + " )"
self.connObj.execute(string)
if(commit):
self.connObj.commit()
# function to return the last ID
# id will be none if no data is present in table
def returnLastKey(self , tableName = None):
tableName = self.getOperableTableName(tableName)
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
id = None
for row in cursor:
id = int(row[0])
return id
# print data of a particular key
def printDataOfKey(self, key , errorMessage = "key could not be found" , tableName = None):
tableName = self.getOperableTableName(tableName)
# column list in the table
colList = []
# datatypes of the cols in the table
dataTypeList = []
# getting the table info
cor = self.connObj.execute("PRAGMA table_info(" + "'" + tableName + "'" + ")")
# generating col list and datatype list
for i in cor:
colList.append(i[1])
dataTypeList.append(i[2])
table = []
found = False
# prasing through the table
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
for row in cursor:
tempTable = []
# the key matches with id in table
if(int(row[0] == key)):
count = 0
# adding all the cols data
for i,j in zip(colList , dataTypeList):
# ID is not encryted
# things will be conv to respective data type after decryption
if(self.security and (i != "ID")):
if(j == "TEXT"):
try:
tempTable.append(self.decrypter((row[count])))
except Exception:
tempTable.append(None)
elif(j == "INT"):
try:
tempTable.append(int(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
else:
try:
tempTable.append(float(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
# if the data is not ecrypted
else:
tempTable.append(row[count])
count += 1
found = True
if(len(tempTable) > 0):
table.append(tempTable)
if(found):
print(tabulate(table, headers=colList))
else:
print(errorMessage)
# print entire data in a table
def printData(self , errorMessage = "No data in table" , tableName = None):
tableName = self.getOperableTableName(tableName)
# column list in the table
colList = []
# datatypes of the cols in the table
dataTypeList = []
# getting the table info
cor = self.connObj.execute("PRAGMA table_info(" + "'" + tableName + "'" + ")")
# generating col list and datatype list
for i in cor:
colList.append(i[1])
dataTypeList.append(i[2])
table = []
found = False
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
for row in cursor:
tempTable = []
count = 0
# getting all col data
for i,j in zip(colList , dataTypeList):
# ID is not encrypted
if(self.security and (i != "ID")):
if(j == "TEXT"):
try:
tempTable.append(self.decrypter((row[count])))
except Exception:
tempTable.append(None)
elif(j == "INT"):
try:
tempTable.append(int(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
else:
try:
tempTable.append(float(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
# if the data is not encrypted
else:
tempTable.append(row[count])
count += 1
found = True
table.append(tempTable)
if(found):
print(tabulate(table, headers=colList))
else:
print(errorMessage)
# in built tabulate printer
def tabulatePrinter(self , table , headersList):
print(tabulate(table, headers=headersList))
# return data of a particular key
# returns none if not found
def returnDataOfKey(self, key , tableName = None):
tableName = self.getOperableTableName(tableName)
# column list in the table
colList = []
# datatypes of the cols in the table
dataTypeList = []
# getting the table info
cor = self.connObj.execute("PRAGMA table_info(" + "'" + tableName + "'" + ")")
# generating col list and datatype list
for i in cor:
colList.append(i[1])
dataTypeList.append(i[2])
table = []
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
for row in cursor:
tempTable = []
if(int(row[0] == key)):
count = 0
# adding all the cols data
for i,j in zip(colList , dataTypeList):
if(self.security and (i != "ID")):
if(j == "TEXT"):
try:
tempTable.append(self.decrypter((row[count])))
except Exception:
tempTable.append(None)
elif(j == "INT"):
try:
tempTable.append(int(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
else:
try:
tempTable.append(float(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
# if not encrypted
else:
tempTable.append(row[count])
count += 1
if(len(tempTable) > 0):
table.append(tempTable)
if(len(table) > 0):
return table
else:
return None
# return entire data in a table
# returns None if no data is present
# i[1] of this list is the list of col names
def returnData(self , tableName = None):
tableName = self.getOperableTableName(tableName)
# column list in the table
colList = []
# datatypes of the cols in the table
dataTypeList = []
# getting the table info
cor = self.connObj.execute("PRAGMA table_info(" + "'" + tableName + "'" + ")")
# generating col list and datatype list
for i in cor:
colList.append(i[1])
dataTypeList.append(i[2])
table = []
table.append(colList)
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
for row in cursor:
tempTable = []
count = 0
# getting all col data
for i,j in zip(colList , dataTypeList):
if(self.security and (i != "ID")):
if(j == "TEXT"):
try:
tempTable.append(self.decrypter((row[count])))
except Exception:
tempTable.append(None)
elif(j == "INT"):
try:
tempTable.append(int(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
else:
try:
tempTable.append(float(self.decrypter((row[count]))))
except ValueError:
tempTable.append((self.decrypter((row[count]))))
except Exception:
tempTable.append(None)
# if not encrypted
else:
tempTable.append(row[count])
count += 1
table.append(tempTable)
if(len(table) > 0):
return table
else:
return None
# function for updating a col data in row of particular key
# returns True if key was found and operation was sucessfull
# return False else wise
def updateRow(self , colName , value , key , tableName = None , commit = True):
tableName = self.getOperableTableName(tableName)
# query string
string = "UPDATE " + "'" + tableName + "'" + " set " + "'" + str(colName) + "'" + " = "
# gettign table info
cor = self.connObj.execute("PRAGMA table_info(" + "'" + tableName + "'" + ")")
if(self.security):
# id is not ecnrypted
if(colName != "ID"):
string = string + "'" + self.encrypter(value) + "' "
else:
string = string + str(value) + " "
else:
# finding if the value to be updated is text or not
valueIsText = False
for i in cor:
if(i[1] == colName):
if(i[2] == 'TEXT'):
valueIsText = True
if(valueIsText):
string = string + "'" + value + "' "
else:
string = string + str(value) + " "
string = string + "where ID = " + str(key)
self.connObj.execute(string)
if(commit):
self.connObj.commit()
# function for deleting a row
def deleteRow(self, key , updateId = True , tableName = None , commit = True):
tempTableName = tableName
tableName = self.getOperableTableName(tableName)
# query
string = "DELETE from " + "'" + tableName + "'" + " where ID = " + str(key) + ";"
self.connObj.execute(string)
if(commit):
self.connObj.commit()
if(updateId):
self.updateIDs(tempTableName , commit=commit)
# updated id's function
def updateIDs(self , tableName = None , commit = True):
tempTableName = tableName
tableName = self.getOperableTableName(tableName)
# getting col list
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
colList = list(map(lambda x: x[0], cursor.description))
# updating the ids
string = "SELECT "
for i in colList:
string = string + "'" + i + "'" + ","
string = string[:-1] + " from " + "'" + tableName + "'"
cursor = self.connObj.execute(string)
count = 0
for row in cursor:
if(row[0] == "ID"):
pass
elif(int(row[0]) == count):
pass
else:
self.updateRow("ID" , count , row[0] , tempTableName , commit)
count = count + 1
if(commit):
self.connObj.commit()
# function to upadte the entire row corresponding to a key
def updateEntireRow(self , valuesList , key , tableName = None , commit = True):
tempTableName = tableName
tableName = self.getOperableTableName(tableName)
key = int(key)
count = 1
# getting col list
cursor = self.connObj.execute('select * from ' + "'" + tableName + "'")
colList = list(map(lambda x: x[0], cursor.description))
for i in valuesList:
self.updateRow(colList[count] , i , key , tempTableName , commit)
count = count + 1
def delEntireTable(self , tableName = None , commit = True):
tableName = self.getOperableTableName(tableName)
string = "DROP TABLE " + "'" + tableName + "'"
self.connObj.execute(string)
if(commit):
self.connObj.commit()
# function to add a col to data base
def addColToTable(self , colName , dataType = "TEXT" , NULL = False , tableName = None , commit = True):
tableName = self.getOperableTableName(tableName)
string = "alter table " + "'" + tableName + "'" + " add column " + "'" + colName + "'" + " " + dataType + " " + "'" + str(int(NULL)) + "'"
self.connObj.execute(string)
if(commit):
self.connObj.commit()
def changePassword(self , oldPassword , newPassword , oldPin = 123456 , newPin = 123456):
# if their is not password in db then no need to do operation
# operation will fail if the wrong credentials are provided
status = self.setPassword(oldPassword , oldPin)
if(status == None):
return True
elif(status == False):
return False
# getting the tableNames
tableNames = self.connObj.execute("SELECT name FROM sqlite_master WHERE type='table';")
# adding tableNames to a list
tableNamesList = []
for i in tableNames:
tableNamesList.append(i[0])
# reserving the current tableName in obj
tempTableName = self.tableName
tempSecurity = self.security
# traversing each table
for i in tableNamesList:
# if the table contains secured by SED then we need to update that table as it has to be now encrypted and decrypted by the new password
# also the password storer table is not affected , it is changed that last
if(ESQLiteGlobalMethods.isSubString(i , self.tableNameAdd) and (not(ESQLiteGlobalMethods.isSubString(i , "0b242ba11ab4a144a48cd25e88b98d161a7ba1c68ad65646cb9207f66aee1a64")))):
# setting the old credentails for decrypting
self.objSecurity.setPassword_Pin(oldPassword , oldPin)
# getting the table data
tableData = self.returnData(i)
# getting the col details
cor = self.connObj.execute("PRAGMA table_info(" + "'" + i + "'" + ")")
contentList = []
# setting new credentials for ecryption
self.objSecurity.setPassword_Pin(newPassword , newPin)
# generating the content list
for j in cor:
if(not(j[1] == "ID")):
tempList = []
# col name
tempList.append(j[1])
# data type
tempList.append(j[2])
# null or not null
tempList.append(j[3])
contentList.append(tempList)
# deleting the old table
self.delEntireTable(i)
# creating new table
self.security = True
self.createTable(i , contentList , True)
# getting the no of cols
lenCol = len(tableData[1])
# the ids id in the table data will not be added to value list
for j in tableData:
if(not(j[0] == "ID")):
valueList = []
count = 1
# adding data to value list - id will not be added
while(count < lenCol):
valueList.append(j[count])
count += 1
# inserting the data into table
self.security = True
self.insertIntoTable(valueList)
# restting the password table with new credentials
self.delEntireTable(self.passwordStorerTable + " " + self.tableNameAdd)
self.setPassword(newPassword , newPin)
# restoring prev obj data
self.security = tempSecurity
self.tableName = tempTableName
return True
def checkForPasswordTable(self):
tableNames = self.connObj.execute("SELECT name FROM sqlite_master WHERE type='table';")
for i in tableNames:
if(i[0] == (self.passwordStorerTable + " " + self.tableNameAdd)):
return True
return False
#TODO: method for checking the data base exist or not
# for testing purpose
if __name__ == "__main__":
obj = SQLiteConnect()
obj.setDatabase("test.db")
print(obj.checkForPasswordTable())
print(obj.setPassword("hello boi"))
obj.setSecurityStatus(True)
contentList = [["test1 456" , "TEXT" , 1] , ["test2 456" , "TEXT" , 1] , ["test3 456" , "INT" , 0]]
obj.createTable("testTable" , contentList)
valuesList = ["hello" , "world" , 123]
obj.insertIntoTable(valuesList)
valuesList = ["hello1" , "world1" , 456]
obj.insertIntoTable(valuesList)
valuesList = ["hello3" , "world3" , 789]
obj.insertIntoTable(valuesList)
valuesList = ["hello4" , "world4" , 1234]
obj.insertIntoTable(valuesList)
valuesList = ["hello5" , "world45" , 5678]
obj.insertIntoTable(valuesList)
obj.printData()
obj.updateRow("test1 456" , "hello69" , 3)
print("\n\n")
obj.printData()
obj.deleteRow(2 , False)
print("\n\n")
obj.printData()
valuesList = ["hello55" , "world455" , 91234]
obj.insertIntoTable(valuesList)
print("\n\n")
obj.printData()
obj.deleteRow(4 , True)
print("\n\n")
obj.printData()
# obj.delEntireTable()
valuesList = ["hello555" , "world4555" , 456789]
obj.updateEntireRow(valuesList , 2)
print("\n\n")
obj.printData()
print("\n\n")
print(obj.returnData())
# obj.addColToTable("next")
print(obj.changePassword("hello boi" , "hello boi my name is harsh"))
print("isTable = " , obj.checkForPasswordTable())
print(obj.setPassword("hello boi my name is harsh"))
print("isTable = " , obj.checkForPasswordTable())
print("\n\n")
obj.printData()
# make the db delete fucntion as well
# if to commit or not
# del col function using copying the data from table -> del table -> create new table
# raise exception or not if wrong data type is passed
# None error