Everything that Coldfusion does, is bascially Java under the hood, this is something almost everyone using Coldfusion would know. I have always been curious to find out more about this (basically to find out what more we can do by making use of this). Recently, I read some blog posts on how we can use Java Reflection to find out more about what Java object, each CF object is mapped to under the hood. So, I decided to do some of this as well and I choose cfquery.
I first created a very simple query like below :-
<cfquery name="myqry" datasource="myDS"> select top 1 orderid from tblorder </cfquery>
Next, I wanted to find out what Java object is this. This was pretty straight forward :-
<cfdump var= "#myqry.getClass().getName()#">
That gives me coldfusion.sql.QueryTable , so I know what object this is internally.
So, in order to find out more about this, I did this :-
<cfset sql = createObject("java","coldfusion.sql.QueryTable")> <cfdump var = "#sql#">
That gave me some interesting information. There are a lot of available functions in there and I still havent looked into all of those but the 2 that caught my attention were these.
findColumn() and getShallowCopy()
So, now, remember my original query (myQry) , this is where it comes useful. In order to find out if a column exists in my query or not (really useful when folks sometimes run Select * …… queries) , I can easily do this
<cfdump var = "#myqry.findColumn('orderid')#"> - returns 1 (because this column exists in my query) <cfdump var = "#myqry.findColumn('somecolumn')#"> - returns 0 (because this doesnt)
Now, thats great. I think this can be very useful. There are always situations where you write a big query and when you try to output the resultset, you end up going to the database to look for what the exact column names were.
The next interesting thing I found was this :-
<cfdump var = "#myqry.getShallowCopy()#">
That gives some useful information as well.
Here is a dump of what it looks like. It gives you the executiontime of the query and tells you if the query is cached or not apart from giving you the actual SQL statement and the resultset.
I am sure there must be lot more to find out here, but as soon as I find more, I can post here.
Lastly, because a lot many people have been playing with CF underlying Java objects, its very much possible that this post is completely out-dated today and everyone knows this already and its just that I am too late to find this out. If thats the case, I will just keep it for myself to read in future 🙂
This code was tested on Coldfusion 8, I assume it would work on CF7 and MX as well, but havent tested it myself.