2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
2018-06-18 19:15:45 +08:00
|
|
|
const queryUtils = require('../utils/query-utils');
|
2016-05-17 21:00:18 +08:00
|
|
|
|
2018-04-10 01:47:24 +08:00
|
|
|
function OverviewsMetadataBackend(pgQueryRunner) {
|
2016-04-20 23:47:43 +08:00
|
|
|
this.pgQueryRunner = pgQueryRunner;
|
2016-01-20 02:31:43 +08:00
|
|
|
}
|
|
|
|
|
2018-04-10 01:47:24 +08:00
|
|
|
module.exports = OverviewsMetadataBackend;
|
2016-01-20 02:31:43 +08:00
|
|
|
|
2018-04-10 01:47:24 +08:00
|
|
|
OverviewsMetadataBackend.prototype.getOverviewsMetadata = function (username, sql, callback) {
|
2016-04-20 20:30:13 +08:00
|
|
|
// FIXME: Currently using internal function _cdb_schema_name
|
2016-04-20 04:50:05 +08:00
|
|
|
// CDB_Overviews should provide the schema information directly.
|
2018-06-18 19:15:45 +08:00
|
|
|
const query = `
|
|
|
|
SELECT *, _cdb_schema_name(base_table)
|
|
|
|
FROM CDB_Overviews(
|
|
|
|
CDB_QueryTablesText($windshaft$${queryUtils.substituteDummyTokens(sql)}$windshaft$)
|
|
|
|
);
|
|
|
|
`;
|
2016-01-26 18:38:21 +08:00
|
|
|
this.pgQueryRunner.run(username, query, function handleOverviewsRows(err, rows) {
|
|
|
|
if (err){
|
2016-02-24 01:15:14 +08:00
|
|
|
callback(err);
|
2016-01-26 18:38:21 +08:00
|
|
|
return;
|
2016-01-20 18:51:46 +08:00
|
|
|
}
|
2016-04-20 23:24:16 +08:00
|
|
|
var metadata = rows.reduce(function(metadata, row){
|
2016-01-26 18:38:21 +08:00
|
|
|
var table = row.base_table;
|
2016-04-20 04:50:05 +08:00
|
|
|
var schema = row._cdb_schema_name;
|
2016-04-21 00:01:34 +08:00
|
|
|
if ( !metadata[table] ) {
|
|
|
|
metadata[table] = {};
|
2016-01-26 18:38:21 +08:00
|
|
|
}
|
2016-04-21 00:01:34 +08:00
|
|
|
metadata[table][row.z] = { table: row.overview_table };
|
|
|
|
metadata[table].schema = schema;
|
2016-04-20 23:24:16 +08:00
|
|
|
return metadata;
|
|
|
|
}, {});
|
2016-01-26 18:38:21 +08:00
|
|
|
return callback(null, metadata);
|
2016-01-20 02:31:43 +08:00
|
|
|
});
|
|
|
|
};
|