Avg. Rating 5.0

Problem

How can I create a thread safe query of query using only cfscript?

Solution

Using the new script based query functions in ColdFusion 9 we are now easily able to accomplish this.

Detailed explanation

In ColdFusion 9 there is now support for script based queries.  Not only can we now do queries in cfscript we can also do queries of queries.  This also utilizes the local scope to make the queries thread safe. This example shows how to do this by calling a CFC. 

 

CFM code to call CFC:

<cfscript>
    theQryObj = CreateObject('component', 'qryExamples');  
    writedump(theQryObj.doRunQuery());
</cfscript>

CFC code (qryExamples.cfc):

component  output="false"
{

    // dsn for cfc
    this.DSN = 'yourDSN';

   
    public any function doRunQuery()
     output="false"
    {
        // run query
        local.myQry = new Query(datasource="#this.DSN#",
sql='select * from myTable').execute();
       
        // set local var to result of 1st query
        local.qry1Result = local.myQry.getResult();
       
        // create new query object
        local.qoq = new Query();
       
        // set attribute of new query object to be result of first
query
        // the attribute of "QoQsrcTable" is arbortrary.  It can be
anything you want.
        local.qoq.setAttributes(QoQsrcTable = local.qry1Result);
       
        // use previously set attribute as table name for QoQ and
set dbtype = query
        local.qry2Result = local.qoq.execute(sql="select * from
QoQsrcTable where someColumn = 'someValue'", dbtype="query");
       
        // return result
        return local.qry2Result.getResult();
       
    }

}

 

 


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes