Refactored BaseAdapter delete method - Special case code for sqlite and spatialite should not be in the BaseAdapter it should be in SQLiteAdapter, this should also provide a negligible performance boost in delete operations for most adapters including SQLiteAdapter.

This commit is contained in:
Leonel Câmara
2014-08-02 10:52:47 +01:00
parent f0691a64ea
commit 444a09b0b2
+21 -13
View File
@@ -1638,24 +1638,11 @@ class BaseAdapter(ConnectionPool):
def delete(self, tablename, query):
sql = self._delete(tablename, query)
### special code to handle CASCADE in SQLite & SpatiaLite
db = self.db
table = db[tablename]
if self.dbengine in ('sqlite', 'spatialite') and table._referenced_by:
deleted = [x[table._id.name] for x in db(query).select(table._id)]
### end special code to handle CASCADE in SQLite & SpatiaLite
self.execute(sql)
try:
counter = self.cursor.rowcount
except:
counter = None
### special code to handle CASCADE in SQLite & SpatiaLite
if self.dbengine in ('sqlite', 'spatialite') and counter:
for field in table._referenced_by:
if field.type == 'reference '+table._tablename \
and field.ondelete == 'CASCADE':
db(field.belongs(deleted)).delete()
### end special code to handle CASCADE in SQLite & SpatiaLite
return counter
def get_table(self, query):
@@ -2451,6 +2438,27 @@ class SQLiteAdapter(BaseAdapter):
return '(%s REGEXP %s)' % (self.expand(first),
self.expand(second, 'string'))
def delete(self, tablename, query):
sql = self._delete(tablename, query)
### Special code to Handle CASCADE in SQLite & SpatiaLite
db = self.db
table = db[tablename]
deleted = [x[table._id.name] for x in db(query).select(table._id)]
### end special code to handle CASCADE in SQLite & SpatiaLite
self.execute(sql)
try:
counter = self.cursor.rowcount
except:
counter = None
### special code to handle CASCADE in SQLite & SpatiaLite
if counter:
for field in table._referenced_by:
if field.type == 'reference '+table._tablename \
and field.ondelete == 'CASCADE':
db(field.belongs(deleted)).delete()
### end special code to handle CASCADE in SQLite & SpatiaLite
return counter
def select(self, query, fields, attributes):
"""
Simulate `SELECT ... FOR UPDATE` with `BEGIN IMMEDIATE TRANSACTION`.