Permalink
Browse files

Moves mysql DBQuery to helpers zone. It will most probably be used by…

… postgres
  • Loading branch information...
1 parent a95ea03 commit 6110f61958aa1d86d8fed9b5fd93da4e2f0fef1e @dresende committed Jun 15, 2012
Showing with 29 additions and 28 deletions.
  1. +24 −0 lib/databases/helpers.js
  2. +5 −28 lib/databases/mysql.js
View
24 lib/databases/helpers.js
@@ -1,8 +1,32 @@
var util = require("util");
+var events = require("events");
var re_order_mode = /^(a|de)sc$/i;
+function DBQuery(query) {
+ events.EventEmitter.call(this);
+
+ query.on("row", (function (o) {
+ return function (row) {
+ o.emit("record", row);
+ };
+ })(this));
+ query.on("end", (function (o) {
+ return function (res) {
+ o.emit("end", res);
+ };
+ })(this));
+ query.on("error", (function (o) {
+ return function (err) {
+ o.emit("error", err);
+ };
+ })(this));
+}
+util.inherits(DBQuery, events.EventEmitter);
+
module.exports = {
+ DBQuery: DBQuery,
+
// order should be in the format "<field>[ <order>][ <field>[ <order>] ..]"
// - <order> must be "asc" or "desc"
buildSqlOrder: function (order, escapeCb) {
View
33 lib/databases/mysql.js
@@ -1,36 +1,11 @@
-var util = require("util");
-var events = require("events");
var helpers = require("./helpers");
-function DBQuery(query) {
- events.EventEmitter.call(this);
-
- query.on("row", (function (o) {
- return function (row) {
- o.emit("record", row);
- };
- })(this));
- query.on("end", (function (o) {
- return function (res) {
- o.emit("end", res);
- };
- })(this));
- query.on("error", (function (o) {
- return function (err) {
- o.emit("error", err);
- };
- })(this));
-}
-util.inherits(DBQuery, events.EventEmitter);
-
function DBClient(client) {
this._client = client;
}
-
DBClient.prototype.getClient = function () {
return this._client;
};
-
DBClient.prototype.createCollection = function (collection, fields, assocs, opts) {
var _table = collection.toLowerCase();
var _query = "", _fields = [], _indexes = [];
@@ -166,8 +141,10 @@ DBClient.prototype.selectRecords = function (collection, config) {
if (config.rel && config.rel.length) {
for (var i = 0; i < config.rel.length; i++) {
- _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]);
+ _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;
}
}
@@ -194,7 +171,7 @@ DBClient.prototype.selectRecords = function (collection, config) {
config.callback(null, info);
});
} else {
- return new DBQuery(this._client.query(_query, _values));
+ return new helpers.DBQuery(this._client.query(_query, _values));
}
};
DBClient.prototype.clearRecords = function (collection, config, callback) {

0 comments on commit 6110f61

Please sign in to comment.