Sunday, 17 May 2015

Updating Query Column value in CF

Hi,


I faced a  problem while updating a Date value in in Query Object.

I had one task to change date of one column by checking Date value of other column of same query object.

For updating Query Column value most of people use javacast(type,variable) function.

Accepting types in javaCast are
bigdecimal,boolean,byte,char,double,byte,char,double,float,int,long,null,short and string

These are all from java.lang class. But there is no Type for date or datetime

I found there are three date classes used in CF (that can be more).

In This is code snippet , By making Query object adding cell and values and updating values.

 <cfset VARIABLES.user = queryNew('SENDDATE,AGE','date,Integer')>  
 <cfset VARIABLES.userDate1 = createDate(year(now()),month(now()),day(now())-2)>  
 <cfset VARIABLES.userDate2 = createDate(year(now()),month(now()),day(now())-3)>  
 <cfset VARIABLES.userDate3 = createDate(year(now()),month(now()),day(now())-4)>  
 <cfset VARIABLES.t = queryAddRow(VARIABLES.user,1)>  
 <cfset QuerySetCell(VARIABLES.user,'SENDDATE',VARIABLES.userDate1)>  
 <cfset QuerySetCell(VARIABLES.user,'AGE',21)>  
 <cfset VARIABLES.t = queryAddRow(VARIABLES.user,1)>  
 <cfset QuerySetCell(VARIABLES.user,'SENDDATE',VARIABLES.userDate3)>  
 <cfset QuerySetCell(VARIABLES.user,'AGE',22)>  
 <cfdump var="#VARIABLES.user#" >  
 <cfloop query="VARIABLES.user">  
      <!---Sample of updating a column value --->  
      <cfset VARIABLES.user['age'][VARIABLES.user.currentRow] = VARIABLES.user.age +1>  
      <!---We can do javacast also for this --->  
      <cfset VARIABLES.user['age'][VARIABLES.user.currentRow] = javacast("int",VARIABLES.user.age +1)>  
      <!---We can update date by normal assigning --->  
      <cfset VARIABLES.user [ 'SENDDATE' ][ VARIABLES.user.currentRow ] = createDate(year(now()),month(now()),day(now())-10) >  
 </cfloop>  
 <cfoutput >  
           #getMetadata(VARIABLES.user.SENDDATE).getName()#</br>  
           #getMetadata(VARIABLES.user.AGE).getName()#</br>  
 </cfoutput>  
 <cfdump var="#VARIABLES.user#" >  


Here is the second one where getting result by Executing Query.

 <cfquery name="VARIABLES.activity" datasource="rsa">  
 SELECT top 1 DATEADD,LISTIDS,EMAIL_ID FROM emaillist WHERE DATEADD IS NOT NULL       
 </cfquery>  
 <cfdump var="#VARIABLES.activity#" >  
 <!---Sample of updating a column value of Query Object we can use javaCast(type,variable) only for Email_ID or ListIDS here  --->  
 <cfset VARIABLES.activity['EMAIL_ID'][VARIABLES.activity.currentRow] = VARIABLES.activity.EMAIL_ID +5>  
 <cfset VARIABLES.activity['DATEADD'][VARIABLES.activity.currentRow] = createDate(year(now()),month(now()),day(now())-10)>  
 <cfdump var="#VARIABLES.activity#" >  


















The normal CF date

 <cfset VARIABLES.now = now()>  
 <cfoutput >  
 In Normal CF:<br/>   
      #getMetadata(VARIABLES.now).getName()#<br/>  
 </cfoutput>  






As you can see there are 3 Date class in above.

1) The Query Object Created and added Date from CF is java.sql.Date.
2) The Query Object got from SQL server is  java.sql.Timestamp
3) The normal Date in CF is coldfusion.runtime.OleDateTime

We can update using just by assigning value. If we use javaCast it will make us confuse.

Note: 
QOQ-Query of Query(DBTYPE='QUERY') can't be used on the updated Query object