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 {
|
|
|
|
var parallelism = 2;
|
|
|
|
var q = queue(parallelism);
|
|
|
|
|
2016-01-20 19:42:43 +08:00
|
|
|
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 17:07:19 +08:00
|
|
|
}
|
2016-01-22 18:03:01 +08:00
|
|
|
done(null, [tableName, rows]);
|
2016-01-20 02:31:43 +08:00
|
|
|
});
|
2016-01-20 19:42:43 +08:00
|
|
|
});
|
|
|
|
});
|
2016-01-20 02:31:43 +08:00
|
|
|
|
2016-01-22 18:03:01 +08:00
|
|
|
var metadata = {};
|
|
|
|
q.awaitAll(function(err, results){
|
2016-01-20 02:31:43 +08:00
|
|
|
if (err) {
|
|
|
|
return callback(err);
|
|
|
|
} else {
|
2016-01-22 18:03:01 +08:00
|
|
|
results.forEach(function(table_rows) {
|
|
|
|
var tableName = table_rows[0];
|
|
|
|
var rows = table_rows[1];
|
|
|
|
if ( rows.length > 0 ) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2016-01-20 02:31:43 +08:00
|
|
|
return callback(null, metadata);
|
|
|
|
}
|
|
|
|
});
|
2016-01-20 18:51:46 +08:00
|
|
|
}
|
2016-01-20 02:31:43 +08:00
|
|
|
});
|
|
|
|
};
|