1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
|
unit fraconnections;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, ComCtrls, ActnList, StdCtrls, DB, sqldb, PropertyStorage, sqldbrestschema, sqldbrestbridge,sqldbschemaedittools;
type
{ TfraConnections }
TfraConnections = class(TFrame)
AConnectionAdd: TAction;
AConnectionDelete: TAction;
AConnectionEdit: TAction;
AConnectionExpose: TAction;
AConnectionRefresh: TAction;
AConnectionsHide: TAction;
aLConnections: TActionList;
ILConnections: TImageList;
Label1: TLabel;
ToolBar1: TToolBar;
TBConnectionAdd: TToolButton;
TBConnectionEdit: TToolButton;
TBConnectionDelete: TToolButton;
TBSep1: TToolButton;
TBConnectionRefresh: TToolButton;
TBConnectionExpose: TToolButton;
ToolButton1: TToolButton;
TBConnectionsHide: TToolButton;
TVConnections: TTreeView;
procedure AConnectionAddExecute(Sender: TObject);
procedure AConnectionDeleteExecute(Sender: TObject);
procedure AConnectionDeleteUpdate(Sender: TObject);
procedure AConnectionEditExecute(Sender: TObject);
procedure AConnectionEditUpdate(Sender: TObject);
procedure AConnectionExposeExecute(Sender: TObject);
procedure AConnectionExposeUpdate(Sender: TObject);
procedure AConnectionRefreshExecute(Sender: TObject);
procedure AConnectionRefreshUpdate(Sender: TObject);
procedure AConnectionsHideExecute(Sender: TObject);
procedure TVConnectionsEdited(Sender: TObject; Node: TTreeNode; var S: string);
procedure TVConnectionsEditing(Sender: TObject; Node: TTreeNode; var AllowEdit: Boolean);
procedure TVConnectionsStartDrag(Sender: TObject; var DragObject: TDragObject);
private
FConnections: TSQLDBRestConnectionList;
FFileName: String;
FModified: Boolean;
FOnImportSchema: TNotifyEvent;
function GetCurrentConnection: TMySQLDBRestConnection;
procedure SetConnections(AValue: TSQLDBRestConnectionList);
procedure ShowConnection(TN: TTreeNode; aConnection: TMySQLDBRestConnection; ShowTables: Boolean=True);
procedure ShowConnectionTables(TN: TTreeNode; aConnection: TMySQLDBRestConnection);
procedure ShowTable(TN: TTreeNode; aConnection: TMySQLDBRestConnection; const aTableName: string);
Protected
procedure ReadConnectionsFromIni(const aFileName, ASection: String);
procedure WriteConnectionsToIni(const aFileName, ASection: String);
Procedure Changed;
public
Constructor Create(AOwner : TComponent); override;
Destructor Destroy; override;
procedure DeleteConnection(aConnection: TSQLDBRestConnection);
procedure LoadConnections(const aFileName: string);
procedure SaveConnections(const aFileName: String);
Procedure ShowConnections;
Procedure LoadSession(aStorage : TCustomPropertyStorage); virtual;
Procedure SaveSession(aStorage : TCustomPropertyStorage); virtual;
Property Connections : TSQLDBRestConnectionList read FConnections write SetConnections;
Property CurrentConnection : TMySQLDBRestConnection Read GetCurrentConnection;
Property OnImportSchema : TNotifyEvent Read FOnImportSchema Write FOnImportSchema;
// Last used filename in Load/saveConnections
Property FileName : String Read FFileName;
Property Modified : Boolean Read FModified;
end;
implementation
uses dialogs, strutils, inifiles, sqldbrestini, dlgsqldbrestconnection;
{$R *.lfm}
Const
KeyConnections = 'Connections';
KeyLoadOptions = 'LoadOptions';
Resourcestring
SErrDuplicateConnection = 'Duplicate connection name: %s';
{ TfraConnections }
procedure TfraConnections.AConnectionsHideExecute(Sender: TObject);
begin
Self.Visible:=False;
end;
procedure TfraConnections.TVConnectionsEdited(Sender: TObject; Node: TTreeNode; var S: string);
Var
D,C : TMySQLDBRestConnection;
begin
C:=TObject(Node.Data) as TMySQLDBRestConnection;
D:=TMySQLDBRestConnection(Connections.FindConnection(S));
if (D<>Nil) and (D<>C) then
begin
ShowMessage(SErrDuplicateConnection);
S:=C.Name;
end
else
begin
C.Name:=S;
Changed;
end;
end;
procedure TfraConnections.TVConnectionsEditing(Sender: TObject;
Node: TTreeNode; var AllowEdit: Boolean);
begin
allowEdit:=(Node.ImageIndex=idxConnection);
end;
procedure TfraConnections.TVConnectionsStartDrag(Sender: TObject; var DragObject: TDragObject);
Var
SDO : TStringsDragObject;
I : Integer;
TN : TTreeNode;
begin
SDO:=TStringsDragObject.Create(TVConnections);
DragObject:=SDO;
for I:=0 to TVConnections.Items.Count-1 do
begin
TN:=TVConnections.Items[I];
if TN.Selected and (TN.ImageIndex=idxTable) then
SDO.Items.AddObject(TN.Text,TObject(TN.Data) as TMySQLDBRestConnection);
end;
end;
procedure TfraConnections.SetConnections(AValue: TSQLDBRestConnectionList);
begin
if FConnections=AValue then Exit;
FConnections.Assign(AValue);
ShowConnections;
end;
function TfraConnections.GetCurrentConnection: TMySQLDBRestConnection;
Var
N : TTreeNode;
begin
N:=TVConnections.Selected;
While (N<>nil) and (N.ImageIndex<>idxConnection) do
N:=N.Parent;
if N=nil then
Result:=nil
else
Result:=TObject(N.Data) as TMySQLDBRestConnection;
end;
constructor TfraConnections.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
{$IF DECLARED(TSQLDBFakeRestParam)}
FConnections:=TSQLDBRestConnectionList.Create(TMySQLDBRestConnection);
{$ELSE}
FConnections:=TSQLDBRestConnectionList.Create(Self,TMySQLDBRestConnection);
{$ENDIF}
end;
destructor TfraConnections.Destroy;
begin
FreeAndNil(FConnections);
inherited Destroy;
end;
procedure TfraConnections.ShowTable(TN: TTreeNode; aConnection: TMySQLDBRestConnection;
const aTableName: string);
begin
TN.ImageIndex:=idxTable;
TN.SelectedIndex:=idxTable;
TN.Data:=aConnection;
TN.Text:=aTableName;
end;
procedure TfraConnections.ShowConnectionTables(TN : TTreeNode; aConnection : TMySQLDBRestConnection);
Var
L : TStringList;
S : String;
N : TTreeNode;
begin
TN.DeleteChildren;
if aConnection.MyConnection=Nil then
aConnection.CreateConnection;
try
aConnection.MyConnection.Connected:=True;
except
On E : EDatabaseError do
begin
ShowMessage(Format(SErrShowingTablesConnectingTo,[aConnection.Name,E.Message]));
exit;
end;
end;
L:=TStringList.Create;
try
aConnection.MyConnection.GetTableNames(L);
L.Sorted:=true;
For S in L do
begin
N:=TVConnections.Items.AddChild(Tn,S);
ShowTable(N,aConnection,S);
end;
finally
L.Free;
end;
end;
procedure TfraConnections.ShowConnection(TN: TTreeNode; aConnection: TMySQLDBRestConnection; ShowTables: Boolean);
begin
TN.Text:=aConnection.Name;
TN.ImageIndex:=idxConnection;
TN.SelectedIndex:=idxConnection;
TN.Data:=aConnection;
if ShowTables then
ShowConnectionTables(TN,aConnection);
end;
procedure TfraConnections.ShowConnections;
Var
TN : TTreeNode;
C : TMySQLDBRestConnection;
I : Integer;
begin
TVConnections.Items.BeginUpdate;
try
TVConnections.Items.Clear;
For I:=0 to Connections.Count-1 do
begin
C:=Connections[i] as TMySQLDBRestConnection;
TN:=TVConnections.Items.AddChild(Nil,C.Name);
ShowConnection(TN,C);
end;
finally
TVConnections.Items.EndUpdate;
end;
end;
procedure TfraConnections.LoadSession(aStorage: TCustomPropertyStorage);
begin
// Do nothing
end;
procedure TfraConnections.SaveSession(aStorage: TCustomPropertyStorage);
begin
// Do nothing
end;
procedure TfraConnections.DeleteConnection(aConnection : TSQLDBRestConnection);
Var
TN : TTreeNode;
begin
TN:=TVConnections.Items.FindNodeWithData(aConnection);
if Assigned(TN) then
TVConnections.Items.Delete(TN);
aConnection.Free;
Changed;
end;
procedure TfraConnections.AConnectionAddExecute(Sender: TObject);
Var
C : TMySQLDBRestConnection;
TN : TTreeNode;
begin
C:=FConnections.Add as TMySQLDBRestConnection;
if not EditSQLDBRestConnection(C) then
C.Free
else
begin
TN:=TVConnections.Items.AddChild(Nil,C.Name);
ShowConnection(Tn,C,True);
Changed;
end;
end;
procedure TfraConnections.AConnectionDeleteExecute(Sender: TObject);
begin
DeleteConnection(CurrentConnection);
end;
procedure TfraConnections.AConnectionDeleteUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentConnection);
end;
procedure TfraConnections.AConnectionEditExecute(Sender: TObject);
Var
C : TMySQLDBRestConnection;
TN : TTreeNode;
begin
C:=CurrentConnection;
if EditSQLDBRestConnection(C) then
begin
TN:=TVConnections.Items.FindNodeWithData(C);
if Assigned(TN) then
ShowConnection(Tn,C,True);
Changed;
end;
end;
procedure TfraConnections.AConnectionEditUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentConnection);
end;
procedure TfraConnections.AConnectionExposeExecute(Sender: TObject);
begin
if Assigned(OnImportSchema) then
OnImportSchema(CurrentConnection);
end;
procedure TfraConnections.AConnectionExposeUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(OnImportSchema);
end;
procedure TfraConnections.AConnectionRefreshExecute(Sender: TObject);
Var
C : TMySQLDBRestConnection;
TN : TTreeNode;
begin
C:=CurrentConnection;
if Assigned(C) then
begin
TN:=TVConnections.Items.FindNodeWithData(C);
if Assigned(TN) then
ShowConnectionTables(TN,C);
end;
end;
procedure TfraConnections.AConnectionRefreshUpdate(Sender: TObject);
begin
(Sender as TAction).Enabled:=Assigned(CurrentConnection);
end;
procedure TfraConnections.WriteConnectionsToIni(const aFileName,
ASection: String);
Var
S,L : String;
I : Integer;
aIni : TMemIniFile;
begin
aIni:=TMemIniFile.Create(aFileName);
try
L:='';
for I:=0 to Connections.Count-1 do
begin
if (L<>'') then
L:=L+',';
L:=L+Connections[i].Name;
end;
aIni.WriteString(aSection,KeyConnections,L);
for I:=0 to Connections.Count-1 do
begin
S:=Connections[i].Name;
L:=ConnectionIniOptionsToStr([]);
Connections[i].SaveToIni(aIni,S,[]);
aIni.WriteString(S,KeyLoadOptions,L);
end;
finally
aIni.Free;
end;
end;
procedure TfraConnections.Changed;
begin
FModified:=True;
end;
procedure TfraConnections.ReadConnectionsFromIni(const aFileName,
ASection: String);
Var
S,L : String;
I : Integer;
C : TSQLDBRestConnection;
CIO : TConnectionIniOptions;
aIni : TMemIniFile;
begin
// Read connections
aIni:=TMemIniFile.Create(aFileName);
try
L:=aIni.ReadString(aSection,KeyConnections,'');
For I:=1 to WordCount(L,[',']) do
begin
S:=ExtractWord(I,L,[',']);
C:=Connections.AddConnection('','','','','');
C.Name:=S;
CIO:=StrToConnectionIniOptions(aIni.ReadString(S,KeyLoadOptions,''));
C.LoadFromIni(aIni,S,CIO);
end;
finally
aIni.Free;
end;
end;
procedure TfraConnections.LoadConnections(const aFileName: string);
begin
case lowercase(ExtractFileExt(aFileName)) of
'.ini' : ReadConnectionsFromIni(aFileName,'Connections');
'.json' : Connections.LoadFromFile(aFileName)
end;
ShowConnections;
FModified:=False;
FFileName:=aFileName;
end;
procedure TfraConnections.SaveConnections(const aFileName: String);
begin
case lowercase(ExtractFileExt(aFileName)) of
'.ini' : WriteConnectionsToIni(aFileName,'Connections');
'.json' : Connections.SaveToFile(aFileName)
end;
FModified:=False;
FFileName:=aFileName;
end;
end.
|