| 1 | |
|---|
| 2 | def execute(self, req, db=None, cached_ids=None): |
|---|
| 3 | if not db: |
|---|
| 4 | db = self.env.get_db_cnx() |
|---|
| 5 | cursor = db.cursor() |
|---|
| 6 | |
|---|
| 7 | sql, args = self.get_sql(req, cached_ids) |
|---|
| 8 | |
|---|
| 9 | self.env.log.debug("Query SQL: " + sql % tuple([repr(a) for a in args])) |
|---|
| 10 | try: |
|---|
| 11 | cursor.execute(sql, args) |
|---|
| 12 | except: |
|---|
| 13 | db.rollback() |
|---|
| 14 | raise |
|---|
| 15 | columns = get_column_names(cursor) |
|---|
| 16 | fields = [] |
|---|
| 17 | for column in columns: |
|---|
| 18 | fields += [f for f in self.fields if f['name'] == column] or [None] |
|---|
| 19 | results = [] |
|---|
| 20 | |
|---|
| 21 | column_indices = range(len(columns)) |
|---|
| 22 | restrict_ids = [] |
|---|
| 23 | for row in cursor: |
|---|
| 24 | result = {} |
|---|
| 25 | for i in column_indices: |
|---|
| 26 | name, field, val = columns[i], fields[i], row[i] |
|---|
| 27 | if name == self.group: |
|---|
| 28 | val = val or 'None' |
|---|
| 29 | elif name == 'reporter': |
|---|
| 30 | val = val or 'anonymous' |
|---|
| 31 | elif name == 'id': |
|---|
| 32 | val = int(val) |
|---|
| 33 | result['href'] = req.href.ticket(val) |
|---|
| 34 | elif val is None: |
|---|
| 35 | val = '--' |
|---|
| 36 | elif name in ('changetime', 'time'): |
|---|
| 37 | val = datetime.fromtimestamp(int(val or 0), utc) |
|---|
| 38 | elif field and field['type'] == 'checkbox': |
|---|
| 39 | try: |
|---|
| 40 | val = bool(int(val)) |
|---|
| 41 | except (TypeError, ValueError): |
|---|
| 42 | val = False |
|---|
| 43 | result[name] = val |
|---|
| 44 | if 'TICKET_VIEW' in req.perm('ticket', result['id']): |
|---|
| 45 | restrict_ids.append(result['id']) |
|---|
| 46 | results.append(result) |
|---|
| 47 | if len(restrict_ids) > 0: |
|---|
| 48 | self.constraints['id'] = [] |
|---|
| 49 | self.constraints['id'].append ("%s" % (','.join([str(id) for id in restrict_ids]))) |
|---|
| 50 | cursor.close() |
|---|
| 51 | |
|---|
| 52 | self.num_items = len(results) |
|---|
| 53 | |
|---|
| 54 | if self.num_items <= self.max: |
|---|
| 55 | self.has_more_pages = False |
|---|
| 56 | |
|---|
| 57 | if self.has_more_pages: |
|---|
| 58 | max = self.max |
|---|
| 59 | if self.group: |
|---|
| 60 | max += 1 |
|---|
| 61 | if (self.page > int(ceil(float(self.num_items) / self.max)) and |
|---|
| 62 | self.num_items != 0): |
|---|
| 63 | raise TracError(_('Page %(page)s is beyond the number of ' |
|---|
| 64 | 'pages in the query', page=self.page)) |
|---|
| 65 | # re-query |
|---|
| 66 | sql, args = self.get_sql(req, cached_ids) |
|---|
| 67 | sql = sql + " LIMIT %d OFFSET %d" % (max, self.offset) |
|---|
| 68 | self.env.log.debug("Query SQL: " + sql % tuple([repr(a) for a in args])) |
|---|
| 69 | |
|---|
| 70 | cursor = db.cursor() |
|---|
| 71 | |
|---|
| 72 | try: |
|---|
| 73 | cursor.execute(sql, args) |
|---|
| 74 | except: |
|---|
| 75 | db.rollback() |
|---|
| 76 | raise |
|---|
| 77 | |
|---|
| 78 | results = [] |
|---|
| 79 | for row in cursor: |
|---|
| 80 | result = {} |
|---|
| 81 | for i in column_indices: |
|---|
| 82 | name, field, val = columns[i], fields[i], row[i] |
|---|
| 83 | if name == self.group: |
|---|
| 84 | val = val or 'None' |
|---|
| 85 | elif name == 'reporter': |
|---|
| 86 | val = val or 'anonymous' |
|---|
| 87 | elif name == 'id': |
|---|
| 88 | val = int(val) |
|---|
| 89 | result['href'] = req.href.ticket(val) |
|---|
| 90 | elif val is None: |
|---|
| 91 | val = '--' |
|---|
| 92 | elif name in ('changetime', 'time'): |
|---|
| 93 | val = datetime.fromtimestamp(int(val or 0), utc) |
|---|
| 94 | elif field and field['type'] == 'checkbox': |
|---|
| 95 | try: |
|---|
| 96 | val = bool(int(val)) |
|---|
| 97 | except (TypeError, ValueError): |
|---|
| 98 | val = False |
|---|
| 99 | result[name] = val |
|---|
| 100 | if 'TICKET_VIEW' in req.perm('ticket', result['id']): |
|---|
| 101 | results.append(result) |
|---|
| 102 | cursor.close() |
|---|
| 103 | |
|---|
| 104 | return results |
|---|
| 105 | |
|---|