Permalink
Browse files

Begin including table-name on conditions

  • Loading branch information...
1 parent c3bf40f commit 339788a305ee514c5f8c01e4c9968f3cf3050de3 mde committed Mar 24, 2013
Showing with 32 additions and 12 deletions.
  1. +4 −2 lib/adapters/sql/postgres.js
  2. +4 −1 lib/adapters/transformers/sql.js
  3. +3 −2 lib/query/comparison.js
  4. +21 −7 lib/query/query.js
@@ -77,18 +77,20 @@ 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 = ''
, conditions = this.transformConditions(query.conditions)
+ , tableName = this._tableizeModelName(query.model.modelName)
, opts = query.opts
, sort = opts.sort
, limit = opts.limit
, skip = opts.skip;
- sql += 'SELECT * FROM ' + this._tableizeModelName(query.model.modelName);
+ sql += 'SELECT ' + tableName + '.* FROM ' + tableName
sql += ' ';
if (conditions) {
sql += 'WHERE ' + conditions;
@@ -107,7 +109,7 @@ utils.mixin(Adapter.prototype, new (function () {
_itemsWithSQL.apply(this, [sql, query.model, function (err, res) {
// If explicitly limited to one, just return the single instance
// This is also used by the `first` method
- if (query.opts.limit == 1) {
+ if (res && query.opts.limit == 1) {
res = res[0];
}
callback(null, res);
@@ -67,7 +67,10 @@ var sql = utils.mixin(new BaseTransformer(), new (function () {
};
this.transformComparisonFieldName = function (comp) {
- var name = this._columnizePropertyName(comp.field);
+ var name;
+ name = this._tableizeModelName(comp.model);
+ name += '.';
+ name += this._columnizePropertyName(comp.field);
if (comp.opts.nocase) {
name = 'LOWER(' + name + ')';
}
View
@@ -25,12 +25,13 @@ comparison.create = function () {
};
ComparisonBase = function () {
- this.initialize = function (field, value, datatype, opts) {
+ this.initialize = function (model, field, value, datatype, opts) {
this.parent = null;
this.descendants = [];
+ // FIXME: Should use Property objects
+ this.model = model;
this.field = field;
this.value = value;
- // FIXME: Should use Property objects
this.datatype = datatype;
this.opts = opts || {};
};
View
@@ -104,11 +104,25 @@ Query.prototype = new (function () {
, _createComparison = function (val, key) {
var type
- , modelName = this.model.modelName
- , props = model.descriptionRegistry[modelName].properties
- , descr = props[key]
+ , keyName = key
+ , keyNameArr
+ , modelName
+ , props
+ , descr
, datatype
- , opts = _createComparisonOpts.apply(this, [key]);
+ , opts = _createComparisonOpts.apply(this, [keyName]);
+
+ if (keyName.indexOf('.') > -1) {
+ keyNameArr = keyName.split('.');
+ modelName = keyNameArr[1];
+ keyName = keyNameArr[0];
+ }
+ else {
+ modelName = this.model.modelName
+ }
+
+ props = model.descriptionRegistry[modelName].properties
+ descr = props[keyName]
// Non-null objects
if (val && typeof val == 'object') {
@@ -130,7 +144,7 @@ Query.prototype = new (function () {
// FIXME: How the fuck to handle IDs?
// id isn't in the defined props
- if (key == 'id') {
+ if (keyName == 'id') {
datatype = 'string';
}
else {
@@ -140,8 +154,8 @@ Query.prototype = new (function () {
// TODO: Validate the value for both the particular field
// (e.g., must be a numeric) and the type of comparison
// (e.g., 'IN' must be a collection, etc)
- return comparison.create(Query.comparisonTypes[type], key, val,
- datatype, opts);
+ return comparison.create(Query.comparisonTypes[type], modelName,
+ keyName, val, datatype, opts);
}
, _createComparisonOpts = function (key) {

0 comments on commit 339788a

Please sign in to comment.