Skip to content

Commit 05c20c8

Browse files
committed
Adds possibility to pass an Array to aggregate functions as the columns to aggregate
Example: SELECT MIN(a, b, c) ..
1 parent 3df46bc commit 05c20c8

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

lib/Select.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,20 @@ function SelectQuery(Dialect) {
140140
for (j = 0; j < sql.from[i].select.length; j++) {
141141
str = sql.from[i].select[j].f + "(";
142142

143-
if (sql.from.length == 1) {
144-
str += (sql.from[i].select[j].c ? Dialect.escapeId(sql.from[i].select[j].c) : "*");
143+
if (sql.from[i].select[j].c && !Array.isArray(sql.from[i].select[j].c)) {
144+
sql.from[i].select[j].c = [ sql.from[i].select[j].c ];
145+
}
146+
147+
if (Array.isArray(sql.from[i].select[j].c)) {
148+
str += sql.from[i].select[j].c.map(function (el) {
149+
if (sql.from.length == 1) {
150+
return Dialect.escapeId(el);
151+
} else {
152+
return Dialect.escapeId(sql.from[i].a, el);
153+
}
154+
}).join(", ");
145155
} else {
146-
str += (sql.from[i].select[j].c ? Dialect.escapeId(sql.from[i].a, sql.from[i].select[j].c) : "*");
156+
str += "*";
147157
}
148158

149159
str += ")";

test/integration/test-select-aggregate.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,8 @@ assert.equal(
1515
common.Select().from('table1').avg().max('col1').min('col2').build(),
1616
"SELECT AVG(MAX(`col1`)), MIN(`col2`) FROM `table1`"
1717
);
18+
19+
assert.equal(
20+
common.Select().from('table1').avg().max([ 'col1', 'col2' ]).min('col3').build(),
21+
"SELECT AVG(MAX(`col1`, `col2`)), MIN(`col3`) FROM `table1`"
22+
);

0 commit comments

Comments
 (0)