@@ -68,6 +68,7 @@ export default function Query({onTextSelect, setQtStatePartial}) {
6868 const preferencesStore = usePreferences ( ) ;
6969 const modalId = MODAL_DIALOGS . QT_CONFIRMATIONS ;
7070 const fmUtilsObj = useMemo ( ( ) => new FileManagerUtils ( queryToolCtx . api , { } ) , [ ] ) ;
71+ const initialTransIdRef = React . useRef ( queryToolCtx . params . trans_id ) ;
7172
7273 const highlightError = ( cmObj , { errormsg : result , data} , executeCursor ) => {
7374 let errorLineNo = 0 ,
@@ -363,8 +364,9 @@ export default function Query({onTextSelect, setQtStatePartial}) {
363364
364365 const change = useCallback ( ( ) => {
365366 eventBus . fireEvent ( QUERY_TOOL_EVENTS . QUERY_CHANGED , editor . current . isDirty ( ) ) ;
366-
367- if ( isSaveToolDataEnabled ( 'sqleditor' ) && editor . current . isDirty ( ) ) {
367+ const value = editor . current . getValue ( ) || '' ;
368+ const isDirty = editor . current . isDirty ( ) ;
369+ if ( isSaveToolDataEnabled ( 'sqleditor' ) && ( isDirty || value . length === 0 ) ) {
368370 eventBus . fireEvent ( QUERY_TOOL_EVENTS . TRIGGER_SAVE_QUERY_TOOL_DATA ) ;
369371 }
370372
@@ -379,10 +381,22 @@ export default function Query({onTextSelect, setQtStatePartial}) {
379381
380382
381383 const [ saveQtData , setSaveQtData ] = useState ( false ) ;
382- useDelayDebounce ( ( ) => {
383- let connectionInfo = { ..._ . find ( queryToolCtx . connection_list , c => c . is_selected ) ,
384- 'open_file_name' :queryToolCtx . current_file , 'is_editor_dirty' : editor . current . isDirty ( ) } ;
385- saveToolData ( 'sqleditor' , connectionInfo , queryToolCtx . params . trans_id , editor . current . getValue ( ) ) ;
384+
385+ useDelayDebounce ( ( ) => {
386+ if ( ! editor . current ) {
387+ setSaveQtData ( false ) ;
388+ return ;
389+ }
390+ const currentTransId = initialTransIdRef . current || queryToolCtx . params . trans_id ;
391+ const currentConnList = queryToolCtx . connection_list ;
392+ const currentFile = queryToolCtx . current_file ;
393+
394+ let connectionInfo = {
395+ connection_list : currentConnList ,
396+ open_file_name : currentFile ,
397+ is_editor_dirty : editor . current . isDirty ( )
398+ } ;
399+ saveToolData ( 'sqleditor' , connectionInfo , currentTransId , editor . current . getValue ( ) ) ;
386400 setSaveQtData ( false ) ;
387401 } , saveQtData , 500 ) ;
388402
0 commit comments