Posted in ColdFusion | Posted on 03-19-2009 | 3,412 views
Using an ORM means never having to write SQL. Well, ok, maybe not. As much as I love Transfer (and am beginning to love Hibernate), there are still times when you have to resort to writing SQL. Here is a quick tip.
Using getDatasource() to get access to the datasource configuration? If so, you may have code that looks like this:
2 <cfset var ds = getDatasource()>
3 <cfset var foo = "">
4
5 <cfquery name="foo" datasource="#ds.getName()#">
6 select sum(rabbits) as total
7 from huntinglog
8 where club_no = <cfqueryparam cfsqltype="cf_sql_varchar" value="#getId()#">
9 </cfquery>
10 <cfreturn val(foo.total)>
11
12</cffunction>
Notice I pass ds.getName() to load the datasource name. My datasource.xml looks like so:
2<datasource>
3 <name>romulanale</name>
4 <username></username>
5 <password></password>
6</datasource>
Notice that I did not specify a username/password. But what happens if the production system needs this? It is trivial to supply it in the XML. Transfer will use it. But my query above will fail. Luckily I can just switch to:
What's nice is that this works just fine when the username/password values are blank. Now I'm set no matter what.


@Ray: Sometimes it's not really necessary to var out the extra variable. Just put datasource="#getDatasource().getName()#" user="#getDatasource().getUser()#" etc. Then you can create a snippet in Eclipse to quickstart your queries, and not have to have that extra command since you'll likely only use it for the query itself 99% of the time.
If so, and if they could guess your server, then they could use management studio to connect to your database.
The trick is to make your config.xml be config.xml.cfm. You read it like a normal xml file, it's just text. But you put <cfabort /> in an application.cfm in the same folder...voila..untouchable.
I've seen config.xml.cfm before with a cfabort at the top.
I seen a peanut stand, heard a rubber band.
But I ain't never seen a config.xml.cfm with an application.cfm! That's kinda of clever.
http://lyricsplayground.com/alpha/songs/w/whenisee...