Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. from contextlib import closing
  2.  
  3. class ProgressStore:
  4. def _chunks(self, data, rows=10000):
  5. for i in range(0, len(data), rows):
  6. yield data[i:i+rows]
  7.  
  8. def _execute(self, sql):
  9. with closing(self.connection.cursor()) as cursor:
  10. return [o for o in cursor.execute(sql)]
  11.  
  12. def _commit(self):
  13. self.connection.commit()
  14.  
  15. def __init__(self, db_location, bnumbers = None, table_name = 'progress'):
  16. self.db_location = db_location
  17. self.table_name = table_name
  18.  
  19. self._bnumber_count = 0
  20.  
  21. self.connection = sqlite3.connect(db_location)
  22.  
  23. sql = (
  24. f"CREATE TABLE IF NOT EXISTS {self.table_name} "
  25. f"(bnumber text PRIMARY KEY, status text, notes text)"
  26. )
  27.  
  28. self._execute(sql)
  29. self._commit()
  30.  
  31. if (bnumbers):
  32. print(f"Initialising with reset!")
  33. self.reset(bnumbers)
  34.  
  35.  
  36. def count(self):
  37. sql = (
  38. f"SELECT COUNT(*) FROM {self.table_name}"
  39. )
  40.  
  41. return self._execute(sql)[0][0]
  42.  
  43. def reset(self, bnumbers):
  44. print(f"Resetting ProgressStore.")
  45. sql = f"DELETE FROM {self.table_name}"
  46.  
  47. self._execute(sql)
  48. self._commit()
  49.  
  50. self._bnumber_count = len(bnumbers)
  51.  
  52. chunks = self._chunks(bnumbers)
  53.  
  54. print(f"Loading {len(bnumbers)} bnumbers in chunks.")
  55.  
  56. for chunk in chunks:
  57. self._execute('BEGIN TRANSACTION')
  58.  
  59. for bnumber in chunk:
  60. sql = (
  61. f"INSERT INTO {self.table_name} "
  62. f"(bnumber) VALUES ('{bnumber}')"
  63. )
  64. self.cursor.execute(sql)
  65.  
  66. self._execute('COMMIT')
  67.  
  68. print("Done resetting.")
  69.  
  70. def close(self):
  71. self.connection.close()
  72.  
  73. def get_all(self):
  74. sql = (
  75. f"SELECT * FROM {self.table_name}"
  76. )
  77.  
  78. with closing(self.connection.cursor()) as cursor:
  79. result = cursor.execute(sql)
  80. next_one = result.fetchone()
  81.  
  82. while next_one is not None:
  83. yield next_one
  84. next_one = result.fetchone()
  85.  
  86. def get_status(self, status):
  87. sql = (
  88. f"SELECT * FROM {self.table_name} "
  89. f"WHERE status = '{status}'"
  90. )
  91.  
  92. return self._execute(sql)
  93.  
  94. def get(self, bnumber):
  95. sql = (
  96. f"SELECT * FROM {self.table_name} "
  97. f"WHERE bnumber = '{bnumber}'"
  98. )
  99.  
  100. return self._execute(sql)
  101.  
  102. def update(self, bnumber, status, notes = None, commit = True):
  103. if(bnumber is None):
  104. raise Exception('Must include bnumber in status update!')
  105.  
  106. if(status is None):
  107. raise Exception('Must include status in status update!')
  108.  
  109. if(notes is None):
  110. notes = ""
  111.  
  112. status = str(status).strip()
  113.  
  114. sql = (
  115. f"UPDATE {self.table_name} SET "
  116. f"status = '{status}', notes = '{notes}' "
  117. f"WHERE bnumber = '{bnumber}'"
  118. )
  119.  
  120. self._execute(sql)
  121.  
  122. if commit:
  123. self._commit()
  124.  
  125. def batch_update(self, status_updates):
  126. chunks = self._chunks(status_updates)
  127.  
  128. print(f"Loading {len(status_updates)} status updates in chunks.")
  129.  
  130. for chunk in chunks:
  131. self._execute('BEGIN TRANSACTION')
  132.  
  133. for status_update in status_updates:
  134. self.update(
  135. bnumber = status_update.get('bnumber'),
  136. status = status_update.get('status'),
  137. notes = status_update.get('notes'),
  138. commit = False
  139. )
  140.  
  141. self._execute('COMMIT')
  142.  
  143. print(f"Done loading status updates.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement