2017-06-16 00:04:35 +08:00
|
|
|
var dot = require('dot');
|
|
|
|
dot.templateSettings.strip = false;
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
function BaseDataview() {}
|
|
|
|
|
|
|
|
module.exports = BaseDataview;
|
|
|
|
|
|
|
|
BaseDataview.prototype.getResult = function(psql, override, callback) {
|
|
|
|
var self = this;
|
|
|
|
this.sql(psql, override, function(err, query) {
|
2017-08-03 18:19:34 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
|
2016-03-22 20:10:42 +08:00
|
|
|
psql.query(query, function(err, result) {
|
|
|
|
if (err) {
|
2017-08-01 19:10:55 +08:00
|
|
|
return callback(err, result);
|
2016-03-22 20:10:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result = self.format(result, override);
|
|
|
|
result.type = self.getType();
|
|
|
|
|
|
|
|
return callback(null, result);
|
|
|
|
|
|
|
|
}, true); // use read-only transaction
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
BaseDataview.prototype.search = function(psql, userQuery, callback) {
|
|
|
|
return callback(null, this.format({ rows: [] }));
|
|
|
|
};
|
2017-06-16 00:04:35 +08:00
|
|
|
|
|
|
|
var FLOAT_OIDS = {
|
|
|
|
700: true,
|
2017-06-22 00:59:36 +08:00
|
|
|
701: true,
|
|
|
|
1700: true
|
2017-06-16 00:04:35 +08:00
|
|
|
};
|
|
|
|
|
2017-06-16 18:57:46 +08:00
|
|
|
var DATE_OIDS = {
|
|
|
|
1082: true,
|
|
|
|
1114: true,
|
|
|
|
1184: true
|
|
|
|
};
|
|
|
|
|
2017-06-16 00:04:35 +08:00
|
|
|
var columnTypeQueryTpl = dot.template(
|
|
|
|
'SELECT pg_typeof({{=it.column}})::oid FROM ({{=it.query}}) _cdb_column_type limit 1'
|
|
|
|
);
|
|
|
|
|
|
|
|
BaseDataview.prototype.getColumnType = function (psql, column, query, callback) {
|
|
|
|
var readOnlyTransaction = true;
|
|
|
|
|
|
|
|
var columnTypeQuery = columnTypeQueryTpl({
|
|
|
|
column: column, query: query
|
|
|
|
});
|
|
|
|
|
|
|
|
psql.query(columnTypeQuery, function(err, result) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
}
|
|
|
|
var pgType = result.rows[0].pg_typeof;
|
|
|
|
callback(null, getPGTypeName(pgType));
|
|
|
|
}, readOnlyTransaction);
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|