coldbox-modules / quick

A ColdBox ORM Engine
https://quick.ortusbooks.com
MIT License
23 stars 19 forks source link

Unexpected `where ... and` combinator applied with `orWhere` in a `when` #210

Closed davidAtInleague closed 1 year ago

davidAtInleague commented 1 year ago

Using an orWhere to produce a predicate OR'd with an outer clause from within a when conditional produces an unexpectedly AND'd result.

grammar is mssql.

repro

// component "X"
component extends="quick.models.BaseEntity" table="X" accessors=true {
    property name="ID" type="string" sqltype="idstamp";
}

// table
create table X(id uniqueidentifier);

// do it
a = getInstance("X")
    .whereID(createGUID())
    .when(/*someRuntimeBoolean*/ true,
        (X) => X.orWhereID(createGUID())
    )

b = getInstance("X").whereID(createGUID())

if (/*someRuntimeBoolean*/ true) { // workaround, use if statement
    b.orWhereID(createGUID())
}

writedump(a.toSql()); // unexpected AND, expected OR -- select ID from X where ID = ? AND ID = ?
writedump(b.toSql())  // workaround OK -- select ID from X where ID = ? OR ID = ?

The above repro is against v4.1.3 and v5.2.7

elpete commented 1 year ago

https://qb.ortusbooks.com/migration-guide#where-clauses-with-an-or-combinator-are-now-automatically-wrapped-inside-when-callbacks

On October 28, 2022 at 9:22:05 AM, davidAtInleague @.**@.)) wrote:

Using an orWhere to produce a predicate OR'd with an outer clause from within a when conditional produces an unexpectedly AND'd result.

grammar is mssql.

repro

// component "X" component extends="quick.models.BaseEntity" table="X" accessors=true { property name="ID" type="string" sqltype="idstamp"; } // table create table X(id uniqueidentifier); // do it a = getInstance("X") .whereID(createGUID()) .when(/someRuntimeBoolean/ true, (X) => X.orWhereID(createGUID()) ) b = getInstance("X").whereID(createGUID()) if (/someRuntimeBoolean/ true) { // workaround, use if statement b.orWhereID(createGUID()) } writedump(a.toSql()); // unexpected AND, expected OR -- select ID from X where ID = ? AND ID = ? writedump(b.toSql()) // workaround OK -- select ID from X where ID = ? OR ID = ?

— Reply to this email directly, view it on GitHub(https://github.com/coldbox-modules/quick/issues/210), or unsubscribe(https://github.com/notifications/unsubscribe-auth/AATWYXVJEH4GDIV6VJNWZWLWFPVR3ANCNFSM6AAAAAARRFSNUU). You are receiving this because you are subscribed to this thread.Message ID: @.***>

davidAtInleague commented 1 year ago

Ok, I see that this is working as intended and the solution is either the following

a = getInstance("X")
    .whereID(createGUID())
    .orWhere((X) => {
        X.when(/*someRuntimeBoolean*/ true,
            (X) => X.whereID(createGUID())
        )
    })

or the literal if-statement based guard. Thanks!