Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Public API similar to the private Cursor._query. #696

Open
felixxm opened this issue Dec 11, 2023 · 1 comment
Open

Public API similar to the private Cursor._query. #696

felixxm opened this issue Dec 11, 2023 · 1 comment

Comments

@felixxm
Copy link

felixxm commented Dec 11, 2023

It seems that we would like to restore a feature in Django based on the Cursor._query attribute, so I open this issue as per its docs: "If you would like to build reliable features using this object, please get in touch so we can try and design an useful interface for it."

Django provides a wrapper around database query executions. Unfortunately, it's not logged (and cannot be debug) properly with psycopg because we don't have access to the query that was actually sent to the database. Check out the complete report.

What do you think about extending psycopg's API?

@dvarrazzo
Copy link
Member

I understand that Django uses client cursors, right?

I would rather keep the flexibility of changing the query object. However what you need seems a rather stable attribute:

>>> import psycopg
>>> conn = psycopg.connect("", autocommit=True)
>>> cur = psycopg.ClientCursor(conn)
>>> cur.execute("select %s", [1])
>>> cur._query
<psycopg._queries.PostgresClientQuery at 0x7f7d593580b0>
>>> cur._query.query
b'select 1'

Is this ok? If so I propose:

  • use the _query object as it currently is. For instance:
    if is_psycopg3:

        def last_executed_query(self, cursor, sql, params):
            if cursor._query and cursor._query.query is not None:
                return cursor._query.query.decode(cursor.connection.info.encoding, "replace")
            return None
  • add a test to exercise the feature in the test suite.

Should we break this interface, we will know as we run Django tests and I can provide an alternative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants