From 3a2b196f643f51363a62be821bd1f02c04652cdc Mon Sep 17 00:00:00 2001 From: Diogo Resende Date: Sat, 6 Jul 2013 00:00:33 +0100 Subject: [PATCH] Fixes duplicated debug in mysql for aggregate functions, creates .call() in aggregates for generic functions (#204) --- lib/AggregateFunctions.js | 20 +++++++++++++++----- lib/Drivers/DML/mysql.js | 6 +++--- lib/Drivers/DML/postgres.js | 8 +++++++- lib/Drivers/DML/sqlite.js | 5 ++++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/AggregateFunctions.js b/lib/AggregateFunctions.js index e3b5d940..165aeebc 100644 --- a/lib/AggregateFunctions.js +++ b/lib/AggregateFunctions.js @@ -70,6 +70,20 @@ function AggregateFunctions(opts) { return this; }, + call: function (fun, args) { + 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 this; + }, get: function (cb) { if (typeof cb != "function") { throw ErrorCodes.generateError(ErrorCodes.MISSING_CALLBACK, "You must pass a callback to Model.aggregate().get()"); @@ -86,7 +100,7 @@ function AggregateFunctions(opts) { for (i = 0; i < aggregates.length; i++) { for (j = 0; j < aggregates[i].length; j++) { - query[aggregates[i][j].f](aggregates[i][j].a, aggregates[i][j].alias); + query.fun(aggregates[i][j].f, aggregates[i][j].a, aggregates[i][j].alias); } } @@ -107,10 +121,6 @@ function AggregateFunctions(opts) { query = query.build(); - if (opts.driver.opts && opts.driver.opts.debug) { - require("./Debug").sql(opts.driver_name, query); - } - opts.driver.execQuery(query, function (err, data) { if (err) { return cb(err); diff --git a/lib/Drivers/DML/mysql.js b/lib/Drivers/DML/mysql.js index 0dedad1d..a565d7ae 100755 --- a/lib/Drivers/DML/mysql.js +++ b/lib/Drivers/DML/mysql.js @@ -80,14 +80,14 @@ Driver.prototype.getQuery = function () { }; Driver.prototype.execQuery = function (query, cb) { + if (this.opts.debug) { + require("../../Debug").sql('mysql', query); + } if (this.opts.pool) { this.poolQuery(query, cb); } else { this.db.query(query, cb); } - if (this.opts.debug) { - require("../../Debug").sql('mysql', query); - } }; Driver.prototype.find = function (fields, table, conditions, opts, cb) { diff --git a/lib/Drivers/DML/postgres.js b/lib/Drivers/DML/postgres.js index 726f2ace..2312e98c 100644 --- a/lib/Drivers/DML/postgres.js +++ b/lib/Drivers/DML/postgres.js @@ -49,6 +49,9 @@ var switchableFunctions = { }); }, execQuery: function (query, cb) { + if (this.opts.debug) { + require("../../Debug").sql('mysql', query); + } this.db.connect(this.config, function (err, client, done) { if (err) return cb(err); @@ -75,7 +78,10 @@ var switchableFunctions = { this.db.connect(cb); }, execQuery: function (query, cb) { - this.db.query(query, function(err, result) { + if (this.opts.debug) { + require("../../Debug").sql('mysql', query); + } + this.db.query(query, function (err, result) { if (err) { cb(err); } else { diff --git a/lib/Drivers/DML/sqlite.js b/lib/Drivers/DML/sqlite.js index 11200dff..6e1c8bd6 100644 --- a/lib/Drivers/DML/sqlite.js +++ b/lib/Drivers/DML/sqlite.js @@ -58,6 +58,9 @@ Driver.prototype.getQuery = function () { }; Driver.prototype.execQuery = function (query, cb) { + if (this.opts.debug) { + require("../../Debug").sql('mysql', query); + } this.db.all(query, cb); }; @@ -262,7 +265,7 @@ Driver.prototype.propertyToValue = function (value, property) { millis = '0' + millis; } strdate += ' ' + hours + ':' + minutes + ':' + seconds + '.' + millis + '000'; - return strdate; + return strdate; } }