Skip to content

Commit d8f0843

Browse files
committed
Fixed an issue when the server/database connection was lost; the filter dialog is not getting saved. #6044
1 parent 168aaa2 commit d8f0843

File tree

6 files changed

+93
-70
lines changed

6 files changed

+93
-70
lines changed

web/pgadmin/tools/sqleditor/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
from flask import Response, url_for, render_template, session, current_app
2424
from flask import request
2525
from flask_babel import gettext
26+
from pgadmin.tools.sqleditor.utils.query_tool_connection_check \
27+
import query_tool_connection_check
2628
from pgadmin.user_login_check import pga_login_required
2729
from flask_security import current_user
2830
from pgadmin.misc.file_manager import Filemanager
@@ -821,13 +823,14 @@ def start_view_data(trans_id):
821823

822824
# Connect to the Server if not connected.
823825
if not default_conn.connected():
824-
view = SchemaDiffRegistry.get_node_view('server')
825-
response = view.connect(trans_obj.sgid,
826-
trans_obj.sid, True)
827-
if response.status_code == 428:
826+
# This will check if view/edit data tool connection is lost or not,
827+
# if lost then it will reconnect
828+
status, error_msg, conn, trans_obj, session_obj, response = \
829+
query_tool_connection_check(trans_id)
830+
# This is required for asking user to enter password
831+
# when password is not saved for the server
832+
if response is not None:
828833
return response
829-
else:
830-
conn = manager.connection(did=trans_obj.did)
831834

832835
status, msg = default_conn.connect()
833836
if not status:

web/pgadmin/tools/sqleditor/command.py

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -566,62 +566,23 @@ def get_primary_keys(self, default_conn=None):
566566
def get_all_columns_with_order(self, default_conn=None):
567567
"""
568568
It is overridden method specially for Table because we all have to
569-
fetch primary keys and rest of the columns both.
569+
fetch primary keys.
570570
571571
Args:
572572
default_conn: Connection object
573573
574574
Returns:
575575
all_sorted_columns: Sorted columns for the Grid
576-
all_columns: List of columns for the select2 options
577576
"""
578-
driver = get_driver(PG_DEFAULT_DRIVER)
579-
if default_conn is None:
580-
manager = driver.connection_manager(self.sid)
581-
conn = manager.connection(did=self.did, conn_id=self.conn_id)
582-
else:
583-
conn = default_conn
584577

585578
all_sorted_columns = []
586579
data_sorting = self.get_data_sorting()
587-
all_columns = []
588-
# Fetch the primary key column names
589-
query = render_template(
590-
"/".join([self.sql_path, 'primary_keys.sql']),
591-
table_name=self.object_name,
592-
table_nspname=self.nsp_name,
593-
conn=conn,
594-
)
595-
596-
status, result = conn.execute_dict(query)
597580

598-
if not status:
599-
raise ExecuteError(result)
600-
601-
for row in result['rows']:
602-
all_columns.append(row['attname'])
603-
604-
# Fetch the rest of the column names
605-
query = render_template(
606-
"/".join([self.sql_path, 'get_columns.sql']),
607-
table_name=self.object_name,
608-
table_nspname=self.nsp_name,
609-
conn=conn,
610-
)
611-
status, result = conn.execute_dict(query)
612-
if not status:
613-
raise ExecuteError(result)
614-
615-
for row in result['rows']:
616-
# Only append if not already present in the list
617-
if row['attname'] not in all_columns:
618-
all_columns.append(row['attname'])
619-
620-
# If user has custom data sorting then pass as it as it is
581+
# If user has custom data sorting then pass as it is
621582
if data_sorting and len(data_sorting) > 0:
622583
all_sorted_columns = data_sorting
623584

624-
return all_sorted_columns, all_columns
585+
return all_sorted_columns
625586

626587
def can_edit(self):
627588
return True

web/pgadmin/tools/sqleditor/utils/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
from .update_session_grid_transaction import update_session_grid_transaction
1313
from .start_running_query import *
1414
from .apply_explain_plan_wrapper import *
15+
from .query_tool_connection_check import *

web/pgadmin/tools/sqleditor/utils/filter_dialog.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ def get(*args):
3737
msg = gettext('Success')
3838

3939
try:
40-
columns, column_list = \
41-
trans_obj.get_all_columns_with_order(conn)
40+
columns = \
41+
trans_obj.get_all_columns_with_order()
42+
column_list = [col_name for col_name in
43+
session_obj['columns_info'].keys()]
4244
except (ConnectionLost, SSHTunnelConnectionLost):
4345
raise
4446
except Exception as e:
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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

web/pgadmin/tools/sqleditor/utils/start_running_query.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
from pgadmin.utils.exception import ConnectionLost, SSHTunnelConnectionLost,\
2929
CryptKeyMissing
3030
from pgadmin.utils.constants import ERROR_MSG_TRANS_ID_NOT_FOUND
31-
from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry
3231

3332

3433
class StartRunningQuery:
@@ -82,26 +81,15 @@ def execute(self, sql, trans_id, http_session, connect=False):
8281

8382
# Connect to the Server if not connected.
8483
if connect and not conn.connected():
85-
view = SchemaDiffRegistry.get_node_view('server')
86-
response = view.connect(transaction_object.sgid,
87-
transaction_object.sid, True)
88-
if response.status_code == 428:
84+
from pgadmin.tools.sqleditor.utils import \
85+
query_tool_connection_check
86+
87+
_, _, _, _, _, response = \
88+
query_tool_connection_check(trans_id)
89+
# This is required for asking user to enter password
90+
# when password is not saved for the server
91+
if response is not None:
8992
return response
90-
else:
91-
conn = manager.connection(
92-
did=transaction_object.did,
93-
conn_id=self.connection_id,
94-
auto_reconnect=False,
95-
use_binary_placeholder=True,
96-
array_to_string=True,
97-
**({"database": transaction_object.dbname} if hasattr(
98-
transaction_object, 'dbname') else {}))
99-
100-
status, msg = conn.connect()
101-
if not status:
102-
self.logger.error(msg)
103-
return internal_server_error(errormsg=str(msg))
104-
10593
effective_sql_statement = apply_explain_plan_wrapper_if_needed(
10694
manager, sql)
10795

0 commit comments

Comments
 (0)