Warning:
Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.
Ticket #1601: simulate_offset_auto.diff
| File simulate_offset_auto.diff,
2.2 KB
(added by joelpearson, 4 years ago) |
|
Workaround is on by default for certain databases, but can be forced on or off in config file
|
-
|
|
|
|
| 31 | 31 | |
| 32 | 32 | log = logging.getLogger("turbogears.paginate") |
| 33 | 33 | |
| | 34 | # lists of databases that lack support for OFFSET |
| | 35 | # this will need to be updated periodically as modules change |
| | 36 | _so_no_offset = 'mssql maxdb sybase'.split() |
| | 37 | _sa_no_offset = 'mssql maxdb access'.split() |
| | 38 | |
| | 39 | # this is a global that is set the first time paginate() is called |
| | 40 | _simulate_offset = None |
| | 41 | |
| 34 | 42 | def paginate(var_name, default_order='', default_reversed=False, limit=10, |
| 35 | 43 | allow_limit_override=False, max_pages=5, dynamic_limit=None): |
| 36 | 44 | ''' |
| … |
… |
|
| 200 | 208 | # we replace the var with the sliced one |
| 201 | 209 | endpoint = offset + limit_ |
| 202 | 210 | log.debug("slicing data between %d and %d", offset, endpoint) |
| 203 | | output[var_name] = var_data[offset:endpoint] |
| 204 | 211 | |
| | 212 | global _simulate_offset |
| | 213 | get = turbogears.config.get |
| | 214 | if _simulate_offset is None: |
| | 215 | _simulate_offset = get('paginate.simulate_offset', None) |
| | 216 | if _simulate_offset is None: |
| | 217 | _simulate_offset = False |
| | 218 | so_db = get('sqlobject.dburi', 'NOMATCH:').split(':', 1)[0] |
| | 219 | sa_db = get('sqlalchemy.dburi', 'NOMATCH:').split(':', 1)[0] |
| | 220 | if so_db in _so_no_offset or sa_db in _sa_no_offset: |
| | 221 | _simulate_offset = True |
| | 222 | log.warning("simulating OFFSET, paginate may be slow") |
| | 223 | log.warning("to turn off, set " |
| | 224 | "paginate.simulate_offset=False") |
| | 225 | |
| | 226 | if _simulate_offset: |
| | 227 | var_data_iter = iter(var_data[:endpoint]) |
| | 228 | # skip over the number of records specified by offset |
| | 229 | for i in range(offset): |
| | 230 | var_data_iter.next() |
| | 231 | # return the records that remain |
| | 232 | output[var_name] = list(var_data_iter) |
| | 233 | else: |
| | 234 | output[var_name] = var_data[offset:endpoint] |
| | 235 | |
| 205 | 236 | return output |
| 206 | 237 | return decorated |
| 207 | 238 | return weak_signature_decorator(entangle) |
Download in other formats: