QueryMap()

49 views
Skip to first unread message

AJ Mercer

unread,
Sep 22, 2015, 10:53:22 PM9/22/15
to lu...@googlegroups.com
I am doing through all the query functions and creating example to help me learn and also to add as example to http://docs.lucee.org/

But am having issues with QueryMap()

As I read it, the returned struct is used to populate the new query, but I am just getting the original query

​It would seem that you can only change the value of existing query fields
not:
  • create a new query
  • add a new column
Is this the case?
<cfscript>
try {
qryPeople = queryNew("name,dob","varchar,date",[["Susi", CreateDate(1970,1,1)],["Urs",CreateDate(1995,1,1)],["Fred", CreateDate(1960,1,1)],["Jim", CreateDate(1988,1,1)]]);


// map dob to age
qryPeopleAge = qryPeople.map(function(row, rowNumber, qryData){
row['age'] = DateDiff('yyyy', row.dob, Now());
dump(var=row, abort=false);
return row;
});
dump(var=qryPeopleAge, label='map() - map dob to age');


} catch (any error) {
dump(var=error, abort=true);

}
</cfscript>



--

Nando Breiter

unread,
Sep 23, 2015, 8:21:41 AM9/23/15
to lu...@googlegroups.com
If I change your code to set the dob field to the age, it works:

row['dob'] = DateDiff('yyyy', row.dob, Now());

Apparently there is no mechanism in the underlying implementation to add a query column on the fly. (Might be convenient if there would be ...) 

According to the above, your example should work if you add an empty age column to the original query.



Aria Media Sagl
Via Rompada 40
6987 Caslano
Switzerland

+41 (0)91 600 9601
+41 (0)76 303 4477 cell
skype: ariamedia

--
See Lucee at CFCamp Oct 22 & 23 2015 @ Munich Airport, Germany - Get your ticket NOW - http://www.cfcamp.org/
---
You received this message because you are subscribed to the Google Groups "Lucee" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lucee+un...@googlegroups.com.
To post to this group, send email to lu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lucee/CAPURtC3uhhK8cdbfdCXnFgeBH4s%3DD7%3DCQH-xp1o-M%3DCw-xKygQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Nando Breiter

unread,
Sep 23, 2015, 8:30:17 AM9/23/15
to lu...@googlegroups.com
On second thought, adding a query column on the fly via a struct might be problematic, because a query is a grid, whereas a struct can have an arbitrary number of keys. There is nothing to prevent code within the map function to have conditionally adds a key so that only some rows would have the added column, which doesn't make sense in query-land. So although it might be convenient to add a query column on the fly, it would probably necessitate an underlying implementation that is not efficient - "opps, we don't have that column, let be go back and add it and then start again"



Aria Media Sagl
Via Rompada 40
6987 Caslano
Switzerland

+41 (0)91 600 9601
+41 (0)76 303 4477 cell
skype: ariamedia

AJ Mercer

unread,
Sep 28, 2015, 7:08:17 PM9/28/15
to lu...@googlegroups.com
yeah - would seem my understanding of map() was incorrect.

Thanks for replies


For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages