Use wrapDates function from vector adapter
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
const AggregationMapConfig = require('../../aggregation/aggregation-mapconfig');
|
||||
const utilsService = require('../../../utils/get-column-types');
|
||||
const queryUtils = require('../../../utils/query-utils');
|
||||
|
||||
// Generate query to detect time columns
|
||||
// For every column cast to unix timestamp
|
||||
@@ -25,15 +26,33 @@ module.exports = class VectorMapConfigAdapter {
|
||||
}
|
||||
|
||||
|
||||
// // Get columns
|
||||
utilsService.getColumns(user, this.pgConnection, requestMapConfig.layers[0])
|
||||
this._wrapDates(requestMapConfig, user)
|
||||
.then(updatedRequestMapConfig => callback(null, updatedRequestMapConfig))
|
||||
.catch(callback);
|
||||
}
|
||||
|
||||
_wrapDates(requestMapConfig, user) {
|
||||
const originalQuery = requestMapConfig.layers[0].options.sql;
|
||||
return this._getColumns(user, originalQuery)
|
||||
.then(result => {
|
||||
const newSqlQuery = utilsService.wrapDates(requestMapConfig.layers[0].options.sql, result.fields);
|
||||
const newSqlQuery = utilsService.wrapDates(originalQuery, result.fields);
|
||||
requestMapConfig.layers[0].options.sql = newSqlQuery;
|
||||
return callback(null, requestMapConfig);
|
||||
})
|
||||
.catch(err => {
|
||||
return callback(err);
|
||||
return requestMapConfig;
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
_getColumns(user, originalQuery) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.pgConnection.getConnection(user, (err, connection) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
const query = `SELECT * FROM (${originalQuery}) _cdb_column_type limit 0`;
|
||||
queryUtils.queryPromise(connection, query)
|
||||
.then(resolve)
|
||||
.catch(reject);
|
||||
// TODO release pgConnection
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -1,10 +1,10 @@
|
||||
// Postgress ID of date types
|
||||
const DATE_OIDS = {
|
||||
1082: true,
|
||||
1114: true,
|
||||
1184: true
|
||||
};
|
||||
|
||||
const DATE_OIDS = Object.freeze({
|
||||
1082: 'DATE',
|
||||
1083: 'TIME',
|
||||
1114: 'TIMESTAMP',
|
||||
1184: 'TIMESTAMPTZ',
|
||||
1266: 'TIMETZ'
|
||||
});
|
||||
|
||||
/**
|
||||
* Wrap a query transforming all date columns into a unix epoch
|
||||
@@ -14,18 +14,16 @@ const DATE_OIDS = {
|
||||
function wrapDates(originalQuery, fields) {
|
||||
return `
|
||||
SELECT
|
||||
${fields.map(field => DATE_OIDS.hasOwnProperty(field.dataTypeID) ? _castColumnToEpoch(field.name) : `${field.name}`).join(',')}
|
||||
${fields.map(field => _isDateType(field) ? _castColumnToEpoch(field.name) : `${field.name}`).join(',')}
|
||||
FROM
|
||||
(${originalQuery}) _cdb_epoch_transformation `;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a list of all the columns in the query
|
||||
* @param {*} dbConnection
|
||||
* @param {*} originalQuery
|
||||
* @param {object} field
|
||||
*/
|
||||
function getColumns(user, dbConnection, layer) {
|
||||
return _getColumns(user, dbConnection, layer.options.sql);
|
||||
function _isDateType(field) {
|
||||
return DATE_OIDS.hasOwnProperty(field.dataTypeID);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,25 +34,6 @@ function _castColumnToEpoch(columnName) {
|
||||
return `date_part('epoch', ${columnName}) as ${columnName}`;
|
||||
}
|
||||
|
||||
function _getColumns(user, dbConnection, originalQuery) {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
dbConnection.getConnection(user, (err, connection) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
connection.query(`SELECT * FROM (${originalQuery}) _cdb_column_type limit 0`, (err, res) => {
|
||||
if (err) {
|
||||
return reject(err);
|
||||
}
|
||||
resolve(res);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
wrapDates,
|
||||
getColumns,
|
||||
wrapDates
|
||||
};
|
||||
Reference in New Issue
Block a user