Skip to content
Permalink
Browse files
fix(BaseGrammar): Improve column wrapping with trimming
The BaseGrammar (which the other grammars inherit from) now
trims string columns before attempting to parse aliases. This
will prevent things like `.select( "foo " )` from being
parsed as an alias and blowing up.
  • Loading branch information
elpete committed Feb 28, 2020
1 parent 29b31d0 commit d98a5cb65851c154b6755e90254d1a2c1df82833
Showing 1 changed file with 14 additions and 13 deletions.
@@ -800,34 +800,35 @@ component displayname="Grammar" accessors="true" singleton {
public string function wrapColumn( required any column ) {
// In this case, isInstanceOf takes ~30 ms while this takes ~0 ms
if (
!isSimpleValue( column ) &&
isObject( column ) &&
structKeyExists( column, "getSQL" )
!isSimpleValue( arguments.column ) &&
isObject( arguments.column ) &&
structKeyExists( arguments.column, "getSQL" )
) {
return column.getSQL();
return trim( arguments.column.getSQL() );
}

arguments.column = trim( arguments.column );
var alias = "";
if ( column.findNoCase( " as " ) > 0 ) {
if ( arguments.column.findNoCase( " as " ) > 0 ) {
var matches = reFindNoCase(
"(.*)(?:\sAS\s)(.*)",
column,
arguments.column,
1,
true
);
if ( matches.pos.len() >= 3 ) {
alias = mid( column, matches.pos[ 3 ], matches.len[ 3 ] );
column = mid( column, matches.pos[ 2 ], matches.len[ 2 ] );
alias = mid( arguments.column, matches.pos[ 3 ], matches.len[ 3 ] );
arguments.column = mid( arguments.column, matches.pos[ 2 ], matches.len[ 2 ] );
}
} else if ( column.findNoCase( " " ) > 0 ) {
alias = listGetAt( column, 2, " " );
column = listGetAt( column, 1, " " );
} else if ( arguments.column.findNoCase( " " ) > 0 ) {
alias = listGetAt( arguments.column, 2, " " );
arguments.column = listGetAt( arguments.column, 1, " " );
}
column = column
arguments.column = arguments.column
.listToArray( "." )
.map( wrapValue )
.toList( "." );
return alias == "" ? column : column & " AS " & wrapValue( alias );
return alias == "" ? arguments.column : arguments.column & " AS " & wrapValue( alias );
}

/**

0 comments on commit d98a5cb

Please sign in to comment.