Skip to content

Commit

Permalink
fix sum when 0
Browse files Browse the repository at this point in the history
  • Loading branch information
joeferner committed Oct 26, 2012
1 parent 53da15a commit 0277f8f
Showing 1 changed file with 48 additions and 41 deletions.
89 changes: 48 additions & 41 deletions lib/query.js
Expand Up @@ -6,12 +6,12 @@ var SqlTree = require('./sqltree');
var type = require('./type');
var inflection = require('./inflection');

var Query = function (connection, model) {
var Query = function(connection, model) {
var self = this;
this._getConnection = function () { return connection; }; // hide from JSON.stringify
this._getConnection = function() { return connection; }; // hide from JSON.stringify
this.model = model;
var tableIndex = 0;
this._getNextTableAlias = function () { return 't' + (tableIndex++); };
this._getNextTableAlias = function() { return 't' + (tableIndex++); };
this.count = persistUtil.bind('count', this.count, this);
this.all = persistUtil.bind('all', this.all, this);
this.first = persistUtil.bind('first', this.first, this);
Expand All @@ -22,7 +22,7 @@ var Query = function (connection, model) {
this.max = persistUtil.bind('max', this.max, this);

var sqlTree = new SqlTree(model);
this._sqlTree = function () { return sqlTree; }; // hide from JSON.stringify
this._sqlTree = function() { return sqlTree; }; // hide from JSON.stringify

sqlTree.tableAlias = this._getNextTableAlias();
if (model.columns) {
Expand All @@ -40,7 +40,7 @@ var Query = function (connection, model) {
// "name = ? AND age = ?", ["bob", 6]
// "name = 'bob'"
// { "name": "bob", "age", 6 }
Query.prototype.where = function () {
Query.prototype.where = function() {
if (typeof (arguments[0]) === 'string') {
var expr = arguments[0];
var params = Array.prototype.slice.call(arguments, 1);
Expand All @@ -63,12 +63,11 @@ Query.prototype.whereIn = function() {
var expr = arguments[0];
var params = arguments[1];
var paramCount = params.length;
expr = expr + ' IN (';
expr = expr + ' IN (';
var i;
for(i = 0; i < paramCount-1; i++)
{
expr = expr + "?,"
}
for (i = 0; i < paramCount - 1; i++) {
expr = expr + "?,"
}
expr = expr + '?)'
this.where(expr, params);
}
Expand All @@ -80,7 +79,7 @@ Query.prototype.whereIn = function() {

// "people"
// ["people", "phones"]
Query.prototype.include = function () {
Query.prototype.include = function() {
var join;
if (typeof (arguments[0]) === 'string') {
var associationPropertyName = arguments[0];
Expand Down Expand Up @@ -130,20 +129,20 @@ Query.prototype.include = function () {
return this;
};

Query.prototype.join = function (otherTable, otherTableId, thisTableId) {
Query.prototype.join = function(otherTable, otherTableId, thisTableId) {
return this._join.apply(this, ['join'].concat(persistUtil.toArray(arguments)));
};

Query.prototype.leftJoin = function (otherTable, otherTableId, thisTableId) {
Query.prototype.leftJoin = function(otherTable, otherTableId, thisTableId) {
return this._join.apply(this, ['left join'].concat(persistUtil.toArray(arguments)));
};

// type, otherTable, otherTableId, thisTableId
// type, associationPropertyName
Query.prototype._join = function (type, otherTable, otherTableId, thisTableId) {
Query.prototype._join = function(type, otherTable, otherTableId, thisTableId) {
if (arguments.length === 2) {
var association = this.model.associations[arguments[1]]
|| this.model.associations[inflection.singularize(arguments[1])]
|| this.model.associations[inflection.singularize(arguments[1])]
|| this.model.associations[inflection.pluralize(arguments[1])];
otherTable = association.model.tableName;
if (association.type === 'hasOne') {
Expand Down Expand Up @@ -171,7 +170,7 @@ Query.prototype._join = function (type, otherTable, otherTableId, thisTableId) {
return this;
};

Query.prototype.orderBy = function (name, orderByDirection) {
Query.prototype.orderBy = function(name, orderByDirection) {
var column = this._sqlTree().getColumnByPropertyName(name);
if (column === null) {
throw new Error("Invalid property name [" + name + "] for order by clause.");
Expand All @@ -180,15 +179,15 @@ Query.prototype.orderBy = function (name, orderByDirection) {
return this;
};

Query.prototype.limit = function (count, offset) {
Query.prototype.limit = function(count, offset) {
this._sqlTree().limitCount = count;
this._sqlTree().limitOffset = offset;
return this;
};

// connection, callback, doneCallback
// callback, doneCallback
Query.prototype.each = function () {
Query.prototype.each = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand All @@ -206,7 +205,7 @@ Query.prototype.each = function () {
connection.each(this._sqlTree(), callback, doneCallback);
};

Query.prototype.count = function () {
Query.prototype.count = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand All @@ -223,11 +222,11 @@ Query.prototype.count = function () {
// field (chaining)
// connection, field, callback
// field, callback
Query.prototype.min = function () {
Query.prototype.min = function() {
var self = this;
if (arguments.length === 1) {
var fieldName = arguments[0];
return function (connection, callback) {
return function(connection, callback) {
self.min(connection, fieldName, callback);
};
}
Expand All @@ -251,11 +250,11 @@ Query.prototype.min = function () {
// field (chaining)
// connection, field, callback
// field, callback
Query.prototype.max = function () {
Query.prototype.max = function() {
var self = this;
if (arguments.length === 1) {
var fieldName = arguments[0];
return function (connection, callback) {
return function(connection, callback) {
self.max(connection, fieldName, callback);
};
}
Expand All @@ -279,11 +278,11 @@ Query.prototype.max = function () {
// field (chaining)
// connection, field, callback
// field, callback
Query.prototype.sum = function () {
Query.prototype.sum = function() {
var self = this;
if (arguments.length === 1) {
var fieldName = arguments[0];
return function (connection, callback) {
return function(connection, callback) {
self.sum(connection, fieldName, callback);
};
}
Expand All @@ -300,11 +299,19 @@ Query.prototype.sum = function () {

this._sqlTree().action = 'sum';
this._sqlTree().columns = [ this._sqlTree().getColumnByPropertyName(field) ];
connection.single(this._sqlTree(), 'sum', callback);
connection.single(this._sqlTree(), 'sum', function(err, sum) {
if (err) {
return callback(err);
}
if (sum === null) {
sum = 0;
}
return callback(null, sum);
});
return null;
};

Query.prototype.all = function () {
Query.prototype.all = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand All @@ -317,7 +324,7 @@ Query.prototype.all = function () {
connection.all(this._sqlTree(), callback);
};

Query.prototype.first = function () {
Query.prototype.first = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand All @@ -332,7 +339,7 @@ Query.prototype.first = function () {
if (this._sqlTree().includes.length === 0) {
this.limit(1);
}
this.all(connection, function (err, rows) {
this.all(connection, function(err, rows) {
if (err) {
callback(err);
return;
Expand All @@ -345,7 +352,7 @@ Query.prototype.first = function () {
});
};

Query.prototype.last = function () {
Query.prototype.last = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand All @@ -355,21 +362,21 @@ Query.prototype.last = function () {
}
var callback = arguments[arguments.length - 1];

this.all(connection, function (err, rows) {
this.all(connection, function(err, rows) {
if (err) {
callback(err);
return;
}
if (rows.length === 0) {
callback(null, null);
} else {
callback(null, rows[rows.length-1]);
return;
}
if (rows.length === 0) {
callback(null, null);
} else {
callback(null, rows[rows.length - 1]);
}
});
};


Query.prototype.getById = function () {
Query.prototype.getById = function() {
var args = persistUtil.toArray(arguments);
var connection = this._getConnection();
if (args[0] instanceof Connection) {
Expand All @@ -382,7 +389,7 @@ Query.prototype.getById = function () {
var id = args[0];
var callback = args[args.length - 1];

this.where(this.model.getIdPropertyName() + " = ?", id).first(connection, function (err, row) {
this.where(this.model.getIdPropertyName() + " = ?", id).first(connection, function(err, row) {
if (err) {
callback(err);
return;
Expand All @@ -391,7 +398,7 @@ Query.prototype.getById = function () {
});
};

Query.prototype.deleteAll = function () {
Query.prototype.deleteAll = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand All @@ -406,7 +413,7 @@ Query.prototype.deleteAll = function () {
this.all(connection, callback);
};

Query.prototype.updateAll = function () {
Query.prototype.updateAll = function() {
var connection = this._getConnection();
if (arguments[0] instanceof Connection) {
connection = arguments[0];
Expand Down

0 comments on commit 0277f8f

Please sign in to comment.