Permalink
Browse files

Added database helpers for common SQL tasks, upgraded mysql to use th…

…e new helpers
  • Loading branch information...
1 parent e297234 commit 01ea63ac39dbb0eca261c74741b1930fe0038aa4 @dresende committed Jul 23, 2012
Showing with 34 additions and 26 deletions.
  1. +1 −1 lib/databases/helpers.js
  2. +33 −25 lib/databases/mysql.js
View
@@ -140,7 +140,7 @@ module.exports = {
};
},
- handleSqlInsertCall: function (cb) {
+ handleSqlInsertCall: function (cb, orm) {
return function (err, info) {
if (err) {
return cb(err);
View
@@ -133,36 +133,41 @@ DBClient.prototype.createCollection = function (collection, fields, assocs, opts
});
};
DBClient.prototype.selectRecords = function (collection, config) {
- var _table = collection.toLowerCase(collection);
- var _query = "SELECT * FROM ", tmp, _values = [];
- var _query_tables = this._escapeId(_table) + " t0";
+ var query = "SELECT * FROM ";
+ var query_tables = this._escapeId(this._collectionToTable(collection)) + " t0";
+ var values = [];
+ var tmp;
config = config || {};
if (config.rel && config.rel.length) {
for (var i = 0; i < config.rel.length; i++) {
- _query_tables = "(" + _query_tables + ") JOIN " +
+ query_tables = "(" + query_tables + ") JOIN " +
this._escapeId(config.rel[i].collection) + " t" + (i + 1) +
" ON " + this._escapeId("t" + i + "." + config.rel[i].rel[0]) +
" = " + this._escapeId("t" + (i + 1) + "." + config.rel[i].rel[1]);
config.conditions["t" + (i + 1) + "." + config.rel[i].rel[2]] = config.rel[i].value;
}
}
- _query += _query_tables;
+ query += query_tables;
if (config.conditions) {
tmp = this._addQueryConditions(config.conditions);
- _query += tmp[0];
- _values = _values.concat(tmp[1]);
+ query += tmp[0];
+ values = values.concat(tmp[1]);
+ }
+ if (config.order) {
+ query += helpers.buildSqlOrder(config.order, this._escapeId);
+ }
+ if (config.limit) {
+ query += helpers.buildSqlLimit(config.limit, config.skip);
}
- if (config.order) _query = this._addQueryOrder(_query, config.order);
- if (config.limit) _query = this._addQueryLimit(_query, config.limit, config.skip);
//console.log(_query, _values);
if (typeof config.callback == "function") {
- this._client.query(_query, _values, function (err, info) {
+ this._client.query(query, values, function (err, info) {
if (err) {
config.callback(err);
return;
@@ -171,24 +176,29 @@ DBClient.prototype.selectRecords = function (collection, config) {
config.callback(null, info);
});
} else {
- return new helpers.DBQuery(this._client.query(_query, _values));
+ return new helpers.DBQuery(this._client.query(query, values));
}
};
DBClient.prototype.clearRecords = function (collection, config, callback) {
- var _table = collection.toLowerCase(collection);
- var _query = "DELETE FROM " + this._escapeId(_table), tmp, _values = [];
+ var query = "DELETE FROM " + this._escapeId(this._collectionToTable(collection));
+ var tmp;
+ var values = [];
config = config || {};
if (config.conditions) {
tmp = this._addQueryConditions(config.conditions);
- _query += tmp[0];
- _values = _values.concat(tmp[1]);
+ query += tmp[0];
+ values = values.concat(tmp[1]);
+ }
+ if (config.order) {
+ query += helpers.buildSqlOrder(config.order, this._escapeId);
+ }
+ if (config.limit) {
+ query += helpers.buildSqlLimit(config.limit, config.skip);
}
- if (config.order) _query = this._addQueryOrder(_query, config.order);
- if (config.limit) _query = this._addQueryLimit(_query, config.limit, config.skip);
- this._client.query(_query, _values, function (err, info) {
+ this._client.query(query, values, function (err, info) {
if (err) {
config.callback(err);
return;
@@ -209,20 +219,21 @@ DBClient.prototype.saveRecord = function (idProp, collection, data, callback) {
};
DBClient.prototype._insertRecord = function (collection, data, callback) {
return helpers.createSqlInsert({
- table: collection.toLowerCase(),
+ table: this._collectionToTable(collection),
escape: this._escapeId,
info: helpers.escapeInsertFields(data, {
date_convert_fmt: "FROM_UNIXTIME(?)",
boolean_convert: this._booleanToSqlValue
}),
+ orm: this._orm,
data: data,
db: this._client,
callback: helpers.handleSqlInsertCall(callback)
});
};
DBClient.prototype._updateRecord = function (collection, idProp, data, id, callback) {
return helpers.createSqlUpdate({
- table: collection.toLowerCase(),
+ table: this._collectionToTable(collection),
key: idProp,
id: id,
escape: this._escapeId,
@@ -240,11 +251,8 @@ DBClient.prototype._addQueryConditions = function (conditions) {
boolean_convert: this._booleanToSqlValue
});
};
-DBClient.prototype._addQueryOrder = function (query, order) {
- return query + helpers.buildSqlOrder(order, this._escapeId);
-};
-DBClient.prototype._addQueryLimit = function (query, limit, skip) {
- return query + helpers.buildSqlLimit(limit, skip);
+DBClient.prototype._collectionToTable = function (collection) {
+ return collection.toLowerCase();
};
DBClient.prototype._booleanToSqlValue = function (value) {
return value ? 1 : 0;

0 comments on commit 01ea63a

Please sign in to comment.