Permalink
Browse files

Adds more helper database functions

Creates full INSERT and UPDATE queries.

Adds an option to allow different drivers to format boolean values (to insert on database).
  • Loading branch information...
1 parent 2ea103f commit 2197ccb115d7b43bf39b5576a5b2d742c536f8e5 @dresende committed Jun 20, 2012
Showing with 79 additions and 40 deletions.
  1. +46 −2 lib/databases/helpers.js
  2. +26 −36 lib/databases/mysql.js
  3. +7 −2 test/database-helpers.js
View
@@ -80,7 +80,11 @@ module.exports = {
if (typeof conditions[k] == "boolean") {
_conditions.push(prop + op + "?");
- _values.push(conditions[k] ? 1 : 0);
+ if (typeof opts.boolean_convert == "function") {
+ _values.push(opts.boolean_convert(conditions[k]));
+ } else {
+ _values.push(conditions[k] ? 1 : 0);
+ }
continue;
}
if (Array.isArray(conditions[k])) {
@@ -105,6 +109,47 @@ module.exports = {
return [ " WHERE " + _conditions.join(" AND "), _values ];
},
+ createSqlUpdate: function (opts) {
+ var query = "UPDATE " + opts.escape(opts.table) +
+ " SET %values WHERE " + opts.escape(opts.key) + " = " + opts.id;
+
+ query = query.replace("%values", opts.info.query);
+
+ //console.log(query);
+ return opts.db.query(query, opts.info.values, this.handleSqlUpdateCall(opts.callback));
+ },
+
+ createSqlInsert: function (opts) {
+ var query = "INSERT INTO " + opts.escape(opts.table) + " (%fields) " +
+ "VALUES (%values)";
+
+ query = query.replace("%fields", Object.keys(opts.data).map(opts.escape).join(", "));
+ query = query.replace("%values", opts.info.escapes.join(", "));
+
+ //console.log(query);
+ return opts.db.query(query, opts.info.values, this.handleSqlInsertCall(opts.callback));
+ },
+
+ handleSqlUpdateCall: function (cb) {
+ return function (err, info) {
+ if (err) {
+ return cb(err);
+ }
+
+ return callback(null);
+ };
+ },
+
+ handleSqlInsertCall: function (cb) {
+ return function (err, info) {
+ if (err) {
+ return cb(err);
+ }
+
+ return callback(null, info.insertId);
+ };
+ },
+
escapeUpdateFields: function (data, escapeCb, opts) {
var val = [], id = [];
@@ -132,7 +177,6 @@ module.exports = {
return { query: id.join(", "), values: val };
},
-
escapeInsertFields: function (data, opts) {
var val = [], escapes = [];
View
@@ -208,49 +208,36 @@ DBClient.prototype.saveRecord = function (idProp, collection, data, callback) {
}
};
DBClient.prototype._insertRecord = function (collection, data, callback) {
- var _table = collection.toLowerCase();
- var _query = "INSERT INTO " + this._escapeId(_table) + " (%fields) VALUES (%values)";
- var info = helpers.escapeInsertFields(data, {
- date_convert_fmt: "FROM_UNIXTIME(?)",
- boolean_convert: function (v) { return v ? 1 : 0; }
- });
-
- _query = _query.replace("%fields", Object.keys(data).map(this._escapeId).join(", "));
- _query = _query.replace("%values", info.escapes.join(", "));
- //console.log(_query, info);
-
- this._client.query(_query, info.values, function (err, info) {
- if (err) {
- callback(err);
- return;
- }
-
- callback(null, info.insertId);
+ return helpers.createSqlInsert({
+ table: collection.toLowerCase(),
+ escape: this._escapeId,
+ info: helpers.escapeInsertFields(data, {
+ date_convert_fmt: "FROM_UNIXTIME(?)",
+ boolean_convert: this._booleanToSqlValue
+ }),
+ data: data,
+ db: this._client,
+ callback: helpers.handleSqlInsertCall(callback)
});
};
DBClient.prototype._updateRecord = function (collection, idProp, data, id, callback) {
- var _table = collection.toLowerCase();
- var _query = "UPDATE " + this._escapeId(_table) + " SET %values WHERE " + this._escapeId(idProp) + " = " + id;
- var info = helpers.escapeUpdateFields(data, this._escapeId, {
- date_convert_fmt: "FROM_UNIXTIME(?)",
- boolean_convert: function (v) { return v ? 1 : 0; }
- });
-
- _query = _query.replace("%values", info.query);
- //console.log(_query, info);
-
- this._client.query(_query, info.values, function (err, info) {
- if (err) {
- callback(err);
- return;
- }
-
- callback(null);
+ return helpers.createSqlUpdate({
+ table: collection.toLowerCase(),
+ key: idProp,
+ id: id,
+ escape: this._escapeId,
+ info: helpers.escapeUpdateFields(data, this._escapeId, {
+ date_convert_fmt: "FROM_UNIXTIME(?)",
+ boolean_convert: this._booleanToSqlValue
+ }),
+ db: this._client,
+ callback: helpers.handleSqlUpdateCall(callback)
});
};
DBClient.prototype._addQueryConditions = function (conditions) {
return helpers.buildSqlWhere(conditions, this._escapeId, {
- additional_operators:"LIKE", "ILIKE" ]
+ additional_operators:"LIKE", "ILIKE" ],
+ boolean_convert: this._booleanToSqlValue
});
};
DBClient.prototype._addQueryOrder = function (query, order) {
@@ -259,6 +246,9 @@ DBClient.prototype._addQueryOrder = function (query, order) {
DBClient.prototype._addQueryLimit = function (query, limit, skip) {
return query + helpers.buildSqlLimit(limit, skip);
};
+DBClient.prototype._booleanToSqlValue = function (value) {
+ return value ? 1 : 0;
+};
DBClient.prototype._escapeId = function (id) {
if (id.indexOf(".") == -1) {
return "`" + id + "`";
View
@@ -1,5 +1,10 @@
var helpers = require("../lib/databases/helpers");
var should = require("should");
+var sqlWhereOpts = {
+ boolean_convert: function (val) {
+ return val ? 'pass' : 'fail';
+ }
+};
describe("helpers", function () {
var tests = {
@@ -22,8 +27,8 @@ describe("helpers", function () {
[ [ { "prop1": 2 }, escapeCb ], [ " WHERE $prop1$=?", [ 2 ] ] ],
[ [ { "prop1": 'a' }, escapeCb ], [ " WHERE $prop1$=?", [ 'a' ] ] ],
[ [ { "prop1": 2, "prop2": '2' }, escapeCb ], [ " WHERE $prop1$=? AND $prop2$=?", [ 2, '2' ] ] ],
- [ [ { "prop1": 2, "prop2": true }, escapeCb ], [ " WHERE $prop1$=? AND $prop2$=?", [ 2, 1 ] ] ],
- [ [ { "prop1": 2, "prop2": false }, escapeCb ], [ " WHERE $prop1$=? AND $prop2$=?", [ 2, 0 ] ] ],
+ [ [ { "prop1": 2, "prop2": true }, escapeCb, sqlWhereOpts ], [ " WHERE $prop1$=? AND $prop2$=?", [ 2, 'pass' ] ] ],
+ [ [ { "prop1": 2, "prop2": false }, escapeCb, sqlWhereOpts ], [ " WHERE $prop1$=? AND $prop2$=?", [ 2, 'fail' ] ] ],
[ [ { "prop1 !": 2 }, escapeCb ], [ " WHERE $prop1$!=?", [ 2 ] ] ],
[ [ { "prop1 !=": 2 }, escapeCb ], [ " WHERE $prop1$!=?", [ 2 ] ] ],
[ [ { "prop1 >": 2 }, escapeCb ], [ " WHERE $prop1$>?", [ 2 ] ] ],

0 comments on commit 2197ccb

Please sign in to comment.