Skip to content

Commit

Permalink
Try to fix postgresql escaping similar to mysql escaping.
Browse files Browse the repository at this point in the history
The difference is node-mysql uses "?" as escape value dropin and
node-postgres uses "$1", "$2", etc.. An advantage is the it seems
it escapes dates directly.
  • Loading branch information
dresende committed Jan 9, 2012
1 parent d04e2aa commit 8b42b33
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions lib/databases/postgresql.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,34 +205,35 @@ DBClient.prototype.saveRecord = function (collection, data, callback) {
};
DBClient.prototype._insertRecord = function (collection, data, callback) {
var _table = collection.toLowerCase(), self = this;
var _query = "INSERT INTO \"" + _table + "\" (%fields) VALUES (%values)", _fields = [], _values = [];
var _query = "INSERT INTO \"" + _table + "\" (%fields) VALUES (%values)",
_fields = [], _values = [], _escapes = [], n = 1;

for (k in data) {
if (!data.hasOwnProperty(k)) continue;

_fields.push("\"" + k + "\"");
_values.push("$" + (n++));

switch (typeof data[k]) {
case "number":
_values.push(data[k]);
break;
case "boolean":
_values.push(data[k] ? 1 : 0);
_escapes.push(data[k] ? 1 : 0);
break;
case "undefined":
_values.pop();
_fields.pop();
n--;
break;
default:
_values.push("'" + data[k].replace("'", "''") + "'");
_escapes.push(data[k]);
}
}

_query = _query.replace("%fields", _fields.join(", "));
_query = _query.replace("%values", _values.join(", "));

//console.log(_query);
//console.log(_query, _escapes);

this._client.query(_query, function (err, info) {
this._client.query(_query, _escapes, function (err, info) {
if (err) {
callback(err);
return;
Expand Down

0 comments on commit 8b42b33

Please sign in to comment.