Skip to content

Commit

Permalink
Merge pull request #94 from endlisnis/master
Browse files Browse the repository at this point in the history
Add a blocking commit after each modification if autocommit is enabled.
  • Loading branch information
mpenkov authored Jan 1, 2020
2 parents b8137a9 + 39e2ade commit f5c57f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sqlitedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ def __setitem__(self, key, value):

ADD_ITEM = 'REPLACE INTO "%s" (key, value) VALUES (?,?)' % self.tablename
self.conn.execute(ADD_ITEM, (key, self.encode(value)))
if self.autocommit:
self.commit()

def __delitem__(self, key):
if self.flag == 'r':
Expand All @@ -260,6 +262,8 @@ def __delitem__(self, key):
raise KeyError(key)
DEL_ITEM = 'DELETE FROM "%s" WHERE key = ?' % self.tablename
self.conn.execute(DEL_ITEM, (key,))
if self.autocommit:
self.commit()

def update(self, items=(), **kwds):
if self.flag == 'r':
Expand All @@ -275,6 +279,8 @@ def update(self, items=(), **kwds):
self.conn.executemany(UPDATE_ITEMS, items)
if kwds:
self.update(kwds)
if self.autocommit:
self.commit()

def __iter__(self):
return self.iterkeys()
Expand Down
6 changes: 6 additions & 0 deletions tests/autocommit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sqlitedict

d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite', autocommit=True)

for i in range(1000):
d[i] = i
11 changes: 11 additions & 0 deletions tests/test_autocommit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sys, os
import sqlitedict

def test():
"Verify autocommit just before program exits."
assert os.system('PYTHONPATH=. %s tests/autocommit.py' % sys.executable) == 0
# The above script relies on the autocommit feature working correctly.
# Now, let's check if it actually worked.
d = sqlitedict.SqliteDict('tests/db/autocommit.sqlite')
for i in range(1000):
assert d[i] == i, "actual: %s expected: %s" % (d[i], i)

0 comments on commit f5c57f1

Please sign in to comment.