@@ -51,6 +51,7 @@ export class ResultSetUtils {
5151 this . historyQuerySource = null ;
5252 this . hasQueryCommitted = false ;
5353 this . queryToolCtx = queryToolCtx ;
54+ this . setLoaderText = null ;
5455 }
5556
5657 static generateURLReconnectionFlag ( baseUrl , transId , shouldReconnect ) {
@@ -445,23 +446,72 @@ export class ResultSetUtils {
445446 ) ;
446447 }
447448
448- saveData ( reqData ) {
449+ saveData ( reqData , shouldReconnect ) {
450+ // Generate the URL with the optional `connect=1` parameter.
451+ const url = ResultSetUtils . generateURLReconnectionFlag ( 'sqleditor.save' , this . transId , shouldReconnect ) ;
452+
449453 return this . api . post (
450- url_for ( 'sqleditor.save' , {
451- 'trans_id' : this . transId
452- } ) ,
454+ url ,
453455 JSON . stringify ( reqData )
454- ) . then ( response => {
456+ ) . then ( ( response ) => {
455457 if ( response . data ?. data ?. status ) {
456458 // Set the commit flag to true if the save was successful
457459 this . hasQueryCommitted = true ;
458460 }
459461 return response ;
460- } ) . catch ( ( error ) => {
461- // Set the commit flag to false if there was an error
462- this . hasQueryCommitted = false ;
463- throw error ;
464- } ) ;
462+ } )
463+ . catch ( async ( error ) => {
464+ if ( error . response ?. status === 428 ) {
465+ // Handle 428: Show password dialog.
466+ return new Promise ( ( resolve , reject ) => {
467+ this . connectServerModal (
468+ error . response ?. data ?. result ,
469+ async ( formData ) => {
470+ try {
471+ await this . connectServer (
472+ this . queryToolCtx . params . sid ,
473+ this . queryToolCtx . params . user ,
474+ formData ,
475+ async ( ) => {
476+ let retryRespData = await this . saveData ( reqData ) ;
477+ // Set the commit flag to true if the save was successful
478+ this . hasQueryCommitted = true ;
479+ pgAdmin . Browser . notifier . success ( gettext ( 'Server Connected.' ) ) ;
480+ resolve ( retryRespData ) ;
481+ }
482+ ) ;
483+
484+ } catch ( retryError ) {
485+ reject ( retryError ) ;
486+ }
487+ } ,
488+ ( ) => this . setLoaderText ( null )
489+ ) ;
490+ } ) ;
491+ } else if ( error . response ?. status === 503 ) {
492+ // Handle 503: Fire HANDLE_API_ERROR and wait for connectionLostCallback.
493+ return new Promise ( ( resolve , reject ) => {
494+ this . eventBus . fireEvent ( QUERY_TOOL_EVENTS . HANDLE_API_ERROR , error , {
495+ connectionLostCallback : async ( ) => {
496+ try {
497+ // Retry saveData with connect=1
498+ let retryRespData = await this . saveData ( reqData , true ) ;
499+ resolve ( retryRespData ) ;
500+ } catch ( retryError ) {
501+ reject ( retryError ) ;
502+ }
503+ } ,
504+ checkTransaction : true ,
505+ cancelCallback : ( ) => this . setLoaderText ( null )
506+ } ,
507+ ) ;
508+ } ) ;
509+ } else {
510+ // Set the commit flag to false if there was an error
511+ this . hasQueryCommitted = false ;
512+ throw error ;
513+ }
514+ } ) ;
465515 }
466516
467517 async saveResultsToFile ( fileName ) {
@@ -861,6 +911,8 @@ export function ResultSet() {
861911
862912 rsu . current . setEventBus ( eventBus ) ;
863913 rsu . current . setQtPref ( queryToolCtx . preferences ?. sqleditor ) ;
914+ // To use setLoaderText to the ResultSetUtils.
915+ rsu . current . setLoaderText = setLoaderText ;
864916
865917 const isDataChanged = ( ) => {
866918 return Boolean ( _ . size ( dataChangeStore . updated ) || _ . size ( dataChangeStore . added ) || _ . size ( dataChangeStore . deleted ) ) ;
0 commit comments