Windshaft-cartodb/lib/backends/overviews-metadata.js

38 lines
1.3 KiB
JavaScript
Raw Normal View History

'use strict';
const queryUtils = require('../utils/query-utils');
2019-10-22 01:07:24 +08:00
function OverviewsMetadataBackend (pgQueryRunner) {
this.pgQueryRunner = pgQueryRunner;
2016-01-20 02:31:43 +08:00
}
module.exports = OverviewsMetadataBackend;
2016-01-20 02:31:43 +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
// CDB_Overviews should provide the schema information directly.
const query = `
SELECT *, cartodb._cdb_schema_name(base_table)
FROM cartodb.CDB_Overviews(
cartodb.CDB_QueryTablesText($windshaft$${queryUtils.substituteDummyTokens(sql)}$windshaft$)
);
`;
2019-10-22 01:07:24 +08:00
this.pgQueryRunner.run(username, query, function handleOverviewsRows (err, rows) {
if (err) {
2016-02-24 01:15:14 +08:00
callback(err);
return;
2016-01-20 18:51:46 +08:00
}
2019-10-22 01:07:24 +08:00
var metadata = rows.reduce(function (metadata, row) {
var table = row.base_table;
var schema = row._cdb_schema_name;
2019-10-22 01:07:24 +08:00
if (!metadata[table]) {
2016-04-21 00:01:34 +08:00
metadata[table] = {};
}
2016-04-21 00:01:34 +08:00
metadata[table][row.z] = { table: row.overview_table };
metadata[table].schema = schema;
return metadata;
}, {});
return callback(null, metadata);
2016-01-20 02:31:43 +08:00
});
};