The function to invoke for each item. The function will be passed 3 arguments: the query row as a struct, the row number, the query. You can alternatively pass a Java Predicate which will only receive the 1st arg.
parallel
boolean
false
false
maxThreads
integer
false
Examples
Filter a query
news = queryNew( "id,type,title", "integer,varchar,varchar" );
queryAddRow( news, [
{
ID : 1,
TYPE : "book",
TITLE : "Cloud Atlas"
},
{
ID : 2,
TYPE : "book",
TITLE : "Lord of The Rings"
},
{
ID : 3,
TYPE : "film",
TITLE : "Men in Black"
}
] );
books = queryFilter( news, ( Any _news ) => {
return _news.TYPE == "book";
} );
writeDump( queryColumnData( books, "title" ).toList( ", " ) );
Result: Cloud Atlas, Lord of The Rings
Filter a query as member function
news = queryNew( "id,type,title", "integer,varchar,varchar" );
queryAddRow( news, [
{
ID : 1,
TYPE : "book",
TITLE : "Cloud Atlas"
},
{
ID : 2,
TYPE : "book",
TITLE : "Lord of The Rings"
},
{
ID : 3,
TYPE : "film",
TITLE : "Men in Black"
}
] );
books = news.filter( ( Any _news ) => {
return _news.TYPE == "book";
} );
writeDump( queryColumnData( books, "title" ).toList( ", " ) );
Result: Cloud Atlas, Lord of The Rings
Additional Examples
people = QueryNew( "name,dob", "varchar,date", [
[
"Susi",
CreateDate( 1970, 1, 1 ),
0
],
[
"Urs",
CreateDate( 1995, 1, 1 ),
0
],
[
"Fred",
CreateDate( 1960, 1, 1 ),
0
],
[
"Jim",
CreateDate( 1988, 1, 1 ),
0
],
[
"Bob",
CreateDate( 1988, 1, 1 ),
0
]
] );
Dump( var=people, label="people - original query" );
/* Output:
*
* | name | dob |
* ------------------------------
* | Susi | 1970-01-01 00:00:00 |
* | Urs | 1995-01-01 00:00:00 |
* | Fred | 1960-01-01 00:00:00 |
* | Jim | 1988-01-01 00:00:00 |
* | Bob | 1988-01-01 00:00:00 |
*/
// filter - born in 1988
qryPeopleBornIn1988 = people.filter( ( Any row, Any rowNumber, Any qryData ) => {
return Year( row.DOB ) == 1988;
} );
dump( var=qryPeopleBornIn1988, label="qryPeopleBornIn1988 - Born in 1988" );
/* Output:
*
* | name | dob |
* ------------------------------
* | Jim | 1988-01-01 00:00:00 |
* | Bob | 1988-01-01 00:00:00 |
*/
<bx:script>
q = QueryNew( "name, description" );
bx:loop times=3 {
getFunctionList().each( ( Any f ) => {
var fd = getFunctionData( arguments.F );
var r = QueryAddRow( q );
QuerySetCell( q, "name", fd.NAME, r );
QuerySetCell( q, "description", fd.DESCRIPTION, r );
} );
}
dump( var=q.RECORDCOUNT, label="demo data set size" );
s = "the";
</bx:script>
<bx:timer type="outline" label="Query of Query">
<bx:query dbtype="query" name="q1">
select name, description
from q
where description like <bx:queryparam value="%#s#%" sqltype="varchar">
</bx:query>
</bx:timer>
<bx:dump var="#q1.RECORDCOUNT#">
<bx:timer type="outline" label="query.filter() with scoped variables">
<bx:script>
q2 = q.filter( ( Any row ) => {
return (arguments.ROW.DESCRIPTION contains s);
} );
</bx:script>
</bx:timer>
<bx:dump var="#q2.RECORDCOUNT#">
<bx:timer type="outline" label="query.filter() without unscoped variables">
<bx:script>
q3 = q.filter( ( Any row ) => {
return (row.DESCRIPTION contains s);
} );
</bx:script>
</bx:timer>
<bx:dump var="#q3.RECORDCOUNT#">