From c2594e40c0b426b559a575248a07884913a35387 Mon Sep 17 00:00:00 2001 From: Ansgar Becker Date: Thu, 21 Dec 2017 15:25:03 +0100 Subject: [PATCH] Find out whether the previously selected database has some locked tables, and if so, don't select it at startup time. Closes #49 --- source/dbconnection.pas | 28 +++++++++++++++++++++++++++- source/main.pas | 5 ++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/source/dbconnection.pas b/source/dbconnection.pas index 8c69ffdfd..ce04c4578 100644 --- a/source/dbconnection.pas +++ b/source/dbconnection.pas @@ -286,7 +286,8 @@ TConnectionParameters = class(TObject) spSessionVariables, spGlobalVariables, spISTableSchemaCol, spUSEQuery, spKillQuery, spKillProcess, - spFuncLength, spFuncCeil); + spFuncLength, spFuncCeil, + spLockedTables); TDBConnection = class(TComponent) private @@ -435,6 +436,7 @@ TDBConnection = class(TComponent) property LockedByThread: TThread read FLockedByThread write SetLockedByThread; property Datatypes: TDBDataTypeArray read FDatatypes; property Favorites: TStringList read FFavorites; + function GetLockedTableCount(db: String): Integer; published property Active: Boolean read FActive write SetActive default False; property Database: String read FDatabase write SetDatabase; @@ -2034,6 +2036,7 @@ procedure TDBConnection.DoBeforeConnect; FSQLSpecifities[spKillProcess] := 'KILL %d'; FSQLSpecifities[spFuncLength] := 'LENGTH'; FSQLSpecifities[spFuncCeil] := 'CEIL'; + FSQLSpecifities[spLockedTables] := ''; end; ngMSSQL: begin FSQLSpecifities[spEmptyTable] := 'DELETE FROM '; @@ -2050,6 +2053,7 @@ procedure TDBConnection.DoBeforeConnect; FSQLSpecifities[spKillProcess] := 'KILL %d'; FSQLSpecifities[spFuncLength] := 'LEN'; FSQLSpecifities[spFuncCeil] := 'CEILING'; + FSQLSpecifities[spLockedTables] := ''; end; ngPgSQL: begin FSQLSpecifities[spEmptyTable] := 'DELETE FROM '; @@ -2066,6 +2070,7 @@ procedure TDBConnection.DoBeforeConnect; FSQLSpecifities[spKillProcess] := 'SELECT pg_cancel_backend(%d)'; FSQLSpecifities[spFuncLength] := 'LENGTH'; FSQLSpecifities[spFuncCeil] := 'CEIL'; + FSQLSpecifities[spLockedTables] := ''; end; end; @@ -2225,6 +2230,9 @@ procedure TMySQLConnection.DoAfterConnect; if ServerVersionInt >= 50000 then FSQLSpecifities[spKillQuery] := 'KILL QUERY %d'; + + if ServerVersionInt >= 50124 then + FSQLSpecifities[spLockedTables] := 'SHOW OPEN TABLES FROM %s WHERE in_use!=0'; end; @@ -3997,6 +4005,24 @@ function TPGConnection.MaxAllowedPacket: Int64; end; +function TDBConnection.GetLockedTableCount(db: String): Integer; +var + sql: String; + LockedTables: TStringList; +begin + // Find tables which are currently locked. + // Used to prevent waiting time in GetDBObjects. + sql := GetSQLSpecifity(spLockedTables); + if sql.IsEmpty then begin + Result := 0; + end else begin + LockedTables := GetCol(Format(sql, [QuoteIdent(db,False)])); + Result := LockedTables.Count; + LockedTables.Free; + end; +end; + + function TMySQLConnection.GetRowCount(Obj: TDBObject): Int64; var Rows: String; diff --git a/source/main.pas b/source/main.pas index 72a039e77..86384435f 100644 --- a/source/main.pas +++ b/source/main.pas @@ -3693,7 +3693,10 @@ function TMainform.InitConnection(Params: TConnectionParameters; ActivateMe: Boo RestoreLastActiveDatabase := AppSettings.ReadBool(asRestoreLastUsedDB); AppSettings.SessionPath := Params.SessionPath; LastActiveDatabase := AppSettings.ReadString(asLastUsedDB); - if RestoreLastActiveDatabase and (Connection.AllDatabases.IndexOf(LastActiveDatabase) >- 1) then begin + if RestoreLastActiveDatabase + and (Connection.AllDatabases.IndexOf(LastActiveDatabase) >- 1) + and (Connection.GetLockedTableCount(LastActiveDatabase) = 0) + then begin SetActiveDatabase(LastActiveDatabase, Connection); DBNode := FindDBNode(DBtree, Connection, LastActiveDatabase); if Assigned(DBNode) then