diff --git a/web/pgadmin/tools/datagrid/templates/datagrid/index.html b/web/pgadmin/tools/datagrid/templates/datagrid/index.html index 7f1c468e..5dd0bdcd 100644 --- a/web/pgadmin/tools/datagrid/templates/datagrid/index.html +++ b/web/pgadmin/tools/datagrid/templates/datagrid/index.html @@ -342,7 +342,6 @@ - {% endblock %} diff --git a/web/pgadmin/tools/sqleditor/__init__.py b/web/pgadmin/tools/sqleditor/__init__.py index aa26e087..51f81297 100644 --- a/web/pgadmin/tools/sqleditor/__init__.py +++ b/web/pgadmin/tools/sqleditor/__init__.py @@ -1393,29 +1393,16 @@ def save_file(): @login_required def start_query_download_tool(trans_id): sync_conn = None - (status, error_msg, conn, trans_obj, + (status, error_msg, sync_conn, trans_obj, session_obj) = check_transaction_status(trans_id) - if status and conn is not None and \ + if status and sync_conn is not None and \ trans_obj is not None and session_obj is not None: data = request.args if request.args else None try: if data and 'query' in data: sql = data['query'] - conn_id = str(random.randint(1, 9999999)) - sync_conn = conn.manager.connection( - did=trans_obj.did, - conn_id=conn_id, - auto_reconnect=False, - async_=False - ) - - sync_conn.connect(autocommit=False) - - def cleanup(): - conn.manager.connections[sync_conn.conn_id]._release() - del conn.manager.connections[sync_conn.conn_id] # This returns generator of records. status, gen = sync_conn.execute_on_server_as_csv( @@ -1427,7 +1414,6 @@ def start_query_download_tool(trans_id): r.headers[ "Content-Disposition" ] = "attachment;filename=error.csv" - r.call_on_close(cleanup) return r r = Response( @@ -1459,13 +1445,11 @@ def start_query_download_tool(trans_id): "Content-Disposition" ] = "attachment;filename={0}".format(filename) - r.call_on_close(cleanup) return r except Exception as e: r = Response('"{0}"'.format(e), mimetype='text/csv') r.headers["Content-Disposition"] = "attachment;filename=error.csv" - r.call_on_close(cleanup) return r else: return internal_server_error( diff --git a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js index 65ea1629..f0636e9c 100644 --- a/web/pgadmin/tools/sqleditor/static/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/static/js/sqleditor.js @@ -3517,7 +3517,6 @@ define('tools.querytool', [ // Trigger query result download to csv. trigger_csv_download: function(query, filename) { var self = this, - link = $(this.container).find('#download-csv'), url = url_for('sqleditor.query_tool_download', { 'trans_id': self.transId, }); @@ -3526,7 +3525,48 @@ define('tools.querytool', [ query: query, filename: filename, }); - link.attr('src', url); + + // Get the CSV file + $.ajax({ + typs: 'GET', + url: url, + cache: false, + async: true, + xhrFields: { + responseType: 'blob', + }, + beforeSend: function() { + // Disable the Execute button + $('#btn-flash').prop('disabled', true); + + self.trigger( + 'pgadmin-sqleditor:loading-icon:show', + gettext('Downloading CSV...') + ); + }, + }) + .done(function(response) { + let urlCreator = window.URL || window.webkitURL, + url = urlCreator.createObjectURL(response), + link = document.createElement('a'); + + link.setAttribute('href', url); + link.setAttribute('download', filename); + link.click(); + + // Enable the execute button + $('#btn-flash').prop('disabled', false); + self.trigger( + 'pgadmin-sqleditor:loading-icon:hide'); + + }) + .fail(function(err) { + let msg = httpErrorHandler.handleQueryToolAjaxError( + pgAdmin, self, err, gettext('Download CSV'), [], true + ); + alertify.alert(gettext('Download CSV error'), msg); + }); + }, call_cache_preferences: function() { diff --git a/web/pgadmin/utils/driver/psycopg2/connection.py b/web/pgadmin/utils/driver/psycopg2/connection.py index bbbf0bc7..012266d2 100644 --- a/web/pgadmin/utils/driver/psycopg2/connection.py +++ b/web/pgadmin/utils/driver/psycopg2/connection.py @@ -673,7 +673,7 @@ WHERE Returns: Generator response """ - status, cur = self.__cursor(server_cursor=True) + status, cur = self.__cursor() self.row_count = 0 if not status: