Commit 489b50e3 authored by Jérome Perrin's avatar Jérome Perrin

sql_browser: improve display of results with big numbers

Javascript does not support big integers, so the backend have to be
careful not to return too large numbers in the json. Nowadays there
is bigint, but not equivalent for decimal numbers, so the backend
tries to detect if numbers are too big and returns them as string
in that case - which is better than returning a different number
because it displays correctly in the result table. The pivot table
will still wrong, but at least the table of results is OK.
parent b9dcb855
......@@ -8,8 +8,6 @@ response = container.REQUEST.RESPONSE
start = time.time()
try:
results = context.manage_test(query)
data = [ results.names() ]
data.extend(results.tuples())
except Exception, e:
response.setStatus(500)
try:
......@@ -18,10 +16,15 @@ except Exception, e:
response.write(str(e))
return
# handle non JSON serializable data
new_data = [data[0]]
for line in data[1:]:
# https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger
MAX_SAFE_INTEGER = 2**53 - 1
def isSafeInteger(num):
return (- MAX_SAFE_INTEGER) < num < MAX_SAFE_INTEGER
data = [ results.names() ]
for line in results.tuples():
new_line = []
# handle non JSON serializable data
for v in line:
if isinstance(v, DateTime):
v = v.ISO()
......@@ -29,9 +32,15 @@ for line in data[1:]:
v = v.isoformat()
elif isinstance(v, Decimal):
v = float(v)
elif isinstance(v, (long, int, float)) and not isSafeInteger(v):
# if numbers are too large to be handled by javascript, we simply return them
# as string, this will still not work for pivot table, but at least the spreadsheet
# will not display truncated values.
v = str(v)
new_line.append(v)
new_data.append(new_line)
data.append(new_line)
response.setHeader("Server-Timing", 'db;dur=%s;desc="SQL query"' % (1000 * (time.time() - start)))
response.setHeader('Content-Type', 'application/json')
return json.dumps(new_data, indent=2)
return json.dumps(data, indent=2)
......@@ -47,6 +47,32 @@
<td>2</td>
</tr>
<!-- regression: numbers larger than MAX_SAFE_INTEGER should not be truncated. -->
<tr>
<td>assertEval</td>
<td>
selenium.browserbot.getCurrentWindow().monaco_editor.trigger('', 'editor.action.smartSelect.expand');
selenium.browserbot.getCurrentWindow().monaco_editor.trigger('', 'editor.action.deleteLines');
selenium.browserbot.getCurrentWindow().monaco_editor.trigger('', 'type', {'text': 'select 9007199254740992123'});
</td>
<td>null</td>
</tr>
<tr>
<td>click</td>
<td>//button[@name="Query"]</td>
<td></td>
</tr>
<tr>
<td>waitForText</td>
<td>xpath=(//div[contains(@class, "handsontable")]//th//span[@class="colHeader"])[2]</td>
<td>9007199254740992123</td>
</tr>
<tr>
<td>assertText</td>
<td>//div[contains(@class, "handsontable")]//tbody/tr/td</td>
<td>9007199254740992123</td>
</tr>
</tbody>
</table>
</body>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment