How can I create a thread safe query of query using only cfscript?
Using the new script based query functions in ColdFusion 9 we are now easily able to accomplish this.
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();
}
}
+