|
| 1 | +########################################################################## |
| 2 | +# |
| 3 | +# pgAdmin 4 - PostgreSQL Tools |
| 4 | +# |
| 5 | +# Copyright (C) 2013 - 2025, The pgAdmin Development Team |
| 6 | +# This software is released under the PostgreSQL Licence |
| 7 | +# |
| 8 | +########################################################################## |
| 9 | + |
| 10 | +"""Check for query tool connection""" |
| 11 | +import pickle |
| 12 | +from flask_babel import gettext |
| 13 | + |
| 14 | +from config import PG_DEFAULT_DRIVER |
| 15 | +from pgadmin.utils.ajax import internal_server_error |
| 16 | +from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry |
| 17 | +from pgadmin.tools.sqleditor.utils.start_running_query import StartRunningQuery |
| 18 | +from flask import Response, current_app, session |
| 19 | + |
| 20 | +from pgadmin.utils.driver import get_driver |
| 21 | + |
| 22 | + |
| 23 | +def query_tool_connection_check(trans_id): |
| 24 | + # This function will check if the query tool has the connection or not |
| 25 | + # if not then establishes the connection. |
| 26 | + session_obj = StartRunningQuery.retrieve_session_information( |
| 27 | + session, |
| 28 | + trans_id |
| 29 | + ) |
| 30 | + if isinstance(session_obj, Response): |
| 31 | + return session_obj |
| 32 | + |
| 33 | + transaction_object = pickle.loads(session_obj['command_obj']) |
| 34 | + |
| 35 | + # To verify if the transaction details for the specific query tool |
| 36 | + # or View/Edit Data tool is available or not and if the server is |
| 37 | + # disconnected from the Object Explorer then it reconnects |
| 38 | + if transaction_object is not None and session_obj is not None: |
| 39 | + view = SchemaDiffRegistry.get_node_view('server') |
| 40 | + response = view.connect(transaction_object.sgid, |
| 41 | + transaction_object.sid, True) |
| 42 | + # This is required for asking user to enter password |
| 43 | + # when password is not saved for the server |
| 44 | + if response.status_code == 428: |
| 45 | + return False, None, None, None, None, response |
| 46 | + else: |
| 47 | + manager = get_driver( |
| 48 | + PG_DEFAULT_DRIVER).connection_manager( |
| 49 | + transaction_object.sid) |
| 50 | + conn = manager.connection( |
| 51 | + did=transaction_object.did, |
| 52 | + conn_id=transaction_object.conn_id, |
| 53 | + auto_reconnect=False, |
| 54 | + use_binary_placeholder=True, |
| 55 | + array_to_string=True, |
| 56 | + **({"database": transaction_object.dbname} if hasattr( |
| 57 | + transaction_object, 'dbname') else {})) |
| 58 | + |
| 59 | + status, msg = conn.connect() |
| 60 | + if not status: |
| 61 | + current_app.logger.error(msg) |
| 62 | + return internal_server_error(errormsg=str(msg)) |
| 63 | + return status, None, conn, transaction_object, session_obj, None |
| 64 | + else: |
| 65 | + status = False |
| 66 | + error_msg = gettext( |
| 67 | + 'Either transaction object or session object not found.') |
| 68 | + return status, error_msg, None, None, None, None |
0 commit comments