=== trac/ticket/query.py
==================================================================
|
|
|
|
| 226 | 226 | return cols |
| 227 | 227 | |
| 228 | 228 | def count(self, req, db=None, cached_ids=None): |
| 229 | | sql, args = self.get_sql(req, cached_ids) |
| 230 | | return self._count(sql, args) |
| | 229 | self.execute(req, db, cached_ids) |
| | 230 | return self.num_items |
| 231 | 231 | |
| 232 | | def _count(self, sql, args, db=None): |
| 233 | | if not db: |
| 234 | | db = self.env.get_db_cnx() |
| 235 | | cursor = db.cursor() |
| 236 | | |
| 237 | | count_sql = 'SELECT COUNT(*) FROM (' + sql + ') AS foo' |
| 238 | | # self.env.log.debug("Count results in Query SQL: " + count_sql % |
| 239 | | # tuple([repr(a) for a in args])) |
| 240 | | |
| 241 | | cnt = 0 |
| 242 | | try: |
| 243 | | cursor.execute(count_sql, args); |
| 244 | | except: |
| 245 | | db.rollback() |
| 246 | | raise |
| 247 | | for cnt, in cursor: |
| 248 | | break |
| 249 | | self.env.log.debug("Count results in Query: %d" % cnt) |
| 250 | | return cnt |
| 251 | | |
| 252 | 232 | def execute(self, req, db=None, cached_ids=None): |
| 253 | 233 | if not db: |
| 254 | 234 | db = self.env.get_db_cnx() |
| 255 | 235 | cursor = db.cursor() |
| 256 | 236 | |
| 257 | 237 | sql, args = self.get_sql(req, cached_ids) |
| 258 | | self.num_items = self._count(sql, args, db) |
| 259 | 238 | |
| 260 | | if self.num_items <= self.max: |
| 261 | | self.has_more_pages = False |
| 262 | | |
| 263 | | if self.has_more_pages: |
| 264 | | max = self.max |
| 265 | | if self.group: |
| 266 | | max += 1 |
| 267 | | sql = sql + " LIMIT %d OFFSET %d" % (max, self.offset) |
| 268 | | if (self.page > int(ceil(float(self.num_items) / self.max)) and |
| 269 | | self.num_items != 0): |
| 270 | | raise TracError(_('Page %(page)s is beyond the number of ' |
| 271 | | 'pages in the query', page=self.page)) |
| 272 | | |
| 273 | 239 | self.env.log.debug("Query SQL: " + sql % tuple([repr(a) for a in args])) |
| 274 | 240 | try: |
| 275 | 241 | cursor.execute(sql, args) |
| … |
… |
|
| 304 | 270 | except (TypeError, ValueError): |
| 305 | 271 | val = False |
| 306 | 272 | result[name] = val |
| 307 | | results.append(result) |
| | 273 | if 'TICKET_VIEW' in req.perm('ticket', result['id']): |
| | 274 | results.append(result) |
| 308 | 275 | cursor.close() |
| | 276 | |
| | 277 | self.num_items = len(results) |
| | 278 | |
| | 279 | if self.num_items <= self.max: |
| | 280 | self.has_more_pages = False |
| | 281 | |
| | 282 | if self.has_more_pages: |
| | 283 | max = self.max |
| | 284 | if self.group: |
| | 285 | max += 1 |
| | 286 | if (self.page > int(ceil(float(self.num_items) / self.max)) and |
| | 287 | self.num_items != 0): |
| | 288 | raise TracError(_('Page %(page)s is beyond the number of ' |
| | 289 | 'pages in the query', page=self.page)) |
| 309 | 290 | return results |
| 310 | 291 | |
| 311 | 292 | def get_href(self, href, id=None, order=None, desc=None, format=None, |
| … |
… |
|
| 667 | 648 | |
| 668 | 649 | results = Paginator(tickets, |
| 669 | 650 | self.page - 1, |
| 670 | | self.max, |
| 671 | | self.num_items) |
| | 651 | self.max) |
| 672 | 652 | |
| 673 | 653 | if req: |
| 674 | 654 | if results.has_next_page: |