Add some features to advanced queries

This commit is contained in:
Amir M. Mahmoudi 2012-02-04 14:55:07 +03:30
parent 84881a00e9
commit a34d277ef8
1 changed files with 27 additions and 12 deletions

View File

@ -104,7 +104,13 @@ MySQL.prototype.toDatabase = function (prop, val) {
return this.toDatabase(prop, val[0]) + return this.toDatabase(prop, val[0]) +
' AND ' + ' AND ' +
this.toDatabase(prop, val[1]); this.toDatabase(prop, val[1]);
} } else if (operator == 'inq' || operator == 'nin') {
if (!(val.propertyIsEnumerable('length')) && typeof val === 'object' && typeof val.length === 'number') { //if value is array
return val.join(',');
} else {
return val;
}
}
} }
if (prop.type.name === 'Number') return val; if (prop.type.name === 'Number') return val;
if (prop.type.name === 'Date') { if (prop.type.name === 'Date') {
@ -205,22 +211,31 @@ MySQL.prototype.all = function all(model, filter, callback) {
var sqlCond = keyEscaped; var sqlCond = keyEscaped;
switch (condType) { switch (condType) {
case 'gt': case 'gt':
sqlCond += ' > '; sqlCond += ' > ';
break; break;
case 'gte': case 'gte':
sqlCond += ' >= '; sqlCond += ' >= ';
break; break;
case 'lt': case 'lt':
sqlCond += ' < '; sqlCond += ' < ';
break; break;
case 'lte': case 'lte':
sqlCond += ' <= '; sqlCond += ' <= ';
break; break;
case 'between': case 'between':
sqlCond += ' BETWEEN '; sqlCond += ' BETWEEN ';
break; break;
case 'inq':
sqlCond += ' IN ';
break;
case 'nin':
sqlCond += ' NOT IN ';
break;
case 'neq':
sqlCond + ' != ';
break;
} }
sqlCond += val; sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + val + ')' : val;
cs.push(sqlCond); cs.push(sqlCond);
} else { } else {
cs.push(keyEscaped + ' = ' + val); cs.push(keyEscaped + ' = ' + val);