Skip to content

Commit

Permalink
Support for or/arbitrary where clauses, tests run
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg committed Aug 22, 2014
1 parent c22a526 commit 3f98c2f
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 139 deletions.
200 changes: 108 additions & 92 deletions lib/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ PG.prototype.toDatabase = function (prop, val) {
var iso = escape(val.toISOString()).substring(1);
return 'TIMESTAMP WITH TIME ZONE ' + iso;
}
return escape(val.toString());
return escape(val ? val.toString() : 'NULL');

};

Expand Down Expand Up @@ -265,102 +265,118 @@ PG.prototype.all = function all(model, filter, callback) {
}.bind(this));
};

PG.prototype.toFilter = function (model, filter) {
if (filter && typeof filter.where === 'function') {
return filter();
}
if (!filter) return '';
PG.prototype.processWhere = function(model, conds) {
var props = this._models[model].properties;
var out = '';
if (filter.where) {
var fields = [];
var conds = filter.where;
if (typeof conds === 'string') {
fields.push(conds);
} else if (util.isArray(conds)) {
var query = conds.shift().replace(/\?/g, function (s) {
return escape(conds.shift());
});
fields.push(query);
} else {
Object.keys(conds).forEach(function (key) {
if (filter.where[key] && filter.where[key].constructor.name === 'RegExp') {
var regex = filter.where[key];
var sqlCond = '"' + key + '"';
var fields = [];

if (regex.ignoreCase) {
sqlCond += ' ~* ';
} else {
sqlCond += ' ~ ';
}
if (typeof conds === 'string') {
fields.push(conds);
} else if (util.isArray(conds)) {
var query = conds.shift().replace(/\?/g, function (s) {
return escape(conds.shift());
});
fields.push(query);
} else {
Object.keys(conds).forEach(function (key) {
if (conds[key] && conds[key].constructor.name === 'RegExp') {
var regex = conds[key];
var sqlCond = '"' + key + '"';

sqlCond += "'"+regex.source+"'";
if (regex.ignoreCase) {
sqlCond += ' ~* ';
} else {
sqlCond += ' ~ ';
}

fields.push(sqlCond);
sqlCond += "'"+regex.source+"'";

return;
}
if (props[key]) {
var filterValue = this.toDatabase(props[key], filter.where[key]);
if (conds[key] && conds[key].constructor.name === 'Object') {
var condType = Object.keys(conds[key])[0];
var sqlCond = '"' + key + '"';
if ((condType == 'inq' || condType == 'nin') && filterValue.length == 0) {
fields.push(condType == 'inq' ? 'FALSE' : 'TRUE');
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':
sqlCond += ' IN ';
break;
case 'nin':
sqlCond += ' NOT IN ';
break;
case 'neq':
if (filterValue === 'NULL') {
sqlCond += ' IS NOT ';
} else {
sqlCond += ' != ';
}
break;
case 'like':
sqlCond += ' LIKE ';
break;
case 'nlike':
sqlCond += ' NOT LIKE ';
break;
default:
sqlCond += ' ' + condType + ' ';
break;
}
sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + filterValue + ')' : filterValue;
fields.push(sqlCond);
fields.push(sqlCond);

return;
}
if (props[key]) {
var filterValue = this.toDatabase(props[key], conds[key]);
if (conds[key] && conds[key].constructor.name === 'Object') {
var condType = Object.keys(conds[key])[0];
var sqlCond = '"' + key + '"';
if ((condType == 'inq' || condType == 'nin') && filterValue.length == 0) {
fields.push(condType == 'inq' ? 'FALSE' : 'TRUE');
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':
sqlCond += ' IN ';
break;
case 'nin':
sqlCond += ' NOT IN ';
break;
case 'neq':
if (filterValue === 'NULL') {
sqlCond += ' IS NOT ';
} else {
sqlCond += ' != ';
}
break;
case 'like':
sqlCond += ' LIKE ';
break;
case 'nlike':
sqlCond += ' NOT LIKE ';
break;
default:
sqlCond += ' ' + condType + ' ';
break;
}
sqlCond += (condType == 'inq' || condType == 'nin') ? '(' + filterValue + ')' : filterValue;
fields.push(sqlCond);
} else {
if (filterValue === 'NULL') {
fields.push('"' + key + '" IS ' + filterValue);
} else {
if (filterValue === 'NULL') {
fields.push('"' + key + '" IS ' + filterValue);
} else {
fields.push('"' + key + '" = ' + filterValue);
}
fields.push('"' + key + '" = ' + filterValue);
}
}
}.bind(this));
}
}
else if (key === 'or') {
var or = this.processWhere(model, conds[key]);
if (or.length) {
fields.push('(' + or.join(' OR ') + ')');
}
}
else if (key === 'arbitrary') {
fields.push(conds[key])
}

}.bind(this));
}

return fields;
};

PG.prototype.toFilter = function (model, filter) {
if (filter && typeof filter.where === 'function') {
return filter();
}
if (!filter) return '';
var out = '';

if (filter.where) {
var fields = this.processWhere(model, filter.where);
if (fields.length) {
out += ' WHERE ' + fields.join(' AND ');
}
Expand Down Expand Up @@ -432,7 +448,7 @@ PG.prototype.autoupdate = function (cb) {
if (err) {
console.log(err);
}

getIndexStatus.call(self, model, function(err, indexes) {
self.alterTableIndexes(model, indexes, done);
});
Expand All @@ -442,7 +458,7 @@ PG.prototype.autoupdate = function (cb) {
if (err) {
console.log(err);
}

// we just created the table, so there will be no indexes
var sql = getIndexesToAdd.call(self, model, []);
applySqlIndexChanges.call(self, model, sql, done);
Expand Down Expand Up @@ -664,7 +680,7 @@ function getIndexesToModify(model, actualIndexes) {
});
}
return sql;

function columnOrderChanged(oldColumns, actualColumns) {
for (var i = 0; i < oldColumns.length; i++) {
if (oldColumns[i].trim() !== actualColumns[i].trim()) {
Expand Down Expand Up @@ -727,7 +743,7 @@ function getIndexesToDrop(model, actualIndexes) {
}
});
return sql;

function actualIndexNotPresentInModel(actualIndex, model) {
return !m.settings.indexes || !m.settings.indexes[actualIndex]
};
Expand Down

0 comments on commit 3f98c2f

Please sign in to comment.