Skip to content

Commit

Permalink
Make Connection.parameter_statuses a dict
Browse files Browse the repository at this point in the history
Previously Connection.parameter_statuses was a list of raw key values,
but now we decode them to strs and put them in a dictionary. Hopefully
this is more convenient for people.
  • Loading branch information
tlocke committed Jan 2, 2024
1 parent a93944e commit 796cd01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
24 changes: 21 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,7 @@ PostgreSQL `notices
<https://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html>`_ are
stored in a deque called ``Connection.notices`` and added using the ``append()``
method. Similarly there are ``Connection.notifications`` for `notifications
<https://www.postgresql.org/docs/current/static/sql-notify.html>`_ and
``Connection.parameter_statuses`` for changes to the server configuration. Here's an
example:
<https://www.postgresql.org/docs/current/static/sql-notify.html>`_. Here's an example:

>>> import pg8000.native
>>>
Expand All @@ -293,6 +291,26 @@ example:
>>> con.close()


Parameter Statuses
``````````````````

`Certain parameter values are reported by the server automatically at connection startup or whenever
their values change
<https://www.postgresql.org/docs/current/libpq-status.html#LIBPQ-PQPARAMETERSTATUS>`_ and pg8000
stores the latest values in a dict called ``Connection.parameter_statuses``. Here's an example where
we set the ``aplication_name`` parameter and then read it from the ``parameter_statuses``:

>>> import pg8000.native
>>>
>>> con = pg8000.native.Connection(
... "postgres", password="cpsnow", application_name='AGI')
>>>
>>> con.parameter_statuses['application_name']
'AGI'
>>>
>>> con.close()


LIMIT ALL
`````````

Expand Down
16 changes: 8 additions & 8 deletions pg8000/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def __init__(
)
self.notifications = deque(maxlen=100)
self.notices = deque(maxlen=100)
self.parameter_statuses = deque(maxlen=100)
self.parameter_statuses = {}

if user is None:
raise InterfaceError("The 'user' connection parameter cannot be None")
Expand Down Expand Up @@ -842,20 +842,20 @@ def handle_NOTICE_RESPONSE(self, data, context):

def handle_PARAMETER_STATUS(self, data, context):
pos = data.find(NULL_BYTE)
key, value = data[:pos], data[pos + 1 : -1]
self.parameter_statuses.append((key, value))
if key == b"client_encoding":
encoding = value.decode("ascii").lower()
key, value = data[:pos].decode("ascii"), data[pos + 1 : -1].decode("ascii")
self.parameter_statuses[key] = value
if key == "client_encoding":
encoding = value.lower()
self._client_encoding = PG_PY_ENCODINGS.get(encoding, encoding)

elif key == b"integer_datetimes":
if value == b"on":
elif key == "integer_datetimes":
if value == "on":
pass

else:
pass

elif key == b"server_version":
elif key == "server_version":
pass


Expand Down

0 comments on commit 796cd01

Please sign in to comment.