Elvis operator doesn't work on query results?

I recently found out about the elvis operator in cfscript and rejoiced!
…then I found some weird behavior…

Essentially I’ve got some db records that might have a custom value for a field and if it’s null or empty string, I want to fall back to the default value. Problem is that this doesn’t seem to work on query result fields at all.

I messed around a bit and found that it does work on nulls in structs, but not queries.

It doesn’t seem to work for empty strings in either case, which is probably fine (it is a value), but this query behavior seems like a bug.

Here’s some sample code that shows the issue with Lucee 5.3.7.48 (Null Support: full)

<cfscript>
var qry = queryNew("customval,fallbackval", "varchar,varchar", [
	[ "fuzzy", "wuzzy" ],
	[ null, "honey" ],
	[ "" , "bear"]
	]);
var arr = [
	{customval: "fuzzy", fallbackval: "wuzzy"},
	{customval: null, fallbackval: "honey"},
	{customval: "", fallbackval: "bear"}
];
</cfscript>
<cfoutput>
	<h3>Query</h3>
	<table>
		<tr> <th>customval</th> <th>fallbackval</th> <th>myval</th> <th>isnull(myval)</th> </tr>
		<cfloop query="qry">
			<cfset myval = (qry.customval ?: qry.fallbackval)>
			<tr> <td>#qry.customval#</td> <td>#qry.fallbackval#</td> <td>#myval#</td> <td>#isnull(myval)#</td></tr>
		</cfloop>
	</table>

	<h3>Array of Structs</h3>
	<table>
		<tr>
			<th>customval</th> <th>fallbackval</th> <th>myval</th> <th>isnull(myval)</th>
		</tr>
		<cfloop array="#arr#" item="it">
			<cfset myval = (it.customval ?: it.fallbackval)>
			<tr> <td>#it.customval#</td> <td>#it.fallbackval#</td> <td>#myval#</td> <td>#isnull(myval)#</td></tr>
		</cfloop>
	</table>
</cfoutput>
1 Like

interesting, does this happen with the latest 5.3.8.159-SNAPSHOT?

Yup.