diff --git a/source/main.pas b/source/main.pas index a2bd0dcc..1fdedac1 100644 --- a/source/main.pas +++ b/source/main.pas @@ -725,9 +725,10 @@ type FLastMouseUpOnPageControl : Cardinal; FLastTabNumberOnMouseUp : Integer; DataGridResult : TGridResult; - // Filter text per tab for filter panel + // Filter text per tab for filter panel FilterTextVariables, FilterTextStatus, FilterTextProcessList, FilterTextCommandStats, FilterTextDatabase, FilterTextData: WideString; + PreviousFocusedNode: PVirtualNode; function GetParamValue(const paramChar: Char; const paramName: string; var curIdx: Byte; out paramValue: string): Boolean; procedure SetDelimiter(Value: String); @@ -4799,11 +4800,13 @@ procedure TMainForm.SetSelectedDatabase(db: WideString); var n: PVirtualNode; begin - n := FindDBNode(db); - if Assigned(n) then begin - DBtree.Selected[n] := true; - DBtree.FocusedNode := n; - end else + if db = '' then + n := DBtree.GetFirst + else + n := FindDBNode(db); + if Assigned(n) then + SelectNode(DBtree, n) + else raise Exception.Create('Database node ' + db + ' not found in tree.'); end; @@ -5976,12 +5979,27 @@ begin 0: ShowHost; 1: begin newDb := Databases[Node.Index]; - Connection.Database := newDb; - ShowDatabase( newDb ); + // Selecting a database can cause an SQL error if the db was deleted from outside. Select previous node in that case. + try + Connection.Database := newDb; + except on E:Exception do begin + MessageDlg(E.Message, mtError, [mbOK], 0); + SelectNode(DBtree, PreviousFocusedNode); + Exit; + end; + end; + ShowDatabase(newDb); end; 2: begin newDb := Databases[Node.Parent.Index]; - Connection.Database := newDb; + try + Connection.Database := newDb; + except on E:Exception do begin + MessageDlg(E.Message, mtError, [mbOK], 0); + SelectNode(DBtree, PreviousFocusedNode); + Exit; + end; + end; newDbObject := SelectedTable.Text; tabEditor.TabVisible := True; tabData.TabVisible := SelectedTable.NodeType in [lntTable, lntView]; @@ -6016,6 +6034,7 @@ begin end; end; end; + PreviousFocusedNode := DBTree.FocusedNode; if newDb <> '' then LoadDatabaseProperties(newDb); FixQueryTabCloseButtons; @@ -6025,7 +6044,7 @@ end; procedure TMainForm.DatabaseChanged(Database: WideString); begin - if (Database <> ActiveDatabase) and (Databases.IndexOf(Database) > -1) then + if (Database='') or (Databases.IndexOf(Database) > -1) then ActiveDatabase := Database; end; diff --git a/source/mysql_connection.pas b/source/mysql_connection.pas index e40b55a6..be5fd44b 100644 --- a/source/mysql_connection.pas +++ b/source/mysql_connection.pas @@ -323,9 +323,17 @@ begin FServerStarted := FConnectionStarted - StrToIntDef(GetVar('SHOW STATUS LIKE ''Uptime''', 1), 1); FServerVersionUntouched := mysql_get_server_info(FHandle); DetectCapabilities; - tmpdb := FDatabase; - FDatabase := ''; - SetDatabase(tmpdb); + if FDatabase <> '' then begin + tmpdb := FDatabase; + FDatabase := ''; + try + Database := tmpdb; + except + // Trigger OnDatabaseChange event for if wanted db is not available + FDatabase := tmpdb; + Database := ''; + end; + end; end; end @@ -372,7 +380,7 @@ begin raise Exception.Create(GetLastError); end else begin // We must call mysql_store_result() + mysql_free_result() to unblock the connection - // See: http://dev.mysql.com/doc/refman/5.0/en/mysql-store-result.html + // See: http://dev.mysql.com/doc/refman/5.0/en/mysql-store-result.html FRowsAffected := mysql_affected_rows(FHandle); TimerStart := GetTickCount; Result := mysql_store_result(FHandle); @@ -404,9 +412,14 @@ end; } procedure TMySQLConnection.SetDatabase(Value: WideString); begin - if (Value = '') or (Value = FDatabase) then - Exit; - Query('USE '+QuoteIdent(Value), False); + if Value <> FDatabase then begin + if Value = '' then begin + FDatabase := Value; + if Assigned(FOnDatabaseChanged) then + FOnDatabaseChanged(Value); + end else + Query('USE '+QuoteIdent(Value), False); + end; end;