Skip to content

Commit

Permalink
Update QueryColumn assignment logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeers committed Jan 4, 2024
1 parent ab678e9 commit b073d2b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/main/java/ortus/boxlang/runtime/dynamic/Referencer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import ortus.boxlang.runtime.interop.DynamicObject;
import ortus.boxlang.runtime.scopes.Key;
import ortus.boxlang.runtime.types.Array;
import ortus.boxlang.runtime.types.Query;
import ortus.boxlang.runtime.types.QueryColumn;
import ortus.boxlang.runtime.types.Struct;
import ortus.boxlang.runtime.types.exceptions.BoxRuntimeException;

Expand Down Expand Up @@ -149,7 +151,7 @@ public static Object setDeep( IBoxContext context, Object object, Object value,
next = new Struct();
set( context, object, key, next );
// If it's not null, it needs to be a Map
} else if ( ! ( next instanceof Map || next instanceof Array ) ) {
} else if ( ! ( next instanceof Map || next instanceof Array || next instanceof Query || next instanceof QueryColumn ) ) {
throw new BoxRuntimeException(
String.format( "Cannot assign to key [%s] because it is a [%s] and not a Struct or Array",
key.getName(),
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/ortus/boxlang/runtime/types/QueryColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ public Object dereference( IBoxContext context, Key name, Boolean safe ) {
// Check if the key is numeric
int index = getIntFromKey( name, true );
// If dereferncing a query column with a number like qry.col[1], then we ALWAYS get the value from that row
if ( index < 0 ) {
return getCell( index );
if ( index > 0 ) {
return getCell( index - 1 );
}

// 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
Expand Down Expand Up @@ -215,8 +215,8 @@ public Object assign( IBoxContext context, Key name, Object value ) {
// Check if the key is numeric
int index = getIntFromKey( name, true );
// If assign a query column with a number like qry.col[1]='new value', then we ALWAYS get the value from that row
if ( index < 0 ) {
setCell( index, value );
if ( index > 0 ) {
setCell( index - 1, value );
return value;
}

Expand Down
44 changes: 44 additions & 0 deletions src/test/java/TestCases/phase2/QueryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,48 @@ public void testQuery() {
assertThat( variables.get( Key.of( "colList" ) ) ).isEqualTo( "col1,col2" );
}

@DisplayName( "Query Column assignemtn" )
@Test
public void testColumnAssignemtnQuery() {

instance.executeSource(
"""
import java:ortus.boxlang.runtime.scopes.Key;
import java:ortus.boxlang.runtime.types.QueryColumnType;
import java:ortus.boxlang.runtime.types.Query;
qry = new java:Query();
qry.addColumn( Key.of( "col1" ), QueryColumnType.VARCHAR )
qry.addColumn( Key.of( "col2" ), QueryColumnType.INTEGER )
qry.addRow( [ "brad", 1000 ] )
qry.addRow( [ "luis", 2000 ] )
recordCount = qry.recordCount
i=0
println( qry.recordCount )
colavg = arrayAvg( qry.col2 )
collen = len( qry.col2 )
colList = qry.columnList;
for( row in qry ) {
variables[ "row#++i#" ]=row
println( row )
println( qry.col1 )
println( qry.col2 )
println( qry.currentRow )
}
qry.col1[1] = "test"
firstCol = qry.col1;
result = qry
""",
context );
assertThat( variables.getAsQuery( result ) instanceof Query ).isEqualTo( true );
assertThat( variables.getAsInteger( Key.of( "recordcount" ) ) ).isEqualTo( 2 );
assertThat( variables.getAsStruct( Key.of( "row1" ) ) instanceof Struct ).isEqualTo( true );
assertThat( variables.getAsStruct( Key.of( "row2" ) ) instanceof Struct ).isEqualTo( true );
assertThat( variables.getAsString( Key.of( "firstCol" ) ) ).isEqualTo( "test" );
assertThat( variables.get( Key.of( "colavg" ) ) ).isEqualTo( 1500 );
assertThat( variables.get( Key.of( "collen" ) ) ).isEqualTo( 4 );
assertThat( variables.get( Key.of( "colList" ) ) ).isEqualTo( "col1,col2" );
}

}

0 comments on commit b073d2b

Please sign in to comment.