2016-02-04 17:26:31 +08:00
|
|
|
function OverviewsMetadataApi(pgQueryRunner) {
|
2016-04-20 23:47:43 +08:00
|
|
|
this.pgQueryRunner = pgQueryRunner;
|
2016-01-20 02:31:43 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 17:26:31 +08:00
|
|
|
module.exports = OverviewsMetadataApi;
|
2016-01-20 02:31:43 +08:00
|
|
|
|
2016-02-04 17:26:31 +08:00
|
|
|
// TODO: share this with QueryTablesApi? ... or maintain independence?
|
2016-01-26 18:38:21 +08:00
|
|
|
var affectedTableRegexCache = {
|
|
|
|
bbox: /!bbox!/g,
|
|
|
|
scale_denominator: /!scale_denominator!/g,
|
|
|
|
pixel_width: /!pixel_width!/g,
|
|
|
|
pixel_height: /!pixel_height!/g
|
|
|
|
};
|
2016-01-20 02:31:43 +08:00
|
|
|
|
2016-01-26 18:38:21 +08:00
|
|
|
function prepareSql(sql) {
|
|
|
|
return sql
|
|
|
|
.replace(affectedTableRegexCache.bbox, 'ST_MakeEnvelope(0,0,0,0)')
|
|
|
|
.replace(affectedTableRegexCache.scale_denominator, '0')
|
|
|
|
.replace(affectedTableRegexCache.pixel_width, '1')
|
|
|
|
.replace(affectedTableRegexCache.pixel_height, '1')
|
|
|
|
;
|
|
|
|
}
|
2016-01-20 02:31:43 +08:00
|
|
|
|
2016-02-04 17:26:31 +08:00
|
|
|
OverviewsMetadataApi.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.
|
|
|
|
var query = 'SELECT *, _cdb_schema_name(base_table)' +
|
|
|
|
' FROM CDB_Overviews(CDB_QueryTablesText($windshaft$' + prepareSql(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-01-26 18:38:21 +08:00
|
|
|
var table_metadata = metadata[table];
|
|
|
|
if ( !table_metadata ) {
|
2016-04-20 23:24:16 +08:00
|
|
|
table_metadata = metadata[table] = {};
|
2016-01-26 18:38:21 +08:00
|
|
|
}
|
|
|
|
table_metadata[row.z] = { table: row.overview_table };
|
2016-04-20 04:50:05 +08:00
|
|
|
table_metadata.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
|
|
|
});
|
|
|
|
};
|