From 07a5a20d5f7e1aff25bb5654bd9cc2844baf5207 Mon Sep 17 00:00:00 2001 From: Sandro Santilli Date: Thu, 27 Mar 2014 12:47:34 +0100 Subject: [PATCH] Fix paging with queries starting with comments Closes #144 Includes testcase --- NEWS.md | 3 +++ app/models/psql.js | 3 +++ test/acceptance/app.test.js | 27 +++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/NEWS.md b/NEWS.md index 9b0cfe44..c5c15b58 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 ------------------ diff --git a/app/models/psql.js b/app/models/psql.js index 633f948f..043a61da 100644 --- a/app/models/psql.js +++ b/app/models/psql.js @@ -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) ) { diff --git a/test/acceptance/app.test.js b/test/acceptance/app.test.js index a3d596c5..76d2d655 100644 --- a/test/acceptance/app.test.js +++ b/test/acceptance/app.test.js @@ -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