Jan 20

Problem with query of queries and flex

Posted by James Netherton | Saturday 20 January 2007 6:18 PM | In ColdFusion

It may be a quirk or just me misunderstanding the way in which query of queries works.

Here's an example:

<cfscript>
   variables.boyNameList = "James,David,John";   
   variables.boyNames = queryNew("id, name");

   variables.girlNameList = "Jane,Sarah,Betty";   
   variables.girlNames = queryNew("id, name");
      
   for(i = 1; i lt listLen(variables.boyNameList); i = i + 1)
   {
      queryAddRow(variables.boyNames);
      queryAddRow(variables.girlNames);

      querySetCell(variables.boyNames, "id", i);
      querySetCell(variables.boyNames, "name", listGetAt(variables.boyNameList,i));

      querySetCell(variables.girlNames, "id", i);
      querySetCell(variables.girlNames, "name", listGetAt(variables.girlNameList,i));
      
   }   
</cfscript>

Which produces the following query recordsets:

Record sets

Now create a cartesian product:

<cfquery name="joinNames" dbtype="query">
   SELECT
      boyNames.name AS boysNames,
    girlNames.name AS girlsNames
   FROM
      boyNames,
      girlNames            
</cfquery>

Record sets

Even though the column names were aliased, the query metadata hasn't changed.

Query metadata

Why would this ever be any problem? It turns out that if you were to return this query object back to Flex and bind it to the DataGrid component, only one column titled 'name' would be displayed.

It appears that Flex uses the query metadata to determine the query column names, so no matter what aliases exist, they are ignored.

So if one wanted to change the query metadata, how could this be achieved? ColdFusion reperesents query metadata through objects of type coldfusion.sql.QueryTableMetaData. This object exposes a method that allows column names to be set:

public void setColumnLabel(String colLabel[])
{
   column_label = colLabel;
}

Change the metadata:

<cfset newColumnNames = "boysNames,girlsNames">
<cfset joinNames.getmetaData().setColumnLabel(newColumnNames.Split(","))>

And the query metadata has changed:

Query metadata

 

0 Comments

[Post comment]


Leave a comment








Captcha text