Skip to content

Commit cda60da

Browse files
author
Justin Reidy
committed
Fix brianc#10: prevent leaking and mutation of column state
1 parent d495ee3 commit cda60da

2 files changed

Lines changed: 18 additions & 3 deletions

File tree

lib/column.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ var unaryMethod = function(name, operator) {
3434
}
3535
}
3636

37+
var contextify = function(base) {
38+
var context = Object.create(Column.prototype);
39+
Object.keys(base).forEach(function (key) {
40+
context[key] = base[key];
41+
})
42+
return context;
43+
}
44+
3745
Column.prototype.value = function(value) {
3846
this._value = value;
3947
return this;
@@ -45,12 +53,13 @@ Column.prototype.getValue = function() {
4553

4654
Column.prototype.toNode = function() {
4755
//creates a query node from this column
48-
return new ColumnNode(this);
56+
return new ColumnNode(contextify(this));
4957
}
5058

5159
Column.prototype.as = function(alias) {
52-
this.alias = alias;
53-
return new ColumnNode(this);
60+
var context = contextify(this);
61+
context.alias = alias;
62+
return new ColumnNode(context);
5463
}
5564

5665
binaryMethod('equals', '=');

test/dialect-tests.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ test({
102102
pg : 'SELECT "user"."name" as "user name" FROM "user" WHERE ("user"."name" = $1)'
103103
});
104104

105+
//Fix #10: prevent column state mutation
106+
test({
107+
query : user.select(user.name).from(user).where(user.name.equals('brian')),
108+
pg : 'SELECT "user"."name" FROM "user" WHERE ("user"."name" = $1)'
109+
});
110+
105111
var u = user.as('u');
106112
test({
107113
query : u.select(u.name).from(u),

0 commit comments

Comments
 (0)