From 11f7b38c6975144ff3ac9abfb0aae19f085e23c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 15 Sep 2017 11:54:56 +0200 Subject: [PATCH 1/6] Do not use dot module to build column type query --- lib/cartodb/models/dataview/base.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/lib/cartodb/models/dataview/base.js b/lib/cartodb/models/dataview/base.js index 29069d37..fb5f520e 100644 --- a/lib/cartodb/models/dataview/base.js +++ b/lib/cartodb/models/dataview/base.js @@ -1,6 +1,3 @@ -var dot = require('dot'); -dot.templateSettings.strip = false; - function BaseDataview() {} module.exports = BaseDataview; @@ -43,15 +40,14 @@ var DATE_OIDS = { 1184: true }; -var columnTypeQueryTpl = dot.template( - 'SELECT pg_typeof({{=it.column}})::oid FROM ({{=it.query}}) _cdb_column_type limit 1' -); +var columnTypeQueryTpl = ctx => `SELECT pg_typeof(${ctx.column})::oid FROM (${ctx.query}) _cdb_column_type limit 1`; BaseDataview.prototype.getColumnType = function (psql, column, query, callback) { var readOnlyTransaction = true; var columnTypeQuery = columnTypeQueryTpl({ - column: column, query: query + column: column, + query: query }); psql.query(columnTypeQuery, function(err, result) { From b4ce13e4293c5770d4852415c2e1e33f1f7d4219 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 15 Sep 2017 11:56:59 +0200 Subject: [PATCH 2/6] Use object shorthand notation --- lib/cartodb/models/dataview/base.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/cartodb/models/dataview/base.js b/lib/cartodb/models/dataview/base.js index fb5f520e..cacfaa77 100644 --- a/lib/cartodb/models/dataview/base.js +++ b/lib/cartodb/models/dataview/base.js @@ -45,10 +45,7 @@ var columnTypeQueryTpl = ctx => `SELECT pg_typeof(${ctx.column})::oid FROM (${ct BaseDataview.prototype.getColumnType = function (psql, column, query, callback) { var readOnlyTransaction = true; - var columnTypeQuery = columnTypeQueryTpl({ - column: column, - query: query - }); + var columnTypeQuery = columnTypeQueryTpl({ column, query }); psql.query(columnTypeQuery, function(err, result) { if (err) { From ada58f6ea2a5c8590193ab67359655f78af69f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 15 Sep 2017 13:35:00 +0200 Subject: [PATCH 3/6] Use const keyword to declare varibles --- lib/cartodb/models/dataview/base.js | 91 ++++++++++++++--------------- 1 file changed, 44 insertions(+), 47 deletions(-) diff --git a/lib/cartodb/models/dataview/base.js b/lib/cartodb/models/dataview/base.js index cacfaa77..bc7d13bd 100644 --- a/lib/cartodb/models/dataview/base.js +++ b/lib/cartodb/models/dataview/base.js @@ -1,60 +1,16 @@ -function BaseDataview() {} - -module.exports = BaseDataview; - -BaseDataview.prototype.getResult = function(psql, override, callback) { - var self = this; - this.sql(psql, override, function(err, query) { - if (err) { - return callback(err); - } - - psql.query(query, function(err, result) { - if (err) { - return callback(err, result); - } - - result = self.format(result, override); - result.type = self.getType(); - - return callback(null, result); - - }, true); // use read-only transaction - }); - -}; - -BaseDataview.prototype.search = function(psql, userQuery, callback) { - return callback(null, this.format({ rows: [] })); -}; - -var FLOAT_OIDS = { +const FLOAT_OIDS = { 700: true, 701: true, 1700: true }; -var DATE_OIDS = { +const DATE_OIDS = { 1082: true, 1114: true, 1184: true }; -var columnTypeQueryTpl = ctx => `SELECT pg_typeof(${ctx.column})::oid FROM (${ctx.query}) _cdb_column_type limit 1`; - -BaseDataview.prototype.getColumnType = function (psql, column, query, callback) { - var readOnlyTransaction = true; - - var columnTypeQuery = columnTypeQueryTpl({ column, query }); - - psql.query(columnTypeQuery, function(err, result) { - if (err) { - return callback(err); - } - var pgType = result.rows[0].pg_typeof; - callback(null, getPGTypeName(pgType)); - }, readOnlyTransaction); -}; +const columnTypeQueryTpl = ctx => `SELECT pg_typeof(${ctx.column})::oid FROM (${ctx.query}) _cdb_column_type limit 1`; function getPGTypeName (pgType) { return { @@ -62,3 +18,44 @@ function getPGTypeName (pgType) { date: DATE_OIDS.hasOwnProperty(pgType) }; } + +module.exports = class BaseDataview { + getResult (psql, override, callback) { + const self = this; + this.sql(psql, override, function(err, query) { + if (err) { + return callback(err); + } + + psql.query(query, function(err, result) { + if (err) { + return callback(err, result); + } + + result = self.format(result, override); + result.type = self.getType(); + + return callback(null, result); + + }, true); // use read-only transaction + }); + } + + search (psql, userQuery, callback) { + return callback(null, this.format({ rows: [] })); + }; + + getColumnType (psql, column, query, callback) { + const readOnlyTransaction = true; + + const columnTypeQuery = columnTypeQueryTpl({ column, query }); + + psql.query(columnTypeQuery, function(err, result) { + if (err) { + return callback(err); + } + const pgType = result.rows[0].pg_typeof; + callback(null, getPGTypeName(pgType)); + }, readOnlyTransaction); + } +}; From 90c4796d4ede54f649f0f8d7112afae325c32b7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 15 Sep 2017 13:41:54 +0200 Subject: [PATCH 4/6] Remove empty line --- lib/cartodb/models/dataview/base.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/cartodb/models/dataview/base.js b/lib/cartodb/models/dataview/base.js index bc7d13bd..702f43e5 100644 --- a/lib/cartodb/models/dataview/base.js +++ b/lib/cartodb/models/dataview/base.js @@ -43,11 +43,10 @@ module.exports = class BaseDataview { search (psql, userQuery, callback) { return callback(null, this.format({ rows: [] })); - }; + } getColumnType (psql, column, query, callback) { const readOnlyTransaction = true; - const columnTypeQuery = columnTypeQueryTpl({ column, query }); psql.query(columnTypeQuery, function(err, result) { From 1959a841fd8823383f5e37e3bf7028a2f71377b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa=20Aubert?= Date: Fri, 15 Sep 2017 14:26:22 +0200 Subject: [PATCH 5/6] Use arrow function to take advantage of bound context --- lib/cartodb/models/dataview/base.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/cartodb/models/dataview/base.js b/lib/cartodb/models/dataview/base.js index 702f43e5..ea7f9474 100644 --- a/lib/cartodb/models/dataview/base.js +++ b/lib/cartodb/models/dataview/base.js @@ -21,8 +21,7 @@ function getPGTypeName (pgType) { module.exports = class BaseDataview { getResult (psql, override, callback) { - const self = this; - this.sql(psql, override, function(err, query) { + this.sql(psql, override, (err, query) => { if (err) { return callback(err); } @@ -32,8 +31,8 @@ module.exports = class BaseDataview { return callback(err, result); } - result = self.format(result, override); - result.type = self.getType(); + result = this.format(result, override); + result.type = this.getType(); return callback(null, result); From 8fe31c45f34b53ff1e22a1f20ebf6da66e31d5c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Mart=C3=ADn?= Date: Wed, 4 Oct 2017 11:10:17 +0200 Subject: [PATCH 6/6] fix 'this' scope with arrow function --- lib/cartodb/models/dataview/base.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/cartodb/models/dataview/base.js b/lib/cartodb/models/dataview/base.js index ea7f9474..8c1ada5d 100644 --- a/lib/cartodb/models/dataview/base.js +++ b/lib/cartodb/models/dataview/base.js @@ -26,7 +26,7 @@ module.exports = class BaseDataview { return callback(err); } - psql.query(query, function(err, result) { + psql.query(query, (err, result) => { if (err) { return callback(err, result); } @@ -48,7 +48,7 @@ module.exports = class BaseDataview { const readOnlyTransaction = true; const columnTypeQuery = columnTypeQueryTpl({ column, query }); - psql.query(columnTypeQuery, function(err, result) { + psql.query(columnTypeQuery, (err, result) => { if (err) { return callback(err); }