Skip to content

Commit

Permalink
Adding a CSV option for output.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrjbq7 committed Jun 1, 2010
1 parent d25036f commit 9e7a293
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions tracsql/templates/sql.html
Expand Up @@ -33,6 +33,14 @@ <h1>Query</h1>
<input type="checkbox" name="raw" />Raw Output <input type="checkbox" name="raw" />Raw Output
</py:otherwise> </py:otherwise>
</py:choose> </py:choose>
<py:choose test="csv">
<py:when>
<input type="checkbox" name="csv" checked="checked" />CSV
</py:when>
<py:otherwise>
<input type="checkbox" name="csv" />CSV
</py:otherwise>
</py:choose>
</td><td align="right"> </td><td align="right">
<py:if test="not error"> <py:if test="not error">
<b>${len(rows)}</b> results (<b>${took}</b> seconds) <b>${len(rows)}</b> results (<b>${took}</b> seconds)
Expand Down
20 changes: 19 additions & 1 deletion tracsql/web_ui.py
Expand Up @@ -106,6 +106,7 @@ def _process(self, req, cursor, data):


sql = req.args.get('query', '') sql = req.args.get('query', '')
raw = req.args.get('raw', '') raw = req.args.get('raw', '')
csv = req.args.get('csv', '')


cols = rows = [] cols = rows = []
took = 0 took = 0
Expand All @@ -125,13 +126,30 @@ def _process(self, req, cursor, data):
except BaseException, e: except BaseException, e:
error = e.message error = e.message


if csv:
text = []
for col in cols:
text.append('%s,' % col)
text.append('\n')
for row in rows:
for cell in row:
text.append("%s," % cell)
text.append('\n')
text = str(''.join(text))
req.send_response(200)
req.send_header('Content-Type', 'text/csv')
req.send_header('Content-Length', str(len(text)))
req.end_headers()
req.write(text)
return

if not raw: if not raw:


format = { format = {
'path' : lambda x: html.A(x, href=req.href.browser(x)), 'path' : lambda x: html.A(x, href=req.href.browser(x)),
'rev' : lambda x: html.A(x, href=req.href.changeset(x)), 'rev' : lambda x: html.A(x, href=req.href.changeset(x)),
'ticket' : lambda x: html.A(x, href=req.href.ticket(x)), 'ticket' : lambda x: html.A(x, href=req.href.ticket(x)),
'query' : lambda x: html.FONT(x, face="monospace"), 'query' : lambda x: html.PRE(x, style="padding: 0; margin: 0;"),
'time' : lambda x: fmt_timestamp(x), 'time' : lambda x: fmt_timestamp(x),
} }


Expand Down

0 comments on commit 9e7a293

Please sign in to comment.