new arg to SQLFORM.grid to help with large datasets

This commit is contained in:
niphlod
2013-04-17 23:01:01 +02:00
parent b0e3e386e7
commit 4d8dedeb27
+29 -15
View File
@@ -1780,7 +1780,8 @@ class SQLFORM(FORM):
selectable_submit_button='Submit', selectable_submit_button='Submit',
buttons_placement = 'right', buttons_placement = 'right',
links_placement = 'right', links_placement = 'right',
noconfirm=False noconfirm=False,
cache_count=None
): ):
# jQuery UI ThemeRoller classes (empty if ui is disabled) # jQuery UI ThemeRoller classes (empty if ui is disabled)
@@ -1834,6 +1835,32 @@ class SQLFORM(FORM):
editable = wenabled and editable editable = wenabled and editable
deletable = wenabled and deletable deletable = wenabled and deletable
def fetch_count(dbset):
##FIXME for google:datastore cache_count is ignored
## if it's not an integer
if cache_count is None or isinstance(cache_count, tuple):
if groupby:
c = 'count(*)'
nrows = db.executesql(
'select count(*) from (%s);' %
dbset._select(c, left=left, cacheable=True,
groupby=groupby, cache=cache_count)[:-1])[0][0]
elif left:
c = 'count(*)'
nrows = dbset.select(c, left=left, cacheable=True, cache=cache_count).first()[c]
elif dbset._db._adapter.dbengine=='google:datastore':
#if we don't set a limit, this can timeout for a large table
nrows = dbset.db._adapter.count(dbset.query, limit=1000)
else:
nrows = dbset.count(cache=cache_count)
elif isinstance(cache_count, (int, long)):
nrows = cache_count
elif callable(cache_count):
nrows = cache_count(dbset, request.vars)
else:
nrows = 0
return nrows
def url(**b): def url(**b):
b['args'] = args + b.get('args', []) b['args'] = args + b.get('args', [])
localvars = request.get_vars.copy() localvars = request.get_vars.copy()
@@ -2129,20 +2156,7 @@ class SQLFORM(FORM):
if subquery: if subquery:
dbset = dbset(subquery) dbset = dbset(subquery)
try: try:
if groupby: nrows = fetch_count(dbset)
c = 'count(*)'
nrows = db.executesql(
'select count(*) from (%s);' %
dbset._select(c, left=left, cacheable=True,
groupby=groupby)[:-1])[0][0]
elif left:
c = 'count(*)'
nrows = dbset.select(c, left=left, cacheable=True).first()[c]
elif dbset._db._adapter.dbengine=='google:datastore':
#if we don't set a limit, this can timeout for a large table
nrows = dbset.db._adapter.count(dbset.query, limit=1000)
else:
nrows = dbset.count()
except: except:
nrows = 0 nrows = 0
error = T('Unsupported query') error = T('Unsupported query')