The i-Technology Media!
Register | Log in
   
 
.NET  ·  AJAX  ·  CLOUD  ·  ECLIPSE  ·  FLEX  ·  OPEN WEB  ·  iPHONE  ·  JAVA  ·  LINUX  ·  OPEN SOURCE  ·  ORACLE  ·  PBDJ  ·  SEARCH  ·  SILVERLIGHT  ·  SOA  ·  VIRTUALIZATION  ·  WEB 2.0  ·  WIRELESS  ·  XML
Comments
Plone and Drupal: Different Approaches, Different Results
paul.nowak wrote: Matt, thanks for the comments. I made an error on the version of Plone. It's 2.5 Plone running on Zope 2.9x. In regards to the additional products, we have a skin installed and we have a product that we had custom developed for us that connects to a PostgreSQL database. We've looked at slow PostgreSQL queries causing problems and have not been able to find an issue. We've also tested for the case where the PostgreSQL server is down and have not been able to create an issue. We therefor...
Nov. 4, 2009 04:19 PM EST
Cloud Expo on Google News
Did you read today's front page stories & breaking news?


2009 East
PLATINUM SPONSORS:
IBM
Smarter Business Solutions Through Dynamic Infrastructure
IBM
Smarter Insights: How the CIO Becomes a Hero Again
Microsoft
Windows Azure
GOLD SPONSORS:
Appsense
Why VDI?
CA
Maximizing the Business Value of Virtualization in Enterprise and Cloud Computing Environments
ExactTarget
Messaging in the Cloud - Email, SMS and Voice
Freedom OSS
Stairway to the Cloud
Sun
Sun's Incubation Platform: Helping Startups Serve the Enterprise
POWER PANELS:
Cloud Computing & Enterprise IT: Cost & Operational Benefits
How and Why is a Flexible IT Infrastructure the Key To the Future?
Click For 2008 West
Event Webcasts

2009 East
GOLD SPONSORS:
CA
Get Your Transactions Under Control: SOA Performance Management
Software AG
Performance Driven Adoption: The Secret to Advancing SOA
Intel
The Evolving SOA Appliance: 3 Game-Changing Innovations
SILVER SPONSOR:
Denodo
Data Mashups: Deliver Your Project Faster with Virtualized Data Services Across Internal & External Sources
POWER PANELS:
The Business Value of Service Orientation
Driving Profitability Through User Experience
Click For 2008 West
Event Webcasts
Live Google News by SYS-CON!
Top Three Links You Must Click On


CF Tips & Techniques
TIPS
TIPS

By: Charlie Arehart
Sep. 17, 2003 12:00 AM

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

Dynamic SQL Queries

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.


Listing Session and Application Variables

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

Using Structures to Delete a Session Variable

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.


Joining Multiple Tables: The Correct Syntax

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.


Emulating a "Words" Function

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 Sep. 17, 2003— Reads 9,829
Copyright © 2003 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About 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.

Add Your Feedback

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers

ADS BY GOOGLE

Breaking Java News
Commercial Metals Company Announces Upcoming Organizational Realignment
Volcano Corporation Presentation at Canaccord Adams Conference to Be Webcast
Farmers(R) Group, Inc. and Subsidiaries Foremost and Bristol West Prepare for Tropical Storm Ida Heading for Georgia, Offer Tips to Customers to be Prepared
Farmers(R) Group, Inc. and Subsidiaries Foremost and Bristol West Prepare for Tropical Storm Ida Heading for Florida Panhandle, Offer Tips to Customers to be Prepared
Calgary Based Social Media Company Hosts 1st Annual Social Media Innovation Summit
CORRECTION FROM SOURCE/Imperial Metals Corporation: Red Chris Drill Hole Returns 4.12% Copper and 8.83 g/t Gold Over 152.5 Metres
ProLogis Releases Interim Update to Annual Summary
Thoratec Presentation at Canaccord Adams Conference to be Webcast
Pengrowth Energy Trust Reports Strong Third Quarter 2009 Results
INTELECOM Intelligent Telecommunications Receives 2009 Gold Davey Award

ADVERTISE   |   MAGAZINE SUBSCRIPTIONS   |   FREE BREAKING-NEWSLETTERS!   |   SYS-CON.TV   |   BLOG-N-PLAY!   |   WEBCAST   |   EDUCATION   |   RESEARCH

.NET Developer's Journal - .NETDJ   |   ColdFusion Developer's Journal - CFDJ   |   Eclipse Developer's Journal - EDJ   |   Enterprise Open Source Magazine - EOS
Open Web Developer's Journal - OPENWEB   |   iPhone Developer's Journal - iPHONE   |   Virtualization - Virtualization   |   Java Developer's Journal - JDJ   |   Linux.SYS-CON.com
PowerBuilder Developer's Journal - PBDJ   |   SEO / SEM Journal - SJ   |   SOAWorld Magazine - SOAWM   |   IT Solutions Guide - ITSG   |   Symbian Developer's Journal - SDJ
WebLogic Developer's Journal - WLDJ   |   WebSphere Journal - WJ   |   Wireless Business & Technology - WBT   |   XML-Journal - XMLJ   |   Internet Video - iTV
Flex Developer's Journal - Flex   |   AJAXWorld Magazine - AWM   |   Silverlight Developer's Journal - SLDJ   |   PHP.SYS-CON.com   |   Web 2.0 Journal - WEB2
Apache   |   CMS   |   CRM   |   HP   |   Oracle Journal   |   Perl   |   Python   |   Red Hat   |   Ruby on Rails   |   SAP   |   SaaS

SYS-CON MEDIA:   ABOUT US   |   CONTACT US   |   COMPANY NEWS   |   CAREERS   |   SITE MAP
SYS-CON EVENTS:   |  AJAXWorld Conference & Expo  |  iPhone Developer Summit  |  Cloud Computing Conference & Expo  |  SOA World Conference & Expo  |  Virtualization Conference & Expo
INTERNATIONAL SITES:   India  |  U.K.  |  Canada  |  Germany  |  France  |  Australia  |  Italy  |  Spain  |  Netherlands  |  Brazil  |  Belgium
 Terms of Use & Our Privacy Statement     About Newsfeeds / Video Feeds
Copyright ©1994-2008 SYS-CON Publications, Inc. All Rights Reserved. All marks are trademarks of SYS-CON Media.
Reproduction in whole or in part in any form or medium without express written permission of SYS-CON Publications, Inc. is prohibited.
 
close this window