Skip to content

Commit 948dc84

Browse files
authored
Implement SimpleGlideAggregate class for aggregations
1 parent 8225905 commit 948dc84

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
var SimpleGlideAggregate = Class.create();
2+
SimpleGlideAggregate.prototype = {
3+
initialize: function(tableName) {
4+
if (!tableName) {
5+
throw new Error("Table name is required.");
6+
}
7+
this._table = tableName;
8+
this._ga = new GlideAggregate(tableName);
9+
this._fields = [];
10+
this._conditionsAdded = false;
11+
},
12+
13+
/**
14+
* Adds a query condition.
15+
* Usage: addQuery('priority', '=', '1') or addQuery('active', true)
16+
*/
17+
addQuery: function(field, operator, value) {
18+
if (value === undefined) {
19+
this._ga.addQuery(field, operator);
20+
} else {
21+
this._ga.addQuery(field, operator, value);
22+
}
23+
this._conditionsAdded = true;
24+
return this;
25+
},
26+
27+
/**
28+
* Adds COUNT aggregate.
29+
*/
30+
count: function() {
31+
this._fields.push({type: 'COUNT', field: null});
32+
return this;
33+
},
34+
35+
/**
36+
* Adds SUM aggregate on a field.
37+
*/
38+
sum: function(field) {
39+
if (!field) throw new Error("Field name required for sum.");
40+
this._fields.push({type: 'SUM', field: field});
41+
return this;
42+
},
43+
44+
/**
45+
* Adds MIN aggregate on a field.
46+
*/
47+
min: function(field) {
48+
if (!field) throw new Error("Field name required for min.");
49+
this._fields.push({type: 'MIN', field: field});
50+
return this;
51+
},
52+
53+
/**
54+
* Adds MAX aggregate on a field.
55+
*/
56+
max: function(field) {
57+
if (!field) throw new Error("Field name required for max.");
58+
this._fields.push({type: 'MAX', field: field});
59+
return this;
60+
},
61+
62+
/**
63+
* Executes the aggregate query and returns results as an object.
64+
* Keys are aggregate type or type_field (for field aggregates).
65+
*/
66+
execute: function() {
67+
var self = this;
68+
69+
if (this._fields.length === 0) {
70+
throw new Error("At least one aggregate function must be added.");
71+
}
72+
73+
this._fields.forEach(function(agg) {
74+
if (agg.field) {
75+
self._ga.addAggregate(agg.type, agg.field);
76+
} else {
77+
self._ga.addAggregate(agg.type);
78+
}
79+
});
80+
81+
this._ga.query();
82+
83+
var results = {};
84+
if (this._ga.next()) {
85+
this._fields.forEach(function(agg) {
86+
var key = agg.field ? agg.type + '_' + agg.field : agg.type;
87+
var value = agg.field ? self._ga.getAggregate(agg.type, agg.field) : self._ga.getAggregate(agg.type);
88+
results[key] = agg.type === 'COUNT' ? parseInt(value, 10) : parseFloat(value);
89+
});
90+
} else {
91+
// No rows matched, all aggregates 0 or null
92+
this._fields.forEach(function(agg) {
93+
var key = agg.field ? agg.type + '_' + agg.field : agg.type;
94+
results[key] = 0;
95+
});
96+
}
97+
98+
return results;
99+
},
100+
101+
type: 'SimpleGlideAggregate'
102+
};

0 commit comments

Comments
 (0)