QueryAddRow(), Date/Times, WDDX, And ORDER BY, OH MY!

Posted February 28, 2007 at 8:15 AM by Ben Nadel

Tags: ColdFusion

I am working on this mini ColdFusion application that uses a query object and caches it as a WDDX XML file. Usually, I think that WDDX is sooo freakin' cool, I am shocked I didn't start using it earlier. But, when it comes to Date/Time objects, WDDX seems to not be playing well with manually built ColdFusion queries.

Now, I am not sure if this is a WDDX problem or a query building problem. As I have talked about before, building ColdFusion queries manually can be problematic because the underlying Java ResultSet object is type-sensitive where as ColdFusion is NOT type sensitive. When I add rows to the cached query using QueryAddRow(), I have to JavaCast() the values that I set into the new record cells otherwise performing further ColdFusion query of queries will fail on ORDER BY clauses (potentially) as the values are not of the correct type and the generic CompareTo() fails.

Most values are easy to deal with, but the one that keeps throwing me through a loop is the Date/Time object (think CF_SQL_TIMESTAMP). I can't just throw a regular time stamp into the query (Now()), as that gets stored as a Java String object and will certainly crash if used in conjunction with ORDER BY clauses. To overcome this, I usually cast the Date/Time object a FLOAT value when storing it. This works great and the ORDER BY clauses work great. Even the WDDX seems to convert everything nicely ... we sort of; the WDDX conversion is still storing the Date/Time stamp as a float in the XML file (ex. 39141.3359375). Still, it's cool, going from the WDDX back into a query works fine with the float.

The problem with this approach in this application is that I am actually using some AJAX and need to convert the query values to JSON. This causes a serious problem as the value coming out of the cached query, while still technically a date, looks nothing like a date to the algorithm that uses IsDate() when converting the ColdFusion value to JSON. I could use IsNumericDate(), but then values that were ACTUALLY numbers and not Date/Time stamps would also get converted to JSON date objects.

To get around this, I tried storing a Java Date object directly into the query:

  • <cfset APPLICATION.NoteTable[ "date_posted" ][ intRow ] =
  • CreateObject(
  • "java",
  • "java.util.Date"
  • ).Init(
  • Year( Now() ),
  • Month( Now() ),
  • Day( Now() ),
  • Hour( Now() ),
  • Minute( Now() ),
  • Second( Now() )
  • ) />

This stores fine and works swimmingly with further ORDER BY query of query clauses. It seems to crap out when WDDX converts this value to XML and then later back into a ColdFusion query object. The numbers get changed... or rather, the IN-dates are different than the OUT-dates. Such a pain in the butt!

I love ColdFusion query of queries and the ColdFusion query object in general. I really think it is an amazing feature of the language. But, sometimes, they can be such a pain when you are really trying to push the limits on how they get used.

For now, I will just go with storing a float value. Not a great solution, but ok for the Beta.




Reader Comments

Jul 1, 2009 at 1:56 PM // reply »
2 Comments

Ben - another option when dealing with these queries made from scratch is to use something else that returns a query and manipulate it - for example you could do "SELECT 0 as id, some_date_column as mydate FROM some table or tables WHERE 1=2" and then just add rows to it - the point being that rather than use queryNew() just get yourself a query object with no rows from a database and stuff things in there - but your initial query object that you create is properly constructed with the underlying types. of course this is a pain if you dont even have a DB in the site, but then again, you can do a cfdirectory whith a wacky filter that you know will return no rows, and then just make do with the column names you have (or use a QofQ to rename them) and then start stuffing data in there with queryAddRow.
of course all of this kinda pre-dates the cf7 ability to create types with the query new, but i have seen issues with both wddx and more specifically serializeJSON around queries in general, and so its always good to think of some end-arounds. cheers!


Jul 1, 2009 at 2:29 PM // reply »
10,640 Comments

@Jon,

Hmm, definitely an interesting idea. Thanks for the tip.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »