From b073d2bafb75b8643521f7b71bca0cd8b6dadefe Mon Sep 17 00:00:00 2001 From: Jacob Beers Date: Thu, 4 Jan 2024 13:25:17 -0600 Subject: [PATCH] Update QueryColumn assignment logic --- .../boxlang/runtime/dynamic/Referencer.java | 4 +- .../boxlang/runtime/types/QueryColumn.java | 8 ++-- src/test/java/TestCases/phase2/QueryTest.java | 44 +++++++++++++++++++ 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/main/java/ortus/boxlang/runtime/dynamic/Referencer.java b/src/main/java/ortus/boxlang/runtime/dynamic/Referencer.java index 0f52b6261..3934df761 100644 --- a/src/main/java/ortus/boxlang/runtime/dynamic/Referencer.java +++ b/src/main/java/ortus/boxlang/runtime/dynamic/Referencer.java @@ -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; @@ -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(), diff --git a/src/main/java/ortus/boxlang/runtime/types/QueryColumn.java b/src/main/java/ortus/boxlang/runtime/types/QueryColumn.java index a7f86d6ae..e68684fbb 100644 --- a/src/main/java/ortus/boxlang/runtime/types/QueryColumn.java +++ b/src/main/java/ortus/boxlang/runtime/types/QueryColumn.java @@ -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 @@ -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; } diff --git a/src/test/java/TestCases/phase2/QueryTest.java b/src/test/java/TestCases/phase2/QueryTest.java index a50da8a63..282e85f53 100644 --- a/src/test/java/TestCases/phase2/QueryTest.java +++ b/src/test/java/TestCases/phase2/QueryTest.java @@ -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" ); + } + } \ No newline at end of file