Skip to content

Commit

Permalink
Find out whether the previously selected database has some locked tab…
Browse files Browse the repository at this point in the history
…les, and if so, don't select it at startup time. Closes #49
  • Loading branch information
ansgarbecker committed Dec 21, 2017
1 parent 829e7e6 commit c2594e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
28 changes: 27 additions & 1 deletion source/dbconnection.pas
Expand Up @@ -286,7 +286,8 @@ TConnectionParameters = class(TObject)
spSessionVariables, spGlobalVariables,
spISTableSchemaCol,
spUSEQuery, spKillQuery, spKillProcess,
spFuncLength, spFuncCeil);
spFuncLength, spFuncCeil,
spLockedTables);

TDBConnection = class(TComponent)
private
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ';
Expand All @@ -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 ';
Expand All @@ -2066,6 +2070,7 @@ procedure TDBConnection.DoBeforeConnect;
FSQLSpecifities[spKillProcess] := 'SELECT pg_cancel_backend(%d)';
FSQLSpecifities[spFuncLength] := 'LENGTH';
FSQLSpecifities[spFuncCeil] := 'CEIL';
FSQLSpecifities[spLockedTables] := '';
end;

end;
Expand Down Expand Up @@ -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;


Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion source/main.pas
Expand Up @@ -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
Expand Down

0 comments on commit c2594e4

Please sign in to comment.