Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix sum when 0

  • Loading branch information...
commit 0277f8f365f793b382aa4975ee18ccef9c4000e2 1 parent 53da15a
Joe Ferner authored
Showing with 48 additions and 41 deletions.
  1. +48 −41 lib/query.js
89 lib/query.js
View
@@ -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);
@@ -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) {
@@ -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);
@@ -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);
}
@@ -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];
@@ -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') {
@@ -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.");
@@ -180,7 +179,7 @@ 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;
@@ -188,7 +187,7 @@ Query.prototype.limit = function (count, offset) {
// 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];
@@ -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];
@@ -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);
};
}
@@ -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);
};
}
@@ -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);
};
}
@@ -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];
@@ -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];
@@ -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;
@@ -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];
@@ -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) {
@@ -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;
@@ -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];
@@ -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];
Please sign in to comment.
Something went wrong with that request. Please try again.