-
Notifications
You must be signed in to change notification settings - Fork 377
/
AggregateFunctions.js
175 lines (145 loc) · 4.63 KB
/
AggregateFunctions.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var ORMError = require("./Error");
var Utilities = require("./Utilities");
module.exports = AggregateFunctions;
function AggregateFunctions(opts) {
if (typeof opts.driver.getQuery !== "function") {
throw new ORMError('NO_SUPPORT', "This driver does not support aggregate functions");
}
if (!Array.isArray(opts.driver.aggregate_functions)) {
throw new ORMError('NO_SUPPORT', "This driver does not support aggregate functions");
}
var aggregates = [ [] ];
var group_by = null;
var used_distinct = false;
var appendFunction = function (fun) {
return function () {
var args = (arguments.length && Array.isArray(arguments[0]) ? arguments[0] : Array.prototype.slice.apply(arguments));
if (args.length > 0) {
aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });
aggregates.push([]);
} else {
aggregates[aggregates.length - 1].push({ f: fun, alias: aggregateAlias(fun, args) });
}
if (fun === "distinct") {
used_distinct = true;
}
return proto;
};
};
var proto = {
groupBy: function () {
group_by = Array.prototype.slice.apply(arguments);
return this;
},
limit: function (offset, limit) {
if (typeof limit === "number") {
opts.limit = [ offset, limit ];
} else {
opts.limit = [ 0, offset ]; // offset = limit
}
return this;
},
order: function () {
opts.order = Utilities.standardizeOrder(Array.prototype.slice.apply(arguments));
return this;
},
select: function () {
if (arguments.length === 0) {
throw new ORMError('PARAM_MISMATCH', "When using append you must at least define one property");
}
if (Array.isArray(arguments[0])) {
opts.propertyList = opts.propertyList.concat(arguments[0]);
} else {
opts.propertyList = opts.propertyList.concat(Array.prototype.slice.apply(arguments));
}
return this;
},
as: function (alias) {
if (aggregates.length === 0 || (aggregates.length === 1 && aggregates[0].length === 0)) {
throw new ORMError('PARAM_MISMATCH', "No aggregate functions defined yet");
}
var len = aggregates.length;
aggregates[len - 1][aggregates[len - 1].length - 1].alias = alias;
return this;
},
call: function (fun, args) {
if (args && args.length > 0) {
aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });
// aggregates.push([]);
} else {
aggregates[aggregates.length - 1].push({ f: fun, alias: aggregateAlias(fun, args) });
}
if (fun.toLowerCase() === "distinct") {
used_distinct = true;
}
return this;
},
get: function (cb) {
if (typeof cb !== "function") {
throw new ORMError('MISSING_CALLBACK', "You must pass a callback to Model.aggregate().get()");
}
if (aggregates[aggregates.length - 1].length === 0) {
aggregates.length -= 1;
}
if (aggregates.length === 0) {
throw new ORMError('PARAM_MISMATCH', "Missing aggregate functions");
}
var query = opts.driver.getQuery().select().from(opts.table).select(opts.propertyList);
var i, j;
for (i = 0; i < aggregates.length; i++) {
for (j = 0; j < aggregates[i].length; j++) {
query.fun(aggregates[i][j].f, aggregates[i][j].a, aggregates[i][j].alias);
}
}
query.where(opts.conditions);
if (group_by !== null) {
query.groupBy.apply(query, group_by);
}
if (opts.order) {
for (i = 0; i < opts.order.length; i++) {
query.order(opts.order[i][0], opts.order[i][1]);
}
}
if (opts.limit) {
query.offset(opts.limit[0]).limit(opts.limit[1]);
}
query = query.build();
opts.driver.execQuery(query, function (err, data) {
if (err) {
return cb(err);
}
if (group_by !== null) {
return cb(null, data);
}
var items = [], i;
if (used_distinct && aggregates.length === 1) {
for (i = 0; i < data.length; i++) {
items.push(data[i][Object.keys(data[i]).pop()]);
}
return cb(null, items);
}
for (i = 0; i < aggregates.length; i++) {
for (var j = 0; j < aggregates[i].length; j++) {
items.push(data[0][aggregates[i][j].alias] || null);
}
}
items.unshift(null);
return cb.apply(null, items);
});
}
};
for (var i = 0; i < opts.driver.aggregate_functions.length; i++) {
addAggregate(proto, opts.driver.aggregate_functions[i], appendFunction);
}
return proto;
}
function addAggregate(proto, fun, builder) {
if (Array.isArray(fun)) {
proto[fun[0].toLowerCase()] = builder((fun[1] || fun[0]).toLowerCase());
} else {
proto[fun.toLowerCase()] = builder(fun.toLowerCase());
}
}
function aggregateAlias(fun, fields) {
return fun + (fields && fields.length ? "_" + fields.join("_") : "");
}