Skip to content

Commit 46b7f7a

Browse files
author
Sandro Santilli
committed
Parse all numbers as floats. Closes #100
1 parent ae50233 commit 46b7f7a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
1.5.2
22
-----
3+
* Keep numbers as such in JSON output (#100)
34

45
1.5.1
56
-----

app/models/psql.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ pg.on('error', function(err, client) {
1919
console.log("PostgreSQL connection error: " + err);
2020
});
2121

22+
// Workaround for https://github.com/Vizzuality/CartoDB-SQL-API/issues/100
23+
var types = require(__dirname + '/../../node_modules/pg/lib/types');
24+
var arrayParser = require(__dirname + '/../../node_modules/pg/lib/types/arrayParser');
25+
var floatParser = function(val) {
26+
return parseFloat(val);
27+
};
28+
var floatArrayParser = function(val) {
29+
if(!val) { return null; }
30+
var p = arrayParser.create(val, function(entry) {
31+
return floatParser(entry);
32+
});
33+
return p.parse();
34+
};
35+
types.setTypeParser(1700, floatParser);
36+
types.setTypeParser(700, floatParser);
37+
types.setTypeParser(701, floatParser);
38+
types.setTypeParser(1021, floatArrayParser);
39+
types.setTypeParser(1022, floatArrayParser);
40+
types.setTypeParser(1231, floatArrayParser);
41+
42+
43+
44+
2245

2346
// PSQL
2447
//

test/acceptance/app.test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,44 @@ test('field names and types are exposed', function(done){
881881
});
882882
});
883883

884+
// See https://github.com/Vizzuality/CartoDB-SQL-API/issues/100
885+
test('numeric fields are rendered as numbers in JSON', function(done){
886+
assert.response(app, {
887+
url: '/api/v1/sql?' + querystring.stringify({
888+
q: "WITH inp AS ( SELECT 1::int2 as a, 2::int4 as b, " +
889+
"3::int8 as c, 4::float4 as d, " +
890+
"5::float8 as e, 6::numeric as f" +
891+
") SELECT a,b,c,d,e,f," +
892+
" ARRAY[a] AS _a, " +
893+
" ARRAY[b] AS _b, " +
894+
" ARRAY[c] AS _c, " +
895+
" ARRAY[d] AS _d, " +
896+
" ARRAY[e] AS _e, " +
897+
" ARRAY[f] AS _f " +
898+
"FROM inp"
899+
}),
900+
headers: {host: 'vizzuality.cartodb.com'},
901+
method: 'GET'
902+
},{ }, function(res) {
903+
assert.equal(res.statusCode, 200, res.body);
904+
var parsedBody = JSON.parse(res.body);
905+
var row = parsedBody.rows[0];
906+
assert.equal(typeof(row.a), 'number');
907+
assert.equal(typeof(row.b), 'number');
908+
assert.equal(typeof(row.c), 'number');
909+
assert.equal(typeof(row.d), 'number');
910+
assert.equal(typeof(row.e), 'number');
911+
assert.equal(typeof(row.f), 'number');
912+
assert.equal(typeof(row._a[0]), 'number');
913+
assert.equal(typeof(row._b[0]), 'number');
914+
assert.equal(typeof(row._c[0]), 'number');
915+
assert.equal(typeof(row._d[0]), 'number');
916+
assert.equal(typeof(row._e[0]), 'number');
917+
assert.equal(typeof(row._f[0]), 'number');
918+
done();
919+
});
920+
});
921+
884922
// Timezone information is retained with JSON output
885923
//
886924
// NOTE: results of these tests rely on the TZ env variable

0 commit comments

Comments
 (0)