Propagate changed list of object names in selected database through the new event OnObjectnamesChanged to the owner object, not through an internal reference to a string list.

This commit is contained in:
Ansgar Becker
2016-06-18 07:24:41 +00:00
parent ecd1c14ef6
commit a3853b5d6c
2 changed files with 47 additions and 29 deletions

View File

@ -300,6 +300,7 @@ type
FOnLog: TDBLogEvent; FOnLog: TDBLogEvent;
FOnConnected: TDBEvent; FOnConnected: TDBEvent;
FOnDatabaseChanged: TDBEvent; FOnDatabaseChanged: TDBEvent;
FOnObjectnamesChanged: TDBEvent;
FRowsFound: Int64; FRowsFound: Int64;
FRowsAffected: Int64; FRowsAffected: Int64;
FWarningCount: Cardinal; FWarningCount: Cardinal;
@ -317,7 +318,6 @@ type
FSessionVariables: TDBQuery; FSessionVariables: TDBQuery;
FInformationSchemaObjects: TStringList; FInformationSchemaObjects: TStringList;
FDatabaseCache: TDatabaseCache; FDatabaseCache: TDatabaseCache;
FObjectNamesInSelectedDB: TStrings;
FResultCount: Integer; FResultCount: Integer;
FStatementNum: Cardinal; FStatementNum: Cardinal;
FCurrentUserHostCombination: String; FCurrentUserHostCombination: String;
@ -355,7 +355,6 @@ type
function GetRowCount(Obj: TDBObject): Int64; virtual; abstract; function GetRowCount(Obj: TDBObject): Int64; virtual; abstract;
procedure ClearCache(IncludeDBObjects: Boolean); procedure ClearCache(IncludeDBObjects: Boolean);
procedure FetchDbObjects(db: String; var Cache: TDBObjectList); virtual; abstract; procedure FetchDbObjects(db: String; var Cache: TDBObjectList); virtual; abstract;
procedure SetObjectNamesInSelectedDB;
procedure SetLockedByThread(Value: TThread); virtual; procedure SetLockedByThread(Value: TThread); virtual;
procedure KeepAliveTimerEvent(Sender: TObject); procedure KeepAliveTimerEvent(Sender: TObject);
procedure Drop(Obj: TDBObject); virtual; procedure Drop(Obj: TDBObject); virtual;
@ -430,7 +429,6 @@ type
property CharsetTable: TDBQuery read GetCharsetTable; property CharsetTable: TDBQuery read GetCharsetTable;
property CharsetList: TStringList read GetCharsetList; property CharsetList: TStringList read GetCharsetList;
property InformationSchemaObjects: TStringList read GetInformationSchemaObjects; property InformationSchemaObjects: TStringList read GetInformationSchemaObjects;
property ObjectNamesInSelectedDB: TStrings read FObjectNamesInSelectedDB write FObjectNamesInSelectedDB;
property ResultCount: Integer read FResultCount; property ResultCount: Integer read FResultCount;
property CurrentUserHostCombination: String read GetCurrentUserHostCombination; property CurrentUserHostCombination: String read GetCurrentUserHostCombination;
property LockedByThread: TThread read FLockedByThread write SetLockedByThread; property LockedByThread: TThread read FLockedByThread write SetLockedByThread;
@ -443,6 +441,7 @@ type
property OnLog: TDBLogEvent read FOnLog write FOnLog; property OnLog: TDBLogEvent read FOnLog write FOnLog;
property OnConnected: TDBEvent read FOnConnected write FOnConnected; property OnConnected: TDBEvent read FOnConnected write FOnConnected;
property OnDatabaseChanged: TDBEvent read FOnDatabaseChanged write FOnDatabaseChanged; property OnDatabaseChanged: TDBEvent read FOnDatabaseChanged write FOnDatabaseChanged;
property OnObjectnamesChanged: TDBEvent read FOnObjectnamesChanged write FOnObjectnamesChanged;
end; end;
TDBConnectionList = TObjectList<TDBConnection>; TDBConnectionList = TObjectList<TDBConnection>;
@ -3038,7 +3037,8 @@ begin
s := QuoteIdent(Value); s := QuoteIdent(Value);
Query(Format(GetSQLSpecifity(spUSEQuery), [s]), False); Query(Format(GetSQLSpecifity(spUSEQuery), [s]), False);
end; end;
SetObjectNamesInSelectedDB; if Assigned(FOnObjectnamesChanged) then
FOnObjectnamesChanged(Self, FDatabase);
end; end;
end; end;
@ -4300,7 +4300,8 @@ begin
Cache.Sort; Cache.Sort;
// Add list of objects in this database to cached list of all databases // Add list of objects in this database to cached list of all databases
FDatabaseCache.Add(Cache); FDatabaseCache.Add(Cache);
SetObjectNamesInSelectedDB; if Assigned(FOnObjectnamesChanged) then
FOnObjectnamesChanged(Self, FDatabase);
end; end;
Result := nil; Result := nil;
@ -4639,29 +4640,6 @@ begin
end; end;
procedure TDBConnection.SetObjectNamesInSelectedDB;
var
i: Integer;
Objects: TDBObjectList;
ObjNames: String;
begin
// Add object names to additional stringlist
if Assigned(FObjectNamesInSelectedDB) then begin
ObjNames := '';
if DbObjectsCached(FDatabase) then begin
Objects := GetDbObjects(FDatabase);
// Limit slow highlighter to 1000 table names. See http://www.heidisql.com/forum.php?t=16307
if Objects.Count < 1000 then begin
for i:=0 to Objects.Count-1 do
ObjNames := ObjNames + Objects[i].Name + CRLF;
end;
end;
if FObjectNamesInSelectedDB.Text <> ObjNames then
FObjectNamesInSelectedDB.Text := ObjNames;
end;
end;
function TDBConnection.GetKeyColumns(Columns: TTableColumnList; Keys: TTableKeyList): TStringList; function TDBConnection.GetKeyColumns(Columns: TTableColumnList; Keys: TTableKeyList): TStringList;
var var
i: Integer; i: Integer;

View File

@ -1057,6 +1057,7 @@ type
procedure UpdateFilterPanel(Sender: TObject); procedure UpdateFilterPanel(Sender: TObject);
procedure ConnectionReady(Connection: TDBConnection; Database: String); procedure ConnectionReady(Connection: TDBConnection; Database: String);
procedure DatabaseChanged(Connection: TDBConnection; Database: String); procedure DatabaseChanged(Connection: TDBConnection; Database: String);
procedure ObjectnamesChanged(Connection: TDBConnection; Database: String);
procedure UpdateLineCharPanel; procedure UpdateLineCharPanel;
procedure SetSnippetFilenames; procedure SetSnippetFilenames;
function TreeClickHistoryPrevious(MayBeNil: Boolean=False): PVirtualNode; function TreeClickHistoryPrevious(MayBeNil: Boolean=False): PVirtualNode;
@ -3547,7 +3548,7 @@ begin
Connection.OnLog := LogSQL; Connection.OnLog := LogSQL;
Connection.OnConnected := ConnectionReady; Connection.OnConnected := ConnectionReady;
Connection.OnDatabaseChanged := DatabaseChanged; Connection.OnDatabaseChanged := DatabaseChanged;
Connection.ObjectNamesInSelectedDB := SynSQLSyn1.TableNames; Connection.OnObjectnamesChanged := ObjectnamesChanged;
try try
Connection.Active := True; Connection.Active := True;
// We have a connection // We have a connection
@ -8041,6 +8042,45 @@ begin
end; end;
procedure TMainForm.ObjectnamesChanged(Connection: TDBConnection; Database: String);
var
DBObjects: TDBObjectList;
Obj: TDBObject;
TableNames, ProcNames: TStringList;
begin
// Tell SQL highlighter about names of tables and procedures in selected database
SynSQLSyn1.TableNames.Clear;
SynSQLSyn1.ProcNames.Clear;
if Connection.DbObjectsCached(Database) then begin
DBObjects := Connection.GetDBObjects(Database);
TableNames := TStringList.Create;
TableNames.BeginUpdate;
ProcNames := TStringList.Create;
ProcNames.BeginUpdate;
for Obj in DBObjects do begin
case Obj.NodeType of
lntTable, lntView: begin
// Limit slow highlighter to 1000 table names. See http://www.heidisql.com/forum.php?t=16307
// ... and here: https://github.com/SynEdit/SynEdit/issues/28
if TableNames.Count < 1000 then
TableNames.Add(Obj.Name);
end;
lntProcedure, lntFunction: begin
if ProcNames.Count < 1000 then
ProcNames.Add(Obj.Name);
end;
end;
end;
TableNames.EndUpdate;
ProcNames.EndUpdate;
SynSQLSyn1.TableNames.Text := TableNames.Text;
SynSQLSyn1.ProcNames.Text := ProcNames.Text;
TableNames.Free;
ProcNames.Free;
end;
end;
procedure TMainForm.DBtreeDblClick(Sender: TObject); procedure TMainForm.DBtreeDblClick(Sender: TObject);
var var
DBObj: PDBObject; DBObj: PDBObject;