diff --git a/lib/column.js b/lib/column.js index bf58fdd4..f94f0540 100644 --- a/lib/column.js +++ b/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', '='); diff --git a/test/dialect-tests.js b/test/dialect-tests.js index 0162913a..6e760945 100644 --- a/test/dialect-tests.js +++ b/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),