Skip to content

Commit

Permalink
Parse column and table aliases without AS in them
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Jan 18, 2017
1 parent 6937d35 commit 9d04a89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion models/Query/Builder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ component displayname="Builder" accessors="true" {
public Builder function join(
required string table,
required any first,
string operator,
string operator = "=",
string second,
string type = "inner",
boolean where = false
Expand Down
9 changes: 9 additions & 0 deletions models/Query/Grammars/Grammar.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ component displayname="Grammar" accessors="true" {
table = mid( table, matches.pos[2], matches.len[2] );
}
}
else if ( table.findNoCase( " " ) > 0 ) {
alias = listGetAt( table, 2, " " );
table = listGetAt( table, 1, " " );
}

table = table.listToArray( "." ).map( function( tablePart, index ) {
return wrapValue( index == 1 ? getTablePrefix() & tablePart : tablePart );
} ).toList( "." );
Expand All @@ -296,6 +301,10 @@ component displayname="Grammar" accessors="true" {
column = mid( column, matches.pos[2], matches.len[2] );
}
}
else if ( column.findNoCase( " " ) > 0 ) {
alias = listGetAt( column, 2, " " );
column = listGetAt( column, 1, " " );
}
column = column.listToArray( "." ).map( wrapValue ).toList( "." );
return alias == "" ? column : column & " AS " & wrapValue( alias );
}
Expand Down
37 changes: 37 additions & 0 deletions tests/specs/Query/Builder+GrammarSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,43 @@ component extends="testbox.system.BaseSpec" {
} );
} );

describe( "aliases", function() {
describe( "column aliases", function() {
it( "can parse column aliases with AS in them", function() {
var builder = getBuilder();
builder.select( "id AS user_id" ).from( "users" );
expect( builder.toSql() ).toBeWithCase(
"SELECT ""id"" AS ""user_id"" FROM ""users"""
);
} );

it( "can parse column aliases without AS in them", function() {
var builder = getBuilder();
builder.select( "id user_id" ).from( "users" );
expect( builder.toSql() ).toBeWithCase(
"SELECT ""id"" AS ""user_id"" FROM ""users"""
);
} );
} );
describe( "table aliases", function() {
it( "can parse table aliases with AS in them", function() {
var builder = getBuilder();
builder.select( "*" ).from( "users as people" );
expect( builder.toSql() ).toBeWithCase(
"SELECT * FROM ""users"" AS ""people"""
);
} );

it( "can parse table aliases without AS in them", function() {
var builder = getBuilder();
builder.select( "*" ).from( "users people" );
expect( builder.toSql() ).toBeWithCase(
"SELECT * FROM ""users"" AS ""people"""
);
} );
} );
} );

describe( "wheres", function() {
describe( "basic wheres", function() {
it( "can add a where statement", function() {
Expand Down

0 comments on commit 9d04a89

Please sign in to comment.