Monday, March 22, 2010    
Home My Books Blog ColdFusion About Me Back    

Calendar
<< Jun 2009 >>
S M T W T F S
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        

Search

Categories
 • Acrobat (3) [RSS]
 • Adobe (90) [RSS]
 • AdobeMAX06 (45) [RSS]
 • AdobeMAX07 (59) [RSS]
 • AdobeMAX08 (66) [RSS]
 • AdobeMAX09 (39) [RSS]
 • AdobeMAX10 (1) [RSS]
 • AIR (219) [RSS]
 • Appearances (191) [RSS]
 • Books (72) [RSS]
 • CFEclipse (15) [RSS]
 • ColdFusion (1382) [RSS]
 • Data Services (34) [RSS]
 • Fish Tank (5) [RSS]
 • Flash (198) [RSS]
 • Flex (499) [RSS]
 • Home Automation (5) [RSS]
 • Jobs (116) [RSS]
 • JRun (14) [RSS]
 • Labs (43) [RSS]
 • LiveCycle (34) [RSS]
 • MAX (232) [RSS]
 • Mobile (120) [RSS]
 • Regular Expressions (17) [RSS]
 • RIA (21) [RSS]
 • SQL (40) [RSS]
 • Stuff (536) [RSS]
 • Tips (CF Studio) (80) [RSS]
 • Tips (CF) (795) [RSS]
 • Tips (Dreamweaver) (91) [RSS]
 • Tips (Flex Builder) (2) [RSS]
 • Using CF (162) [RSS]

Other BLOGs
 • Charlie Arehart
 • Lee Brimelow
 • Ray Camden
 • Christophe Coenraets
 • Sean Corfield
 • Mihai Corlan
 • Cornel Creanga
 • Mark Doherty
 • John Dowdell
 • Danny Dura
 • Enrique Duvos
 • Steven Erat
 • Kevin Hoyt
 • Serge Jespers
 • Adam Lehman
 • Duane Nickull
 • Miti Pricope
 • Andrew Shorten
 • Ryan Stewart
 • James Ward
 • Greg Wilson
 • Full As A Goog

RSS Feeds
 • Feed
 • Subscribe

Join my mailing list and find out about new books and other topics of interest.

Thoughts, ideas, tips, musings, and pontifications (not necessarily in that order) by Ben Forta ...
NOTE: This is my personal blog, and the opinions and statements voiced here are my own.

Viewing By Day : June 20, 2009 / Main
June 20, 2009

Look, No Datasource

Look at this code snippet. Notice anything odd about it?

<cfquery name="Art">
SELECT *
FROM art
ORDER BY ArtName
</cfquery>

Yep, the <CFQUERY> tag in the above snippet is missing the DATASOURCE attribute. And yet, this code works. Why? Take a look at this Application.cfc file:

<cfcomponent>
    <cfset this.datasource="cfartgallery">
</cfcomponent>

In ColdFusion Centaur you can now specify an application wide default datasource by setting this.datasource in Application.cfc. The specified datasource becomes the default for all <CFQUERY>, <CFSTOREDPROC>, and other tags that expect a datasource to be specified. Of course, datasource can still be specified manually if needed, and a specified datasource will override the application default one.

This is one of those duh! enhancements that just makes sense. And, as I'll explain in an upcoming post, it also has another important purpose.

TrackBacks
Datasource als optionales Attribut bei cfquery in Coldfusion 9
Adobes ColdFusion-Evangelist Ben Forta hat am Wochenende ein nettes Feature von ColdFusion 9 ausgepackt. So wird es künftig möglich sein, ColdFusion-Queries ohne die Angabe der Datasource abzusetzen.
Tracked by Mischa Sameli 's Blog | Tracked on 6/22/09 2:55 AM
Using cfquery Without a Datasource Specified
Using cfquery without specifying a datasource is a great time-saver and especially will allow for increased code sharing between developers and applications...
Tracked by ColdFusioning | Tracked on 6/22/09 1:34 PM

No trackback URL. Trackbacks are only allowed via interactive form.

Comments
That's just one step ahead of a Coda clip I have...

<cfquery name="read" dataSource="#application.datasource#">
SELECT
FROM
WHERE
</cfquery>

That's a nice little enhancement. Thanks for sharing!
# Posted By Freelance Web Development | 6/20/09 11:34 PM
Perfect! One less redundant thing to code.
# Posted By Russ S. | 6/21/09 2:46 AM
Newbie question...can this work with Application.cfm?
# Posted By Barry | 6/21/09 5:30 AM
Barry, no, the application wide THIS settings (included those for custom tag path and mappings added in CF8) require Application.cfc.

--- Ben
# Posted By Ben Forta | 6/21/09 7:53 AM
@Barry: One more reason to stop using App.cfm. I only use App.cfm in my OS apps that have to support CFMX.
# Posted By Raymond Camden | 6/21/09 10:23 AM
Yet another great enhancement in the Centaur release! Thanks for the blog on it!
# Posted By Sid Wing | 6/21/09 10:23 AM
It's nice that this can be set in the 'this' scope. now all we need is the same for cfmail.
# Posted By Glyn Jackson | 6/21/09 10:49 AM
What with datasources that need user / password data to authenticate? Will this be supported in Application.cfc? I'm just thinking about shared hosting where you have to specify user / password with all cfquery tags.
# Posted By Ulf Unger | 6/21/09 1:57 PM
What with datasources that need user / password data to authenticate? Will this be supported in Application.cfc? I'm just thinking about shared hosting where you have to specify user / password with all cfquery tags.
# Posted By Ulf Unger | 6/21/09 1:57 PM
Wow!

Well, bye bye to our ubiquitous, old reliable #application.dsn# ;-)

I have been with CF since version 3. Man I could have used this ... last century!!!

And there is was, hiding there in plain site (just waiting to be added as a feature in version 9).

Better late than never. And again confirming exactly why CF is, bar none, the language with which I consistently remain my most productive in.

Thanks!!!

Best regards,

g
# Posted By greg h | 6/21/09 5:41 PM
@Ulf: While not quite as nice, don't forget you can pass structures to cf tags. This means you could create an application scoped structure with default mail params:

<cfset s = {server="127.0.0.1",username="mail",password="pass"}>

and then do

<cfmail to="some@where.com" from="admin@foo.com" subject="Your Email" attributeCollection="#s#">
# Posted By Raymond Camden | 6/21/09 9:26 PM
What if you have more than one datasource in an application? How will the cfquery tag know which datasource to refer to?
# Posted By Jose | 6/22/09 9:32 AM
You can define only one datasource in Application.cfc. That would be the default one. If there is one in cfquery, the one defined in cfquery will be considered.
# Posted By Hemant Khandelwal | 6/22/09 12:06 PM
Is this.datasource available in CF8?
# Posted By nchen | 1/28/10 2:36 PM
What would cause the error "Variable APPDSN is undefined" (where APPDSN is the name of the datasource variable). I have the following in my Application.cfc file:
<cfset this.datasource = "APPDSN">
Thanks for the useful info, I'm looking forward to using it once I get it to work.....
:)
# Posted By tam | 2/16/10 9:22 AM
nchecn, no, this was added to CF9.

tam, make sure you don't actually specify the datasource explicitly in your <cfquery> tags, remove the whole datasource="" attribute if you use this.datasource.

--- Ben
# Posted By Ben Forta | 2/16/10 9:48 AM

  © Copyright 1997-2009 Ben Forta, All Rights Reserved