Skip to content

Commit

Permalink
Fix buildWhere for count
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed May 6, 2013
1 parent 41db63f commit 1df4d34
Show file tree
Hide file tree
Showing 5 changed files with 350 additions and 273 deletions.
154 changes: 82 additions & 72 deletions lib/adapters/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,11 @@ MySQL.prototype.all = function all(model, filter, callback) {

var sql = 'SELECT * FROM ' + this.tableEscaped(model);
var self = this;
var props = this._models[model].properties;

if (filter) {

if (filter.where) {
sql += ' ' + buildWhere(filter.where);
sql += ' ' + buildWhere(filter.where, self, model);
}

if (filter.order) {
Expand All @@ -230,76 +229,6 @@ MySQL.prototype.all = function all(model, filter, callback) {
}.bind(this));

return sql;

function buildWhere(conds) {
var cs = [];
Object.keys(conds).forEach(function (key) {
var keyEscaped = '`' + key.replace(/\./g, '`.`') + '`'
var val = self.toDatabase(props[key], conds[key]);
if (conds[key] === null) {
cs.push(keyEscaped + ' IS NULL');
} else if (conds[key].constructor.name === 'Object') {
var condType = Object.keys(conds[key])[0];
var sqlCond = keyEscaped;
if ((condType == 'inq' || condType == 'nin') && val.length == 0) {
cs.push(condType == 'inq' ? 0 : 1);
return true;
}
switch (condType) {
case 'gt':
sqlCond += ' > ';
break;
case 'gte':
sqlCond += ' >= ';
break;
case 'lt':
sqlCond += ' < ';
break;
case 'lte':
sqlCond += ' <= ';
break;
case 'between':
sqlCond += ' BETWEEN ';
break;
case 'inq':
case 'in':
sqlCond += ' IN ';
break;
case 'nin':
sqlCond += ' NOT IN ';
break;
case 'neq':
case 'ne':
sqlCond += ' != ';
break;
case 'regex':
sqlCond += ' REGEXP ';
break;
}
sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + val + ')' : val;
cs.push(sqlCond);
} else if (/^\//gi.test(conds[key])) {
var reg = val.toString().split('/');
cs.push(keyEscaped + ' REGEXP "' + reg[1] + '"');
} else {
cs.push(keyEscaped + ' = ' + val);
}
});
if (cs.length === 0) {
return '';
}
return 'WHERE ' + cs.join(' AND ');
}

function buildOrderBy(order) {
if (typeof order === 'string') order = [order];
return 'ORDER BY ' + order.join(', ');
}

function buildLimit(limit, offset) {
return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit);
}

};

MySQL.prototype.autoupdate = function (cb) {
Expand Down Expand Up @@ -544,3 +473,84 @@ function datatype(p) {
return dt;
}


function buildWhere(conds, adapter, model) {
var cs = [],
self = adapter,
props = self._models[model].properties;

Object.keys(conds).forEach(function (key) {
var keyEscaped = '`' + key.replace(/\./g, '`.`') + '`'
var val = self.toDatabase(props[key], conds[key]);
if (conds[key] === null) {
cs.push(keyEscaped + ' IS NULL');
} else if (conds[key].constructor.name === 'Object') {
var condType = Object.keys(conds[key])[0];
var sqlCond = keyEscaped;
if ((condType == 'inq' || condType == 'nin') && val.length == 0) {
cs.push(condType == 'inq' ? 0 : 1);
return true;
}
switch (condType) {
case 'gt':
sqlCond += ' > ';
break;
case 'gte':
sqlCond += ' >= ';
break;
case 'lt':
sqlCond += ' < ';
break;
case 'lte':
sqlCond += ' <= ';
break;
case 'between':
sqlCond += ' BETWEEN ';
break;
case 'inq':
case 'in':
sqlCond += ' IN ';
break;
case 'nin':
sqlCond += ' NOT IN ';
break;
case 'neq':
case 'ne':
sqlCond += ' != ';
break;
case 'regex':
sqlCond += ' REGEXP ';
break;
case 'like':
sqlCond += ' LIKE ';
break;
case 'nlike':
sqlCond += ' NOT LIKE ';
break;
default:
sqlCond += ' ' + condType + ' ';
break;
}
sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + val + ')' : val;
cs.push(sqlCond);
} else if (/^\//gi.test(conds[key])) {
var reg = val.toString().split('/');
cs.push(keyEscaped + ' REGEXP "' + reg[1] + '"');
} else {
cs.push(keyEscaped + ' = ' + val);
}
});
if (cs.length === 0) {
return '';
}
return 'WHERE ' + cs.join(' AND ');
}

function buildOrderBy(order) {
if (typeof order === 'string') order = [order];
return 'ORDER BY ' + order.join(', ');
}

function buildLimit(limit, offset) {
return 'LIMIT ' + (offset ? (offset + ', ' + limit) : limit);
}
Loading

0 comments on commit 1df4d34

Please sign in to comment.