سؤال

I have a parameterized postgresql query that users interact with through a PHP front-end. One of the parameters involves a boolean value, and I want to give the user the option of "True", "False", "Unknown" (Null) or "Doesn't matter" (return any result).

Rewriting the actual query isn't an option (the parameterized queries are stored in a database table, not in the PHP code), so I need to have a boolean "any" value.

Put another way, what value can I assign ":parameter1" in this query to get any row, regardless of the value of "boolean_column"?

SELECT some_column, some_other_column
FROM some_table
WHERE boolean_column = :parameter1

EDIT: I need to clarify -- I can rewrite the query, but the program cannot rewrite the query (i.e., I can't drop or rewrite the where clause in response to user input at runtime).

EDIT2: Here's my final solution (thanks couling!)

SELECT some_column, some_other_column
FROM some_table
WHERE (:parameter1::text = 'any' OR boolean_column = :parameter1::boolean)

Of course, the program has to restrict possible values to 'any' or a string value that can be cast to boolean (e.g., 'true', 't', 'false', 'f').

EDIT3: The previous solution works in PHP with PDO, but does not work with python's psycopg2 or directly in postgresql for some reason. I've tried a variation with a case statement, like so:

SELECT some_column, some_other_column
FROM some_table
WHERE (CASE WHEN :parameter1::text in ('true', 'false') THEN boolean_column = :parameter1::boolean ELSE TRUE END)

But that doesn't work either. It seems no matter what, I can't stop postgresql from trying to evaluate ":parameter1::boolean", even if it's behind a false WHEN condition. This is a real pickle...

هل كانت مفيدة؟

المحلول

Well your query must change. Booleans have two values true and false with nulls never being equal to anything (even null doesn't equal null).

You could of course use:

SELECT some_column, some_other_column
FROM some_table
WHERE (boolean_column = :parameter1 OR :parameter1 is null)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top