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) {
|
2017-09-15 20:26:22 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|