Refactor metadata queryPromise helper
This function to adapt query execution as a Promise was unnecessary complex.
This commit is contained in:
parent
6384f5538c
commit
ebc086106f
@ -34,24 +34,11 @@ MapnikLayerStats.prototype.is = function (type) {
|
||||
return this._types[type] ? this._types[type] : false;
|
||||
};
|
||||
|
||||
function queryPromise(dbConnection, query, adaptResults, errorHandler) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
dbConnection.query(query, function (err, res) {
|
||||
if (err) {
|
||||
if (errorHandler) {
|
||||
resolve(errorHandler(err));
|
||||
}
|
||||
else {
|
||||
reject(err);
|
||||
}
|
||||
}
|
||||
else {
|
||||
resolve(adaptResults(res));
|
||||
}
|
||||
});
|
||||
|
||||
function queryPromise(dbConnection, query) {
|
||||
return new Promise((resolve, reject) => {
|
||||
dbConnection.query(query, (err, res) => err ? reject(err) : resolve(res));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function columnAggregations(field) {
|
||||
if (field.type === 'number') {
|
||||
@ -76,22 +63,16 @@ function _getSQL(ctx, query, type='pre', zoom=0) {
|
||||
}
|
||||
|
||||
function _estimatedFeatureCount(ctx) {
|
||||
return queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, queryUtils.getQueryRowEstimation),
|
||||
res => ({ estimatedFeatureCount: res.rows[0].rows }),
|
||||
() => ({ estimatedFeatureCount: -1 })
|
||||
);
|
||||
return queryPromise(ctx.dbConnection, _getSQL(ctx, queryUtils.getQueryRowEstimation))
|
||||
.then(res => ({ estimatedFeatureCount: res.rows[0].rows }))
|
||||
.catch(() => ({ estimatedFeatureCount: -1 }));
|
||||
}
|
||||
|
||||
function _featureCount(ctx) {
|
||||
if (ctx.metaOptions.featureCount) {
|
||||
// TODO: if ctx.metaOptions.columnStats we can combine this with column stats query
|
||||
return queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, queryUtils.getQueryActualRowCount),
|
||||
res => ({ featureCount: res.rows[0].rows })
|
||||
);
|
||||
return queryPromise(ctx.dbConnection, _getSQL(ctx, queryUtils.getQueryActualRowCount))
|
||||
.then(res => ({ featureCount: res.rows[0].rows }));
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -103,9 +84,8 @@ function _aggrFeatureCount(ctx) {
|
||||
// return metadata for multiple levels.
|
||||
return queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, queryUtils.getQueryActualRowCount, 'post', ctx.metaOptions.aggrFeatureCount),
|
||||
res => ({ aggrFeatureCount: res.rows[0].rows })
|
||||
);
|
||||
_getSQL(ctx, queryUtils.getQueryActualRowCount, 'post', ctx.metaOptions.aggrFeatureCount)
|
||||
).then(res => ({ aggrFeatureCount: res.rows[0].rows }));
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -113,11 +93,8 @@ function _aggrFeatureCount(ctx) {
|
||||
function _geometryType(ctx) {
|
||||
if (ctx.metaOptions.geometryType) {
|
||||
const geometryColumn = AggregationMapConfig.getAggregationGeometryColumn();
|
||||
return queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, sql => queryUtils.getQueryGeometryType(sql, geometryColumn)),
|
||||
res => ({ geometryType: res.rows[0].geom_type })
|
||||
);
|
||||
return queryPromise(ctx.dbConnection, _getSQL(ctx, sql => queryUtils.getQueryGeometryType(sql, geometryColumn)))
|
||||
.then(res => ({ geometryType: res.rows[0].geom_type }));
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -125,11 +102,8 @@ function _geometryType(ctx) {
|
||||
function _columns(ctx) {
|
||||
if (ctx.metaOptions.columns || ctx.metaOptions.columnStats) {
|
||||
// note: post-aggregation columns are in layer.options.columns when aggregation is present
|
||||
return queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, sql => queryUtils.getQueryLimited(sql, 0)),
|
||||
res => formatResultFields(ctx.dbConnection, res.fields)
|
||||
);
|
||||
return queryPromise(ctx.dbConnection, _getSQL(ctx, sql => queryUtils.getQueryLimited(sql, 0)))
|
||||
.then(res => formatResultFields(ctx.dbConnection, res.fields));
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -177,11 +151,8 @@ function _sample(ctx, numRows) {
|
||||
const sampleProb = Math.min(ctx.metaOptions.sample / numRows, 1);
|
||||
// We'll use a safety limit just in case numRows is a bad estimate
|
||||
const limit = Math.ceil(ctx.metaOptions.sample * 1.5);
|
||||
return queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, sql => queryUtils.getQuerySample(sql, sampleProb, limit)),
|
||||
res => ({ sample: res.rows })
|
||||
);
|
||||
return queryPromise(ctx.dbConnection, _getSQL(ctx, sql => queryUtils.getQuerySample(sql, sampleProb, limit)))
|
||||
.then(res => ({ sample: res.rows }));
|
||||
}
|
||||
return Promise.resolve();
|
||||
}
|
||||
@ -206,27 +177,25 @@ function _columnStats(ctx, columns) {
|
||||
queries.push(
|
||||
queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, sql => queryUtils.getQueryTopCategories(sql, name, topN)),
|
||||
res => ({ [name]: { categories: res.rows } })
|
||||
)
|
||||
_getSQL(ctx, sql => queryUtils.getQueryTopCategories(sql, name, topN))
|
||||
).then(res => ({ [name]: { categories: res.rows } }))
|
||||
);
|
||||
}
|
||||
});
|
||||
queries.push(
|
||||
queryPromise(
|
||||
ctx.dbConnection,
|
||||
_getSQL(ctx, sql => `SELECT ${aggr.join(',')} FROM (${sql}) AS __cdb_query`),
|
||||
res => {
|
||||
let stats = {};
|
||||
Object.keys(columns).forEach(name => {
|
||||
stats[name] = {};
|
||||
columnAggregations(columns[name]).forEach(fn => {
|
||||
stats[name][fn] = res.rows[0][`${name}_${fn}`];
|
||||
});
|
||||
_getSQL(ctx, sql => `SELECT ${aggr.join(',')} FROM (${sql}) AS __cdb_query`)
|
||||
).then(res => {
|
||||
let stats = {};
|
||||
Object.keys(columns).forEach(name => {
|
||||
stats[name] = {};
|
||||
columnAggregations(columns[name]).forEach(fn => {
|
||||
stats[name][fn] = res.rows[0][`${name}_${fn}`];
|
||||
});
|
||||
return stats;
|
||||
}
|
||||
)
|
||||
});
|
||||
return stats;
|
||||
})
|
||||
);
|
||||
return Promise.all(queries).then(results => ({ columns: mergeColumns(results) }));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user