Did you know? DZone has great portals for Python, Cloud, NoSQL, and HTML5!

John is the manager and founding member of the official Adobe ColdFusion User Group for Devon. An Adobe Certified Expert in Advanced ColdFusion, John regularly blogs about ColdFusion and contributes to several FOSS projects. His hobbies include writing in the third person. John has posted 20 posts at DZone. You can read more from them at their website. View Full User Profile

Preserving large numbers in Query or Query

06.24.2011
Email
Views: 4246
  • submit to reddit

I hit a problem with QueryNew recently which caused a few headaches because 13 digit strings were being converted to standard notation. This is a pretty unusual use-case but I thought it was worth blogging anyway.

I've got an admin for products where you can enter the 13 digit EAN code for a product. As products can have multiple variants, each with their own EAN code, I decided to parse the form field data submitted to build a Query object of the varients. This makes validation and CRUD operations much easier to read in my code.

So I had something like this:

<!--- simulate form parameters --->
<cfset rc = {
  optionid_1 = "123",
  optionean_1 = "1234567890123",
  optioncostmodifier_1 = "0",
  optionid_2 = "456",
  optionean_2 = "4561237890123",
  optioncostmodifier_2 = "5"
}>

<cfset qryOptions = QueryNew( "id,ean,costmodifier" )>
  
<cfdump var="#rc#">

<cfloop collection="#rc#" item="item">

  <cfif ListFirst( item, "_" ) eq "optionid">
    <cfset row = ListLast( item, "_" )>
    <cfset QueryAddRow( qryOptions )>
    <cfset QuerySetCell( qryOptions, "id", rc[ "optionid_#row#" ] )>
    <cfset QuerySetCell( qryOptions, "ean", rc[ "optionean_#row#" ] )>
    <cfset QuerySetCell( qryOptions, "costmodifier", rc[ "optioncostmodifier_#row#" ] )>
  </cfif>

</cfloop>

<cfdump var="#qryOptions#">

<!--- reorder qryOptions --->
<cfquery name="qryOptions" dbtype="query">
  select * 
  from qryOptions
  order by costmodifier
</cfquery>

<cfdump var="#qryOptions#">

If you run this code you'll see this:

QofQDump

Note that the second dump after the query or queries shows values of 1.23456789012E+012 and 4.56123789012E+012.

My initial thought was to use the optional datatype argument of QueryNew to define the datatype like so:

<cfset qryOptions = QueryNew( "id,ean,costmodifier", "Integer,BigInt,Decimal" )>

This produced the same result (as did using Integer). In the end, I had to use the VarChar datatype to get it preserve the 13 digit number.

<cfset qryOptions = QueryNew( "id,ean,costmodifier", "Integer,Varchar,Decimal" )>
References
Tags:
Published at DZone with permission of its author, John Whish. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sirikant Noori replied on Sun, 2012/01/15 - 12:30pm

Which version of Coldfusion are you using? I cannot duplicate the issue when i test it in cf8 and cf9.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.