-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.js
76 lines (66 loc) · 2.27 KB
/
queries.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var sql = require('sql');
var defs = require('./defs.js');
//define queries for our application.
/*
what are actually queries we need?
buurten:
select name, sum(dumps) dumps, avg(fillperc) fillperc, sum(melds) melds from schiedam.combined group by name;
--if date: where date = $1
--if daterange: where date >= $1 and date <= $2
buurt:
--if date:
select name, dumps, fillperc, melds from schiedam.combined where name = $1 and date = $2;
--if daterange:
select name, dumps, fillperc, melds from schiedam.combined where name = $1 and date >= $2 and date <= $3;
*/
function setDefaultParams(params) {
if (!params.date) {
params.date = '2015-06-30';
};
if (!params.summarize) {
params.summarize = false
}
if (params.agg) {
params.summarize = params.agg
}
return params;
}
function buildQuery(params, path) {
//the basic idea is to choose a query set based on path,
//then compose the query based on params.
//how to do it elegantly?
params = setDefaultParams(params);
var c = defs.combined;
//query is parameterized, with 'text' and 'values'.
var query;
//'/buurt' can now only take one.
// could create JSON structures, nested, and return those, but that's more complicated.
if (params.summarize === 'true') {
query = c.select(c.bu_code,c.name,sql.functions.SUM(c.dumps).as('dumps'),sql.functions.AVG(c.fillperc).as('fillperc'))
.from(c);
if(params.start && params.end) {
query.where((c.date).gte(params.start),(c.date).lte(params.end));
} else if (params.date) {
query.where((c.date).equals(params.date));
}
if (params.name) {
query.and((c.name).equals(params.name));
}
query.group(c.name, c.bu_code);
}
else {
query = c.select(c.date,c.bu_code,c.name, c.dumps, c.fillperc).from(c);
if(params.start && params.end) {
query.where((c.date).gte(params.start),(c.date).lte(params.end));
} else if (params.date) {
query.where((c.date).equals(params.date));
}
if (params.name) {
query.where({name:params.name});
}
}
//console.log('DEBUG: QUERY COMPOSITION');
//console.log(query.toQuery());
return query.toQuery();
}
module.exports = buildQuery;