2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-15 19:35:00 +08:00
|
|
|
const FLOAT_OIDS = {
|
2017-06-16 00:04:35 +08:00
|
|
|
700: true,
|
2017-06-22 00:59:36 +08:00
|
|
|
701: true,
|
|
|
|
1700: true
|
2017-06-16 00:04:35 +08:00
|
|
|
};
|
|
|
|
|
2017-09-15 19:35:00 +08:00
|
|
|
const DATE_OIDS = {
|
2017-06-16 18:57:46 +08:00
|
|
|
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`;
|
2017-06-16 00:04:35 +08:00
|
|
|
|
|
|
|
function getPGTypeName (pgType) {
|
|
|
|
return {
|
2017-06-16 18:57:46 +08:00
|
|
|
float: FLOAT_OIDS.hasOwnProperty(pgType),
|
|
|
|
date: DATE_OIDS.hasOwnProperty(pgType)
|
2017-06-16 00:04:35 +08:00
|
|
|
};
|
|
|
|
}
|
2017-09-15 19:35:00 +08:00
|
|
|
|
|
|
|
module.exports = class BaseDataview {
|
|
|
|
getResult (psql, override, callback) {
|
2018-08-29 21:06:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2017-09-15 20:26:22 +08:00
|
|
|
result = this.format(result, override);
|
|
|
|
result.type = this.getType();
|
2017-09-15 19:35:00 +08:00
|
|
|
|
2019-10-22 01:07:24 +08:00
|
|
|
// Overviews logging
|
2018-08-29 21:06:48 +08:00
|
|
|
const stats = {};
|
|
|
|
|
2018-08-30 20:52:37 +08:00
|
|
|
if (flags && flags.usesOverviews !== undefined) {
|
|
|
|
stats.usesOverviews = flags.usesOverviews;
|
|
|
|
} else {
|
|
|
|
stats.usesOverviews = false;
|
2018-08-29 21:06:48 +08:00
|
|
|
}
|
2018-08-30 20:52:37 +08:00
|
|
|
|
|
|
|
if (this.getType) {
|
|
|
|
stats.dataviewType = this.getType();
|
|
|
|
}
|
|
|
|
|
2018-08-29 21:06:48 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|