Permalink
Browse files

Clean up some of the SQL-generation

  • Loading branch information...
1 parent e29d2e7 commit 40e72a87e63bf77b690b6a79314f48a5826d5e79 mde committed Mar 26, 2013
Showing with 29 additions and 11 deletions.
  1. +12 −0 lib/adapters/sql/base.js
  2. +15 −10 lib/adapters/sql/postgres.js
  3. +2 −1 test/adapters/shared.js
View
@@ -35,6 +35,18 @@ utils.mixin(Adapter.prototype, new (function () {
return propArr.join(', ');
};
+ this._createLeftOuterJoinStatement = function (fromModelName, toModelName) {
+ var fromInfl = utils.string.getInflections(fromModelName)
+ , fromName = fromInfl.constructor.singular
+ , fromTableName = this._tableizeModelName(fromName)
+ , toInfl = utils.string.getInflections(toModelName)
+ , toName = toInfl.constructor.singular
+ , toTableName = this._tableizeModelName(toName)
+ , toColName = this._columnizePropertyName(fromInfl.property.singular + 'Id');
+ return 'LEFT OUTER JOIN ' + toTableName + ' ON (' +
+ fromTableName + '."id" = ' + toTableName + '.' + toColName + ')';
+ };
+
this._createInsertStatement = function (item, props, useAutoIncrementId) {
var sql = ''
, modelName = item.type
@@ -86,13 +86,16 @@ utils.mixin(Adapter.prototype, new (function () {
};
this.exec = function (query, callback) {
+ console.log(query);
this.client.query(query, callback);
};
this.load = function (query, callback) {
- var sql = ''
+ var self = this
+ , sql = ''
, conditions = this.transformConditions(query.conditions)
, tableName = this._tableizeModelName(query.model.modelName)
+ , selects
, includesTableName
, includesColName
, opts = query.opts
@@ -101,20 +104,22 @@ utils.mixin(Adapter.prototype, new (function () {
, limit = opts.limit
, skip = opts.skip;
- sql += 'SELECT ' + this._createSelectStatement(tableName);
+ selects = [tableName];
if (includes) {
- includesTableName = this._tableizeModelName(includes);
- sql += ', ';
- sql += this._createSelectStatement(includes);
+ includes = Array.isArray(includes) ? includes : [includes];
+ selects = selects.concat(includes);
}
+
+ sql += 'SELECT ';
+ sql += selects.map(function (item) {
+ return self._createSelectStatement(item);
+ }).join(', ');
sql += ' ';
sql += 'FROM ' + tableName;
if (includes) {
- sql += ' ';
- includesColName = utils.inflection.singularize(query.model.modelName);
- includesColName = this._columnizePropertyName(includesColName + 'Id');
- sql += 'LEFT OUTER JOIN ' + includesTableName + ' ON (' +
- tableName + '."id" = ' + includesTableName + '.' + includesColName + ')';
+ sql += ' ' + includes.map(function (item) {
+ return self._createLeftOuterJoinStatement(tableName, item);
+ }).join(' ');
}
if (conditions) {
sql += ' ';
View
@@ -534,7 +534,8 @@ tests = {
}
, 'test includes eager-fetch of hasMany association': function (next) {
- User.all({}, {includes: 'accounts'}, function (err, data) {
+ User.all({}, {includes: ['accounts', 'profiles']}, function (err, data) {
+ throw new Error();
next();
});
}

0 comments on commit 40e72a8

Please sign in to comment.