Windshaft-cartodb/lib/cartodb/models/dataview/base.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

2017-09-15 19:35:00 +08:00
const FLOAT_OIDS = {
700: true,
701: true,
1700: true
};
2017-09-15 19:35:00 +08:00
const DATE_OIDS = {
1082: true,
1114: true,
1184: true
};
2017-09-15 19:35:00 +08:00
const columnTypeQueryTpl = ctx => `SELECT pg_typeof(${ctx.column})::oid FROM (${ctx.query}) _cdb_column_type limit 1`;
function getPGTypeName (pgType) {
return {
float: FLOAT_OIDS.hasOwnProperty(pgType),
date: DATE_OIDS.hasOwnProperty(pgType)
};
}
2017-09-15 19:35:00 +08:00
module.exports = class BaseDataview {
getResult (psql, override, callback) {
this.sql(psql, override, (err, query, flags = null) => {
2017-09-15 19:35:00 +08:00
if (err) {
return callback(err);
}
2017-10-04 17:10:17 +08:00
psql.query(query, (err, result) => {
2017-09-15 19:35:00 +08:00
if (err) {
return callback(err, result);
}
result = this.format(result, override);
result.type = this.getType();
2017-09-15 19:35:00 +08:00
2018-08-30 00:00:13 +08:00
//Overviews logging
const stats = {};
if (flags && flags.usesOverviews !== undefined) {
stats.usesOverviews = flags.usesOverviews;
} else {
stats.usesOverviews = false;
}
if (this.getType) {
stats.dataviewType = this.getType();
}
return callback(null, result, stats);
2017-09-15 19:35:00 +08:00
}, true); // use read-only transaction
});
}
search (psql, userQuery, callback) {
return callback(null, this.format({ rows: [] }));
2017-09-15 19:41:54 +08:00
}
2017-09-15 19:35:00 +08:00
getColumnType (psql, column, query, callback) {
const readOnlyTransaction = true;
const columnTypeQuery = columnTypeQueryTpl({ column, query });
2017-10-04 17:10:17 +08:00
psql.query(columnTypeQuery, (err, result) => {
2017-09-15 19:35:00 +08:00
if (err) {
return callback(err);
}
2018-05-11 00:36:08 +08:00
if (!result || !result.rows || !result.rows.length) {
return callback(new Error('The column type could not be determined'));
}
2017-09-15 19:35:00 +08:00
const pgType = result.rows[0].pg_typeof;
callback(null, getPGTypeName(pgType));
}, readOnlyTransaction);
}
};