User Guide Cancel

QueryMap

 

Note:

You can also set the maximum thread count in ColdFusion Administrator. Click Server Settings > Settings and specify the number of threads in Default Maximum Thread Count For Parallel Functions.

Description

This function iterates over each row of a query and calls the closure function to work on row of the query. The returned value will be set at the same index in a new query or the provided result query.

Returns

Modified query.

Category

Query functions

History

ColdFusion (2021 release): Introduced the following parameters:

  • parallel
  • maxThreadCount

ColdFusion (2018 release): Introduced named parameters.

New in Adobe ColdFusion (2016 release)

See also

QueryReduce

Syntax

queryMap(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])
queryMap(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])
queryMap(query, function(row [, currentRow] [, query] ){} [, parallel] [, maxThreadCount])

Parameters

Parameter

Description

query

(Required) The query to be iterated over.

closure

(Required) Map function to be called with each row of the query.

parallel

(Optional) True if you want to enable parallel programming.

maxThreadCount

(Optional) The number of threads the function can execute. The number of threads must be between 1-50. If the value exceeds 50, there is an exception.

template

(Optional) Define the schema of the returned query.

Example 1

<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32}
]);
qmap=QueryMap(myQuery,function(obj){
if (obj.id>1){
obj.name="Hello " & obj.name
return obj
}
else{
obj.name="Hi " & obj.name
return obj
}
})
writeOutput("The new query is:")
writeDump(qmap)
</cfscript>
<cfscript> myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", [ {id=1,name="One",amount=15}, {id=2,name="Two",amount=18}, {id=3,name="Three",amount=32} ]); qmap=QueryMap(myQuery,function(obj){ if (obj.id>1){ obj.name="Hello " & obj.name return obj } else{ obj.name="Hi " & obj.name return obj } }) writeOutput("The new query is:") writeDump(qmap) </cfscript>
<cfscript>
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", 
                [ 
                        {id=1,name="One",amount=15}, 
                        {id=2,name="Two",amount=18}, 
                        {id=3,name="Three",amount=32} 
                ]); 
    qmap=QueryMap(myQuery,function(obj){
        if (obj.id>1){
            obj.name="Hello " & obj.name
            return obj
        }
        else{
            obj.name="Hi " & obj.name
            return obj
        }
    })
    writeOutput("The new query is:")
    writeDump(qmap)
</cfscript>

Output

Example 2

<cfscript>
qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);
function mapQuery(any Obj){
if(Obj.ISSPOTLIGHT == "Y")
Obj.TITLE = "NEW: " & Obj.TITLE;
return Obj;
}
newQuery = QueryMap(sampleQuery, mapQuery);
writedump(newQuery);
</cfscript>
<cfscript> qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"}; sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions); function mapQuery(any Obj){ if(Obj.ISSPOTLIGHT == "Y") Obj.TITLE = "NEW: " & Obj.TITLE; return Obj; } newQuery = QueryMap(sampleQuery, mapQuery); writedump(newQuery); </cfscript>
<cfscript>
               qoptions = {result="myresult", datasource="cfbookclub", fetchclientinfo="yes"};
               sampleQuery = QueryExecute("select * from books order by bookid", [] ,qoptions);

               function mapQuery(any Obj){
                              if(Obj.ISSPOTLIGHT == "Y")
                                             Obj.TITLE = "NEW: " & Obj.TITLE;
                              return Obj;
               }

               newQuery = QueryMap(sampleQuery, mapQuery);
               writedump(newQuery);
</cfscript>

The script prefixes “NEW” on Titles that have Spotlight set to “Y”.

Using member function

<cfscript>
myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
myArray=ArrayNew(1);
// Populate myArrray with values "NO"
for (i=1;i<=myResult.recordcount;i++){
myArray[i]="NO";
}
// Add column InSanFrancisco and populate with values of myArray
QueryAddColumn(myResult,"InSanFrancisco","varchar",myArray);
// Iterate through the array and replace values of InSanFrancisco where location is San Francisco
status=myResult.map(function (col){
if (col.LOCATION=="San Francisco")
col.InSanFrancisco = "YES";
return col;
});
WriteDump(status);
</cfscript>
<cfscript> myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"}); myArray=ArrayNew(1); // Populate myArrray with values "NO" for (i=1;i<=myResult.recordcount;i++){ myArray[i]="NO"; } // Add column InSanFrancisco and populate with values of myArray QueryAddColumn(myResult,"InSanFrancisco","varchar",myArray); // Iterate through the array and replace values of InSanFrancisco where location is San Francisco status=myResult.map(function (col){ if (col.LOCATION=="San Francisco") col.InSanFrancisco = "YES"; return col; }); WriteDump(status); </cfscript>
<cfscript>
       myResult=QueryExecute("SELECT * FROM EMPLOYEES",[],{datasource="cfdocexamples"});
       myArray=ArrayNew(1);
       
       // Populate myArrray with values "NO"
       for (i=1;i<=myResult.recordcount;i++){
             myArray[i]="NO";
             
       }
       // Add column InSanFrancisco and populate with values of myArray
       QueryAddColumn(myResult,"InSanFrancisco","varchar",myArray);
       // Iterate through the array and replace values of InSanFrancisco where location is San Francisco
       status=myResult.map(function (col){
             if (col.LOCATION=="San Francisco")
                    col.InSanFrancisco = "YES";
                    return col;
             
       });
       WriteDump(status);
</cfscript>

Using parallelization

<cfscript>
myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",
[
{id=1,name="One",amount=15},
{id=2,name="Two",amount=18},
{id=3,name="Three",amount=32}
]);
qmap=QueryMap(myQuery,function(obj){
if (obj.id>1){
obj.name="Hello " & obj.name
return obj
}
else{
obj.name="Hi " & obj.name
return obj
}
})
writeOutput("The new query is:")
writeDump(qmap)
</cfscript>
<cfscript> myQuery = queryNew("id,name,amount","Integer,Varchar,Integer", [ {id=1,name="One",amount=15}, {id=2,name="Two",amount=18}, {id=3,name="Three",amount=32} ]); qmap=QueryMap(myQuery,function(obj){ if (obj.id>1){ obj.name="Hello " & obj.name return obj } else{ obj.name="Hi " & obj.name return obj } }) writeOutput("The new query is:") writeDump(qmap) </cfscript>
<cfscript> 
    myQuery = queryNew("id,name,amount","Integer,Varchar,Integer",  
                [  
                        {id=1,name="One",amount=15},  
                        {id=2,name="Two",amount=18},  
                        {id=3,name="Three",amount=32}  
                ]);  
    qmap=QueryMap(myQuery,function(obj){ 
        if (obj.id>1){ 
            obj.name="Hello " & obj.name 
            return obj 
        } 
        else{ 
            obj.name="Hi " & obj.name 
            return obj 
        } 
    }) 
    writeOutput("The new query is:") 
    writeDump(qmap) 
</cfscript>

Get help faster and easier

New user?