Browse Source

Merge pull request #11 from jmreidy/Fix_column_state

Fix #10: prevent leaking and mutation of column state
auto-join
Brian C 12 years ago
parent
commit
87e46d2aab
  1. 15
      lib/column.js
  2. 6
      test/dialect-tests.js

15
lib/column.js

@ -34,6 +34,14 @@ var unaryMethod = function(name, operator) {
}
}
var contextify = function(base) {
var context = Object.create(Column.prototype);
Object.keys(base).forEach(function (key) {
context[key] = base[key];
})
return context;
}
Column.prototype.value = function(value) {
this._value = value;
return this;
@ -45,12 +53,13 @@ Column.prototype.getValue = function() {
Column.prototype.toNode = function() {
//creates a query node from this column
return new ColumnNode(this);
return new ColumnNode(contextify(this));
}
Column.prototype.as = function(alias) {
this.alias = alias;
return new ColumnNode(this);
var context = contextify(this);
context.alias = alias;
return new ColumnNode(context);
}
binaryMethod('equals', '=');

6
test/dialect-tests.js

@ -102,6 +102,12 @@ test({
pg : 'SELECT "user"."name" as "user name" FROM "user" WHERE ("user"."name" = $1)'
});
//Fix #10: prevent column state mutation
test({
query : user.select(user.name).from(user).where(user.name.equals('brian')),
pg : 'SELECT "user"."name" FROM "user" WHERE ("user"."name" = $1)'
});
var u = user.as('u');
test({
query : u.select(u.name).from(u),

Loading…
Cancel
Save