Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a blocking commit after each modification if autocommit is enabled. #94

Merged
merged 9 commits into from
Jan 1, 2020
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

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