summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJ Rob Gant <rgant@alum.wpi.edu>2015-04-27 21:37:16 -0400
committerJ Rob Gant <rgant@alum.wpi.edu>2015-04-27 21:37:16 -0400
commiteb660aa852379fc66ba470f4dd11b1179c14a3d0 (patch)
tree375ea41553257c665e7b9829728cd9d3deb878d8
parentb997cb3e2550958d7b3a561e31b3328ecc407493 (diff)
downloadrequests-cache-eb660aa852379fc66ba470f4dd11b1179c14a3d0.tar.gz
Better use of SQL Statements
-rw-r--r--requests_cache/backends/storage/dbdict.py27
1 files changed, 12 insertions, 15 deletions
diff --git a/requests_cache/backends/storage/dbdict.py b/requests_cache/backends/storage/dbdict.py
index ef0f516..096c403 100644
--- a/requests_cache/backends/storage/dbdict.py
+++ b/requests_cache/backends/storage/dbdict.py
@@ -6,11 +6,12 @@
Dictionary-like objects for saving large data sets to `sqlite` database
"""
+import logging
from collections import MutableMapping
import sqlite3 as sqlite
from contextlib import contextmanager
try:
- import threading
+ import threading
except ImportError:
import dummy_threading as threading
try:
@@ -21,7 +22,6 @@ except ImportError:
from requests_cache.compat import bytes
-
class DbDict(MutableMapping):
""" DbDict - a dictionary-like object for saving large datasets to `sqlite` database
@@ -49,6 +49,10 @@ class DbDict(MutableMapping):
self.table_name = table_name
self.fast_save = fast_save
+ if self.fast_save:
+ logger = logging.getLogger(__name__)
+ logger.debug('Fast Saves for sqlite')
+
#: Transactions can be commited if this property is set to `True`
self.can_commit = True
@@ -122,21 +126,14 @@ class DbDict(MutableMapping):
def __setitem__(self, key, item):
with self.connection(True) as con:
- if con.execute("select key from `%s` where key=?" %
- self.table_name, (key,)).fetchone():
- con.execute("update `%s` set value=? where key=?" %
- self.table_name, (item, key))
- else:
- con.execute("insert into `%s` (key,value) values (?,?)" %
- self.table_name, (key, item))
+ con.execute("insert or replace into `%s` (key,value) values (?,?)" %
+ self.table_name, (key, item))
def __delitem__(self, key):
with self.connection(True) as con:
- if con.execute("select key from `%s` where key=?" %
- self.table_name, (key,)).fetchone():
- con.execute("delete from `%s` where key=?" %
- self.table_name, (key,))
- else:
+ cur = con.execute("delete from `%s` where key=?" %
+ self.table_name, (key,))
+ if not cur.rowcount:
raise KeyError
def __iter__(self):
@@ -148,7 +145,7 @@ class DbDict(MutableMapping):
def __len__(self):
with self.connection() as con:
return con.execute("select count(key) from `%s`" %
- self.table_name).fetchone()[0]
+ self.table_name).fetchone()[0]
def clear(self):
with self.connection(True) as con: