Skip to content

Commit

Permalink
fix(QueryBuilder): Have aliases work with full server qualifications
Browse files Browse the repository at this point in the history
  • Loading branch information
elpete committed Feb 3, 2025
1 parent cbc6ff0 commit 4ee1bb9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
10 changes: 7 additions & 3 deletions models/Query/QueryBuilder.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -776,10 +776,14 @@ component displayname="QueryBuilder" accessors="true" {
}

private string function swapAlias( required string column, required string oldAlias, required string newAlias ) {
if ( listFirst( arguments.column, "." ) != arguments.oldAlias ) {
return arguments.column;
if ( startsWith( arguments.column, arguments.oldAlias ) ) {
return arguments.newAlias & "." & listLast( arguments.column, "." );
}
return arguments.newAlias & "." & listRest( arguments.column, "." );
return arguments.column;
}

private boolean function startsWith( required string word, required string substring ) {
return left( arguments.word, len( arguments.substring ) ) == arguments.substring;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions tests/specs/Query/Abstract/BuilderAliasSpec.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ component extends="testbox.system.BaseSpec" {
qb.withAlias( "u" );
expect( qb.getColumns() ).toBe( [ "u.id", "u.name" ] );
} );

it( "renames aliases with multiple periods", () => {
var qb = new qb.models.Query.QueryBuilder();
qb.from( "MyServer.dbo.users" ).select( [ "MyServer.dbo.users.id", "MyServer.dbo.users.name" ] );
expect( qb.getColumns() ).toBe( [ "MyServer.dbo.users.id", "MyServer.dbo.users.name" ] );
qb.withAlias( "u1" );
expect( qb.getColumns() ).toBe( [ "u1.id", "u1.name" ] );
} );

it( "renames aliases with schema shortcut periods", () => {
var qb = new qb.models.Query.QueryBuilder();
qb.from( "MyServer..users" ).select( [ "MyServer..users.id", "MyServer..users.name" ] );
expect( qb.getColumns() ).toBe( [ "MyServer..users.id", "MyServer..users.name" ] );
qb.withAlias( "u1" );
expect( qb.getColumns() ).toBe( [ "u1.id", "u1.name" ] );
} );
} );

describe( "wheres", () => {
Expand Down

0 comments on commit 4ee1bb9

Please sign in to comment.