Friday, June 15, 2007

Using Reactor vs. T-SQL

Here is how you would query a SQL table to return distinct user names. The order by is not needed but it's there to illustrate how to use it in reactor.
<cfquery datasource="UserDSN" name="userQuery">
select   distinct username
from     users
order by username asc
Here is how to do the same with Reactor 1. Create reactor.xml file:
  <project value="wine">
  <dsn value="UserDSN">
  <type value="mssql">
  <mapping value="/UserSample">
  <mode value="development">

2. Create the reactor object with the path to the reactor.xml config file:
<cfset reactor = CreateObject("Component", "reactor.reactorFactory").init(expandPath("reactor.xml")) />
3. Create and execute the query:
<!--- Create a gateway to the table you want to access --->
<cfset usersGateway=Reactor.createGateway("users")>
<!--- Create a query --->
<cfset q=usersGateway.createQuery() />
<!--- Set the table fields to be returned --->
<cfset q.returnObjectField("users", "username") />
<!--- Set the order by (setAsc or setDesc) --->
<cfset q.getOrder().setAsc("users", "username") />
<!--- Specify that only distinct user names should be returned --->
<cfset q.setDistinct(true) />
<!--- Execute the query --->
<cfset userquery=usersGateway.getByQuery(q) />
I realize that the above is a lot of code for a simple query but this is just an example on how to get started.

No comments:

Post a Comment

// //]]>