Windshaft-cartodb/lib/cartodb/api/overviews_api.js

57 lines
2.0 KiB
JavaScript
Raw Normal View History

2016-01-20 02:31:43 +08:00
var queue = require('queue-async');
var QueryTablesApi = require('./query_tables_api');
function OverviewsApi(pgQueryRunner) {
2016-01-20 18:51:46 +08:00
if (pgQueryRunner.pgQueryRunner !== undefined) {
2016-01-20 02:31:43 +08:00
this.queryTablesApi = pgQueryRunner;
this.pgQueryRunner = this.queryTablesApi.pgQueryRunner;
} else {
this.pgQueryRunner = pgQueryRunner;
this.queryTablesApi = new QueryTablesApi(pgQueryRunner);
}
}
module.exports = OverviewsApi;
OverviewsApi.prototype.getOverviewsMetadata = function (username, sql, callback) {
2016-01-20 20:13:02 +08:00
var self = this;
2016-01-20 02:31:43 +08:00
this.queryTablesApi.getAffectedTablesInQuery(username, sql, function(err, tableNames){
if (err) {
callback(err);
} else {
2016-01-20 18:49:17 +08:00
var metadata = {};
2016-01-20 02:31:43 +08:00
var parallelism = 2;
var q = queue(parallelism);
tableNames.forEach(function(tableName) {
q.defer(function(done){
2016-01-20 18:49:17 +08:00
var query = "SELECT * FROM CDB_Overviews('" + tableName + "');";
2016-01-20 20:13:02 +08:00
self.pgQueryRunner.run(username, query, function handleOverviewsRows(err, rows) {
if (err){
var msg = err.message ? err.message : err;
done(new Error('could not get overviews metadata: ' + msg));
return;
}
2016-01-20 20:13:02 +08:00
var table_metadata = {};
for ( var i=0; i<rows.length; ++i ) {
var row = rows[i];
table_metadata[row.z] = { table: row.overview_table };
}
metadata[tableName] = table_metadata;
done(null);
2016-01-20 02:31:43 +08:00
});
});
});
2016-01-20 02:31:43 +08:00
2016-01-20 18:49:17 +08:00
q.awaitAll(function(err){
2016-01-20 02:31:43 +08:00
if (err) {
return callback(err);
} else {
return callback(null, metadata);
}
});
2016-01-20 18:51:46 +08:00
}
2016-01-20 02:31:43 +08:00
});
};