You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'll start with the question as I am not entirely certain if it is even possible; Is there a way to create a queries.Session using a connection_factory? And if not, would it be possible to add such support?
Or....
perhaps there is another way to accomplish the following...?
In cases where I wanted to use a cursor which supported both a NamedTuple result and also using a LoggingCursor I would combine the two cursors but I would also pass in the connection_factory which sets up the connection to utilize logging for the cursor. Is there some way of doing this in queries?
Set up your custom cursor and connection classes (MyLoggingCursor, MixinLoggedNamedTupleCursor, and MyLoggingConnection) using psycopg2.
Create a psycopg2 connection with your custom connection_factory.
Use the established psycopg2 connection to create a queries.Session object.
Continue using the queries.Session as usual for your queries.
Here's a modified version of your code to illustrate this approach:
importpsycopg2frompsycopg2.extrasimportNamedTupleCursor, LoggingCursor, LoggingConnectionimporttimeimportqueriesclassMyLoggingCursor(LoggingCursor):
# ... your custom cursor logic ...classMyLoggingConnection(LoggingConnection):
# ... your custom connection logic ...classMixinLoggedNamedTupleCursor(MyLoggingCursor, NamedTupleCursor):
pass# Create a psycopg2 connection with the custom connection factorydb_conn=psycopg2.connect(
host=db_host,
port=db_port,
user=db_user,
password=db_pass,
database=db_name,
connect_timeout=timeout,
connection_factory=MyLoggingConnection
)
# Create a queries.Session using the established psycopg2 connectiondb_session=queries.Session(db_conn)
# Now you can use db_session for your queriesforrowindb_session.query('SELECT * FROM foo'):
print(row)
# Don't forget to close the connection and session when donedb_session.close()
db_conn.close()
I'll start with the question as I am not entirely certain if it is even possible; Is there a way to create a
queries.Session
using a connection_factory? And if not, would it be possible to add such support?Or....
perhaps there is another way to accomplish the following...?
In cases where I wanted to use a cursor which supported both a NamedTuple result and also using a
LoggingCursor
I would combine the two cursors but I would also pass in theconnection_factory
which sets up the connection to utilize logging for the cursor. Is there some way of doing this inqueries
?The text was updated successfully, but these errors were encountered: