Re: Query with boolean parameter

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Paolo De Stefani <paolo(at)paolodestefani(dot)it>, Christophe Pettus <xof(at)thebuild(dot)com>
Cc: Psycopg <psycopg(at)postgresql(dot)org>
Subject: Re: Query with boolean parameter
Date: 2022-03-19 17:34:32
Message-ID: 321f190b-d65d-d2f7-d5bb-2ce72aa0b70d@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 3/19/22 05:10, Paolo De Stefani wrote:
> Il 19/03/2022 01:00 Christophe Pettus ha scritto:
>>> On Mar 18, 2022, at 16:56, Paolo De Stefani <paolo(at)paolodestefani(dot)it>
>>> wrote:
>>> Is there any reason why the second query results in a syntax error?
>>
>> There's not IS operator in PostgreSQL (or in SQL).  IS NULL, IS NOT
>> NULL, IS TRUE, and IS FALSE are in effect unary postfix operators, so
>> you can't construct them that way via parameter substitution.
>
> Thanks, i see
>
> The problem is (for me) that with psycopg2 this works:
> cur.execute('SELECT * FROM system.app_user WHERE can_edit_views IS TRUE')
> cur.execute('SELECT * FROM system.app_user WHERE can_edit_views IS %s',
> (True,))
> cur.execute('SELECT * FROM system.app_user WHERE can_edit_views IS %s',
> (None,))

The only way I could get it to work:

cur.execute(sql.SQL("select 'f' IS {}").format(sql.SQL('TRUE')))

or

cur.execute(sql.SQL("select 'f' IS {}").format(sql.SQL(str(True))))

cur.fetchone()

(False,)

cur.execute(sql.SQL("select 'f' IS {}").format(sql.SQL('NULL')))

cur.fetchone()

(False,)

>
> Switching to psycopg 3 i have to consider many more differences than i
> expected
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Browse psycopg by date

  From Date Subject
Next Message Paolo De Stefani 2022-03-19 19:30:32 Re: Query with boolean parameter
Previous Message Christophe Pettus 2022-03-19 16:35:08 Re: Query with boolean parameter