HQL SELECT clauses: watch out for whitespace characters
The other day I was listening to an interesting discussion on Think Vitamin Radio which touched on the advantage developers have in starting their own web business, over entrepreneurs with no coding skills at all. Even with limited technical ability, you could at least build version 1.0 yourself to get the ball rolling. Ryan (Carson) is now primarily a businessman but has a technical background.
Ryan: If you put me in a corner and gave me a week I could build something...
Keir: you'[d need to?] get those ColdFusion tags out again [...] those CF tags? [laughs]
Ryan: [laughs] No, no! Never again!...
Rails is now their platform of choice but they later refer again to the "outdated technology that we occasionally use".
Although just a light-hearted aside, they're right that back in the day CF was seen as a good choice for building apps rapidly with limited technical skills (the very reason it was recommended to me by a Perl programmer in 1997). But it's their information on CF that's outdated. For some years now you've been able to write in script rather than tags to an increasing degree.
Tags have their place
I've been using cfscript
since it was added in version 4, and love the major improvements in CF9 which allow you to write entirely in script. But I've stopped short of full scripting because I think tags lend themselves better to certain operations and I want the option of using them.
Queries are one such case. The simplicity of <cfquery>
was probably what earned CF its reputation as a rapid web database platform. Instead of concatenating SQL strings as variables to be passed on to a data object, you could compose your SQL freely within the tags with whatever formatting you preferred. For me, clarity of formatting really makes a difference to how easily understandable code is, so although I don't go as far as Ben Nadel, tabs and newlines are very important. I know which of the following I'd prefer.
<cfquery name="q">
select post.id,post.dateCreated,post.datePublished,post.title,author.firstname
from posts post join users author on (author.id= post.creatorid) order by post.datePublished desc
</cfquery>
<cfquery name="q">
SELECT
POST.ID
,POST.dateCreated
,POST.datePublished
,POST.title
,AUTHOR.firstname
FROM
posts POST
JOIN
users AUTHOR
ON ( AUTHOR.ID = POST.creatorID )
ORDER BY
POST.datePublished DESC
</cfquery>
And your point about HQL is...?
An improvement to ORM in the CF9.0.1 update was the ability to write HQL within <cfquery>
tags. So instead of being forced to write...
artist = ORMExecuteQuery("SELECT art.Artist.Firstname, art.Artist.Lastname FROM Art as art WHERE art.IsSold=1");
...you now have the option of writing it as...
<cfquery name="artist" dbtype="hql">
SELECT
art.Artist.Firstname
,art.Artist.Lastname
FROM
Art AS art
WHERE
art.IsSold=1
</cfquery>
Except that if you run the above you will get the following error:
"Not supported for select queries"
Turns out that you can't include tabs or new line characters in the SELECT clause - just spaces, so we need to change it to
<cfquery name="artist" dbtype="hql">
SELECT art.Artist.Firstname, art.Artist.Lastname
FROM
Art AS art
WHERE
art.IsSold=1
</cfquery>
This was logged as a bug in ORMExecuteQuery() before CF9.0.1 was released, but for some reason that ticket has been closed, so I've opened a new one as it's clearly not gone away.
Comments