| By Charlie Arehart | Article Rating: |
|
| September 17, 2003 12:00 AM EDT | Reads: |
9,826 |
This Issue:
Dynamic SQL Queries By Mike Chorost
Listing Session and Application Variables by Tad Murphy
Using Structures to Delete a Session Variable by Charles Arehart
Joining Multiple Tables: The Correct Syntax by Charles Arehart
Emulating a "Words" Function by Charles Arehart
By Mike Chorost
Let's say you want to create a dynamic SQL query -- that is, a CFQUERY that looks up different fields depending on circumstances. Why would this be useful? Let's say you're creating an employee roster application. You want it to show full information to employees (such as first name, last name, phone number, email address, etc.) but only partial information to outsiders (such as the above fields minus phone.) Such a thing is simple enough to code when you have only four fields and two levels of access, but what if you have two dozen fields and six levels of access? Then coding separate CFQUERY solutions for each possible combination becomes a nightmare.
The solution is to create a CFQUERY where the SELECT line is generated dynamically.
Let's start with what non-dynamic code would look like. Here is a simple CFQUERY which looks up fields in the way we all know and love:
<CFQUERY Datasource = "xyz" Name = "GetRosterData">
SELECT FirstName, LastName, Phone, Email
FROM Employees
</CFQUERY>
Dynamic code, on the other hand, would look like this:
<CFQUERY Datasource = "xyz" Name = "GetRosterData">
SELECT #evaluate(de(QueryString))#
FROM Employees
</CFQUERY>
QueryString is a list variable, e.g. "FirstName, LastName, Phone, Email." The code #evaluate(de(QueryString))# evaluates QueryString in such a way that ColdFusion thinks it's getting QueryString itself. It's a bloomin' miracle!
You can easily set QueryString to list whatever fields you need for a particular circumstance. You can, of course, set it manually:
<CFSET QueryString = "FirstName, LastName, Phone, Email, Address1, Address2, City, State, Zip">
Even better, you can generate QueryString dynamically. Let's say you've got a table named "PermittedFields" that says which access levels get to see which fields. Then you can generate QueryString in a two-step operation. You run a query that returns what fields the user gets to see:
<CFQUERY Datasource = "xyz" Name = "GetPermittedFields">
SELECT FieldName FROM PermittedFields
WHERE AccessLevel=#Session.AccessLevel#
</CFQUERY>
Then you initialize QueryString and CFLOOP the field names into it:
<CFSET QueryString = "">
<CFLOOP Query = "GetPermittedFields">
<CFSET QueryString = ListAppend(QueryString, "#FieldName#",",") >
</CFLOOP>
And there you have QueryString freshly minted and ready to go.
This is a simple but powerful solution to the problem of needing CFQUERY code that covers a multitude of possible cases.
By Tad Murphy
In Cold Fusion 4.0, Session and Application variables are registered as a structure. This means that you can use any of the Struct functions to gain access to the names and values of these variables. Make sure you are using CFLOCK around all your session and application variable accesses, as these are shared requests. If we fail to use CFLOCK, some other template could update the variables while we are attempting to read them. The CFLOCK will prevent that.
Below is an example of using CFLOOP to access the Session Variables structure, followed by the runtime results of the code. NOTE: This code will not work unless Session management has been "enabled" in the Cold Fusion Administrator and "turned on" with a CFAPPLICATION tag in the page or the application.
First we set our Session Variables
<CFSET Session.test1 = "testvalue1">
<CFSET Session.test2 = "testvalue2">
<CFSET Session.test3 = "testvalue3">
<CFSET Session.test4 = "testvalue4">
Then we loop over our "session" collection
Session Variables
<CFLOCK Timeout = "30" Name = "getSessionVars">
<CFOUTPUT>
<CFLOOP Item = "name" Collection = #session#>
#name# = #session[name]#
</CFLOOP>
</CFOUTPUT>
</CFLOCK>
Runtime Results
Session Variables
TEST1 = testvalue1
TEST2 = testvalue2
TEST3 = testvalue3
TEST4 = testvalue4
By Charles Arehart
In addition to making it easy to refer to session and application variables, using structures also provides a simple and straightforward means to delete such a variable. The StructDelete function does the trick.
The specific format of the StructDelete would be to name the variable type (session or application) as the first parameter, without quotes, and to name the specific variable in the second parameter, in quotes. For example, the following deletes the session variable "login":
<CFSET Foo = StructDelete(session,'login')>>
This isn't too obvious from the reference manual, because it doesn't actually explain what should and shouldn't be quoted, and it speaks in terms of structures and their keys. In our example, session is the structure and "login" is the key.
By Charles Arehart
If you've tried to join more than one table in SQL, you may have encountered a challenge in figuring out the correct syntax. Here is a three-table join in the two different forms of join syntax: the first uses the JOIN keywords in the FROM clause while the second defines the joins in the WHERE clause.
Either approach will work in either Access or SQL Server.
SELECT *
FROM (tablea INNER JOIN tableb ON tablea.columna = tableb.columna)
INNER JOIN tablec ON tableb.columnb = tablec.columnb
Note that the challenge is in arranging the parentheses and where you put things relative to them. When using the JOIN clause, the first join is enclosed in parentheses and lists the first table name before its INNER JOIN keyword. The second join has no parentheses and DOES NOT list a table before the INNER JOIN. Instead, the first join can be thought of as the "table" on which the second join is made.
Using the simpler syntax, the join would be:
SELECT *
FROM tablea, tableb, tablec
WHERE tablea.columna = tableb.columna AND tableb.columnb = tablec.columnb
You could join more tables, but the performance impact should be seriously considered.
By Charles Arehart
Though CF provides many string functions, one common one that it doesn't offer is a "words" function, to count the number of words in a string.
This can be easily emulated, with:
<CFSET words = ListLen ("string to count", " ")>
For the above CFSET, "words" would be set to 3. Kind of nifty.
Published September 17, 2003 Reads 9,826
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Charlie Arehart
A veteran ColdFusion developer since 1997, Charlie Arehart is a long-time contributor to the community and a recognized Adobe Community Expert. He's a certified Advanced CF Developer and Instructor for CF 4/5/6/7 and served as tech editor of CFDJ until 2003. Now an independent contractor (carehart.org) living in Alpharetta, GA, Charlie provides high-level troubleshooting/tuning assistance and training/mentoring for CF teams. He helps run the Online ColdFusion Meetup (coldfusionmeetup.com, an online CF user group), is a contributor to the CF8 WACK books by Ben Forta, and is frequently invited to speak at developer conferences and user groups worldwide.
- Oracle To Keynote Cloud Computing Expo
- Contrary Opinion: Why Silverlight is Good for Adobe
- Analytics for Adobe Air Applications
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Is Microsoft as Free as Open Source?
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- The Planet Named “Bronze Sponsor” of Cloud Computing Expo
- Adobe Reader Sued
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Adobe Enters Cloud Computing with LiveCycle
- Oracle To Keynote Cloud Computing Expo
- Social Media Terrorists
- Adobe Flash Media Server on iPhone
- Contrary Opinion: Why Silverlight is Good for Adobe
- Adobe Flash Based GetJar Surpasses a Half Billion Downloads
- Adobe ColdFusion 9 and ColdFusion Builder Public Betas Now Available
- Adobe Tries Commercializing Its Online Software
- Adobe Open Sources Flash Initiatives
- The Next Programming Models, RIAs and Composite Applications
- Where Are RIA Technologies Headed in 2008?
- Constructing an Application with Flash Forms from the Ground Up
- AJAX World RIA Conference & Expo Kicks Off in New York City
- CFEclipse: The Developer's IDE, Eclipse For ColdFusion
- Personal Branding Checklist
- Adobe Flex 2: Advanced DataGrid
- Has the Technology Bounceback Begun?
- Building a Zip Code Proximity Search with ColdFusion
- i-Technology Viewpoint: We Need Not More Frameworks, But Better Programmers

































