Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions lib/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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', '=');
Expand Down
6 changes: 6 additions & 0 deletions test/dialect-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down