Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Fix for #105
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddentao committed Nov 24, 2014
1 parent fcd413e commit 42b2b85
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion bower.json
@@ -1,6 +1,6 @@
{
"name": "squel",
"version": "3.9.1",
"version": "3.10.0",
"main": "squel.js",
"ignore": [
"**/.*",
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "squel",
"description": "SQL query string builder",
"version": "3.9.1",
"version": "3.10.0",
"author": "Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",
"contributors": [
"Ramesh Nair <ram@hiddentao.com> (http://www.hiddentao.com/)",
Expand Down
7 changes: 6 additions & 1 deletion squel-basic.js
Expand Up @@ -793,6 +793,7 @@ OTHER DEALINGS IN THE SOFTWARE.

function GetFieldBlock(options) {
GetFieldBlock.__super__.constructor.call(this, options);
this._fieldAliases = {};
this._fields = [];
}

Expand Down Expand Up @@ -829,6 +830,10 @@ OTHER DEALINGS IN THE SOFTWARE.
if (alias) {
alias = this._sanitizeFieldAlias(alias);
}
if (this._fieldAliases[field] === alias) {
return;
}
this._fieldAliases[field] = alias;
return this._fields.push({
name: field,
alias: alias
Expand Down Expand Up @@ -1958,7 +1963,7 @@ OTHER DEALINGS IN THE SOFTWARE.
})(cls.QueryBuilder);

squel = {
VERSION: '3.9.1',
VERSION: '3.10.0',
expr: function() {
return new cls.Expression;
},
Expand Down
2 changes: 1 addition & 1 deletion squel-basic.min.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion squel.js
Expand Up @@ -794,6 +794,7 @@ OTHER DEALINGS IN THE SOFTWARE.

function GetFieldBlock(options) {
GetFieldBlock.__super__.constructor.call(this, options);
this._fieldAliases = {};
this._fields = [];
}

Expand Down Expand Up @@ -830,6 +831,10 @@ OTHER DEALINGS IN THE SOFTWARE.
if (alias) {
alias = this._sanitizeFieldAlias(alias);
}
if (this._fieldAliases[field] === alias) {
return;
}
this._fieldAliases[field] = alias;
return this._fields.push({
name: field,
alias: alias
Expand Down Expand Up @@ -1959,7 +1964,7 @@ OTHER DEALINGS IN THE SOFTWARE.
})(cls.QueryBuilder);

squel = {
VERSION: '3.9.1',
VERSION: '3.10.0',
expr: function() {
return new cls.Expression;
},
Expand Down
4 changes: 2 additions & 2 deletions squel.min.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions src/squel.coffee
Expand Up @@ -682,6 +682,7 @@ class cls.IntoTableBlock extends cls.Block
class cls.GetFieldBlock extends cls.Block
constructor: (options) ->
super options
@_fieldAliases = {}
@_fields = []


Expand Down Expand Up @@ -714,6 +715,11 @@ class cls.GetFieldBlock extends cls.Block
field = @_sanitizeField(field, options)
alias = @_sanitizeFieldAlias(alias) if alias

# if field-alias already present then don't add
return if @_fieldAliases[field] is alias

@_fieldAliases[field] = alias

@_fields.push
name: field
alias: alias
Expand Down
24 changes: 24 additions & 0 deletions test/blocks.test.coffee
Expand Up @@ -513,6 +513,30 @@ test['Blocks'] =

assert.same expected, @inst._fields

'field() - discard duplicates':
'saves inputs': ->
@inst.field('field1')
@inst.field('field2', 'alias2')
@inst.field('field2', 'alias2')
@inst.field('field1', 'alias1')

expected = [
{
name: 'field1',
alias: null
},
{
name: 'field2',
alias: '"alias2"'
},
{
name: 'field1',
alias: '"alias1"'
}
]

assert.same expected, @inst._fields

'sanitizes inputs': ->
sanitizeFieldSpy = test.mocker.stub @cls.prototype, '_sanitizeField', -> return '_f'
sanitizeAliasSpy = test.mocker.stub @cls.prototype, '_sanitizeFieldAlias', -> return '_a'
Expand Down

0 comments on commit 42b2b85

Please sign in to comment.