1
0
Fork 0

Add some comments about porting from psycopg 2 to 3 (#1318)

This commit is contained in:
Daniele Varrazzo 2022-02-22 18:02:16 +01:00 committed by GitHub
parent 02134daad7
commit 25eb9f541b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

View File

@ -119,6 +119,7 @@ Contributors:
* Eric R Young (ERYoung11) * Eric R Young (ERYoung11)
* Paweł Sacawa (psacawa) * Paweł Sacawa (psacawa)
* Bruno Inec (sweenu) * Bruno Inec (sweenu)
* Daniele Varrazzo
Creator: Creator:
-------- --------

View File

@ -80,6 +80,8 @@ except ImportError:
from getpass import getuser from getpass import getuser
from psycopg2 import OperationalError, InterfaceError from psycopg2 import OperationalError, InterfaceError
# pg3: https://www.psycopg.org/psycopg3/docs/api/conninfo.html
from psycopg2.extensions import make_dsn, parse_dsn from psycopg2.extensions import make_dsn, parse_dsn
import psycopg2 import psycopg2
@ -1580,12 +1582,15 @@ def format_output(title, cur, headers, status, settings):
if hasattr(cur, "description"): if hasattr(cur, "description"):
column_types = [] column_types = []
for d in cur.description: for d in cur.description:
# pg3: type_name = cur.adapters.types[d.type_code].name
if ( if (
# pg3: type_name in ("numeric", "float4", "float8")
d[1] in psycopg2.extensions.DECIMAL.values d[1] in psycopg2.extensions.DECIMAL.values
or d[1] in psycopg2.extensions.FLOAT.values or d[1] in psycopg2.extensions.FLOAT.values
): ):
column_types.append(float) column_types.append(float)
if ( if (
# pg3: type_name in ("int2", "int4", "int8")
d[1] == psycopg2.extensions.INTEGER.values d[1] == psycopg2.extensions.INTEGER.values
or d[1] in psycopg2.extensions.LONGINTEGER.values or d[1] in psycopg2.extensions.LONGINTEGER.values
): ):

View File

@ -16,6 +16,7 @@ _logger = logging.getLogger(__name__)
# Cast all database input to unicode automatically. # Cast all database input to unicode automatically.
# See http://initd.org/psycopg/docs/usage.html#unicode-handling for more info. # See http://initd.org/psycopg/docs/usage.html#unicode-handling for more info.
# pg3: These should be automatic: unicode is the default
ext.register_type(ext.UNICODE) ext.register_type(ext.UNICODE)
ext.register_type(ext.UNICODEARRAY) ext.register_type(ext.UNICODEARRAY)
ext.register_type(ext.new_type((705,), "UNKNOWN", ext.UNICODE)) ext.register_type(ext.new_type((705,), "UNKNOWN", ext.UNICODE))
@ -32,6 +33,8 @@ _WAIT_SELECT_TIMEOUT = 1
_wait_callback_is_set = False _wait_callback_is_set = False
# pg3: it is already "green" but Ctrl-C breaks the query
# pg3: This should be fixed upstream: https://github.com/psycopg/psycopg/issues/231
def _wait_select(conn): def _wait_select(conn):
""" """
copy-pasted from psycopg2.extras.wait_select copy-pasted from psycopg2.extras.wait_select
@ -74,6 +77,8 @@ def _set_wait_callback(is_virtual_database):
ext.set_wait_callback(_wait_select) ext.set_wait_callback(_wait_select)
# pg3: You can do something like:
# pg3: cnn.adapters.register_loader("date", psycopg.types.string.TextLoader)
def register_date_typecasters(connection): def register_date_typecasters(connection):
""" """
Casts date and timestamp values to string, resolves issues with out of Casts date and timestamp values to string, resolves issues with out of
@ -124,6 +129,7 @@ def register_json_typecasters(conn, loads_fn):
return available return available
# pg3: Probably you don't need this because by default unknown -> unicode
def register_hstore_typecaster(conn): def register_hstore_typecaster(conn):
""" """
Instead of using register_hstore() which converts hstore into a python Instead of using register_hstore() which converts hstore into a python
@ -142,6 +148,7 @@ def register_hstore_typecaster(conn):
pass pass
# pg3: I don't know what is this
class ProtocolSafeCursor(psycopg2.extensions.cursor): class ProtocolSafeCursor(psycopg2.extensions.cursor):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.protocol_error = False self.protocol_error = False
@ -392,6 +399,7 @@ class PGExecute:
return json_data return json_data
def failed_transaction(self): def failed_transaction(self):
# pg3: self.conn.info.transaction_status == psycopg.pq.TransactionStatus.INERROR
status = self.conn.get_transaction_status() status = self.conn.get_transaction_status()
return status == ext.TRANSACTION_STATUS_INERROR return status == ext.TRANSACTION_STATUS_INERROR
@ -541,6 +549,8 @@ class PGExecute:
def view_definition(self, spec): def view_definition(self, spec):
"""Returns the SQL defining views described by `spec`""" """Returns the SQL defining views described by `spec`"""
# pg3: you may want to use `psycopg.sql` for client-side composition
# pg3: (also available in psycopg2 by the way)
template = "CREATE OR REPLACE {6} VIEW {0}.{1} AS \n{3}" template = "CREATE OR REPLACE {6} VIEW {0}.{1} AS \n{3}"
# 2: relkind, v or m (materialized) # 2: relkind, v or m (materialized)
# 4: reloptions, null # 4: reloptions, null