From 8a953c116ca8f92b87e5169b544a65787836e03f 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 --- app/models/psql.js | 3 +++ test/acceptance/app.test.js | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) 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