atk4 / dsql

Object-Oriented SQL Query Builder
https://agiletoolkit.org/
MIT License
58 stars 23 forks source link

Add space before Expression in 'set' value #284

Closed scottfriebel closed 3 years ago

scottfriebel commented 3 years ago

When using an Expression as the value in the set function, a space isn't added after the "=" sign. This causes an exception when trying to do something like this:

->set('premium', $q->expr('!premium'))

which renders as the following which isn't valid:

update `account` set `premium`=!premium  where `userid` = 12451

A workaround would be to rewrite this as:

->set('premium', $q->expr('NOT premium'))
DarkSide666 commented 3 years ago

I think this is as expected. I'm not aware that field = !whatever is correct SQL syntax too. Basically expression is expression - it just renders as such and no additional logic should be in there.

Probably more appropriate would be to use expr('CASE WHEN [premium]=1 THEN 0 ELSE 1 END') or something similar like IF().

scottfriebel commented 3 years ago

I wasn't looking at it as field = !<value> though, but as field = !field which would invert the value of a boolean or tinyint field. It would be nice to be able to use it without needing to add additional parenthesis around it as in this workaround: ->set('premium', $q->expr('(!premium)')).

After further reading I discovered that ! is a valid mysql operator but is "nonstandard" and as of MySQL 8.0.17, it is deprecated. Since my specific use case for the change isn't very solid I'll close the issue.