public inbox for [email protected]
help / color / mirror / Atom feedAdaptation in psycopg3
7+ messages / 3 participants
[nested] [flat]
* Adaptation in psycopg3
@ 2020-11-24 16:52 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` Re: Adaptation in psycopg3 listas <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: Daniele Varrazzo @ 2020-11-24 16:52 UTC (permalink / raw)
To: psycopg
Hello,
I wrote a description of the psycopg3 adaptation system and the main
differences compared to psycopg2: available at
https://www.psycopg.org/articles/2020/11/24/psycopg3-adaptation/
Initial API docs are available at
https://www.psycopg.org/psycopg3/docs/adaptation.html
Feedback is welcome. Cheers!
-- Daniele
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Adaptation in psycopg3
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
@ 2020-11-25 12:29 ` listas <[email protected]>
2020-11-25 14:35 ` Re: Adaptation in psycopg3 Adrian Klaver <[email protected]>
2020-11-25 14:45 ` Re: Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
0 siblings, 2 replies; 7+ messages in thread
From: listas @ 2020-11-25 12:29 UTC (permalink / raw)
To: Daniele Varrazzo <[email protected]>; psycopg
El 24/11/20 a las 17:52, Daniele Varrazzo escribió:
> Hello,
>
> I wrote a description of the psycopg3 adaptation system and the main
> differences compared to psycopg2: available at
> https://www.psycopg.org/articles/2020/11/24/psycopg3-adaptation/
>
> Initial API docs are available at
> https://www.psycopg.org/psycopg3/docs/adaptation.html
>
> Feedback is welcome. Cheers!
>
> -- Daniele
>
Hi Daniele, thanks for your effort.
After reading the docs i have a question about the parameters in the
'in' clause.
In psycopg2 i do:
params = (1,2,3,4)
cursor.execute("select * from mytable where field1 in %s", (params,))
or
params = ('black','red','green')
cursor.execute("select * from mytable where field2 in %s", (params,))
What will it be like in psycopg3, will it be the same?, will I have to
create a special adapter?
Thanks
--
Oswaldo Hernández
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Adaptation in psycopg3
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` Re: Adaptation in psycopg3 listas <[email protected]>
@ 2020-11-25 14:35 ` Adrian Klaver <[email protected]>
1 sibling, 0 replies; 7+ messages in thread
From: Adrian Klaver @ 2020-11-25 14:35 UTC (permalink / raw)
To: listas <[email protected]>; Daniele Varrazzo <[email protected]>; psycopg
On 11/25/20 4:29 AM, listas wrote:
> El 24/11/20 a las 17:52, Daniele Varrazzo escribió:
>> Hello,
>>
>> I wrote a description of the psycopg3 adaptation system and the main
>> differences compared to psycopg2: available at
>> https://www.psycopg.org/articles/2020/11/24/psycopg3-adaptation/
>>
>> Initial API docs are available at
>> https://www.psycopg.org/psycopg3/docs/adaptation.html
>>
>> Feedback is welcome. Cheers!
>>
>> -- Daniele
>>
>
> Hi Daniele, thanks for your effort.
>
> After reading the docs i have a question about the parameters in the
> 'in' clause.
>
> In psycopg2 i do:
>
> params = (1,2,3,4)
> cursor.execute("select * from mytable where field1 in %s", (params,))
>
> or
>
> params = ('black','red','green')
> cursor.execute("select * from mytable where field2 in %s", (params,))
>
> What will it be like in psycopg3, will it be the same?, will I have to
> create a special adapter?
From the link:
"cannot use the IN (...) construct:
# Must use "AND nation = any (%s)"
cur.execute("... AND nation in %s", [("IT", "FR", "DE")])
"
>
> Thanks
> --
> Oswaldo Hernández
>
>
--
Adrian Klaver
[email protected]
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Adaptation in psycopg3
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` Re: Adaptation in psycopg3 listas <[email protected]>
@ 2020-11-25 14:45 ` Daniele Varrazzo <[email protected]>
2020-11-25 18:00 ` Re: Adaptation in psycopg3 listas <[email protected]>
1 sibling, 1 reply; 7+ messages in thread
From: Daniele Varrazzo @ 2020-11-25 14:45 UTC (permalink / raw)
To: listas <[email protected]>; +Cc: psycopg
On Wed, 25 Nov 2020 at 12:29, listas <[email protected]> wrote:
> After reading the docs i have a question about the parameters in the
> 'in' clause.
>
> In psycopg2 i do:
>
> params = (1,2,3,4)
> cursor.execute("select * from mytable where field1 in %s", (params,))
>
> or
>
> params = ('black','red','green')
> cursor.execute("select * from mytable where field2 in %s", (params,))
>
> What will it be like in psycopg3, will it be the same?, will I have to
> create a special adapter?
Hollo Oswaldo,
"IN" cannot be used, because it's a SQL construct, so "(1, 2, 3)" is
not something that postgres will understand as a parameter.
You can use "= any (%s)" and pass a list. This is something you can do
in psycopg2 too, and it's actually a better choice, because it works
with empty lists too, unless `IN ()`, which is a syntax error for
Postgres.
What you can do is:
params = ['black','red','green']
cursor.execute("select * from mytable where field2 = any(%s)", (params,))
interesting fact: "= any" is what postgres really uses internally,
even if you use the "IN ()" syntax:
piro=# explain select * from mytable where myint in (1,2,3);
QUERY PLAN
---------------------------------------------------------
Seq Scan on mytable (cost=0.00..45.06 rows=38 width=4)
Filter: (myint = ANY ('{1,2,3}'::integer[]))
(2 rows)
-- Daniele
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Adaptation in psycopg3
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` Re: Adaptation in psycopg3 listas <[email protected]>
2020-11-25 14:45 ` Re: Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
@ 2020-11-25 18:00 ` listas <[email protected]>
2020-11-25 19:02 ` Re: Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: listas @ 2020-11-25 18:00 UTC (permalink / raw)
To: Daniele Varrazzo <[email protected]>; +Cc: psycopg
El 25/11/20 a las 15:45, Daniele Varrazzo escribió:
> On Wed, 25 Nov 2020 at 12:29, listas <[email protected]> wrote:
>
>> After reading the docs i have a question about the parameters in the
>> 'in' clause.
>>
>> In psycopg2 i do:
>>
>> params = (1,2,3,4)
>> cursor.execute("select * from mytable where field1 in %s", (params,))
>>
>> or
>>
>> params = ('black','red','green')
>> cursor.execute("select * from mytable where field2 in %s", (params,))
>>
>> What will it be like in psycopg3, will it be the same?, will I have to
>> create a special adapter?
>
> Hollo Oswaldo,
>
> "IN" cannot be used, because it's a SQL construct, so "(1, 2, 3)" is
> not something that postgres will understand as a parameter.
>
> You can use "= any (%s)" and pass a list. This is something you can do
> in psycopg2 too, and it's actually a better choice, because it works
> with empty lists too, unless `IN ()`, which is a syntax error for
> Postgres.
>
> What you can do is:
>
> params = ['black','red','green']
> cursor.execute("select * from mytable where field2 = any(%s)", (params,))
>
> interesting fact: "= any" is what postgres really uses internally,
> even if you use the "IN ()" syntax:
>
> piro=# explain select * from mytable where myint in (1,2,3);
> QUERY PLAN
> ---------------------------------------------------------
> Seq Scan on mytable (cost=0.00..45.06 rows=38 width=4)
> Filter: (myint = ANY ('{1,2,3}'::integer[]))
> (2 rows)
>
> -- Daniele
>
Thank for your replies, I will use "=any(params list)" in psycopg3
The second question is: if psycopg3 is going to do the automatic cast of
the types, will it be able to distinguish between a json and a list of
values?.
Example:
data = ["a", "b", "c"]
idList = [4,7,2]
cursor.execute("update mytable set jsfield=%s where id = any(%s)",
(data, idList))
What will be the correct syntax in this case?
Thanks,
--
Oswaldo Hernández
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Adaptation in psycopg3
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` Re: Adaptation in psycopg3 listas <[email protected]>
2020-11-25 14:45 ` Re: Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 18:00 ` Re: Adaptation in psycopg3 listas <[email protected]>
@ 2020-11-25 19:02 ` Daniele Varrazzo <[email protected]>
2020-11-25 20:05 ` Re: Adaptation in psycopg3 listas <[email protected]>
0 siblings, 1 reply; 7+ messages in thread
From: Daniele Varrazzo @ 2020-11-25 19:02 UTC (permalink / raw)
To: listas <[email protected]>; +Cc: psycopg
On Wed, 25 Nov 2020 at 18:00, listas <[email protected]> wrote:
> Thank for your replies, I will use "=any(params list)" in psycopg3
No problem. But if it was not clear, this is something that works
already in psycopg2 too: it could be useful if you want to port code
later.
> The second question is: if psycopg3 is going to do the automatic cast of
> the types, will it be able to distinguish between a json and a list of
> values?.
> Example:
>
> data = ["a", "b", "c"]
> idList = [4,7,2]
>
> cursor.execute("update mytable set jsfield=%s where id = any(%s)",
> (data, idList))
>
> What will be the correct syntax in this case?
You would do like in psycopg2. There isn't a single json type in
python (it could be a list, dict, number, None...) so there is a
"Json" wrapper to tell psycopg to pass e.g. a json number rather than
a number-number:
https://www.psycopg.org/docs/extras.html?highlight=json#json-adaptation
What you would do, both in psycopg2 and 3, would be something like:
cursor.execute("update mytable set jsfield=%s where id = any(%s)",
(Json(data), idList))
Code like this should work in both versions.
Cheers
-- Daniele
^ permalink raw reply [nested|flat] 7+ messages in thread
* Re: Adaptation in psycopg3
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` Re: Adaptation in psycopg3 listas <[email protected]>
2020-11-25 14:45 ` Re: Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 18:00 ` Re: Adaptation in psycopg3 listas <[email protected]>
2020-11-25 19:02 ` Re: Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
@ 2020-11-25 20:05 ` listas <[email protected]>
0 siblings, 0 replies; 7+ messages in thread
From: listas @ 2020-11-25 20:05 UTC (permalink / raw)
To: Daniele Varrazzo <[email protected]>; +Cc: psycopg
...
>
> What you would do, both in psycopg2 and 3, would be something like:
>
> cursor.execute("update mytable set jsfield=%s where id = any(%s)",
> (Json(data), idList))
>
> Code like this should work in both versions.
>
perfect :) thank you very much
--
Oswaldo Hernández
^ permalink raw reply [nested|flat] 7+ messages in thread
end of thread, other threads:[~2020-11-25 20:05 UTC | newest]
Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-11-24 16:52 Adaptation in psycopg3 Daniele Varrazzo <[email protected]>
2020-11-25 12:29 ` listas <[email protected]>
2020-11-25 14:35 ` Adrian Klaver <[email protected]>
2020-11-25 14:45 ` Daniele Varrazzo <[email protected]>
2020-11-25 18:00 ` listas <[email protected]>
2020-11-25 19:02 ` Daniele Varrazzo <[email protected]>
2020-11-25 20:05 ` listas <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox