From spycopg2 to psycopg3 data adaptation

From: Paolo De Stefani <paolo(at)paolodestefani(dot)it>
To: Psycopg <psycopg(at)postgresql(dot)org>
Subject: From spycopg2 to psycopg3 data adaptation
Date: 2021-10-05 11:29:52
Message-ID: 4c05fd7ddf571bfc82baeede1449f7f0@paolodestefani.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Hi
Still working on migration to psycopg3 of my (not so) small
application...
I use PyQt\PySide for user interface and i convert some postgresql data
types to Qt types.
For example to convert timestamptz to QDateTime in psycopg2 i use:

def cast_timestamp_qdatetime(value, cur):
if value is None:
return None
dt = QDateTime.fromString(value[:23], "yyyy-MM-dd HH:mm:ss.zzz")
if not dt.isValid(): # no milliseconds
dt = QDateTime.fromString(value[:19], "yyyy-MM-dd HH:mm:ss")
return dt

QDATETIME = psycopg2.extensions.new_type((1184,), "QDATETIME",
cast_timestamp_qdatetime)
psycopg2.extensions.register_type(QDATETIME)

I tryed this to achieve the same result in psycopg3:

class TimestamptzQDateTimeLoader(Loader):
def load(self, value):
if value is None:
return None
print(value)
dt = QDateTime.fromString(value[:23], "yyyy-MM-dd HH:mm:ss.zzz")
if not dt.isValid(): # no milliseconds
dt = QDateTime.fromString(value[:19], "yyyy-MM-dd HH:mm:ss")
return dt

psycopg.adapters.register_loader('timestamptz',
TimestamptzQDateTimeLoader)

BUT it's not working because the "value" is binary not string:

<memory at 0x000000000939BA00>

So how i can get the same result of psycopg2 in psycopg3 ?

--
Paolo De Stefani

Responses

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2021-10-05 12:05:12 Re: From spycopg2 to psycopg3 data adaptation
Previous Message Daniele Varrazzo 2021-10-04 23:12:47 Re: How to build statically on Windows