OpenBD / openbd-core

The original open source Java powered GPL CFML runtime engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ternary operator broken & no documentation?

AaronJWhite opened this issue · comments

I'm pretty sure the ternary operator in is broken. I'm attempting to use it in cfscript and I just keep getting expression errors. Also, there is no example of using ternary in the openbd documentation so I could be doing something wrong.
I am using the latest nightly.
example:

<cfcomponent output="false">
<cfscript>
public component function init(required string datasource)
{
    variables.datasource = arguments.datasource;
    return THIS;
}
public query function ListAll(boolean inactive = false)
{
    var sql = "SELECT id, name, active FROM external_name " & (arguments.inactive ? ';' : ' WHERE active=1;');

    var qry = QueryRun(
        datasource: variables.datasource
        ,sql:sql
    );
    return qry;
}
</cfscript>
</cfcomponent>

After having a play about with this I believe it is a bug. OpenBD seems to require some sort of code execution inside the ternary condition for it to be valid.

<cfscript>

    isDog = true;
    animal = ( isDog ? 'dog' : 'cat' ); // expression error
    writeDump(animal);

    isDog = true;
    animal = 'cat';
    if (isDog) {
        animal = 'dog';
    }
    writeDump(animal); // "dog"

    isDog = true;
    animal = ( isDog == true ? 'dog' : 'cat' );
    writeDump(animal); // "dog"

    isDog = true;
    animal = ( isDefined('isDog') && isDog ? 'dog' : 'cat' );
    writeDump(animal); // "dog"

    isDog = true;
    animal = ( isBoolean(isDog) && isDog ? 'dog' : 'cat' );
    writeDump(animal); // "dog"

    isDog = true;
    animal = ( !isDog ? 'dog' : 'cat' );
    writeDump(animal); // "cat"

    isDog = true;
    animal = ( !(!isDog) ? 'dog' : 'cat' );
    writeDump(animal); // "dog"

</cfscript>

For now to fix your issue you can just change your code to this and it should work.

<cfcomponent output="false">
<cfscript>
public component function init(required string datasource)
{
    variables.datasource = arguments.datasource;
    return THIS;
}
public query function ListAll(boolean inactive = false)
{
    var sql = "SELECT id, name, active FROM external_name " & (arguments.inactive == true ? ';' : ' WHERE active=1;');

    var qry = QueryRun(
        datasource: variables.datasource
        ,sql:sql
    );
    return qry;
}
</cfscript>
</cfcomponent>