Add ELSE statement for integers to avoid casting issues #1866
+7
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A prepared statement like this
does fail in postgres when the type of value is int/bigint as postgres is not able to determine the type of the value to be expected and throws a typing error.
There would be multiple ways to tell postgres that it's an integer, e.g. by casting the whole case statement like:
The problem is, that this would not be compatible with mysql and we would have to build the query depending on the db system.
Another option to tell the db what type the case values will have, is by providing an ELSE with a static integer. The problem is, that even if you provide
... ELSE 0.., postgres will only interpret it as integer, which will then again not work with a bigint column.The trick which works is by setting the ELSE value to something bigger than 32bit max int. This way the db system is prepared to accept bigints (if there are), if the column is only integer, that is not a problem, as they way we build these
massSingleUpdatestatements, the ELSE clause never will be reached.Both db systems (mysql and postgres), work with this ELSE (2^32) statement, with both int and bigint columns.