ColdFusion Thinks My List Is A Date

coldfusion

I came across something interesting today when build a query for testing.  I did the following:

 

<cfscript>
	q = queryNew("id,name,list");
	queryAddRow(q, 3);
	querySetCell(q, "id", 1, 1);
	querySetCell(q, "name", "robert", 1);
	querySetCell(q, "list", "1,2,3", 1);
	querySetCell(q, "id", 2, 2);
	querySetCell(q, "name", "ryan", 2);
	querySetCell(q, "list", "1,3", 2);
	querySetCell(q, "id", 3, 3);
	querySetCell(q, "name", "greg", 3);
	querySetCell(q, "list", "3,4", 3);
</cfscript>

Out of habit, I dumped the "q" to verify that I have what I want.  When doing this, what do you think I got?

I thought I'd get an INTEGER (id) and 2 VARCHARs (name & list).  Is that what you thought?  Well, that's not the case. QuerySetCell determined that the "list" column values represent dates.  This is what it gave me.

 

 

 

 

When I used 2 or 3 numbers in the list, that fell within some sort of date value range, ColdFusion set it's datatype to DATE. If I add rows that only have 1 list value, more than 3 list values, or list values that seem to break the DATE mold (e.g. "40,40"), then I'd get the VARCHAR list that I wanted.

So what is the lesson learned here?  ALWAYS define your datatypes when using QueryNew.

Have you come across any other functions that produce unexpected results?

0

Looking for Open Source Suggestions

coldfusion

I'm working on my first Open Source project (more on that later) which I plan to release on RIAForge. Do you have any suggestions, tips, or best kept secrets for OSS development?

0

Peter Bell on Encapsulating Recordsets

coldfusion

CFDJ posted an article today from Peter Bell on Encapsulating Recordsets . I've been following Peter's blog for a little while now, and I have to say that he has some very interesting posts. One in particular caught my attention last week. After I read that post, I started doing some work with his idea of IBOs. I really like the way it was setup, but it was at a pretty big performance risk. It wasn't until I read his article today that I realized where I was messing up. The code I put together is very similar to Peter's IBO. The main difference is that I was building a bean object for each record and storing them in a single object. This took it's toll on the server, so I set it aside. Now that I see how Peter's IBO is setup, I'm going to have to start playing around with this again.

Also take note of his use of single set and get functions.

I see this as a potential way to eliminate beans, DAOs, and gateways. Let's see if we can make that work.

Be sure to check out Peter's article

tags:
ColdFusion
0

Using ColdFusion to transform XML with XSLT

coldfusion

Recently, I've been working with XSLT to transform XML documents for a new project (more on the project later). I thought this would be a good item to write about. It's pretty easy to do, so let's just jump right in.

For this example I'll use the RSS 2.0 format. First we have to create the XML that we're going to transform. You could read in an XML file using any normal method (cffile, cfhttp, etc.). I'll just create my own using cfxml. I used the RSS 2.0 example from Wikipedia.

<cfxml variable="myRSS">
<rss version="2.0">
   <channel>
      <title>Liftoff News</title>
      <link>http://liftoff.msfc.nasa.gov/</link>
      <description>Liftoff to Space Exploration.</description>
      <language>en-us</language>
      <pubDate>Tue, 10 Jun 2003 04:00:00 GMT</pubDate>
      <lastBuildDate>Tue, 10 Jun 2003 09:41:01 GMT</lastBuildDate>
      <docs>http://blogs.law.harvard.edu/tech/rss</docs>
      <generator>Weblog Editor 2.0</generator>
      <managingEditor>editor@example.com</managingEditor>
      <webMaster>webmaster@example.com</webMaster>

      <item>
         <title>Star City</title>
         <link>http://liftoff.msfc.nasa.gov/news/2003/news-starcity.asp</link>
         <description>How do Americans get ready to work with Russians aboard the International Space Station? They take a crash course in culture, language and protocol at Russia's Star City.</description>
         <pubDate>Tue, 03 Jun 2003 09:39:21 GMT</pubDate>
         <guid>http://liftoff.msfc.nasa.gov/2003/06/03.html#item573</guid>
      </item>

      <item>
         <title>Space Exploration</title>
         <link>http://liftoff.msfc.nasa.gov/</link>
         <description>Sky watchers in Europe, Asia, and parts of Alaska and Canada will experience a partial eclipse of the Sun on Saturday, May 31st.</description>
         <pubDate>Fri, 30 May 2003 11:06:42 GMT</pubDate>
         <guid>http://liftoff.msfc.nasa.gov/2003/05/30.html#item572</guid>
      </item>

      <item>
         <title>The Engine That Does More</title>
         <link>http://liftoff.msfc.nasa.gov/news/2003/news-VASIMR.asp</link>
         <description>Before man travels to Mars, NASA hopes to design new engines that will let us fly through the Solar System more quickly. The proposed VASIMR engine would do that.</description>
         <pubDate>Tue, 27 May 2003 08:37:32 GMT</pubDate>
         <guid>http://liftoff.msfc.nasa.gov/2003/05/27.html#item571</guid>
      </item>

      <item>
         <title>Astronauts' Dirty Laundry</title>
         <link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
         <description>Compared to earlier spacecraft, the International Space Station has many luxuries, but laundry facilities are not one of them. Instead, astronauts have other options.</description>
         <pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
         <guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
         </item>
      </channel>
</rss>
</cfxml>

Next we have to create the XSTL that will be used to transform the RSS feed. I used the XSLT reference guide from zvon.org as my main reference while putting together this XSTL. Check it out if you need help with XSLT. Like the XML above, you could read in the XSLT many different ways. I'll just create it via cfsavecontent. If you're running 7.0+ you can use cfxml. For some reason, it didn't work in 6.0.

<cfsavecontent variable="myXSL">
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xhtml" indent="yes" omit-xml-declaration="yes" />
   <xsl:template match="/rss/channel">
      <xsl:element name="h1">
         <xsl:element name="a">
            <xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute>
            <xsl:value-of select="title" />
         </xsl:element>
      </xsl:element>

      <xsl:for-each select="item">
         <xsl:element name="h2">
            <xsl:element name="a">
               <xsl:attribute name="href"><xsl:value-of select="link" /></xsl:attribute>
               <xsl:value-of select="title" />
            </xsl:element>
         </xsl:element>

         <xsl:element name="p">
            <xsl:value-of select="description" />
         </xsl:element>
      </xsl:for-each>
   </xsl:template>
</xsl:stylesheet>
</cfsavecontent>

So we have the XML (RSS) and our XSLT. All we have to do now is use ColdFusion's XMLTransform() function and output our results.

<cfset myOutput = xmlTransform(myRSS, myXSL) />
<cfoutput>#myOutput#</cfoutput>

That's really all there is to it. Pretty easy, right. This is just a basic example. Just imagine what else you could do with it.

I want to end this by saying that you don't need CF to use XSL transformations. You can use XSL stylesheets directly in your XML documents. Check out some of these resources.

It should be said that this is my first go at XSLT, so if any of you have suggestions, please share.

Fro

tags:
ColdFusion

Search