Fix paging with queries starting with comments

Closes #144
Includes testcase
This commit is contained in:
Sandro Santilli 2014-03-27 12:47:34 +01:00
parent 73624dc89f
commit 07a5a20d5f
3 changed files with 33 additions and 0 deletions

View File

@ -1,6 +1,9 @@
1.9.1 - 2014-MM-DD
------------------
Bug fixes:
* Fix paging with queries starting with comments (#144)
1.9.0 - 2014-03-20
------------------

View File

@ -269,6 +269,9 @@ PSQL.window_sql = function(sql, limit, offset) {
// only window select functions (NOTE: "values" will be broken, "with" will be broken)
if (!_.isNumber(limit) || !_.isNumber(offset) ) return sql;
// Strip comments
sql = sql.replace(/(^|\n)\s*--.*\n/g, '');
var cte = '';
if ( sql.match(/^\s*WITH\s/i) ) {

View File

@ -292,6 +292,33 @@ test("paging", function(done){
testNext();
});
// Test paging with WITH queries
test("paging starting with comment", function(done){
var sql = "-- this is a comment\n" +
"SELECT * FROM (VALUES(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(v)";
var nrows = 3;
var page = 2;
assert.response(app, {
url: '/api/v1/sql?' + querystring.stringify({
q: sql,
rows_per_page: nrows,
page: page
}),
headers: {host: 'vizzuality.cartodb.com'},
method: 'GET'
}, {}, function(res) {
assert.equal(res.statusCode, 200, res.body);
var parsed = JSON.parse(res.body);
assert.equal(parsed.rows.length, 3);
for (var i=0; i<nrows; ++i) {
var obt = parsed.rows[i].v;
var exp = page * nrows + i + 1;
assert.equal(obt, exp, "Value " + i + " in page " + page + " is " + obt + ", expected " + exp);
}
done();
});
});
test('POST /api/v1/sql with SQL parameter on SELECT only. no database param, just id using headers', function(done){
assert.response(app, {
url: '/api/v1/sql',