Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #This work of code is the property of Clay Sweetser, aka Varriount
  2. # All rights reserved
  3. import apsw
  4. from collections import deque
  5. from threading import Thread
  6. import traceback
  7. class dbconnection(Thread):
  8. "Establish a connection to the memory database and define the cursor."
  9.  
  10. # Startup variables
  11. def __init__(self, world):
  12. self.dbpath = world.basename +"/database.db"
  13. self.memcon = apsw.Connection(':memory:')
  14. self.memcursor = self.memcon.cursor()
  15. self.memlist = deque()
  16. self.disklist = deque()
  17. self.worlddb = apsw.Connection('{0}'.format(self.dbpath))
  18. self.worldcursor = self.worlddb.cursor()
  19. self.worldcursor.execute("pragma journal_mode=wal")
  20. self.counter = 0
  21. self.run = True
  22. try:
  23. self.memcursor.execute('CREATE TABLE main (id INTEGER PRIMARY KEY,matbefore INTEGER, matafter INTEGER, name VARCHAR(50), date DATE)')
  24. except:
  25. pass
  26. try:
  27. self.worldcursor.execute('CREATE TABLE main (id INTEGER PRIMARY KEY,matbefore INTEGER, matafter INTEGER, name VARCHAR(50), date DATE)')
  28. except:
  29. pass
  30. # Core functions, not normally used by plugins
  31. def tableopen(self):
  32. self.memcursor = None
  33. self.worldcursor = None
  34. self.memcon.backup("main", self.worlddb, "main").step()
  35. self.memcursor = self.memcon.cursor()
  36. self.worldcursor = self.worlddb.cursor()
  37. print("World Opened Succesfully")
  38.  
  39. def dbclose(self):
  40. self.memwrite()
  41. self.diskwrite()
  42. print("Database closing..")
  43. self.memcursor = None
  44. self.worldcursor = None
  45. self.memcon = None
  46. self.worlddb = None
  47. self.run = False
  48.  
  49. def writetable(self,blockoffset,matbefore,matafter,name,date):
  50. if self.run:
  51. self.memlist.append((blockoffset,matbefore,matafter,name,date))
  52. self.disklist.append((blockoffset,matbefore,matafter,name,date))
  53. if len(self.memlist) > 150:
  54. self.memwrite()
  55. self.counter = self.counter + 1
  56. if self.counter > 5:
  57. self.diskwrite()
  58.  
  59. def memwrite(self):
  60. if self.run:
  61. self.memcursor.executemany("INSERT OR REPLACE INTO main VALUES (?,?,?,?,?)",self.memlist)
  62. self.memlist.clear()
  63. print("Memory Database updated")
  64.  
  65. def diskwrite(self):
  66. if self.run:
  67. self.worldcursor.executemany("INSERT OR REPLACE INTO main VALUES (?,?,?,?,?)",self.disklist)
  68. self.disklist.clear()
  69. self.counter = 0
  70. print("Disk database updated")
  71.  
  72. def readdb(self,entry,column,filt = None):
  73. returncolumn = 'id,matbefore,matafter,name,date'
  74. #currently tested to accept tuples with strings, lists with strings. won't work with things that have only a single entry, so single strings and ints are out
  75. # Example filters are "and where date is < foo and > foo"
  76. if len(self.memlist) >0:
  77. self.memwrite()
  78. if isinstance(entry,(int, str)):
  79. if filt != None:
  80. string = 'select * from main as main where {0} = ? {1}'.format(column,filt)
  81. else:
  82. string = 'select * from main as main where {0} = ?'.format(column)
  83. print(string)
  84. self.memcursor.execute(string, [entry])
  85. undolist = self.memcursor.fetchall()
  86. if len(undolist) == 1:
  87. undolist = undolist[0]
  88. return(undolist)
  89. #Or you could put your old stuff in here
  90. if isinstance(entry, (tuple,list)):
  91. qmarks = ('?,'*len(entry))[:-1]
  92. if filt != None:
  93. string = 'select * from main as main where {0} in ({1}) {2}'.format(column,qmarks,filt)
  94. else:
  95. string = 'select * from main as main where {0} in ({1})'.format(column,qmarks)
  96. print(string)
  97. self.memcursor.execute(string, entry)
  98. print(self.memcursor.fetchall())
  99. undolist = self.memcursor.fetchall()
  100. return(undolist)
  101. else:
  102. print traceback.format_exc()
  103. print("ERROR - Please make sure readdb input is correct!")
  104. print("Dumping Variables..")
  105. print("Entry:", entry)
  106. print("Column:", column)
  107. print("Return Column:", returncolumn)
  108. print("string:", string)
  109. print("Qmarks:", qmarks)
  110. print("Filter:", filt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement