Skip to content

Commit

Permalink
Little query fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdw429s committed Jan 4, 2024
1 parent 3717741 commit 6601572
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 27 deletions.
9 changes: 5 additions & 4 deletions src/main/java/ortus/boxlang/runtime/types/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -256,13 +256,15 @@ public int addRow( Array row ) {
*/
public int addRow( Struct row ) {
Object[] rowData = new Object[ columns.size() ];
// TODO: Check for missing columns?
// TODO: validate types
int i = 0;
Object o;
for ( QueryColumn column : columns.values() ) {
rowData[ i ] = row.get( column.getName() );
// Missing keys in the struct go in the queyr as an empty string (CF compat)
rowData[ i ] = ( o = row.get( column.getName() ) ) == null ? "" : o;
i++;
}
// We're ignoring extra keys in the struct that aren't query columns. Lucee compat, but not CF compat.
return addRow( rowData );
}

Expand Down Expand Up @@ -475,8 +477,7 @@ public Object dereferenceAndInvoke( IBoxContext context, Key name, Map<Key, Obje

@Override
public Object assign( IBoxContext context, Key name, Object value ) {
// TODO: get row index from context based on if in cfloop/cfoutput query="..."
getColumn( name ).setCell( 0, value );
getColumn( name ).setCell( getRowFromContext( context ), value );
return value;
}

Expand Down
33 changes: 10 additions & 23 deletions src/main/java/ortus/boxlang/runtime/types/QueryColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,6 @@ public QueryColumn setCell( int row, Object value ) {
return this;
}

/**
* Get the value of a cell in this column
*
* @param row The row to get, 0-based index
* @param safe If true, return empty string if query is empty
*
* @return The value of the cell
*/
public Object getCell( int row, boolean safe ) {
if ( safe && query.isEmpty() ) {
return "";
}
return this.query.getData().get( row )[ index ];
}

/**
* Get the value of a cell in this column
*
Expand All @@ -119,7 +104,7 @@ public Object getCell( int row, boolean safe ) {
* @return The value of the cell
*/
public Object getCell( int row ) {
return getCell( row, false );
return getCell( row );
}

/**
Expand Down Expand Up @@ -188,9 +173,11 @@ public static int getIntFromKey( Key key, boolean safe ) {
public Object dereference( IBoxContext context, Key name, Boolean safe ) {

// Special check for $bx
if ( name.equals( BoxMeta.key ) ) {
return getBoxMeta();
}
/*
* if ( name.equals( BoxMeta.key ) ) {
* return getBoxMeta();
* }
*/

// Check if the key is numeric
int index = getIntFromKey( name, true );
Expand All @@ -200,22 +187,22 @@ public Object dereference( IBoxContext context, Key name, Boolean safe ) {
}

// If dereferncing a query column with a NON number like qry.col["key"], then we get the value at the "current" row and dererence it
return Referencer.get( context, getCell( query.getRowFromContext( context ), true ), name, safe );
return Referencer.get( context, getCell( query.getRowFromContext( context ) ), name, safe );

}

@Override
public Object dereferenceAndInvoke( IBoxContext context, Key name, Object[] positionalArguments, Boolean safe ) {
// qry.col.method() will ALWAYS get the value from the current row and call the method on that cell value
// Unlike Lucee/Adobe, we'll never call the method on the query column itself
return DynamicJavaInteropService.invoke( getCell( query.getRowFromContext( context ), true ), name.getName(), safe, positionalArguments );
return DynamicJavaInteropService.invoke( getCell( query.getRowFromContext( context ) ), name.getName(), safe, positionalArguments );
}

@Override
public Object dereferenceAndInvoke( IBoxContext context, Key name, Map<Key, Object> namedArguments, Boolean safe ) {
// qry.col.method() will ALWAYS get the value from the current row and call the method on that cell value
// Unlike Lucee/Adobe, we'll never call the method on the query column itself
return DynamicJavaInteropService.invoke( getCell( query.getRowFromContext( context ), true ), name.getName(), safe, namedArguments );
return DynamicJavaInteropService.invoke( getCell( query.getRowFromContext( context ) ), name.getName(), safe, namedArguments );
}

@Override
Expand All @@ -231,7 +218,7 @@ public Object assign( IBoxContext context, Key name, Object value ) {

// If dereferencing a query column with a NON number like qry.col["key"]="new value",
// then we get the value at the "current" row and assign it (perhaps it's struct etc)
Referencer.set( context, getCell( query.getRowFromContext( context ), true ), name, value );
Referencer.set( context, getCell( query.getRowFromContext( context ) ), name, value );
return value;
}

Expand Down

0 comments on commit 6601572

Please sign in to comment.