public inbox for [email protected]  
help / color / mirror / Atom feed
From: Vladimir Ryabtsev <[email protected]>
To: [email protected]
Subject: Using composite types in psycopg3
Date: Tue, 10 Nov 2020 13:59:31 -0800
Message-ID: <CAMqTPqm7H4yVk0qGA8=TyTtVsNX88bo8x1BJkHc6vrOVYyW=RQ@mail.gmail.com> (raw)

Hello,

I have tried psycopg3 very briefly and I have a question.

I have a couple of use cases in systems I currently work with
that prevented (or seriously limited) usage of psycopg2, so
I had to use other drivers. This generally relates to accessing
composite types that you can construct on-the-fly in SQL queries.
Consider the following example:

'''
import psycopg2

def get_query(fpath):
    with open(fpath, 'rt') as f:
        return f.read()

def main():
    conn = psycopg2.connect('postgres://user:password@host/db')
    cur = conn.cursor()
    cur.execute(get_query('query.sql'))
    result = cur.fetchone()[0]
    print(type(result), result)
    cur.close()
    conn.close()

if __name__ == '__main__':
    main()
'''

Where query.sql is:

'''
with test as (
    select 1 as id, 'one' val
    union all
    select 1, 'one more'
    union all
    select 2, 'two'
)
select array(
    select (id, array_agg(val))
    from test
    group by id
)
'''

psycopg2 returns the 'result' as a basic string, while
in asyncpg and py-postgresql I have structured data
(roughly 'List[Tuple[int, List[str]]]').

I tried the same in psycopg3 and it is little bit better, but
not entirely: it shows the outer list, the tuples inside it,
but the innermost list is still represented as a basic string:
'{one,"one more"}'.

Is it something you are still working on? Any workarounds?

Vladimir


view thread (4+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected]
  Subject: Re: Using composite types in psycopg3
  In-Reply-To: <CAMqTPqm7H4yVk0qGA8=TyTtVsNX88bo8x1BJkHc6vrOVYyW=RQ@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox