Skip to content

Commit 3a2b196

Browse files
committed
Fixes duplicated debug in mysql for aggregate functions, creates .call() in aggregates for generic functions (#204)
1 parent 21de079 commit 3a2b196

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

lib/AggregateFunctions.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ function AggregateFunctions(opts) {
7070

7171
return this;
7272
},
73+
call: function (fun, args) {
74+
if (args.length > 0) {
75+
aggregates[aggregates.length - 1].push({ f: fun, a: args, alias: aggregateAlias(fun, args) });
76+
// aggregates.push([]);
77+
} else {
78+
aggregates[aggregates.length - 1].push({ f: fun, alias: aggregateAlias(fun, args) });
79+
}
80+
81+
if (fun == "distinct") {
82+
used_distinct = true;
83+
}
84+
85+
return this;
86+
},
7387
get: function (cb) {
7488
if (typeof cb != "function") {
7589
throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "You must pass a callback to Model.aggregate().get()");
@@ -86,7 +100,7 @@ function AggregateFunctions(opts) {
86100

87101
for (i = 0; i < aggregates.length; i++) {
88102
for (j = 0; j < aggregates[i].length; j++) {
89-
query[aggregates[i][j].f](aggregates[i][j].a, aggregates[i][j].alias);
103+
query.fun(aggregates[i][j].f, aggregates[i][j].a, aggregates[i][j].alias);
90104
}
91105
}
92106

@@ -107,10 +121,6 @@ function AggregateFunctions(opts) {
107121

108122
query = query.build();
109123

110-
if (opts.driver.opts && opts.driver.opts.debug) {
111-
require("./Debug").sql(opts.driver_name, query);
112-
}
113-
114124
opts.driver.execQuery(query, function (err, data) {
115125
if (err) {
116126
return cb(err);

lib/Drivers/DML/mysql.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ Driver.prototype.getQuery = function () {
8080
};
8181

8282
Driver.prototype.execQuery = function (query, cb) {
83+
if (this.opts.debug) {
84+
require("../../Debug").sql('mysql', query);
85+
}
8386
if (this.opts.pool) {
8487
this.poolQuery(query, cb);
8588
} else {
8689
this.db.query(query, cb);
8790
}
88-
if (this.opts.debug) {
89-
require("../../Debug").sql('mysql', query);
90-
}
9191
};
9292

9393
Driver.prototype.find = function (fields, table, conditions, opts, cb) {

lib/Drivers/DML/postgres.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ var switchableFunctions = {
4949
});
5050
},
5151
execQuery: function (query, cb) {
52+
if (this.opts.debug) {
53+
require("../../Debug").sql('mysql', query);
54+
}
5255
this.db.connect(this.config, function (err, client, done) {
5356
if (err) return cb(err);
5457

@@ -75,7 +78,10 @@ var switchableFunctions = {
7578
this.db.connect(cb);
7679
},
7780
execQuery: function (query, cb) {
78-
this.db.query(query, function(err, result) {
81+
if (this.opts.debug) {
82+
require("../../Debug").sql('mysql', query);
83+
}
84+
this.db.query(query, function (err, result) {
7985
if (err) {
8086
cb(err);
8187
} else {

lib/Drivers/DML/sqlite.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ Driver.prototype.getQuery = function () {
5858
};
5959

6060
Driver.prototype.execQuery = function (query, cb) {
61+
if (this.opts.debug) {
62+
require("../../Debug").sql('mysql', query);
63+
}
6164
this.db.all(query, cb);
6265
};
6366

@@ -262,7 +265,7 @@ Driver.prototype.propertyToValue = function (value, property) {
262265
millis = '0' + millis;
263266
}
264267
strdate += ' ' + hours + ':' + minutes + ':' + seconds + '.' + millis + '000';
265-
return strdate;
268+
return strdate;
266269
}
267270

268271
}

0 commit comments

Comments
 (0)