Unify stripQuotes

This commit is contained in:
Raúl Marín 2019-11-25 13:36:56 +01:00
parent f545b4d002
commit 6eb66de94e
3 changed files with 17 additions and 21 deletions

View File

@ -7,6 +7,7 @@ const AggregationMapConfig = require('../models/aggregation/aggregation-mapconfi
const WebMercatorHelper = require('cartodb-query-tables').utils.webMercatorHelper;
const webmercator = new WebMercatorHelper();
const queryUtils = require('../../lib/utils/query-utils');
module.exports = class ClusterBackend {
getClusterFeatures (mapConfigProvider, params, callback) {
@ -75,15 +76,6 @@ const SKIP_COLUMNS = {
the_geom_webmercator: true
};
function stripQuotes(columnName) {
const quotedName = columnName.length > 2 && columnName[0] === '"' && columnName[columnName.length - 1] === '"';
if (quotedName)
{
return columnName.substring(1, columnName.length - 1);
}
return columnName;
}
function getColumnsName (pg, query, callback) {
const sql = schemaQuery({
query: query
@ -98,7 +90,7 @@ function getColumnsName (pg, query, callback) {
const fields = resultSet.fields || [];
const columnNames = fields.map(field => field.name)
.filter(columnName => !SKIP_COLUMNS[stripQuotes(columnName)]);
.filter(columnName => !SKIP_COLUMNS[queryUtils.stripQuotes(columnName)]);
return callback(null, columnNames);
}, true);

View File

@ -4,6 +4,7 @@ var queue = require('queue-async');
var debug = require('debug')('windshaft:analysis');
var camshaft = require('camshaft');
const queryUtils = require('../../../../lib/utils/query-utils');
var dot = require('dot');
dot.templateSettings.strip = false;
@ -179,16 +180,6 @@ AnalysisMapConfigAdapter.prototype.getMapConfig = function (user, requestMapConf
});
};
function stripQuotes(columnName) {
const quotedName = columnName.length > 2 && columnName[0] === '"' && columnName[columnName.length - 1] === '"';
if (quotedName)
{
return columnName.substring(1, columnName.length - 1);
}
return columnName;
}
var SKIP_COLUMNS = {
the_geom: true,
the_geom_webmercator: true
@ -196,7 +187,7 @@ var SKIP_COLUMNS = {
function skipColumns (columnNames) {
return columnNames
.filter(function (columnName) { return !SKIP_COLUMNS[stripQuotes(columnName)]; });
.filter(function (columnName) { return !SKIP_COLUMNS[queryUtils.stripQuotes(columnName)]; });
}
var wrappedQueryTpl = dot.template([

View File

@ -134,6 +134,19 @@ function substituteTokensForZoom (sql, zoom) {
return SubstitutionTokens.replaceXYZ(sql, { z: zoom, bbox: bbox });
}
/**
* Strips leading and trailing quotes (") from a string
* @param {String} columnName, e.g. ("cartodb_id")
* @returns {String}, e.g. (cartodb_id)
*/
module.exports.stripQuotes = function (columnName) {
const quotedName = columnName.length > 2 && columnName[0] === '"' && columnName[columnName.length - 1] === '"';
if (quotedName) {
return columnName.substring(1, columnName.length - 1);
}
return columnName;
};
module.exports.queryPromise = queryPromise;
module.exports.getQueryLimited = getQueryLimited;
module.exports.substituteDummyTokens = substituteDummyTokens;