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

60 lines
1.5 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) => {
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
return callback(null, result);
}, 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);
}
const pgType = result.rows[0].pg_typeof;
callback(null, getPGTypeName(pgType));
}, readOnlyTransaction);
}
};