Skip to content

Commit 6175a1c

Browse files
Fixed an issue where syntax error was not highlighting for explain query. #6887
1 parent 51b02ae commit 6175a1c

File tree

6 files changed

+46
-9
lines changed

6 files changed

+46
-9
lines changed

web/pgadmin/static/js/api_instance.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ export default function getApiInstance(headers={}) {
2323
});
2424
}
2525

26-
export function parseApiError(error) {
26+
export function parseApiError(error, withData=false) {
2727
if (error.response) {
2828
// The request was made and the server responded with a status code
2929
// that falls out of the range of 2xx
3030
if(error.response.headers['content-type'] == 'application/json') {
31-
return error.response.data.errormsg;
31+
let errormsg = error.response.data.errormsg;
32+
let data = error.response.data.data;
33+
// If we want to use data which came with error set withData
34+
// flag to true.
35+
return withData ? {errormsg, data} : errormsg;
3236
} else {
3337
return error.response.statusText;
3438
}

web/pgadmin/tools/sqleditor/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
from pgadmin.tools.schema_diff.node_registry import SchemaDiffRegistry
5858
from pgadmin.settings import get_setting
5959
from pgadmin.utils.preferences import Preferences
60+
from pgadmin.tools.sqleditor.utils.apply_explain_plan_wrapper import \
61+
get_explain_query_length
6062

6163
MODULE_NAME = 'sqleditor'
6264
TRANSACTION_STATUS_CHECK_FAILED = gettext("Transaction status check failed.")
@@ -956,7 +958,12 @@ def poll(trans_id):
956958
gettext('******* Error *******'),
957959
result
958960
)
959-
return internal_server_error(result)
961+
query_len_data = {
962+
'explain_query_length':
963+
get_explain_query_length(
964+
conn._Connection__async_cursor._query)
965+
}
966+
return internal_server_error(result, query_len_data)
960967
elif status == ASYNC_OK:
961968
status = 'Success'
962969
rows_affected = conn.rows_affected()

web/pgadmin/tools/sqleditor/static/js/components/sections/Query.jsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,17 +253,20 @@ export default function Query() {
253253
cmObj.removeLineClass(markedLine.current, 'wrap', 'CodeMirror-activeline-background');
254254
markedLine.current = 0;
255255
};
256-
const highlightError = (cmObj, result)=>{
256+
const highlightError = (cmObj, {errormsg: result, data})=>{
257257
let errorLineNo = 0,
258258
startMarker = 0,
259259
endMarker = 0,
260-
selectedLineNo = 0;
260+
selectedLineNo = 0,
261+
origQueryLen = cmObj.getValue().length;
261262

262263
removeHighlightError(cmObj);
263264

264265
// In case of selection we need to find the actual line no
265-
if (cmObj.getSelection().length > 0)
266+
if (cmObj.getSelection().length > 0) {
266267
selectedLineNo = cmObj.getCursor(true).line;
268+
origQueryLen = cmObj.getLine(selectedLineNo).length;
269+
}
267270

268271
// Fetch the LINE string using regex from the result
269272
let line = /LINE (\d+)/.exec(result),
@@ -275,6 +278,14 @@ export default function Query() {
275278
errorLineNo = (parseInt(line[1]) - 1) + selectedLineNo;
276279
let errorCharNo = (parseInt(char[1]) - 1);
277280

281+
/* If explain query has been run we need to
282+
calculate the character number.
283+
*/
284+
if(data.explain_query_length) {
285+
let explainQueryLen = data.explain_query_length - parseInt(char[1]);
286+
errorCharNo = origQueryLen - explainQueryLen - 1;
287+
}
288+
278289
/* We need to loop through each line till the error line and
279290
* count the total no of character to figure out the actual
280291
* starting/ending marker point for the individual line. We

web/pgadmin/tools/sqleditor/static/js/components/sections/ResultSet.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ export class ResultSetUtils {
284284
this.eventBus.fireEvent(QUERY_TOOL_EVENTS.FOCUS_PANEL, PANELS.MESSAGES);
285285
this.eventBus.fireEvent(QUERY_TOOL_EVENTS.SET_CONNECTION_STATUS, CONNECTION_STATUS.TRANSACTION_STATUS_INERROR);
286286
if (!flags.external) {
287-
this.eventBus.fireEvent(QUERY_TOOL_EVENTS.HIGHLIGHT_ERROR, parseApiError(error));
287+
this.eventBus.fireEvent(QUERY_TOOL_EVENTS.HIGHLIGHT_ERROR, parseApiError(error, true));
288288
}
289289
this.eventBus.fireEvent(QUERY_TOOL_EVENTS.PUSH_HISTORY, {
290290
status: false,

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,17 @@ def apply_explain_plan_wrapper_if_needed(manager, sql):
2525
return render_template(template_path, sql=sql['sql'], **explain_plan)
2626
else:
2727
return sql['sql']
28+
29+
30+
def get_explain_query_length(query_obj):
31+
"""
32+
This method returns query length if it is explain query.
33+
34+
Args:
35+
query_obj: sql query
36+
"""
37+
query = query_obj.query.decode()
38+
if query.startswith("EXPLAIN"):
39+
return len(query)
40+
else:
41+
return 0

web/pgadmin/utils/ajax.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,13 @@ def make_response(response=None, status=200):
9595
)
9696

9797

98-
def internal_server_error(errormsg=''):
98+
def internal_server_error(errormsg='',data=None):
9999
"""Create a response with HTTP status code 500 - Internal Server Error."""
100100
return make_json_response(
101101
status=500,
102102
success=0,
103-
errormsg=errormsg
103+
errormsg=errormsg,
104+
data=data
104105
)
105106

106107

0 commit comments

Comments
 (0)