Skip to content

Commit

Permalink
allow returning an array from QueryRowData
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Oct 22, 2020
1 parent 27fd599 commit 34560be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
25 changes: 22 additions & 3 deletions core/src/main/java/lucee/runtime/functions/query/QueryRowData.java
Expand Up @@ -28,13 +28,22 @@
import lucee.runtime.type.Query;
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;
import lucee.runtime.type.Array;
import lucee.runtime.type.ArrayImpl;

/**
* implements BIF QueryRowData
*/
public class QueryRowData extends BIF {

public static Struct call(PageContext pc, Query query, double rowNumber) throws PageException {
// is this needed?
private static final long serialVersionUID = -5234853923691806118L;

public static Object call(PageContext pc, Query query, double rowNumber) throws PageException {
return call(pc, query, rowNumber, "struct");
}

public static Object call(PageContext pc, Query query, double rowNumber, String returnFormat) throws PageException {

int row = Caster.toInteger(rowNumber);

Expand All @@ -43,6 +52,16 @@ public static Struct call(PageContext pc, Query query, double rowNumber) throws

Collection.Key[] colNames = query.getColumnNames();

if (returnFormat != null){
if ("array".equalsIgnoreCase(returnFormat.trim())) {
Array resultArray = new ArrayImpl();
for (int col = 0; col < colNames.length; col++){
resultArray.setE(col, query.getAt(colNames[col], row, NullSupportHelper.empty(pc)));
}
return resultArray;
}
// defaults back to Struct, could check and throw?
}
Struct result = new StructImpl();

for (int col = 0; col < colNames.length; col++)
Expand All @@ -53,7 +72,7 @@ public static Struct call(PageContext pc, Query query, double rowNumber) throws

@Override
public Object invoke(PageContext pc, Object[] args) throws PageException {

return call(pc, Caster.toQuery(args[0]), Caster.toInteger(args[1]));
if (args.length == 2) return call(pc, Caster.toQuery(args[0]), Caster.toDouble(args[1]));
else return call(pc, Caster.toQuery(args[0]), Caster.toInteger(args[1]), Caster.toString(args[2]));
}
}
10 changes: 9 additions & 1 deletion core/src/main/java/resource/fld/core-base.fld
Expand Up @@ -9571,9 +9571,17 @@ You can find a list of all available timezones in the Lucee administrator (Setti
<type>number</type>
<required>Yes</required>
<description>position of the row to be returned</description>
</argument>
<argument>
<name>format</name>
<type>string</type>
<required>No</required>
<description>struct or array</description>
<default>struct</default>
<alias>returnFormat</alias>
</argument>
<return>
<type>struct</type>
<type>any</type>
</return>
</function>

Expand Down

0 comments on commit 34560be

Please sign in to comment.