pgadmin4/web/pgadmin/tools/sqleditor/utils/query_history.py
Aditya Toshniwal b5b9ee46a1 1) Port query tool to React. Fixes #6131
2) Added status bar to the Query Tool. Fixes #3253
3) Ensure that row numbers should be visible in view when scrolling horizontally. Fixes #3989
4) Allow removing a single query history. Refs #4113
5) Partially fixed Macros usability issues. Ref #6969
6) Fixed an issue where the Query tool opens on minimum size if the user opens multiple query tool Window quickly. Fixes #6725
7) Relocate GIS Viewer Button to the Left Side of the Results Table. Fixes #6830
8) Fixed an issue where the connection bar is not visible. Fixes #7188
9) Fixed an issue where an Empty message popup after running a query. Fixes #7260
10) Ensure that Autocomplete should work after changing the connection. Fixes #7262
11) Fixed an issue where the copy and paste row does not work if the first column contains no data. Fixes #7294
2022-04-07 17:36:56 +05:30

145 lines
5.0 KiB
Python

from pgadmin.utils.ajax import make_json_response
from pgadmin.model import db, QueryHistoryModel
from config import MAX_QUERY_HIST_STORED
import json
class QueryHistory:
@staticmethod
def get(uid, sid, dbname):
result = db.session \
.query(QueryHistoryModel.query_info) \
.filter(QueryHistoryModel.uid == uid,
QueryHistoryModel.sid == sid,
QueryHistoryModel.dbname == dbname) \
.all()
result = [rec.query_info for rec in list(result)]
return make_json_response(
data={
'status': True,
'msg': '',
'result': result
}
)
@staticmethod
def update_history_dbname(uid, sid, old_dbname, new_dbname):
try:
db.session \
.query(QueryHistoryModel) \
.filter(QueryHistoryModel.uid == uid,
QueryHistoryModel.sid == sid,
QueryHistoryModel.dbname == old_dbname) \
.update({QueryHistoryModel.dbname: new_dbname})
db.session.commit()
except Exception:
db.session.rollback()
# do not affect query execution if history clear fails
@staticmethod
def save(uid, sid, dbname, request):
try:
max_srno = db.session\
.query(db.func.max(QueryHistoryModel.srno)) \
.filter(QueryHistoryModel.uid == uid,
QueryHistoryModel.sid == sid,
QueryHistoryModel.dbname == dbname)\
.scalar()
# if no records present
if max_srno is None:
new_srno = 1
else:
new_srno = max_srno + 1
# last updated flag is used to recognise the last
# inserted/updated record.
# It is helpful to cycle the records
last_updated_rec = db.session.query(QueryHistoryModel) \
.filter(QueryHistoryModel.uid == uid,
QueryHistoryModel.sid == sid,
QueryHistoryModel.dbname == dbname,
QueryHistoryModel.last_updated_flag == 'Y') \
.first()
# there should be a last updated record
# if not present start from sr no 1
if last_updated_rec is not None:
last_updated_rec.last_updated_flag = 'N'
# if max limit reached then recycle
if new_srno > MAX_QUERY_HIST_STORED:
new_srno = (
last_updated_rec.srno % MAX_QUERY_HIST_STORED) + 1
else:
new_srno = 1
# if the limit is lowered and number of records present is
# more, then cleanup
if max_srno > MAX_QUERY_HIST_STORED:
db.session.query(QueryHistoryModel)\
.filter(QueryHistoryModel.uid == uid,
QueryHistoryModel.sid == sid,
QueryHistoryModel.dbname == dbname,
QueryHistoryModel.srno >
MAX_QUERY_HIST_STORED)\
.delete()
history_entry = QueryHistoryModel(
srno=new_srno, uid=uid, sid=sid, dbname=dbname,
query_info=request.data, last_updated_flag='Y')
db.session.merge(history_entry)
db.session.commit()
except Exception:
db.session.rollback()
# do not affect query execution if history saving fails
return make_json_response(
data={
'status': True,
'msg': 'Success',
}
)
@staticmethod
def clear_history(uid, sid, dbname=None, filter=None):
try:
filters = [
QueryHistoryModel.uid == uid,
QueryHistoryModel.sid == sid
]
if dbname is not None:
filters.append(QueryHistoryModel.dbname == dbname)
history = db.session.query(QueryHistoryModel) \
.filter(*filters)
if filter is not None:
for row in history:
query_info = json.loads(row.query_info.decode())
if query_info['query'] == filter['query'] and \
query_info['start_time'] == filter['start_time']:
db.session.delete(row)
else:
history.delete()
db.session.commit()
except Exception:
db.session.rollback()
# do not affect query execution if history clear fails
@staticmethod
def clear(uid, sid, dbname=None, filter=None):
QueryHistory.clear_history(uid, sid, dbname, filter)
return make_json_response(
data={
'status': True,
'msg': 'Success',
}
)